Skip to content

Commit

Permalink
COM-3849: manon's review
Browse files Browse the repository at this point in the history
  • Loading branch information
ulysseferreira committed Dec 4, 2024
1 parent 2963eec commit 8d64a5f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
19 changes: 7 additions & 12 deletions src/screens/courses/profile/AdminCourseProfile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import uniqBy from 'lodash/uniqBy';
import groupBy from 'lodash/groupBy';
import get from 'lodash/get';
import has from 'lodash/has';
import keyBy from 'lodash/keyBy';
import { SafeAreaView } from 'react-native-safe-area-context';
import { StackScreenProps } from '@react-navigation/stack';
import { useFocusEffect } from '@react-navigation/native';
Expand Down Expand Up @@ -77,25 +76,22 @@ const AdminCourseProfile = ({ route, navigation }: AdminCourseProfileProps) => {
const [title, setTitle] = useState<string>('');
const [firstSlot, setFirstSlot] = useState<SlotType | null>(null);
const [noAttendancesMessage, setNoAttendancesMessage] = useState<string>('');
const stepsById: object = useMemo(() => keyBy(course?.subProgram.steps, '_id'), [course]);

const groupedSlotsToBeSigned = useMemo(() => {
if (!isSingle || !course?.slots.length) return {};
const signedSlots = (savedAttendanceSheets as SingleAttendanceSheetType[])
.map(as => get(as, 'slots', []).map(s => s._id))
.flat();

const groupedSlots = groupBy(course.slots
.filter(slot => !signedSlots.includes(slot._id)), 'step');
const groupedSlots = groupBy(course.slots.filter(slot => !signedSlots.includes(slot._id)), 'step');

return Object.keys(stepsById)
.reduce<Record<string, SlotType[]>>((acc, step) => {
return course?.subProgram.steps.map(s => s._id).reduce<Record<string, SlotType[]>>((acc, step) => {
if (groupedSlots[step]) {
acc[step] = groupedSlots[step];
}
return acc;
}, {});
}, [course, isSingle, savedAttendanceSheets, stepsById]);
}, [course, isSingle, savedAttendanceSheets]);

const missingAttendanceSheets = useMemo(() => {
if (!course?.slots.length || !firstSlot) return [];
Expand All @@ -121,11 +117,10 @@ const AdminCourseProfile = ({ route, navigation }: AdminCourseProfileProps) => {
const savedTrainees = interCourseSavedSheets.map(sheet => sheet.trainee?._id);

return [...new Set(
course?.trainees?.map(t => ({ value: t._id, label: formatIdentity(t.identity, LONG_FIRSTNAME_LONG_LASTNAME) }))
.filter(trainee => (isSingle
? Object.values(groupedSlotsToBeSigned).flat().length
: !savedTrainees.includes(trainee.value)
))
course?.trainees?.filter(trainee => (
isSingle ? Object.values(groupedSlotsToBeSigned).flat().length : !savedTrainees.includes(trainee._id)
))
.map(t => ({ value: t._id, label: formatIdentity(t.identity, LONG_FIRSTNAME_LONG_LASTNAME) }))
)];
}, [course, firstSlot, isSingle, savedAttendanceSheets, groupedSlotsToBeSigned]);

Expand Down
34 changes: 19 additions & 15 deletions src/screens/courses/profile/CreateAttendanceSheet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,46 +61,50 @@ const CreateAttendanceSheet = ({ route, navigation }: CreateAttendanceSheetProps
);
}, [course]);

const setOption = useCallback((option: string) => {
const setDataOption = useCallback((option: string) => {
setAttendanceSheetToAdd(option);
if (option) dispatchErrorData({ type: RESET_ERROR });
}, []);

const setOptions = useCallback((options: string[]) => {
const setSlotOptions = useCallback((options: string[]) => {
setSlotsToAdd(options);
if (options.length) dispatchErrorSlots({ type: RESET_ERROR });
}, []);

const go = ({ from, to }: {from: SCREEN_TYPE, to: SCREEN_TYPE}) => {
if (from === DATA_SELECTION && !attendanceSheetToAdd) {
dispatchErrorData({
type: SET_ERROR,
payload: course?.type === INTER_B2B ? 'Veuillez sélectionner un stagiaire' : 'Veuillez sélectionner une date',
});
} else if (from === SLOTS_SELECTION && !slotsToAdd.length) {
const goToNextScreen = ({ from, to }: {from: SCREEN_TYPE, to: SCREEN_TYPE}) => {
if (from === DATA_SELECTION) {
if (!attendanceSheetToAdd) {
dispatchErrorData({
type: SET_ERROR,
payload: course?.type === INTER_B2B ? 'Veuillez sélectionner un stagiaire' : 'Veuillez sélectionner une date',
});
} else {
dispatchErrorData({ type: RESET_ERROR });
navigation.navigate(to);
}
} else if (!slotsToAdd.length) {
dispatchErrorSlots({
type: SET_ERROR,
payload: 'Veuillez sélectionner des créneaux',
});
} else {
if (from === DATA_SELECTION) dispatchErrorData({ type: RESET_ERROR });
if (from === SLOTS_SELECTION) dispatchErrorSlots({ type: RESET_ERROR });
dispatchErrorSlots({ type: RESET_ERROR });
navigation.navigate(to);
}
};

const renderDataSelection = () => (
<AttendanceSheetSelectionForm title={title} error={errorData}
goToNextScreen={() => go({ from: DATA_SELECTION, to: isSingle ? SLOTS_SELECTION : UPLOAD_METHOD })}>
<RadioButtonList options={missingAttendanceSheets} setOption={setOption}
goToNextScreen={() => goToNextScreen({ from: DATA_SELECTION, to: isSingle ? SLOTS_SELECTION : UPLOAD_METHOD })}>
<RadioButtonList options={missingAttendanceSheets} setOption={setDataOption}
checkedRadioButton={attendanceSheetToAdd} />
</AttendanceSheetSelectionForm>
);

const renderSlotSelection = () => (
<AttendanceSheetSelectionForm title={'Pour quels créneaux souhaitez-vous charger une feuille d\'émargement ?'}
error={errorSlots} goToNextScreen={() => go({ from: SLOTS_SELECTION, to: UPLOAD_METHOD })}>
<MultipleCheckboxList optionsGroups={slotsOptions} groupTitles={stepsName} setOptions={setOptions}
error={errorSlots} goToNextScreen={() => goToNextScreen({ from: SLOTS_SELECTION, to: UPLOAD_METHOD })}>
<MultipleCheckboxList optionsGroups={slotsOptions} groupTitles={stepsName} setOptions={setSlotOptions}
checkedList={slotsToAdd}/>
</AttendanceSheetSelectionForm>
);
Expand Down

0 comments on commit 8d64a5f

Please sign in to comment.