Skip to content

Commit ee8ff78

Browse files
committed
#690: Fixed transformDate
1 parent 8a9093e commit ee8ff78

3 files changed

Lines changed: 86 additions & 3 deletions

File tree

src/frontend/src/apis/work-packages.api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { workPackageTransformer } from './transformers/work-packages.transformer
1111

1212
export interface WorkPackageApiInputs {
1313
name: string;
14-
startDate: Date;
14+
startDate: String;
1515
duration: number;
1616
crId: number;
1717
stage: WorkPackageStage | string | null;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { useAllLinkTypes } from '../../hooks/projects.hooks';
2+
import LoadingIndicator from '../LoadingIndicator';
3+
import ErrorPage from '../../pages/ErrorPage';
4+
import { IconButton, MenuItem, Select, TextField } from '@mui/material';
5+
import { FieldArrayWithId, UseFieldArrayAppend, UseFieldArrayRemove, UseFormRegister, UseFormWatch } from 'react-hook-form';
6+
import DeleteIcon from '@mui/icons-material/Delete';
7+
import { getRequiredLinkTypeNames } from '../../utils/link.utils';
8+
import { ProjectFormInput } from '../../pages/ProjectDetailPage/ProjectForm/ProjectForm';
9+
import { Box } from '@mui/system';
10+
import { NERButton } from '../NERButton';
11+
12+
const LinksEditView: React.FC<{
13+
ls: FieldArrayWithId[];
14+
register: UseFormRegister<ProjectFormInput>;
15+
watch: UseFormWatch<ProjectFormInput>;
16+
append: UseFieldArrayAppend<any, any>;
17+
remove: UseFieldArrayRemove;
18+
}> = ({ ls, register, append, remove, watch }) => {
19+
const { isLoading, isError, error, data: linkTypes } = useAllLinkTypes();
20+
if (isLoading || !linkTypes) return <LoadingIndicator />;
21+
if (isError) return <ErrorPage message={error.message} />;
22+
23+
const requiredLinkTypeNames = getRequiredLinkTypeNames(linkTypes);
24+
25+
const links = watch('links');
26+
27+
const currentLinkTypeNames = links.map((link) => link.linkTypeName);
28+
29+
/* Checks whether the link at the given index is of a required type and does not already exist */
30+
const isRequired = (index: number) => {
31+
const link = watch(`links.${index}`);
32+
const { linkTypeName } = link;
33+
return (
34+
requiredLinkTypeNames.includes(linkTypeName) &&
35+
!currentLinkTypeNames.includes(linkTypeName, currentLinkTypeNames.indexOf(linkTypeName) + 1)
36+
);
37+
};
38+
39+
const availableOptions = linkTypes.filter((linkType) => !currentLinkTypeNames.includes(linkType.name));
40+
41+
return (
42+
<>
43+
{ls.map((_element, i) => {
44+
return (
45+
<Box sx={{ display: 'flex', alignItems: 'center', mb: '5px' }}>
46+
<Select
47+
{...register(`links.${i}.linkTypeName`, { required: true })}
48+
sx={{ minWidth: '200px', mr: '5px' }}
49+
disabled={isRequired(i)}
50+
value={watch(`links.${i}.linkTypeName`)}
51+
>
52+
{linkTypes.map((linkType) => (
53+
<MenuItem key={linkType.name} value={linkType.name} disabled={!availableOptions.includes(linkType)}>
54+
{linkType.name}
55+
</MenuItem>
56+
))}
57+
</Select>
58+
<TextField required fullWidth autoComplete="off" {...register(`links.${i}.url`, { required: true })} />
59+
<Box sx={{ minWidth: '56px', height: '40px' }}>
60+
{!isRequired(i) && (
61+
<IconButton type="button" onClick={() => remove(i)} sx={{ mx: 1, my: 0 }}>
62+
<DeleteIcon />
63+
</IconButton>
64+
)}
65+
</Box>
66+
</Box>
67+
);
68+
})}
69+
{availableOptions.length > 0 && (
70+
<NERButton
71+
variant="contained"
72+
color="primary"
73+
onClick={() => append({ linkId: '-1', url: '', linkTypeName: '-1' })}
74+
sx={{ my: 2, width: 'max-content' }}
75+
>
76+
+ Add New Link
77+
</NERButton>
78+
)}
79+
</>
80+
);
81+
};
82+
83+
export default LinksEditView;

src/frontend/src/pages/WorkPackageForm/WorkPackageFormView.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ const WorkPackageFormView: React.FC<WorkPackageFormViewProps> = ({
117117
const { userId } = user;
118118

119119
const transformDate = (date: Date) => {
120-
const month = date.getMonth() + 1 < 10 ? `0${date.getMonth()}` : date.getMonth().toString();
120+
const month = date.getMonth() + 1 < 10 ? `0${date.getMonth() + 1}` : (date.getMonth() + 1).toString();
121121
const day = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate().toString();
122-
return new Date(date.getFullYear(), Number(month), Number(day));
122+
return `${date.getFullYear().toString()}-${month}-${day}`;
123123
};
124124

125125
const onSubmit = async (data: WorkPackageFormViewPayload) => {

0 commit comments

Comments
 (0)