diff --git a/CHANGELOG.md b/CHANGELOG.md index bf524b3..b452e22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,34 +2,30 @@ ## [1.7.0](https://github.com/arkews/joshub/compare/v1.6.0...v1.7.0) (2022-12-29) - ### Features -* add session check logic ([a836245](https://github.com/arkews/joshub/commit/a83624549aaca1c88f28357820f972006315d422)) - +- add session check logic ([a836245](https://github.com/arkews/joshub/commit/a83624549aaca1c88f28357820f972006315d422)) ### Bug Fixes -* avoid unnecessary redirections in the verification of the existence of a pub ([be2495c](https://github.com/arkews/joshub/commit/be2495cc60e693bb89c45b7f39c28471d1f22844)) +- avoid unnecessary redirections in the verification of the existence of a pub ([be2495c](https://github.com/arkews/joshub/commit/be2495cc60e693bb89c45b7f39c28471d1f22844)) ## [1.6.0](https://github.com/arkews/joshub/compare/v1.5.1...v1.6.0) (2022-12-28) - ### Features -* add find customer by id api endpoint ([f62186a](https://github.com/arkews/joshub/commit/f62186a03e3e3cdad48960b7c42d53b1dafd293d)) -* **employees:** new register employee form ui ([ad3b233](https://github.com/arkews/joshub/commit/ad3b233f3c0f4fb468c5ae22997112b9815f8849)) -* new customer field ui ([44c5a6a](https://github.com/arkews/joshub/commit/44c5a6ad632e8b9d90b2e0cd86965f4fdf99c96c)) -* **products:** add new register form ui ([cc42c55](https://github.com/arkews/joshub/commit/cc42c556b56c07dcad7790fa4fc98b3e18337e22)) -* **sales:** new orders form ([e70b09e](https://github.com/arkews/joshub/commit/e70b09eaaf056d0579bd274b491daed64231d03a)) -* **sales:** new sales form ([bed19c0](https://github.com/arkews/joshub/commit/bed19c020abda008b0b7852037b63051d3536667)) -* **transaction-details:** add detail card ([601f5cd](https://github.com/arkews/joshub/commit/601f5cd33ffe3327a91142daf8cc64537602bddc)) -* **transaction-details:** add remove detail support ([94bf227](https://github.com/arkews/joshub/commit/94bf227a0662e674a5fe45dc753f6f4962ae7e35)) - +- add find customer by id api endpoint ([f62186a](https://github.com/arkews/joshub/commit/f62186a03e3e3cdad48960b7c42d53b1dafd293d)) +- **employees:** new register employee form ui ([ad3b233](https://github.com/arkews/joshub/commit/ad3b233f3c0f4fb468c5ae22997112b9815f8849)) +- new customer field ui ([44c5a6a](https://github.com/arkews/joshub/commit/44c5a6ad632e8b9d90b2e0cd86965f4fdf99c96c)) +- **products:** add new register form ui ([cc42c55](https://github.com/arkews/joshub/commit/cc42c556b56c07dcad7790fa4fc98b3e18337e22)) +- **sales:** new orders form ([e70b09e](https://github.com/arkews/joshub/commit/e70b09eaaf056d0579bd274b491daed64231d03a)) +- **sales:** new sales form ([bed19c0](https://github.com/arkews/joshub/commit/bed19c020abda008b0b7852037b63051d3536667)) +- **transaction-details:** add detail card ([601f5cd](https://github.com/arkews/joshub/commit/601f5cd33ffe3327a91142daf8cc64537602bddc)) +- **transaction-details:** add remove detail support ([94bf227](https://github.com/arkews/joshub/commit/94bf227a0662e674a5fe45dc753f6f4962ae7e35)) ### Refactors -* remove unused component and page ([fd805ef](https://github.com/arkews/joshub/commit/fd805efb23ad5a6a4ae527c70f5fab0d9d52dc75)) +- remove unused component and page ([fd805ef](https://github.com/arkews/joshub/commit/fd805efb23ad5a6a4ae527c70f5fab0d9d52dc75)) ## [1.5.1](https://github.com/arkews/joshub/compare/v1.5.0...v1.5.1) (2022-12-27) diff --git a/components/pubs/register/index.tsx b/components/pubs/register/index.tsx index f568ff6..6bdee03 100644 --- a/components/pubs/register/index.tsx +++ b/components/pubs/register/index.tsx @@ -5,6 +5,20 @@ import { PubInputs } from '@joshub/types/pubs' import { useRouter } from 'next/router' import { FC, useEffect } from 'react' import { useUser } from '@supabase/auth-helpers-react' +import { z } from 'zod' +import { zodResolver } from '@hookform/resolvers/zod' + +const PubSchema = z.object({ + name: z.string().min(1, 'El nombre es requerido'), + address: z.string().min(1, 'La dirección es requerida'), + user_id: z.string().min(1, 'El dueño es requerido'), + owner: z.object({ + id: z.string().min(1, 'La identificación es requerida'), + name: z.string().min(1, 'El nombre es requerido'), + phone: z.string().min(1, 'El teléfono es requerido'), + salary: z.coerce.number().min(1, 'El salario es requerido') + }) +}) const RegisterPubForm: FC = () => { const user = useUser() @@ -12,17 +26,18 @@ const RegisterPubForm: FC = () => { register, setValue, handleSubmit, - formState: { errors } + formState: { errors, isSubmitting } } = useForm({ + resolver: zodResolver(PubSchema), defaultValues: { - owner: user?.id, + user_id: user?.id, nit: undefined } }) useEffect(() => { if (user !== undefined && user !== null) { - setValue('owner', user.id) + setValue('user_id', user.id) } }, [user]) @@ -31,7 +46,7 @@ const RegisterPubForm: FC = () => { } const router = useRouter() - const { mutate, isLoading, error } = useMutation(savePub, { + const { mutate, isLoading } = useMutation(savePub, { onSuccess: () => { void router.push('/dashboard') } @@ -44,91 +59,148 @@ const RegisterPubForm: FC = () => { } return ( -
-
-
-
-
-
- + +
+
+
+

