From 41cc82d54dcdff8136b7ff17dddcb46a2b732a9f Mon Sep 17 00:00:00 2001 From: louiscklaw Date: Wed, 23 Apr 2025 02:02:47 +0800 Subject: [PATCH] update customers, --- .../cms/src/app/dashboard/customers/page.tsx | 14 +- .../customer/confirm-delete-modal.tsx | 5 +- .../customer/customer-create-form.tsx | 227 ++++++++++++++---- .../dashboard/customer/customer-edit-form.tsx | 5 +- .../components/dashboard/customer/type.d.tsx | 19 +- 002_source/cms/src/db/Customers/Create.tsx | 7 +- 6 files changed, 215 insertions(+), 62 deletions(-) diff --git a/002_source/cms/src/app/dashboard/customers/page.tsx b/002_source/cms/src/app/dashboard/customers/page.tsx index 1edb4a0..c016c5e 100644 --- a/002_source/cms/src/app/dashboard/customers/page.tsx +++ b/002_source/cms/src/app/dashboard/customers/page.tsx @@ -96,15 +96,13 @@ export default function Page({ searchParams }: PageProps): React.JSX.Element { React.useEffect(() => { if (!isFirstRun.current) { isFirstRun.current = true; + } else if (JSON.stringify(listOption) !== JSON.stringify(lastListOption)) { + // reset page number as tab changes + setLastListOption(listOption); + setCurrentPage(0); + void reloadRows(); } else { - if (JSON.stringify(listOption) !== JSON.stringify(lastListOption)) { - // reset page number as tab changes - setLastListOption(listOption); - setCurrentPage(0); - void reloadRows(); - } else { - void reloadRows(); - } + void reloadRows(); } }, [currentPage, rowsPerPage, listOption]); diff --git a/002_source/cms/src/components/dashboard/customer/confirm-delete-modal.tsx b/002_source/cms/src/components/dashboard/customer/confirm-delete-modal.tsx index 4d5b267..ba9aad5 100644 --- a/002_source/cms/src/components/dashboard/customer/confirm-delete-modal.tsx +++ b/002_source/cms/src/components/dashboard/customer/confirm-delete-modal.tsx @@ -16,6 +16,7 @@ import { useTranslation } from 'react-i18next'; import { logger } from '@/lib/default-logger'; import { toast } from '@/components/core/toaster'; +import { deleteCustomer } from '@/db/Customers/Delete'; export default function ConfirmDeleteModal({ open, @@ -46,7 +47,9 @@ export default function ConfirmDeleteModal({ function handleUserConfirmDelete(): void { if (idToDelete) { setIsDeleteing(true); - deleteQuizLPCategories(idToDelete) + + // RULES: delete + deleteCustomer(idToDelete) .then(() => { reloadRows(); handleClose(); diff --git a/002_source/cms/src/components/dashboard/customer/customer-create-form.tsx b/002_source/cms/src/components/dashboard/customer/customer-create-form.tsx index 7be8fc8..046f16e 100644 --- a/002_source/cms/src/components/dashboard/customer/customer-create-form.tsx +++ b/002_source/cms/src/components/dashboard/customer/customer-create-form.tsx @@ -29,6 +29,8 @@ import { paths } from '@/paths'; import { logger } from '@/lib/default-logger'; import { Option } from '@/components/core/option'; import { toast } from '@/components/core/toaster'; +import { createCustomer } from '@/db/Customers/Create'; +import isDevelopment from '@/lib/check-is-development'; function fileToBase64(file: Blob): Promise { return new Promise((resolve, reject) => { @@ -67,12 +69,19 @@ type Values = zod.infer; const defaultValues = { avatar: '', - name: '', - email: '', - phone: '', + name: 'new name', + email: '123@123.com', + phone: '91234567', company: '', - billingAddress: { country: '', state: '', city: '', zipCode: '', line1: '', line2: '' }, - taxId: '', + billingAddress: { + country: 'US', + state: '00000', + city: 'NY', + zipCode: '00000', + line1: 'test line 1', + line2: 'test line 2', + }, + taxId: '12345', timezone: 'new_york', language: 'en', currency: 'USD', @@ -90,14 +99,15 @@ export function CustomerCreateForm(): React.JSX.Element { } = useForm({ defaultValues, resolver: zodResolver(schema) }); const onSubmit = React.useCallback( - async (_: Values): Promise => { + async (values: Values): Promise => { try { - // Make API request - toast.success('Customer updated'); - router.push(paths.dashboard.customers.details('1')); + // Use standard create method from db/Customers/Create + const record = await createCustomer(values); + toast.success('Customer created'); + router.push(paths.dashboard.customers.details(record.id)); } catch (err) { logger.error(err); - toast.error('Something went wrong!'); + toast.error('Failed to create customer'); } }, [router] @@ -122,12 +132,22 @@ export function CustomerCreateForm(): React.JSX.Element {
- } spacing={4}> + } + spacing={4} + > Account information - + - + - + Avatar Min 400x400px, PNG or JPEG - + - + ( - + Name {errors.name ? {errors.name.message} : null} @@ -180,25 +214,40 @@ export function CustomerCreateForm(): React.JSX.Element { )} /> - + ( - + Email address - + {errors.email ? {errors.email.message} : null} )} /> - + ( - + Phone number {errors.phone ? {errors.phone.message} : null} @@ -206,12 +255,18 @@ export function CustomerCreateForm(): React.JSX.Element { )} /> - + ( - + Company {errors.company ? {errors.company.message} : null} @@ -223,13 +278,22 @@ export function CustomerCreateForm(): React.JSX.Element { Billing information - - + + ( - + Country @@ -344,12 +453,18 @@ export function CustomerCreateForm(): React.JSX.Element { )} /> - + ( - + Language @@ -385,14 +506,24 @@ export function CustomerCreateForm(): React.JSX.Element { - - + +
{JSON.stringify({ errors }, null, 2)}
+
); } diff --git a/002_source/cms/src/components/dashboard/customer/customer-edit-form.tsx b/002_source/cms/src/components/dashboard/customer/customer-edit-form.tsx index e60adaf..8e9d211 100644 --- a/002_source/cms/src/components/dashboard/customer/customer-edit-form.tsx +++ b/002_source/cms/src/components/dashboard/customer/customer-edit-form.tsx @@ -46,7 +46,7 @@ import isDevelopment from '@/lib/check-is-development'; const schema = zod.object({ name: zod.string().min(1, 'Name is required').max(255), email: zod.string().email('Must be a valid email').min(1, 'Email is required').max(255), - phone: zod.string().min(1, 'Phone is required').max(15), + phone: zod.string().min(1, 'Phone is required').max(25), company: zod.string().max(255).optional(), billingAddress: zod.object({ country: zod.string().min(1, 'Country is required').max(255), @@ -596,6 +596,9 @@ export function CustomerEditForm(): React.JSX.Element { + +
{JSON.stringify({ errors }, null, 2)}
+
); } diff --git a/002_source/cms/src/components/dashboard/customer/type.d.tsx b/002_source/cms/src/components/dashboard/customer/type.d.tsx index 85e31b4..a9c7cde 100644 --- a/002_source/cms/src/components/dashboard/customer/type.d.tsx +++ b/002_source/cms/src/components/dashboard/customer/type.d.tsx @@ -16,11 +16,24 @@ export interface Customer { export interface CreateFormProps { name: string; - avatar?: string; email: string; phone?: string; - quota: number; - status: 'pending' | 'active' | 'blocked'; + company?: string; + billingAddress?: { + country: string; + state: string; + city: string; + zipCode: string; + line1: string; + line2?: string; + }; + taxId?: string; + timezone: string; + language: string; + currency: string; + avatar?: string; + // quota?: number; + // status?: 'pending' | 'active' | 'blocked'; } export interface EditFormProps { diff --git a/002_source/cms/src/db/Customers/Create.tsx b/002_source/cms/src/db/Customers/Create.tsx index 436eb72..957f577 100644 --- a/002_source/cms/src/db/Customers/Create.tsx +++ b/002_source/cms/src/db/Customers/Create.tsx @@ -1,6 +1,11 @@ +// api method for crate customer record +// RULES: +// TBA import { pb } from '@/lib/pb'; import { COL_CUSTOMERS } from '@/constants'; +import type { CreateFormProps } from '@/components/dashboard/customer/type.d'; +import type { RecordModel } from 'pocketbase'; -export async function createCustomer(data) { +export async function createCustomer(data: CreateFormProps): Promise { return pb.collection(COL_CUSTOMERS).create(data); }