-
Notifications
You must be signed in to change notification settings - Fork 2
Feature/issue-112-create-trainees #303
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c78719c
created GenderSelect
zalexa19 1036c2f
added a hook tot save the form state
zalexa19 391a97a
wip: saving data to the db
zalexa19 e776fdc
wip: moved the form to its own ncomponent
zalexa19 37dea87
wip: cleaned up
zalexa19 c037ad9
reusing genderselector
zalexa19 733fb8c
reusing the new selectors
zalexa19 5da1941
renamed the new dialog
zalexa19 79d368c
wip: forgot to commit file
zalexa19 b058b7c
fixed linting
zalexa19 aac22fc
changed the card bg to a box
zalexa19 92926c1
fixed according to copilot
zalexa19 6ebc02e
fixed according to pr comments
zalexa19 0f19599
renamed to formValidation
zalexa19 491fad5
Fixed typo in non-tech
zalexa19 8e1f65a
fixed lint
zalexa19 b68622b
styled the form
zalexa19 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { Box, Button } from '@mui/material'; | ||
|
|
||
| import { Add } from '@mui/icons-material'; | ||
| import { AddTraineeDialog } from '../../trainee-profile/create/AddTraineeDialog'; | ||
| import { useState } from 'react'; | ||
|
|
||
| export const ActionsCard = () => { | ||
| const [isAddTraineeDialogOpen, setIsAddTraineeDialogOpen] = useState(false); | ||
|
|
||
| const handleOpenAddTraineeDialog = () => { | ||
| setIsAddTraineeDialogOpen(true); | ||
| }; | ||
|
|
||
| const handleCloseAddTraineeDialog = () => { | ||
| setIsAddTraineeDialogOpen(false); | ||
| }; | ||
| return ( | ||
| <Box sx={{ my: 2, paddingTop: 2, paddingBottom: 2, display: 'flex', justifyContent: 'flex-end', paddingRight: 2 }}> | ||
| <AddTraineeDialog isOpen={isAddTraineeDialogOpen} handleClose={handleCloseAddTraineeDialog} /> | ||
| <Button variant="contained" startIcon={<Add />} onClick={handleOpenAddTraineeDialog}> | ||
| Add Trainee | ||
| </Button> | ||
| </Box> | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
client/src/features/trainee-profile/create/AddTraineeDialog.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import { Alert, Box, Dialog, SelectChangeEvent, Typography } from '@mui/material'; | ||
| import { FormErrors, FormState, NewTraineeForm } from './components/NewTraineeForm'; | ||
| import { JobPath, LearningStatus } from '../../../data/types/Trainee'; | ||
|
|
||
| import { useCreateTraineeProfile } from './data/mutations'; | ||
| import { useNavigate } from 'react-router-dom'; | ||
| import { useState } from 'react'; | ||
| import { validateAndCollectFormErrors } from './lib/formValidation'; | ||
|
|
||
| interface AddTraineeDialogProps { | ||
| isOpen: boolean; | ||
| handleClose: () => void; | ||
| } | ||
| export const AddTraineeDialog: React.FC<AddTraineeDialogProps> = ({ isOpen, handleClose }) => { | ||
| const initialState = { | ||
| firstName: '', | ||
| lastName: '', | ||
| gender: null, | ||
| email: '', | ||
| cohort: 0, | ||
| learningStatus: LearningStatus.Studying, | ||
| jobPath: JobPath.NotGraduated, | ||
| }; | ||
|
|
||
| const navigate = useNavigate(); | ||
| const { | ||
| mutate: createTrainee, | ||
| isPending, | ||
| error: submitError, | ||
| } = useCreateTraineeProfile({ | ||
| onSuccess: (profilePath: string) => onSuccess(profilePath), | ||
| }); | ||
|
|
||
| const [errors, setErrors] = useState<FormErrors | null>(null); | ||
|
|
||
| const [formState, setFormState] = useState<FormState>(initialState); | ||
| const onClose = () => { | ||
| setFormState(initialState); | ||
| setErrors(null); | ||
| handleClose(); | ||
| }; | ||
|
|
||
| const onSuccess = (path: string) => { | ||
| onClose(); | ||
| navigate(path); | ||
| }; | ||
| const handleTextChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
| const { name, value } = e.target; | ||
| setFormState((prevState: FormState) => ({ | ||
| ...prevState, | ||
| [name]: value, | ||
| })); | ||
| }; | ||
|
|
||
| const handleSelectChange = (event: SelectChangeEvent<string | number>) => { | ||
| const { name, value } = event.target; | ||
|
|
||
| setFormState((prevState: FormState) => ({ | ||
| ...prevState, | ||
| [name]: value, | ||
| })); | ||
| }; | ||
|
|
||
| const handleSubmit: React.ComponentProps<'form'>['onSubmit'] = (event) => { | ||
| event.preventDefault(); | ||
| const errors = validateAndCollectFormErrors(formState); | ||
|
|
||
| if (errors) { | ||
| setErrors(errors); | ||
| return; | ||
| } | ||
| createTrainee(formState); | ||
| }; | ||
|
|
||
| return ( | ||
| <Dialog open={isOpen} onClose={handleClose} fullWidth maxWidth="sm"> | ||
| <Box padding={5} sx={{ backgroundColor: 'background.paper' }}> | ||
| <Typography variant="h4" gutterBottom> | ||
| New trainee profile | ||
| </Typography> | ||
| <NewTraineeForm | ||
| isLoading={isPending} | ||
| formState={formState} | ||
| errors={errors} | ||
| handleChange={handleTextChange} | ||
| handleSelect={handleSelectChange} | ||
| handleSubmit={handleSubmit} | ||
| handleClose={onClose} | ||
| /> | ||
|
|
||
| {submitError && ( | ||
| <Box paddingTop={2}> | ||
| <Alert severity="error"> | ||
| An error occurred while creating the trainee profile: {submitError?.message && 'unknown'} | ||
| </Alert> | ||
| </Box> | ||
| )} | ||
| </Box> | ||
| </Dialog> | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { CreateTraineeRequestData } from './types'; | ||
| import { Trainee } from '../../../../data/types/Trainee'; | ||
| import axios from 'axios'; | ||
|
|
||
| export const createTrainee = async (request: CreateTraineeRequestData): Promise<Trainee> => { | ||
| const { data } = await axios.post<Trainee>(`/api/trainees`, request); | ||
| return data; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { | ||
| TraineeContactInfo, | ||
| TraineeEducationInfo, | ||
| TraineeEmploymentInfo, | ||
| TraineePersonalInfo, | ||
| } from '../../../../data/types/Trainee'; | ||
|
|
||
| export type CreateTraineeRequestData = { | ||
| personalInfo: Pick<TraineePersonalInfo, 'firstName' | 'lastName' | 'gender'>; | ||
| contactInfo: Pick<TraineeContactInfo, 'email'>; | ||
| educationInfo: Pick<TraineeEducationInfo, 'startCohort' | 'currentCohort' | 'learningStatus'>; | ||
| employmentInfo: Pick<TraineeEmploymentInfo, 'jobPath'>; | ||
| }; |
109 changes: 109 additions & 0 deletions
109
client/src/features/trainee-profile/create/components/NewTraineeForm.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import { Button, SelectChangeEvent, Stack } from '@mui/material'; | ||
| import { Gender, JobPath, LearningStatus } from '../../../../data/types/Trainee'; | ||
|
|
||
| import { GenderSelect } from '../../profile/components/GenderSelect'; | ||
| import { JobPathSelect } from '../../profile/components/JobPathSelect'; | ||
| import { LearningStatusSelect } from '../../profile/components/LearningStatusSelect'; | ||
| import TextFieldWrapper from './TextFieldWrapper'; | ||
|
|
||
| export type FormState = { | ||
| firstName: string; | ||
| lastName: string; | ||
| gender: Gender | null; | ||
| email: string; | ||
| cohort: number; | ||
| learningStatus: LearningStatus; | ||
| jobPath: JobPath; | ||
| }; | ||
|
|
||
| export type FormErrors = { | ||
| firstName?: string; | ||
| lastName?: string; | ||
| gender?: string; | ||
| email?: string; | ||
| cohort?: string; | ||
| }; | ||
|
|
||
| export const NewTraineeForm: React.FC<{ | ||
| isLoading: boolean; | ||
| formState: FormState; | ||
| errors: FormErrors | null; | ||
| handleChange: (e: React.ChangeEvent<HTMLInputElement>) => void; | ||
| handleClose: () => void; | ||
| handleSelect: (event: SelectChangeEvent<string | number>) => void; | ||
| handleSubmit: React.ComponentProps<'form'>['onSubmit']; | ||
| }> = ({ isLoading, formState, errors, handleChange, handleSelect, handleSubmit, handleClose }) => { | ||
| return ( | ||
| <form onSubmit={handleSubmit}> | ||
| <Stack spacing={2} pt={2}> | ||
| <TextFieldWrapper | ||
| disabled={isLoading} | ||
| id="firstName" | ||
| name="firstName" | ||
| error={!!errors?.firstName} | ||
| helperText={errors?.firstName} | ||
| label="First name" | ||
| value={formState.firstName} | ||
| onChange={handleChange} | ||
| /> | ||
| <TextFieldWrapper | ||
| disabled={isLoading} | ||
| id="lastName" | ||
| name="lastName" | ||
| error={!!errors?.lastName} | ||
| helperText={errors?.lastName} | ||
| label="Last name" | ||
| value={formState.lastName} | ||
| onChange={handleChange} | ||
| /> | ||
| <GenderSelect | ||
| disabled={isLoading} | ||
| isEditing | ||
| value={formState.gender || undefined} | ||
| error={errors?.gender || ''} | ||
| onChange={handleSelect} | ||
| /> | ||
| <TextFieldWrapper | ||
| disabled={isLoading} | ||
| id="email" | ||
| name="email" | ||
| error={!!errors?.email} | ||
| helperText={errors?.email} | ||
| label="Email" | ||
| value={formState.email} | ||
| onChange={handleChange} | ||
| /> | ||
| <Stack direction="row" spacing={2} pb={2}> | ||
| <TextFieldWrapper | ||
| disabled={isLoading} | ||
| id="cohort" | ||
| name="cohort" | ||
| type="number" | ||
| error={!!errors?.cohort} | ||
| helperText={errors?.cohort} | ||
| label="Start cohort" | ||
| value={formState.cohort} | ||
| onChange={handleChange} | ||
| maxLength={3} | ||
| /> | ||
|
|
||
| <LearningStatusSelect | ||
| disabled={isLoading} | ||
| isEditing | ||
| value={formState.learningStatus} | ||
| onChange={handleSelect} | ||
| /> | ||
| <JobPathSelect disabled={isLoading} isEditing value={formState.jobPath} onChange={handleSelect} /> | ||
| </Stack> | ||
| </Stack> | ||
| <Stack direction="row" spacing={2} justifyContent="flex-end" mt={2}> | ||
| <Button variant="outlined" color="secondary" disabled={isLoading} onClick={handleClose}> | ||
| Cancel | ||
| </Button> | ||
| <Button type="submit" variant="contained" color="primary" loading={isLoading} disabled={isLoading}> | ||
zalexa19 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Create | ||
| </Button> | ||
| </Stack> | ||
| </form> | ||
| ); | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.