Bar

+ +
+ + {errors.name != null && ( +

+ {errors.name.message} +

+ )} +
- {errors.name != null && ( - - Este campo es requerido - - )} -
- -
- +
+ + {errors.nit != null && ( +

+ {errors.nit.message} +

+ )} +
+ +
+ + {errors.address != null && ( +

+ {errors.address.message} +

+ )} +
+
+ +
+

Propietario

+ +
+
- -
- + + {errors.owner?.id != null && ( +

+ {errors.owner.id.message} +

+ )} +
+ +
+ + {errors.owner?.name != null && ( +

+ {errors.owner.name.message} +

+ )} +
- {errors.address != null && ( - - Este campo es requerido - - )} -
- - {Boolean(error) && ( -
- Error al registrar el bar. -
+
+ + {errors.owner?.phone != null && ( +

+ {errors.owner.phone.message} +

)} +
-
- -
+
+ + {errors.owner?.salary != null && ( +

+ {errors.owner.salary.message} +

+ )}
- -
+ +
+ +
+
+ ) } diff --git a/pages/api/pubs/index.ts b/pages/api/pubs/index.ts index 8ba0ed2..c1cd4ab 100644 --- a/pages/api/pubs/index.ts +++ b/pages/api/pubs/index.ts @@ -6,23 +6,53 @@ const handler = async ( res: NextApiResponse ): Promise => { if (req.method === 'POST') { - const supabase = createServerSupabaseClient({ req, res }) + const supabase = createServerSupabaseClient({ + req, + res + }) const { body } = req + const { owner, user_id: userId, ...rest } = body + const pub = { + ...rest, + owner: userId + } - const { data, error } = await supabase.from('pubs').insert(body).select() + const { data, error } = await supabase.from('pubs').insert(pub).select() if (error != null) { res.status(500).json({ error: error.message }) return } - res.status(200).json(data) + const pubId = data[0].id + + const { error: ownerError } = await supabase + .from('employees') + .insert({ + ...owner, + pub_id: pubId, + user_id: userId + }) + .select() + + if (ownerError != null) { + // delete the pub + await supabase.from('pubs').delete().eq('id', pubId) + + res.status(500).json({ error: ownerError.message }) + return + } + + res.status(200).json(data[0]) return } if (req.method === 'GET') { - const supabase = createServerSupabaseClient({ req, res }) + const supabase = createServerSupabaseClient({ + req, + res + }) const { owner } = req.query diff --git a/pages/pubs/register/index.tsx b/pages/pubs/register/index.tsx index f2d04db..f98fabb 100644 --- a/pages/pubs/register/index.tsx +++ b/pages/pubs/register/index.tsx @@ -57,12 +57,14 @@ const RegisterPubPage: NextPage = () => { )} {!isLoading && pubs === undefined && ( <> -
-

+
+

Registrar bar

+

+ +
- )}

diff --git a/types/pubs/index.ts b/types/pubs/index.ts index f39c91f..5aa18fb 100644 --- a/types/pubs/index.ts +++ b/types/pubs/index.ts @@ -1,8 +1,14 @@ +import { Employee } from '@joshub/types/employees' + +type PubOwnerInputs = Omit + export interface PubInputs { name: string nit?: string address: string - owner: string + owner: PubOwnerInputs + + user_id: string } export type Pub = PubInputs & { id: string, created_at: string }