From b8f3f36c4a7a932ec873f60644eb09319875de64 Mon Sep 17 00:00:00 2001 From: r1skz3ro Date: Thu, 4 Jul 2024 13:36:41 +0200 Subject: [PATCH] feat: code review reafactor --- .../(peopleFlow)/people/add-new/layout.tsx | 2 +- .../people/add-new/personal-details/page.tsx | 84 +------------------ frontend/src/app/(app)/(root)/people/page.tsx | 10 ++- .../EmployeeSideStepper.tsx | 2 +- .../PersonalDetails/PersonalDetails.hooks.ts | 31 +++++++ .../PersonalDetails.interface.ts | 11 +++ .../pages/PersonalDetails/PersonalDetails.tsx | 50 +++++++++++ .../components/pages/PersonalDetails/index.ts | 1 + frontend/src/store/people/index.ts | 1 + frontend/src/store/people/interfaces.ts | 9 ++ frontend/src/store/people/store.ts | 18 ++++ frontend/src/store/peopleStore.ts | 27 ------ frontend/tailwind.config.ts | 5 +- 13 files changed, 135 insertions(+), 116 deletions(-) create mode 100644 frontend/src/components/pages/PersonalDetails/PersonalDetails.hooks.ts create mode 100644 frontend/src/components/pages/PersonalDetails/PersonalDetails.interface.ts create mode 100644 frontend/src/components/pages/PersonalDetails/PersonalDetails.tsx create mode 100644 frontend/src/components/pages/PersonalDetails/index.ts create mode 100644 frontend/src/store/people/index.ts create mode 100644 frontend/src/store/people/interfaces.ts create mode 100644 frontend/src/store/people/store.ts delete mode 100644 frontend/src/store/peopleStore.ts diff --git a/frontend/src/app/(app)/(peopleFlow)/people/add-new/layout.tsx b/frontend/src/app/(app)/(peopleFlow)/people/add-new/layout.tsx index 14539723..c51e4e58 100644 --- a/frontend/src/app/(app)/(peopleFlow)/people/add-new/layout.tsx +++ b/frontend/src/app/(app)/(peopleFlow)/people/add-new/layout.tsx @@ -7,7 +7,7 @@ export default function PeopleLayout({ children }: Readonly<{ children: React.Re
-
+
{children}
diff --git a/frontend/src/app/(app)/(peopleFlow)/people/add-new/personal-details/page.tsx b/frontend/src/app/(app)/(peopleFlow)/people/add-new/personal-details/page.tsx index a894ea09..41475a48 100644 --- a/frontend/src/app/(app)/(peopleFlow)/people/add-new/personal-details/page.tsx +++ b/frontend/src/app/(app)/(peopleFlow)/people/add-new/personal-details/page.tsx @@ -1,83 +1,3 @@ -'use client'; -import { Button } from '@app/components/common/Button'; -import { FormProvider } from '@app/components/common/FormProvider'; -import { Input } from '@app/components/common/Input'; -import { Typography } from '@app/components/common/Typography'; -import { routes } from '@app/constants'; -import { usePeopleStore } from '@app/store/peopleStore'; -import { useEffect, useState } from 'react'; -import { useForm } from 'react-hook-form'; +import { PersonalDetails } from '@app/components/pages/PersonalDetails/PersonalDetails'; -enum PersonalDetailsFormNames { - firstName = 'firstName', - lastName = 'lastName', - email = 'email', -} -interface PersonalDetailsForm { - [PersonalDetailsFormNames.firstName]: string; - [PersonalDetailsFormNames.lastName]: string; - [PersonalDetailsFormNames.email]: string; -} - -export default function PersonalDetails() { - const form = useForm({ - mode: 'onChange', - defaultValues: { - [PersonalDetailsFormNames.firstName]: '', - [PersonalDetailsFormNames.lastName]: '', - [PersonalDetailsFormNames.email]: '', - }, - }); - const { isDirty, isValid } = form.formState; - const [formValid, setFormValid] = useState(false); - const updateProgress = usePeopleStore((state) => state.updateProgress); - - useEffect(() => { - setFormValid(isDirty && isValid); - }, [isDirty, isValid]); - - // INFO: update progress in sidebar stepper - useEffect(() => { - if (formValid) updateProgress({ [routes.people.addNew.personalDetails]: 'completed' }); - else updateProgress({ [routes.people.addNew.personalDetails]: 'inProgress' }); - }, [formValid, updateProgress]); - - return ( - form={form}> - - Personal details - -
- - - -
-
- -
- - ); -} +export default PersonalDetails; diff --git a/frontend/src/app/(app)/(root)/people/page.tsx b/frontend/src/app/(app)/(root)/people/page.tsx index 24872115..5ba7e75a 100644 --- a/frontend/src/app/(app)/(root)/people/page.tsx +++ b/frontend/src/app/(app)/(root)/people/page.tsx @@ -5,10 +5,12 @@ import Link from 'next/link'; export default function People() { return (
-

People

- +
+

People

+ +
); } diff --git a/frontend/src/components/modules/EmployeeSideStepper/EmployeeSideStepper.tsx b/frontend/src/components/modules/EmployeeSideStepper/EmployeeSideStepper.tsx index dc6ba550..4bbff74f 100644 --- a/frontend/src/components/modules/EmployeeSideStepper/EmployeeSideStepper.tsx +++ b/frontend/src/components/modules/EmployeeSideStepper/EmployeeSideStepper.tsx @@ -1,10 +1,10 @@ 'use client'; -import { usePeopleStore } from '@app/store/peopleStore'; import { SideStepper, StepStates } from '../SideStepper'; import { useEffect, useState } from 'react'; import { usePathname } from 'next/navigation'; import { AddNewPersonRouteKeys } from './EmployeeSideStepper.interface'; import { addEmployeeInitialSteps } from './EmployeeSideStepper.const'; +import { usePeopleStore } from '@app/store/people/store'; export const EmployeeSideStepper = () => { const progress = usePeopleStore((state) => state.progress); diff --git a/frontend/src/components/pages/PersonalDetails/PersonalDetails.hooks.ts b/frontend/src/components/pages/PersonalDetails/PersonalDetails.hooks.ts new file mode 100644 index 00000000..f7fcda6e --- /dev/null +++ b/frontend/src/components/pages/PersonalDetails/PersonalDetails.hooks.ts @@ -0,0 +1,31 @@ +import { routes } from '@app/constants'; +import { useEffect, useState } from 'react'; +import { useForm } from 'react-hook-form'; +import { PersonalDetailsForm, PersonalDetailsFormNames } from './PersonalDetails.interface'; +import { usePeopleStore } from '@app/store/people/store'; + +export const usePersonalDetails = () => { + const form = useForm({ + mode: 'onChange', + defaultValues: { + [PersonalDetailsFormNames.firstName]: '', + [PersonalDetailsFormNames.lastName]: '', + [PersonalDetailsFormNames.email]: '', + }, + }); + const { isDirty, isValid } = form.formState; + const [formValid, setFormValid] = useState(false); + const updateProgress = usePeopleStore((state) => state.updateProgress); + + useEffect(() => { + setFormValid(isDirty && isValid); + }, [isDirty, isValid]); + + // INFO: update progress in sidebar stepper + useEffect(() => { + if (formValid) updateProgress({ [routes.people.addNew.personalDetails]: 'completed' }); + else updateProgress({ [routes.people.addNew.personalDetails]: 'inProgress' }); + }, [formValid, updateProgress]); + + return { form, formValid }; +}; diff --git a/frontend/src/components/pages/PersonalDetails/PersonalDetails.interface.ts b/frontend/src/components/pages/PersonalDetails/PersonalDetails.interface.ts new file mode 100644 index 00000000..503ad06a --- /dev/null +++ b/frontend/src/components/pages/PersonalDetails/PersonalDetails.interface.ts @@ -0,0 +1,11 @@ +export const PersonalDetailsFormNames = { + firstName: 'firstName', + lastName: 'lastName', + email: 'email', +} as const; + +export interface PersonalDetailsForm { + [PersonalDetailsFormNames.firstName]: string; + [PersonalDetailsFormNames.lastName]: string; + [PersonalDetailsFormNames.email]: string; +} diff --git a/frontend/src/components/pages/PersonalDetails/PersonalDetails.tsx b/frontend/src/components/pages/PersonalDetails/PersonalDetails.tsx new file mode 100644 index 00000000..86aee9a4 --- /dev/null +++ b/frontend/src/components/pages/PersonalDetails/PersonalDetails.tsx @@ -0,0 +1,50 @@ +'use client'; +import { Button } from '@app/components/common/Button'; +import { FormProvider } from '@app/components/common/FormProvider'; +import { Input } from '@app/components/common/Input'; +import { Typography } from '@app/components/common/Typography'; +import { PersonalDetailsForm, PersonalDetailsFormNames } from './PersonalDetails.interface'; +import { usePersonalDetails } from './PersonalDetails.hooks'; + +export const PersonalDetails = () => { + const { form, formValid } = usePersonalDetails(); + + return ( + form={form}> + + Personal details + +
+ + + +
+
+ +
+ + ); +}; diff --git a/frontend/src/components/pages/PersonalDetails/index.ts b/frontend/src/components/pages/PersonalDetails/index.ts new file mode 100644 index 00000000..865eb9da --- /dev/null +++ b/frontend/src/components/pages/PersonalDetails/index.ts @@ -0,0 +1 @@ +export { PersonalDetails } from './PersonalDetails'; diff --git a/frontend/src/store/people/index.ts b/frontend/src/store/people/index.ts new file mode 100644 index 00000000..f201f623 --- /dev/null +++ b/frontend/src/store/people/index.ts @@ -0,0 +1 @@ +export { usePeopleStore } from './store'; diff --git a/frontend/src/store/people/interfaces.ts b/frontend/src/store/people/interfaces.ts new file mode 100644 index 00000000..54cc8f3f --- /dev/null +++ b/frontend/src/store/people/interfaces.ts @@ -0,0 +1,9 @@ +import { AddNewPersonRouteKeys } from '@app/components/modules/EmployeeSideStepper'; +import { StepStates } from '@app/components/modules/SideStepper'; + +export type Progress = Record; + +export interface PeopleState { + progress: Progress; + updateProgress: (newProgress: Partial) => void; +} diff --git a/frontend/src/store/people/store.ts b/frontend/src/store/people/store.ts new file mode 100644 index 00000000..0ff3219a --- /dev/null +++ b/frontend/src/store/people/store.ts @@ -0,0 +1,18 @@ +import { routes } from '@app/constants'; +import { create } from 'zustand'; +import { PeopleState } from './interfaces'; + +const initialState: PeopleState = { + progress: { + [routes.people.addNew.personalDetails]: 'notStarted', + [routes.people.addNew.mainLadder]: 'notStarted', + }, + updateProgress: () => {}, +}; + +const usePeopleStore = create()((set) => ({ + ...initialState, + updateProgress: (newProgress) => set((state) => ({ progress: { ...state.progress, ...newProgress } })), +})); + +export { usePeopleStore }; diff --git a/frontend/src/store/peopleStore.ts b/frontend/src/store/peopleStore.ts deleted file mode 100644 index d7aed9e1..00000000 --- a/frontend/src/store/peopleStore.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { AddNewPersonRouteKeys } from '@app/components/modules/EmployeeSideStepper'; -import { StepStates } from '@app/components/modules/SideStepper'; -import { routes } from '@app/constants'; -import { create } from 'zustand'; - -interface PeopleState { - progress: Record; - updateProgress: (newProgress: Record) => void; -} - -const initialState: PeopleState = { - progress: { - [routes.people.addNew.personalDetails]: 'notStarted', - [routes.people.addNew.mainLadder]: 'notStarted', - }, - updateProgress: () => {}, -}; - -const usePeopleStore = create()((set) => ({ - ...initialState, - updateProgress: (newProgress: Record) => - set((state) => { - return { progress: { ...state.progress, ...newProgress } }; - }), -})); - -export { usePeopleStore }; diff --git a/frontend/tailwind.config.ts b/frontend/tailwind.config.ts index 62511f27..8b633604 100644 --- a/frontend/tailwind.config.ts +++ b/frontend/tailwind.config.ts @@ -52,7 +52,7 @@ const config: Config = { }, grey: { 800: '#2C2E3A', - } + }, }, fontSize: { xs: ['0.75rem', '1rem'], // Body XS @@ -70,6 +70,9 @@ const config: Config = { 'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))', 'gradient-conic': 'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))', }, + gridTemplateColumns: { + workflow: 'minmax(200px,1fr),minmax(400px,1100px),1fr', + }, }, }, plugins: [require('@tailwindcss/typography')],