-
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 12 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { SxProps, Theme } from '@mui/material/styles'; | ||
|
|
||
| import Button from '@mui/material/Button'; | ||
| import React from 'react'; | ||
|
|
||
| interface ButtonWithIconProps { | ||
| text: string; | ||
| startIcon?: React.ReactNode; | ||
| onClick?: React.MouseEventHandler<HTMLButtonElement>; | ||
| isLoading?: boolean; | ||
| sx?: SxProps<Theme>; | ||
| type?: 'button' | 'submit' | 'reset'; | ||
| } | ||
|
|
||
| export const ButtonWithIcon: React.FC<ButtonWithIconProps> = ({ | ||
| text, | ||
| startIcon, | ||
| onClick, | ||
| sx, | ||
| type = 'button', | ||
| isLoading = false, | ||
| }) => ( | ||
| <Button variant="contained" startIcon={startIcon} onClick={onClick} sx={sx} type={type} loading={isLoading}> | ||
| {text} | ||
| </Button> | ||
| ); | ||
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 { Box } from '@mui/material'; | ||
| import { ErrorBox } from './ErrorBox'; | ||
zalexa19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| type StyledErrorBoxProps = { | ||
| message: string; | ||
| }; | ||
| export const StyledErrorBox: React.FC<StyledErrorBoxProps> = ({ message }) => { | ||
| return ( | ||
| <Box width="50%" margin="auto" marginTop="2rem" marginBottom="2rem"> | ||
| <ErrorBox errorMessage={message} /> | ||
| </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
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,23 @@ | ||
| import { Add } from '@mui/icons-material'; | ||
| import { AddTraineeDialog } from '../../trainee-profile/create/AddTraineeDialog'; | ||
| import { Box } from '@mui/material'; | ||
| import { ButtonWithIcon } from '../../../components/ButtonWithIcon'; | ||
| 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} /> | ||
| <ButtonWithIcon text="Add Trainee" startIcon={<Add />} onClick={handleOpenAddTraineeDialog} /> | ||
| </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
104 changes: 104 additions & 0 deletions
104
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,104 @@ | ||
| 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 { validateForm } from './lib/formHelper'; | ||
|
|
||
| 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>({}); | ||
|
|
||
| const [formState, setFormState] = useState<FormState>(initialState); | ||
| const onClose = () => { | ||
| setFormState(initialState); | ||
| setErrors({}); | ||
| handleClose(); | ||
| }; | ||
|
|
||
| const onSuccess = (path: string) => { | ||
| onClose(); | ||
| navigate(path); | ||
zalexa19 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
| 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(); | ||
| setErrors({}); | ||
zalexa19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const errors = validateForm(formState); | ||
| setErrors(errors); | ||
|
|
||
| const hasErrors = Object.values(errors).some((error) => error != null); | ||
|
|
||
| if (hasErrors) { | ||
| 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'>; | ||
| }; |
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.