From a01f7952c810c2f0af08f072b923a27f56aacc76 Mon Sep 17 00:00:00 2001 From: HungLV Date: Mon, 19 Aug 2024 15:11:07 +0700 Subject: [PATCH] fix: delete flow --- src/routes/(sidebar)/crud/products/+page.server.ts | 8 ++++---- src/routes/(sidebar)/crud/products/+page.svelte | 9 +++++++++ src/routes/(sidebar)/crud/products/Delete.svelte | 12 +++++++++++- src/routes/(sidebar)/crud/products/Product.svelte | 5 +++-- .../(sidebar)/crud/products/[id]/+page.server.ts | 12 ++++-------- .../(sidebar)/crud/products/create/+page.server.ts | 2 -- src/routes/(sidebar)/crud/users/+page.server.ts | 12 ++++++------ src/routes/(sidebar)/crud/users/+page.svelte | 13 ++++++++++++- src/routes/(sidebar)/crud/users/Delete.svelte | 14 +++++++++++++- src/routes/(sidebar)/crud/users/User.svelte | 2 +- .../(sidebar)/crud/users/[id]/+page.server.ts | 8 ++++---- 11 files changed, 67 insertions(+), 30 deletions(-) diff --git a/src/routes/(sidebar)/crud/products/+page.server.ts b/src/routes/(sidebar)/crud/products/+page.server.ts index 2c1565a..775fe23 100644 --- a/src/routes/(sidebar)/crud/products/+page.server.ts +++ b/src/routes/(sidebar)/crud/products/+page.server.ts @@ -5,16 +5,16 @@ export const actions = { delete: async ({ request }) => { const id = (await request.formData()).get('deleteId')?.toString(); - console.log('id', id); - - if (!id) return fail(400, { missingId: true }); + if (!id) return fail(400, { error: 400, message: 'Id is missing' }); const resposne = await deleteProduct(parseInt(id)); - console.log(resposne); + const responseData = await resposne.json(); if (resposne.status === 200) { throw redirect(303, '/crud/products'); } + + return fail(resposne.status, responseData); } }; diff --git a/src/routes/(sidebar)/crud/products/+page.svelte b/src/routes/(sidebar)/crud/products/+page.svelte index 361fe0a..046c6e9 100644 --- a/src/routes/(sidebar)/crud/products/+page.svelte +++ b/src/routes/(sidebar)/crud/products/+page.svelte @@ -26,12 +26,14 @@ import _ from 'underscore'; import Delete from './Delete.svelte'; import type { ListWithPagingResponse } from '$lib/apis/types'; + import Alert from '$lib/ui-components/views/alert.svelte'; const path: string = '/crud/products'; const description: string = 'A place to create product'; const title: string = 'Admin – Products list'; const subtitle: string = 'Products list'; + export let form; export let data: ListWithPagingResponse; const extractChainNames = (product: ProductResponseData) => product.attributes.filter((att) => att.name === 'Chain').map((att) => att.value); @@ -61,6 +63,13 @@ All products + {#if form?.error} + + {/if}
- +
diff --git a/src/routes/(sidebar)/crud/products/Product.svelte b/src/routes/(sidebar)/crud/products/Product.svelte index d3f3084..e0b0906 100644 --- a/src/routes/(sidebar)/crud/products/Product.svelte +++ b/src/routes/(sidebar)/crud/products/Product.svelte @@ -185,7 +185,8 @@ name="Creator" class="w-22 mb-2 me-4 mt-3 w-full" items={data.users.map((u) => ({ name: u.name, value: u.id.toString() }))} - selected={data.users.find((u) => u.id === data.product.owner.id)?.id?.toString() || ''} + selected={data.users.find((u) => u.id === data.product?.owner?.id)?.id?.toString() || + ''} selectProps={{ name: 'creator', disabled: isViewMode @@ -310,7 +311,7 @@ -
+ diff --git a/src/routes/(sidebar)/crud/products/[id]/+page.server.ts b/src/routes/(sidebar)/crud/products/[id]/+page.server.ts index 75550b5..2ac9b8f 100644 --- a/src/routes/(sidebar)/crud/products/[id]/+page.server.ts +++ b/src/routes/(sidebar)/crud/products/[id]/+page.server.ts @@ -9,8 +9,6 @@ export const actions = { const id = parseInt(params.id); const data = await request.formData(); - console.log('form', data); - let banner = data.get('banner_img') as File; let avatar = data.get('avatar_img') as File; let previews = data.getAll('previews') as File[]; @@ -67,8 +65,6 @@ export const actions = { ] }; - console.log('request', requestData); - const updateResponse = await updateProduct(id, requestData); const responseData = await updateResponse.json(); @@ -83,16 +79,16 @@ export const actions = { delete: async ({ request }) => { const id = (await request.formData()).get('deleteId')?.toString(); - console.log('id', id); - - if (!id) return fail(400, { missingId: true }); + if (!id) return fail(400, { error: 400, message: 'Id is missing' }); const resposne = await deleteProduct(parseInt(id)); - console.log(resposne); + const responseData = await resposne.json(); if (resposne.status === 200) { throw redirect(303, '/crud/products'); } + + return fail(resposne.status, responseData); } }; diff --git a/src/routes/(sidebar)/crud/products/create/+page.server.ts b/src/routes/(sidebar)/crud/products/create/+page.server.ts index 9cc09d1..d9ca0c9 100644 --- a/src/routes/(sidebar)/crud/products/create/+page.server.ts +++ b/src/routes/(sidebar)/crud/products/create/+page.server.ts @@ -64,8 +64,6 @@ export const actions = { const responseData = await createResponse.json(); - console.log('responseData', responseData); // TODO remove - if (createResponse.status === 200) { throw redirect(303, `/crud/products/${responseData.data.id}`); } else { diff --git a/src/routes/(sidebar)/crud/users/+page.server.ts b/src/routes/(sidebar)/crud/users/+page.server.ts index 5ddbbb6..2e902a8 100644 --- a/src/routes/(sidebar)/crud/users/+page.server.ts +++ b/src/routes/(sidebar)/crud/users/+page.server.ts @@ -5,16 +5,16 @@ export const actions = { delete: async ({ request }) => { const id = (await request.formData()).get('deleteId')?.toString(); - console.log('id', id); + if (!id) return fail(400, { error: 400, message: 'Id is missing' }); - if (!id) return fail(400, { missingId: true }); + const resposne = await deleteUser(parseInt(id)); - const response = await deleteUser(parseInt(id)); + const responseData = await resposne.json(); - console.log(await response.json()); - - if (response.status === 200) { + if (resposne.status === 200) { throw redirect(303, '/crud/users'); } + + return fail(resposne.status, responseData); } }; diff --git a/src/routes/(sidebar)/crud/users/+page.svelte b/src/routes/(sidebar)/crud/users/+page.svelte index 40229fe..ed225fb 100644 --- a/src/routes/(sidebar)/crud/users/+page.svelte +++ b/src/routes/(sidebar)/crud/users/+page.svelte @@ -13,6 +13,8 @@ import _ from 'underscore'; import Delete from './Delete.svelte'; import type { ListWithPagingResponse } from '$lib/apis/types'; + import { enhance } from '$app/forms'; + import Alert from '$lib/ui-components/views/alert.svelte'; const path: string = '/crud/users'; const description: string = 'A place to create user'; @@ -20,6 +22,7 @@ const subtitle: string = 'Users list'; export let data: ListWithPagingResponse; + export let form; let searchConditions: ListUserQuery = _.omit(data.paging, 'total'); async function handleSearch(pageQuery?: { limit: number; offset: number }) { @@ -46,6 +49,13 @@ All users + {#if form?.error} + + {/if} -
+ + diff --git a/src/routes/(sidebar)/crud/users/Delete.svelte b/src/routes/(sidebar)/crud/users/Delete.svelte index eeb6985..343bf57 100644 --- a/src/routes/(sidebar)/crud/users/Delete.svelte +++ b/src/routes/(sidebar)/crud/users/Delete.svelte @@ -12,7 +12,19 @@
- +
diff --git a/src/routes/(sidebar)/crud/users/User.svelte b/src/routes/(sidebar)/crud/users/User.svelte index 7454da0..46d2c7c 100644 --- a/src/routes/(sidebar)/crud/users/User.svelte +++ b/src/routes/(sidebar)/crud/users/User.svelte @@ -193,7 +193,7 @@ -
+ diff --git a/src/routes/(sidebar)/crud/users/[id]/+page.server.ts b/src/routes/(sidebar)/crud/users/[id]/+page.server.ts index f6116a2..31462dd 100644 --- a/src/routes/(sidebar)/crud/users/[id]/+page.server.ts +++ b/src/routes/(sidebar)/crud/users/[id]/+page.server.ts @@ -56,16 +56,16 @@ export const actions = { delete: async ({ request }) => { const id = (await request.formData()).get('deleteId')?.toString(); - console.log('id', id); - - if (!id) return fail(400, { missingId: true }); + if (!id) return fail(400, { error: 400, message: 'Id is missing' }); const resposne = await deleteUser(parseInt(id)); - console.log(resposne); + const responseData = await resposne.json(); if (resposne.status === 200) { throw redirect(303, '/crud/users'); } + + return fail(resposne.status, responseData); } };