Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FE][CPF-65] Add employee - personal details #77

Merged
merged 15 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion frontend/.husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# hooks start from the project root directory

cd frontend

yarn lint:fix && yarn format
yarn lint:fix
yarn format
yarn compile
5 changes: 4 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
},
"dependencies": {
"@headlessui/react": "^2.0.4",
"@hookform/error-message": "^2.0.1",
"clsx": "^2.1.0",
"next": "14.1.3",
"react": "^18",
"react-dom": "^18",
"react-hook-form": "^7.52.0",
"react-easy-crop": "^5.0.7",
"react-markdown": "^9.0.1",
"react-tooltip": "^5.26.3",
"remark-gfm": "^4.0.0",
"tailwind-merge": "^2.2.2"
"tailwind-merge": "^2.2.2",
"zustand": "^4.5.4"
},
"devDependencies": {
"@tailwindcss/typography": "^0.5.13",
Expand Down
18 changes: 18 additions & 0 deletions frontend/src/app/(app)/(peopleFlow)/people/add-new/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { EmployeeSideStepper } from '@app/components/modules/EmployeeSideStepper';
import { WorkflowTopbar } from '@app/components/modules/WorkflowTopbar';

export default function PeopleLayout({ children }: Readonly<{ children: React.ReactNode }>) {
return (
<div className="flex">
<div className="w-full">
<WorkflowTopbar />
<main className="p-8">
<div className="grid grid-cols-[minmax(200px,1fr),minmax(400px,1100px),1fr]">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming it's not a one-time use, perhaps it would be worth adding this grid cold to the theme?

<EmployeeSideStepper />
<div className="col-span-1 px-8">{children}</div>
</div>
</main>
</div>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
'use client';
Copy link
Collaborator Author

@r1skz3ro r1skz3ro Jul 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when we set the convention of file and component structure/architecture I will get rid of that line of shame 😄

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned on the channel. Let's go for the interface files, hooks (logic), index and component that takes data from the hook

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left that there because the whole page is a form and needs JS interactivity

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';

enum PersonalDetailsFormNames {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have established to use consts instead of enums

firstName = 'firstName',
lastName = 'lastName',
email = 'email',
}
interface PersonalDetailsForm {
[PersonalDetailsFormNames.firstName]: string;
[PersonalDetailsFormNames.lastName]: string;
[PersonalDetailsFormNames.email]: string;
}

export default function PersonalDetails() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to our agreements about components structure, this should be moved to src/components/pages/PersonalDetails/. Then you can extract the logic to PersonalDetails.hooks.ts.

const form = useForm<PersonalDetailsForm>({
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 (
<FormProvider<PersonalDetailsForm> form={form}>
<Typography variant="head-m/semibold" className="mb-6">
Personal details
</Typography>
<div className="mb-6 grid w-full grid-cols-2 gap-x-8 gap-y-6 rounded-[20px] border border-navy-200 bg-white p-8">
<Input
name={PersonalDetailsFormNames.firstName}
label="First name"
options={{
minLength: { value: 2, message: 'First name must contain at least 2 characters' },
required: { value: true, message: 'First Name is required' },
}}
/>
<Input
name={PersonalDetailsFormNames.lastName}
label="Last name"
options={{
minLength: { value: 2, message: 'Last name must contain at least 2 characters' },
required: { value: true, message: 'Last name is required' },
}}
/>
<Input
name={PersonalDetailsFormNames.email}
label="Email"
options={{
pattern: { value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i, message: 'invalid email address' },
required: { value: true, message: 'Email is required' },
}}
/>
</div>
<div className="flex justify-end">
<Button styleType="primary" variant="border" onClick={(e) => e.preventDefault()} disabled={!formValid}>
Continue
</Button>
</div>
</FormProvider>
);
}
14 changes: 14 additions & 0 deletions frontend/src/app/(app)/(root)/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Sidebar } from '@app/components/modules/Sidebar';
import { Topbar } from '@app/components/modules/Topbar';

export default function AppLayout({ children }: Readonly<{ children: React.ReactNode }>) {
return (
<div className="flex">
<Sidebar />
<div className="w-full">
<Topbar />
<main className="p-8">{children}</main>
</div>
</div>
);
}
File renamed without changes.
14 changes: 14 additions & 0 deletions frontend/src/app/(app)/(root)/people/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Button } from '@app/components/common/Button';
import { routes } from '@app/constants';
import Link from 'next/link';

export default function People() {
return (
<div>
<h1 className="mb-10 text-lg font-semibold leading-6 text-navy-900">People</h1>
<Button>
<Link href={routes.people.addNew.personalDetails}>+ Employee</Link>
</Button>
</div>
);
}
33 changes: 0 additions & 33 deletions frontend/src/app/(app)/layout.tsx

This file was deleted.

11 changes: 0 additions & 11 deletions frontend/src/app/(app)/people/page.tsx

This file was deleted.

19 changes: 19 additions & 0 deletions frontend/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { Metadata } from 'next';
import { Inter } from 'next/font/google';

import './globals.css';

const inter = Inter({ subsets: ['latin'] });

export const metadata: Metadata = {
title: 'CPF - Career Progression Framework',
description: 'Career Progression Framework Application',
};

export default function RootLayout({ children }: Readonly<{ children: React.ReactNode }>) {
return (
<html lang="en">
<body className={`${inter.className} h-full bg-navy-50`}>{children}</body>
</html>
);
}
5 changes: 2 additions & 3 deletions frontend/src/components/common/Button/Button.interface.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { ButtonHTMLAttributes, ReactNode } from 'react';
import { ButtonHTMLAttributes } from 'react';

export interface Props extends ButtonHTMLAttributes<HTMLButtonElement> {
export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
styleType?: StyleTypes;
variant?: Variants;
disabled?: boolean;
className?: string;
children: ReactNode;
}

export type StyleTypes = 'primary' | 'natural' | 'warning';
Expand Down
25 changes: 16 additions & 9 deletions frontend/src/components/common/Button/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { generateClassNames } from '@app/utils';
import React from 'react';
import { Props, StyleTypes, Variants } from './Button.interface';
import React, { FC } from 'react';
import { ButtonProps, StyleTypes, Variants } from './Button.interface';

const types: {
[key in StyleTypes]: {
Expand All @@ -10,38 +10,45 @@ const types: {
primary: {
solid: 'bg-blue-600 text-white hover:bg-blue-900',
border:
'bg-white text-blue-800 border border-blue-800 hover:bg-navy-100 active:bg-white active:text-blue-900 active:border-blue-900',
borderless: 'px-2 bg-transparent text-blue-800 hover:bg-navy-100 active:bg-white active:text-blue-900',
'bg-transparent text-blue-800 border border-blue-800 hover:bg-navy-100 active:text-blue-900 active:border-blue-900',
borderless: 'px-2 bg-transparent text-blue-800 hover:bg-navy-100 active:text-blue-900',
link: 'px-0 bg-transparent text-blue-800 hover:underline hover:text-blue-900 active:text-blue-900 active:no-underline',
},
natural: {
solid: 'bg-navy-600 text-white hover:bg-navy-700',
border: 'bg-white text-navy-600 border border-navy-300 hover:bg-navy-100',
border: 'bg-transparent text-navy-600 border border-navy-300 hover:bg-navy-100',
borderless: 'px-2 bg-transparent text-navy-600 hover:bg-navy-50 active:bg-navy-100',
link: 'px-0 bg-transparent text-navy-600 hover:underline hover:text-navy-700 active:no-underline',
},
warning: {
solid: 'bg-red-600 text-white hover:bg-red-700',
border: 'bg-white text-red-600 border border-red-600 hover:bg-navy-100 hover:text-red-700',
border: 'bg-transparent text-red-600 border border-red-600 hover:bg-navy-100 hover:text-red-700',
borderless:
'px-2 bg-transparent text-red-600 hover:bg-navy-50 hover:border-[1.5px] hover:border-red-700 hover:text-red-700',
link: 'px-0 bg-transparent text-red-600 hover:underline hover:text-red-700 active:no-underline',
},
};

export const Button = ({
const disabledStyles = {
solid: 'bg-navy-200',
border: 'bg-transparent border-navy-200 border',
borderless: 'px-2 border-none',
link: 'px-0 border-none',
};

export const Button: FC<ButtonProps> = ({
styleType = 'primary',
variant = 'solid',
disabled = false,
className,
children,
...props
}: Props) => {
}) => {
const buttonClass = generateClassNames(
'h-11 px-5 items-center justify-center rounded-full transition duration-200',
{
[types[styleType][variant]]: !disabled,
'bg-navy-200 border-navy-200 text-navy-300 cursor-not-allowed': disabled,
[`text-navy-300 cursor-not-allowed ${disabledStyles[variant]}`]: disabled,
},
className,
);
Expand Down
1 change: 0 additions & 1 deletion frontend/src/components/common/Button/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { Button } from './Button';
export type { StyleTypes, Variants, Props } from './Button.interface';
15 changes: 15 additions & 0 deletions frontend/src/components/common/FormProvider/FormProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { PropsWithChildren } from 'react';
import { FieldValues, FormProvider as RHFFormProvider, UseFormReturn } from 'react-hook-form';

interface Props<T extends FieldValues> extends PropsWithChildren {
form: UseFormReturn<T>;
onSubmit?: (values: T) => void;
}

export const FormProvider = <T extends FieldValues>({ form, onSubmit, children }: Props<T>) => {
return (
<RHFFormProvider {...form}>
<form {...(onSubmit && { onSubmit: form.handleSubmit(onSubmit) })}>{children}</form>
</RHFFormProvider>
);
};
1 change: 1 addition & 0 deletions frontend/src/components/common/FormProvider/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { FormProvider } from './FormProvider';
8 changes: 8 additions & 0 deletions frontend/src/components/common/Input/Input.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { FieldValues, RegisterOptions } from 'react-hook-form';

export interface InputProps {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add some props for the icon at the beginning/end of the field?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add input styles from Figma styleguide in the next PR. I didn't want to make this PR too big with all the extras.

label?: string;
placeholder?: string;
name: string;
options?: RegisterOptions<FieldValues, string>;
}
43 changes: 43 additions & 0 deletions frontend/src/components/common/Input/Input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { FC, memo } from 'react';
import { InputProps } from './Input.interface';
import { useFormContext } from 'react-hook-form';
import { generateClassNames } from '@app/utils';
import { ErrorMessage } from '@hookform/error-message';
import { CheckMarkIcon } from '@app/static/icons/AlertTriangleIcon';

export const Input: FC<InputProps> = memo(({ label, name, placeholder, options = {}, ...otherProps }) => {
const { formState, register, getFieldState } = useFormContext();
const error = getFieldState(name).error;

return (
<div className="flex flex-col gap-y-3">
{label && (
<label className="text-navy-900" htmlFor={`input-${name}`}>
{label}
</label>
)}
<input
className={generateClassNames(
'outline-black h-12 w-full rounded-xl border border-navy-200 px-4 outline-none focus:border-navy-700',
{ 'border-red-600 focus:border-red-600': !!error },
)}
placeholder={placeholder}
id={`input-${name}`}
{...register(name, options)}
{...otherProps}
/>
<ErrorMessage
errors={formState.errors}
name={name}
render={({ message }) => (
<div className="flex items-center gap-x-2">
<CheckMarkIcon />
<div className="text-sm text-red-600 first-letter:uppercase">{message}</div>
</div>
)}
/>
</div>
);
});

Input.displayName = 'Input';
2 changes: 2 additions & 0 deletions frontend/src/components/common/Input/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { Input } from './Input';
export type { InputProps } from './Input.interface';
2 changes: 1 addition & 1 deletion frontend/src/components/common/Typography/Typography.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const Typography = ({
case 'h5':
return <h5 className={classnames}>{children}</h5>;
case 'h6':
return <p className={classnames}>{children}</p>;
return <h6 className={classnames}>{children}</h6>;
case 'p':
default:
return <p className={classnames}>{children}</p>;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { routes } from '@app/constants';
import { Step } from '../SideStepper';
import { AddNewPersonRouteKeys } from './EmployeeSideStepper.interface';

export const addEmployeeInitialSteps: Step<AddNewPersonRouteKeys>[] = [
{ label: '1. Personal details', state: 'notStarted', current: true, href: routes.people.addNew.personalDetails },
{ label: '2. Main ladder', state: 'notStarted', current: false, href: routes.people.addNew.mainLadder },
];
Loading