create Item modus added

Signed-off-by: Benny Samir Hierl <bennysamir@posteo.de>
This commit is contained in:
Benny Samir Hierl 2022-02-18 19:49:20 +01:00
parent 9704196276
commit 0642bd4193
5 changed files with 89 additions and 17 deletions

View file

@ -7,14 +7,14 @@
:src="item.imageSrc" :src="item.imageSrc"
:alt="item.title" :alt="item.title"
/> />
<div class="flex w-full flex-col justify-between space-y-2 p-2"> <div class="flex w-full flex-col justify-between space-y-2 p-2">
<Form <h1 v-if="mode === 'create'" class="text-xl">
@submit="onSubmit" {{ t('components.form-wishlist-item.headline-new-item.text') }}
:validation-schema="schema" </h1>
v-slot="{ meta }" <h1 v-else class="text-xl">
class="w-full flex-col" {{ t('components.form-wishlist-item.headline-change-item.text') }}
> </h1>
<form @submit="onSubmit" class="w-full flex-col">
<InputText <InputText
name="title" name="title"
type="text" type="text"
@ -41,6 +41,7 @@
:label="t('components.form-wishlist-item.image-src.label')" :label="t('components.form-wishlist-item.image-src.label')"
/> />
<InputCheckbox <InputCheckbox
v-if="mode === 'update'"
name="bought" name="bought"
:value="item.bought" :value="item.bought"
:label="t('components.form-wishlist-item.bought.label')" :label="t('components.form-wishlist-item.bought.label')"
@ -52,8 +53,9 @@
:icon="IconSave" :icon="IconSave"
>{{ t('components.form-wishlist-item.submit.text') }}</ButtonBase >{{ t('components.form-wishlist-item.submit.text') }}</ButtonBase
> >
</Form> </form>
<ButtonBase <ButtonBase
v-if="mode === 'update'"
class="h-12 w-full" class="h-12 w-full"
mode="danger" mode="danger"
@click.prevent="() => emits('delete')" @click.prevent="() => emits('delete')"
@ -65,7 +67,7 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { useI18n } from 'vue-i18n' import { useI18n } from 'vue-i18n'
import { Form } from 'vee-validate' import { useForm } from 'vee-validate'
import ImagePreview from './ImagePreview.vue' import ImagePreview from './ImagePreview.vue'
import { object, string, boolean } from 'yup' import { object, string, boolean } from 'yup'
import { import {
@ -76,12 +78,29 @@ import {
} from '@/components' } from '@/components'
import { IconSave, IconDelete } from '@/components/icons' import { IconSave, IconDelete } from '@/components/icons'
import { WishlistItem } from '@/types' import { WishlistItem } from '@/types'
defineProps<{ import { PropType } from 'vue'
item: WishlistItem
}>()
const { t } = useI18n()
const emits = defineEmits(['update', 'delete']) const props = defineProps({
mode: {
type: String,
required: true,
},
item: {
type: Object as PropType<WishlistItem>,
default: () => {
return {
id: '',
title: '',
description: '',
url: '',
imageSrc: '',
bought: false,
}
},
},
})
const emits = defineEmits(['update', 'create', 'delete'])
const { t } = useI18n()
const schema = object({ const schema = object({
title: string().required( title: string().required(
@ -97,7 +116,16 @@ const schema = object({
bought: boolean(), bought: boolean(),
}) })
const onSubmit = async (values: any): Promise<void> => { const { handleSubmit, resetForm, meta } = useForm({
emits('update', values) validationSchema: schema,
} })
const onSubmit = handleSubmit((values) => {
if (props.mode === 'create') {
emits('create', values)
resetForm()
} else {
emits('update', values)
}
})
</script> </script>

View file

@ -40,6 +40,21 @@ const update = async (updatedData: Wishlist): Promise<void> => {
} }
} }
const createItem = async (values: WishlistItem): Promise<void> => {
const id = state.value?.id
const payload = {
...values,
}
try {
const { data } = await client.post(`/wishlist/${id}/item`, payload)
state.value?.items?.push(data)
} catch (e: any) {
if (e.isAxiosError && !(<CustomAxiosError>e.ignore)) {
throw e
}
}
}
const updateItem = async ( const updateItem = async (
currentValues: WishlistItem, currentValues: WishlistItem,
newValues: WishlistItem newValues: WishlistItem
@ -91,6 +106,7 @@ export const useWishlistStore = () => {
isReady, isReady,
fetch, fetch,
update, update,
createItem,
updateItem, updateItem,
itemBought, itemBought,
itemDelete, itemDelete,

View file

@ -126,6 +126,12 @@
} }
}, },
"form-wishlist-item": { "form-wishlist-item": {
"headline-new-item": {
"text": "Neuen Eintrag hinzufügen"
},
"headline-change-item": {
"text": "Eintrag bearbeiten"
},
"title": { "title": {
"label": "Titel", "label": "Titel",
"error-requried": "Titel wird benötigt." "error-requried": "Titel wird benötigt."

View file

@ -123,6 +123,12 @@
} }
}, },
"form-wishlist-item": { "form-wishlist-item": {
"headline-new-item": {
"text": "Add new item"
},
"headline-change-item": {
"text": "Change item"
},
"title": { "title": {
"label": "Title", "label": "Title",
"error-requried": "Title is required." "error-requried": "Title is required."

View file

@ -24,6 +24,7 @@ const {
fetch, fetch,
isReady, isReady,
update, update,
createItem,
updateItem, updateItem,
itemBought, itemBought,
itemDelete, itemDelete,
@ -41,6 +42,15 @@ const handleUpdateWishlist = async (wishlist: Wishlist): Promise<void> => {
} }
} }
const handleCreateItem = async (values: WishlistItemType): Promise<void> => {
try {
await createItem(values)
toast.success(t('common.saved.text'))
} catch (error) {
toast.error(t('common.saving-failed.text'))
}
}
const handleUpdateItem = async ( const handleUpdateItem = async (
currentValues: WishlistItemType, currentValues: WishlistItemType,
newValues: WishlistItemType newValues: WishlistItemType
@ -97,6 +107,11 @@ const handleDeleteItem = async (item: WishlistItemType): Promise<void> => {
v-if="filteredItems.length > 0" v-if="filteredItems.length > 0"
class="flex flex-col space-y-14 py-10 md:space-y-8" class="flex flex-col space-y-14 py-10 md:space-y-8"
> >
<FormWishlistItem
v-if="editModeIsActive"
mode="create"
@create="handleCreateItem"
/>
<div v-for="item in filteredItems" :key="item.id"> <div v-for="item in filteredItems" :key="item.id">
<WishlistItem <WishlistItem
v-if="!editModeIsActive" v-if="!editModeIsActive"
@ -106,6 +121,7 @@ const handleDeleteItem = async (item: WishlistItemType): Promise<void> => {
<FormWishlistItem <FormWishlistItem
v-else v-else
:item="item" :item="item"
mode="update"
@update="(updateValues) => handleUpdateItem(item, updateValues)" @update="(updateValues) => handleUpdateItem(item, updateValues)"
@delete="handleDeleteItem(item)" @delete="handleDeleteItem(item)"
/> />