-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: product availability switch (#213)
* fix: navigation on center, form states * feat: product availability switch
- Loading branch information
1 parent
30e977c
commit dfbc69e
Showing
13 changed files
with
124 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
apps/food/app/components/form/UpdateProductAvailability.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<template> | ||
<form> | ||
<div class="min-w-60 px-4 py-3 bg-neutral-50 rounded-2xl"> | ||
<div class="flex items-center gap-4"> | ||
<UiSwitch id="product-switch" :default-checked="isAvailableForPurchase" @update:checked="onSubmit()" /> | ||
<UiLabel for="product-switch" class="leading-tight"> | ||
{{ isAvailableForPurchase ? $t('center.product.available-for-purchase') : $t('center.product.not-available-for-purchase') }} | ||
</UiLabel> | ||
</div> | ||
</div> | ||
</form> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { toTypedSchema } from '@vee-validate/zod' | ||
import { productUpdateSchema } from '~~/server/core/services/product' | ||
import { useForm } from 'vee-validate' | ||
import { useToast } from '~/components/ui/toast' | ||
const { isAvailableForPurchase, productId } = defineProps<{ | ||
isAvailableForPurchase: boolean | ||
productId: string | ||
}>() | ||
const emit = defineEmits(['success']) | ||
const { toast } = useToast() | ||
const { refresh: refreshChannelData } = await useChannel() | ||
const { refresh: refreshProducts } = await useProduct() | ||
const formSchema = toTypedSchema(productUpdateSchema) | ||
const { handleSubmit, handleReset, setFieldValue } = useForm({ | ||
validationSchema: formSchema, | ||
}) | ||
watch( | ||
() => isAvailableForPurchase, | ||
() => { | ||
handleReset() | ||
setFieldValue('isAvailableForPurchase', isAvailableForPurchase) | ||
}, | ||
) | ||
const onSubmit = handleSubmit(async (_, { resetForm }) => { | ||
const { data, error } = await useAsyncData( | ||
'update-product', | ||
() => $fetch(`/api/product/${productId}`, { | ||
method: 'PATCH', | ||
body: { | ||
isAvailableForPurchase: !isAvailableForPurchase, | ||
}, | ||
}), | ||
) | ||
if (error.value) { | ||
console.error(error.value) | ||
toast({ title: 'Ошибка', description: '...' }) | ||
} | ||
if (data.value) { | ||
await refreshChannelData() | ||
await refreshProducts() | ||
emit('success') | ||
toast({ title: 'Продукт обновлен!', description: 'Сейчас обновим данные.' }) | ||
resetForm() | ||
} | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.