Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions admin-wcc-app/components/EditMentor/EditMentorForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export default function EditMentorForm({ mentorId }: EditMentorFormProps) {
control,
handleSubmit,
reset,
setValue,
formState: { errors },
} = useForm<EditMentorFormData>({
resolver: zodResolver(editMentorSchema),
Expand Down Expand Up @@ -165,7 +166,7 @@ export default function EditMentorForm({ mentorId }: EditMentorFormProps) {
longTerm: data.mentorshipType.includes('LONG_TERM') ? { numMentee: 1, hours: 2 } : null,
adHoc: data.mentorshipType.includes('AD_HOC')
? data.monthAvailability
.filter((m) => m.enabled)
.filter((m) => m.enabled && m.hours > 0)
.map((m) => ({ month: m.month, hours: m.hours }))
: [],
},
Expand Down Expand Up @@ -321,7 +322,7 @@ export default function EditMentorForm({ mentorId }: EditMentorFormProps) {
<PersonalInfoSection control={control} errors={errors} />
<BioSection control={control} errors={errors} />
<SkillsSection control={control} errors={errors} />
<MentorshipAvailabilitySection control={control} errors={errors} />
<MentorshipAvailabilitySection control={control} errors={errors} setValue={setValue} />
<ResourcesSection control={control} />
</Stack>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ const MONTHS = [

const monthLabel = (month: string) => month.charAt(0) + month.slice(1).toLowerCase();

export default function MentorshipAvailabilitySection({ control, errors }: FormSectionProps) {
export default function MentorshipAvailabilitySection({
control,
errors,
setValue,
}: FormSectionProps) {
const mentorshipType = useWatch({
control,
name: 'mentorshipType',
Expand Down Expand Up @@ -143,7 +147,12 @@ export default function MentorshipAvailabilitySection({ control, errors }: FormS
control={
<Checkbox
checked={field.value}
onChange={(e) => field.onChange(e.target.checked)}
onChange={(e) => {
field.onChange(e.target.checked);
if (!e.target.checked) {
setValue?.(`monthAvailability.${index}.hours`, 0);
}
}}
size="small"
/>
}
Expand All @@ -159,9 +168,11 @@ export default function MentorshipAvailabilitySection({ control, errors }: FormS
<TextField
{...field}
value={value}
onChange={(e) =>
onChange(e.target.value === '' ? 0 : Number(e.target.value))
}
onChange={(e) => {
const nextHours = e.target.value === '' ? 0 : Number(e.target.value);
onChange(nextHours);
setValue?.(`monthAvailability.${index}.enabled`, nextHours > 0);
}}
type="number"
size="small"
label="hours"
Expand Down
3 changes: 2 additions & 1 deletion admin-wcc-app/components/EditMentor/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Control, FieldErrors } from 'react-hook-form';
import { Control, FieldErrors, UseFormSetValue } from 'react-hook-form';
import { EditMentorFormData } from './schema';

export type { EditMentorFormData } from './schema';

export interface FormSectionProps {
control: Control<EditMentorFormData>;
errors: FieldErrors<EditMentorFormData>;
setValue?: UseFormSetValue<EditMentorFormData>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ public MenteeSection toDto() {

/**
* Converts to a DTO scoped to the given mentorship cycle. For AD_HOC cycles the adHoc
* availability list is filtered to only the entries whose month matches the cycle month, so that
* mentors unavailable in the current cycle month are not surfaced by the AD_HOC type filter.
* availability list is filtered to only the entries whose month matches the cycle month and that
* declare more than zero hours, so that mentors unavailable in the current cycle month (or with no
* real availability, i.e. 0 hours) are not surfaced by the AD_HOC type filter.
*
* @param cycle the active mentorship cycle; when null or LONG_TERM the full adHoc list is kept
* @return a MenteeSection DTO with adHoc availability filtered to the current cycle month
* @return a MenteeSection DTO with adHoc availability filtered to the current cycle month and to
* entries with positive hours
*/
public MenteeSection toDtoForCycle(final MentorshipCycle cycle) {
if (cycle == null
Expand All @@ -66,7 +68,10 @@ public MenteeSection toDtoForCycle(final MentorshipCycle cycle) {
return toDto();
}
final List<MentorMonthAvailability> filteredAdHoc =
adHoc.stream().filter(a -> Objects.equals(a.month(), cycle.month())).toList();
adHoc.stream()
.filter(a -> Objects.equals(a.month(), cycle.month()))
.filter(a -> a.hours() != null && a.hours() > 0)
.toList();
return new MenteeSection(idealMentee, additional, longTerm, filteredAdHoc);
}
}
Loading