Skip to content

Commit b34b11a

Browse files
committed
feat: allow edit status, project and mentor for applicants on any status
1 parent 4d2d531 commit b34b11a

File tree

3 files changed

+36
-66
lines changed

3 files changed

+36
-66
lines changed

app/core/components/RegularSelect/index.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
InputLabel,
66
FormHelperText,
77
} from "@mui/material";
8-
import { useState } from "react";
98
import { useControlField, useField } from "remix-validated-form";
109

1110
interface SelectValue {
@@ -36,31 +35,29 @@ export const RegularSelect = ({
3635

3736
const [value, setValue] = useControlField<SelectValue>(name);
3837
//This state variable is to have a controlled input and avoid errors in the console
39-
const [selectValue, setSelectValue] = useState<string>("");
4038
return (
4139
<FormControl fullWidth id={name} size="small" error={!!error}>
4240
<input type="hidden" name={`${name}.name`} value={value?.name} />
4341
<input type="hidden" name={`${name}.id`} value={value?.id} />
4442
<InputLabel id={name}>{label}</InputLabel>
4543
<Select
4644
label={label}
47-
value={selectValue}
45+
value={value?.id}
4846
error={!!error}
4947
style={style ? style : { margin: "1em 0" }}
5048
onChange={(event) => {
5149
const newValue = valuesList.find(
52-
(item) => item.name === event.target.value
50+
(item) => item.id === event.target.value
5351
);
5452
if (newValue) {
5553
setValue(newValue);
56-
setSelectValue(newValue.name);
5754
}
5855
onChange && onChange(newValue as SelectValue);
5956
}}
6057
disabled={disabled}
6158
>
6259
{valuesList.map((item) => (
63-
<MenuItem key={item.id} value={item.name}>
60+
<MenuItem key={item.id} value={item.id}>
6461
{item.name}
6562
</MenuItem>
6663
))}

app/routes/applicants.$applicantId._index.tsx

Lines changed: 25 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@ import {
66
Paper,
77
Link as ExternalLink,
88
Button,
9-
TextField,
10-
Autocomplete,
11-
debounce,
129
Stack,
1310
IconButton,
1411
Typography,
15-
type AutocompleteChangeReason,
1612
Avatar,
1713
FormControl,
1814
InputLabel,
@@ -80,10 +76,13 @@ export const validator = withZod(
8076
name: z.string().optional(),
8177
})
8278
.optional(),
83-
mentorId: z.string(),
84-
mentorName: z.string().optional(),
79+
mentor: z
80+
.object({
81+
id: z.string().optional(),
82+
name: z.string().optional(),
83+
})
84+
.optional(),
8585
status: z.string(),
86-
projectId: z.string().optional(),
8786
})
8887
);
8988

@@ -130,10 +129,6 @@ export default function Applicant() {
130129
comments,
131130
} = useTypedLoaderData<typeof loader>();
132131
const [openManageModal, setOpenManageModal] = useState(false);
133-
const [mentorSelected, setMentorSelected] = useState<ProfileValue | null>({
134-
id: "",
135-
name: "",
136-
});
137132
const [projectSelected, setProjectSelected] = useState<ProjectValue | null>();
138133

139134
const navigation = useNavigation();
@@ -173,24 +168,23 @@ export default function Applicant() {
173168
);
174169
};
175170

176-
const searchProfilesDebounced = debounce(searchProfiles, 500);
177-
178171
useEffect(() => {
179172
if (profileFetcher.state === "idle" && profileFetcher.data == null) {
180-
profileFetcher.submit({}, profileFetcherOptions);
173+
profileFetcher.submit(
174+
{ q: "", projectId: applicant.projectId },
175+
profileFetcherOptions
176+
);
181177
}
182-
}, [profileFetcher]);
178+
}, [applicant.projectId, profileFetcher]);
183179

184180
const handleSelectProject = (project: ProjectValue) => {
185181
setProjectSelected(project);
186182
searchProfiles("", project.id);
187-
setMentorSelected({ id: "", name: "" });
188183
};
189184

190185
const handleCloseModal = () => {
191186
setOpenManageModal(false);
192187
setProjectSelected(null);
193-
setMentorSelected({ id: "", name: "" });
194188
searchProfiles("", "");
195189
};
196190

@@ -374,7 +368,15 @@ export default function Applicant() {
374368
validator={validator}
375369
method="post"
376370
action="./status"
377-
defaultValues={{ project: { id: "", name: "" } }}
371+
defaultValues={{
372+
project: {
373+
id: applicant.projectId || undefined,
374+
name: applicant.projectName || undefined,
375+
},
376+
mentor: {
377+
id: applicant.mentorId || undefined,
378+
},
379+
}}
378380
>
379381
<input type="hidden" name="applicantId" value={applicant.id} />
380382

@@ -395,43 +397,17 @@ export default function Applicant() {
395397

396398
<RegularSelect
397399
valuesList={projects}
398-
defaultValue={applicant.projectName}
399400
name="project"
400401
label="Select a project"
401402
onChange={handleSelectProject}
402403
/>
403404

404-
<input type="hidden" name="mentorId" value={mentorSelected?.id} />
405-
<Autocomplete
406-
multiple={false}
407-
style={{ margin: "1em 0" }}
408-
options={profileFetcher.data ?? []}
409-
value={mentorSelected?.id ? mentorSelected : applicant.mentorId}
410-
isOptionEqualToValue={(option, value) => option.id === value.id}
411-
id="mentor"
412-
getOptionLabel={(option) => option.name}
413-
onInputChange={(_, value) => searchProfilesDebounced(value)}
414-
renderTags={() => null}
415-
onChange={(
416-
event,
417-
value: { id: string; name: string } | null,
418-
reason: AutocompleteChangeReason
419-
) =>
420-
reason === "clear"
421-
? setMentorSelected({ id: "", name: "" })
422-
: setMentorSelected(value)
423-
}
424-
filterSelectedOptions
425-
renderInput={(params) => (
426-
<TextField
427-
name="mentorName"
428-
label="Select a mentor"
429-
{...params}
430-
placeholder="Select a mentor..."
431-
value={mentorSelected?.name}
432-
/>
433-
)}
405+
<RegularSelect
406+
valuesList={profileFetcher.data ?? []}
407+
name="mentor"
408+
label="Select a mentor"
434409
/>
410+
435411
<Grid container justifyContent="end" alignItems="center">
436412
<Button type="submit">Save Cahnges</Button>
437413
</Grid>

app/routes/applicants.$applicantId.status.tsx

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,13 @@ export const action: ActionFunction = async ({ request }) => {
99
const result = await validator.validate(await request.formData());
1010
const applicantId = parseInt(result.data?.applicantId as string);
1111
const status = result.data?.status;
12-
let projectId = null;
13-
let mentorId = null;
14-
let response = null;
15-
if (status !== "DRAFT") {
16-
projectId = result.data?.project?.id
17-
? result.data?.project.id
18-
: result.data?.projectId;
19-
mentorId = result.data?.mentorId;
20-
} // else reset the values
21-
console.log("action", mentorId, projectId, status, applicantId);
22-
response = await editApplicant({ mentorId, projectId, status }, applicantId);
12+
const response = await editApplicant(
13+
{
14+
mentorId: result.data?.mentor?.id,
15+
projectId: result.data?.project?.id,
16+
status,
17+
},
18+
applicantId
19+
);
2320
return redirect(`/applicants/${response.id}?status=${response.status}`);
2421
};

0 commit comments

Comments
 (0)