From dd388bebf22081beb5be8448ce44ca76a4a0df4e Mon Sep 17 00:00:00 2001 From: Benny Samir Hierl Date: Wed, 16 Feb 2022 22:39:29 +0100 Subject: [PATCH] allow to change wishlist item Signed-off-by: Benny Samir Hierl --- src/components/FormWishlist.vue | 24 +++---- src/components/FormWishlistItem.vue | 101 ++++++++++++++++++++++----- src/components/InputFile.vue | 10 +-- src/composables/useWishlistStore.ts | 27 ++++++++ src/config/locales/de-DE.json | 103 +++++++++++++++++----------- src/config/locales/en-US.json | 98 ++++++++++++++++---------- src/views/DetailView.vue | 18 ++++- 7 files changed, 265 insertions(+), 116 deletions(-) diff --git a/src/components/FormWishlist.vue b/src/components/FormWishlist.vue index 67aafd4..eb7b9d9 100644 --- a/src/components/FormWishlist.vue +++ b/src/components/FormWishlist.vue @@ -9,42 +9,42 @@ name="title" type="text" :value="wishlist.title" - :label="t('components.wishlist-header.main.form.title.label')" + :label="t('components.form-wishlist.title.label')" /> {{ t('components.wishlist-header.main.form.submit.text') }}{{ t('components.form-wishlist.submit.text') }} @@ -74,26 +74,26 @@ const { t } = useI18n() const schema = object().shape( { title: string().required( - t('components.wishlist-header.main.form.title.error-requried') + t('components.form-wishlist.title.error-requried') ), public: boolean(), description: string().max( 300, - t('components.wishlist-header.main.form.description.error-max') + t('components.form-wishlist.description.error-max') ), slugUrlText: string().required( - t('components.wishlist-header.main.form.slug-text.error-requried') + t('components.form-wishlist.slug-text.error-requried') ), imageSrc: string().when('imageFile', { is: (imageFile: string) => !imageFile || imageFile.length === 0, then: string().required( - t('components.wishlist-header.main.form.image-src.error-requried') + t('components.form-wishlist.image-src.error-requried') ), }), imageFile: string().when('imageSrc', { is: (imageSrc: string) => !imageSrc || imageSrc.length === 0, then: string().required( - t('components.wishlist-header.main.form.image-file.error-requried') + t('components.form-wishlist.image-file.error-requried') ), }), }, diff --git a/src/components/FormWishlistItem.vue b/src/components/FormWishlistItem.vue index 2c7c908..1e7f5f6 100644 --- a/src/components/FormWishlistItem.vue +++ b/src/components/FormWishlistItem.vue @@ -1,20 +1,6 @@ - - + diff --git a/src/components/InputFile.vue b/src/components/InputFile.vue index 8bb5a76..6138f80 100644 --- a/src/components/InputFile.vue +++ b/src/components/InputFile.vue @@ -16,19 +16,15 @@
=> { } } +const updateItem = async ( + currentValues: WishlistItem, + newValues: WishlistItem +): Promise => { + const id = state.value?.id + const payload = { + ...currentValues, + ...newValues, + } + try { + const { data } = await client.put( + `/wishlist/${id}/item/${currentValues.id}`, + payload + ) + state.value?.items?.splice( + state.value.items.indexOf(currentValues), + 1, + data + ) + } catch (e: any) { + if (e.isAxiosError && !(e.ignore)) { + throw e + } + } +} + const itemBought = async (item: WishlistItem): Promise => { await client.post(`/wishlist/${item.wishlistId}/item/${item.id}/bought`) item.bought = true @@ -65,6 +91,7 @@ export const useWishlistStore = () => { isReady, fetch, update, + updateItem, itemBought, itemDelete, filteredItems, diff --git a/src/config/locales/de-DE.json b/src/config/locales/de-DE.json index bb1a43c..5c718b7 100644 --- a/src/config/locales/de-DE.json +++ b/src/config/locales/de-DE.json @@ -6,13 +6,11 @@ "loading": { "text": "Lade..." }, - "wishlist": { - "saved": { - "text": "Wunschliste gespeichert" - }, - "saving-failed": { - "text": "Wunschliste konnte nicht gespeichert werden" - } + "saved": { + "text": "Wunschliste gespeichert" + }, + "saving-failed": { + "text": "Wunschliste konnte nicht gespeichert werden" } }, "errors": { @@ -81,6 +79,10 @@ } }, "components": { + "file": { + "text-dropzone-link": "hier", + "text-dropzone": "Ziehen Sie ein beliebiges Bild hierher oder klicken Sie {0}, um den Dialog zu öffnen." + }, "wishlist-item": { "external-product-page-link": { "text": "Produktseite öffnen" @@ -92,39 +94,60 @@ "text": "Löschen" } }, - "wishlist-header": { - "main": { - "form": { - "title": { - "label": "Titel", - "error-requried": "Titel wird benötigt" - }, - "public": { - "label": "Auf der Startseite anzeigen?" - }, - "description": { - "label": "Beschreibung", - "error-max": "Die maximale Länge beträgt 300 Zeichen" - }, - "slug-text": { - "label": "URL Slug-Text", - "error-requried": "URL Slug-Text wird benötigt" - }, - "image-src": { - "label": "Bild-URL", - "error-requried": "Bild-URL wird benötigt" - }, - "image-file": { - "label": "Bild-Datei", - "text-dropzone-link": "hier", - "text-dropzone": "Ziehen Sie ein beliebiges Bild hierher oder klicken Sie {0}, um den Dialog zu öffnen.", - "error-requried": "Bild-Datei wird benötigt", - "error-image-size": "Höhe und Breite dürfen 200px nicht überschreiten." - }, - "submit": { - "text": "Speichern" - } - } + "form-wishlist": { + "title": { + "label": "Titel", + "error-requried": "Titel wird benötigt." + }, + "public": { + "label": "Auf der Startseite anzeigen?" + }, + "description": { + "label": "Beschreibung", + "error-max": "Die maximale Länge beträgt 300 Zeichen." + }, + "slug-text": { + "label": "URL Slug-Text", + "error-requried": "URL Slug-Text wird benötigt." + }, + "image-src": { + "label": "Bild-URL", + "error-requried": "Bild-URL wird benötigt." + }, + "image-file": { + "label": "Bild-Datei", + "text-dropzone-link": "hier", + "text-dropzone": "Ziehen Sie ein beliebiges Bild hierher oder klicken Sie {0}, um den Dialog zu öffnen.", + "error-requried": "Bild-Datei wird benötigt.", + "error-image-size": "Höhe und Breite dürfen 200px nicht überschreiten." + }, + "submit": { + "text": "Speichern" + } + }, + "form-wishlist-item": { + "title": { + "label": "Titel", + "error-requried": "Titel wird benötigt." + }, + "description": { + "label": "Beschreibung", + "error-requried": "Beschreibung wird benötigt.", + "error-max": "Die maximale Länge beträgt 300 Zeichen." + }, + "url": { + "label": "Produkt-URL", + "error-url": "Keine gültige URL." + }, + "image-src": { + "label": "Bild-URL", + "error-url": "Keine gültige URL." + }, + "bought": { + "label": "Gekauft?" + }, + "submit": { + "text": "Speichern" } }, "header": { diff --git a/src/config/locales/en-US.json b/src/config/locales/en-US.json index 9b33633..3cf893d 100644 --- a/src/config/locales/en-US.json +++ b/src/config/locales/en-US.json @@ -6,13 +6,11 @@ "loading": { "text": "Loading..." }, - "wishlist": { - "saved": { - "text": "Wishlist saved" - }, - "saving-failed": { - "text": "Saving wishlist failed" - } + "saved": { + "text": "Wishlist saved" + }, + "saving-failed": { + "text": "Saving wishlist failed" } }, "errors": { @@ -81,6 +79,10 @@ } }, "components": { + "file": { + "text-dropzone-link": "click here", + "text-dropzone": "drag and drop any image here or {0} to open dialog." + }, "wishlist-item": { "external-product-page-link": { "text": "Open product page" @@ -92,37 +94,57 @@ "text": "Delete" } }, - "wishlist-header": { - "main": { - "form": { - "title": { - "label": "Title" - }, - "public": { - "label": "Show on startpage?" - }, - "description": { - "label": "Description", - "error-max": "The max. length is 300 chars." - }, - "slug-text": { - "label": "URL Slug-Text" - }, - "image-src": { - "label": "Image-URL", - "error-requried": "Image-URL is required" - }, - "image-file": { - "label": "Image-File", - "text-dropzone-link": "click here", - "text-dropzone": "drag and drop any image here or {0} to open dialog.", - "error-requried": "Image-File is required", - "error-image-size": "Height and Width must not exceed 200px." - }, - "submit": { - "text": "Save" - } - } + "form-wishlist": { + "title": { + "label": "Title", + "error-requried": "Titel is required." + }, + "public": { + "label": "Show on startpage?" + }, + "description": { + "label": "Description", + "error-max": "The max. length is 300 chars." + }, + "slug-text": { + "label": "URL Slug-Text" + }, + "image-src": { + "label": "Image-URL", + "error-requried": "Image-URL is required." + }, + "image-file": { + "label": "Image-File", + "error-requried": "Image-File is required.", + "error-image-size": "Height and Width must not exceed 200px." + }, + "submit": { + "text": "Save" + } + }, + "form-wishlist-item": { + "title": { + "label": "Title", + "error-requried": "Title is required." + }, + "description": { + "label": "Description", + "error-requried": "Description is required.", + "error-max": "The max. length is 300 chars." + }, + "url": { + "label": "Produkt-URL", + "error-url": "Invalid URL" + }, + "image-src": { + "label": "Bild-URL", + "error-url": "Invalid URL" + }, + "bought": { + "label": "Bought?" + }, + "submit": { + "text": "Save" } }, "header": { diff --git a/src/views/DetailView.vue b/src/views/DetailView.vue index e5e6e01..410fdea 100644 --- a/src/views/DetailView.vue +++ b/src/views/DetailView.vue @@ -24,6 +24,7 @@ const { fetch, isReady, update, + updateItem, itemBought, itemDelete, filteredItems, @@ -33,10 +34,22 @@ await fetch(route.params.slug as string) const handleUpdateWishlist = async (wishlist: Wishlist): Promise => { try { await update(wishlist) - toast.success(t('common.wishlist.saved.text')) + toast.success(t('common.saved.text')) router.push(`/${wishlist.slugUrlText}`) } catch (error) { - toast.error(t('common.wishlist.saving-failed.text')) + toast.error(t('common.saving-failed.text')) + } +} + +const handleUpdateItem = async ( + currentValues: WishlistItemType, + newValues: WishlistItemType +): Promise => { + try { + await updateItem(currentValues, newValues) + toast.success(t('common.saved.text')) + } catch (error) { + toast.error(t('common.saving-failed.text')) } } @@ -93,6 +106,7 @@ const handleDeleteItem = async (item: WishlistItemType): Promise => {