Skip to content

Commit

Permalink
fix(places): limited display of free classrooms
Browse files Browse the repository at this point in the history
  • Loading branch information
FabrizioCostaMedich committed Dec 20, 2024
1 parent 9e46cfb commit 715e635
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
3 changes: 2 additions & 1 deletion assets/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,8 @@
},
"freeRoomsScreen": {
"noFreeRooms": "No free rooms found",
"title": "Free rooms"
"title": "Free rooms",
"maxViewLimitMessage": "Maximum display limit reached "
},
"grades": {
"absent": "Abs",
Expand Down
3 changes: 2 additions & 1 deletion assets/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,8 @@
},
"freeRoomsScreen": {
"noFreeRooms": "Nessuna aula libera trovata",
"title": "Aule libere"
"title": "Aule libere",
"maxViewLimitMessage": "Raggiunto il limite massimo di visulizzazione"
},
"grades": {
"absent": "Ass",
Expand Down
56 changes: 50 additions & 6 deletions src/features/places/screens/FreeRoomsScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { useCallback, useLayoutEffect, useMemo, useState } from 'react';
import {
useCallback,
useEffect,
useLayoutEffect,
useMemo,
useState,
} from 'react';
import { useTranslation } from 'react-i18next';
import { View } from 'react-native';

Expand All @@ -22,6 +28,7 @@ import { useTheme } from '@lib/ui/hooks/useTheme';
import { last } from 'lodash';
import { DateTime } from 'luxon';

import { useFeedbackContext } from '../../../core/contexts/FeedbackContext';
import { useGetFreeRooms } from '../../../core/queries/placesHooks';
import { GlobalStyles } from '../../../core/styles/GlobalStyles';
import { formatTime } from '../../../utils/dates';
Expand All @@ -42,8 +49,11 @@ type Props = MapScreenProps<PlacesStackParamList, 'FreeRooms'>;
const slotStartHour = [19, 17, 16, 14, 13, 11, 10, 8];

const findNearestSlotStartHour = (dt: DateTime) => {
let maxDate = DateTime.now().plus({ day: 1 }).endOf('day');

// Skip Sundays
if (dt.weekday === 7) dt = dt.plus({ day: 1 }).startOf('day');
if (maxDate.weekday === 7) maxDate = maxDate.plus({ day: 1 }).startOf('day');

const nearestStartHourIndex = slotStartHour.findIndex(h => h <= dt.hour);
if (nearestStartHourIndex >= 0 && dt.hour < 20) {
Expand Down Expand Up @@ -71,17 +81,26 @@ const findNearestSlotStartHour = (dt: DateTime) => {
}
}

return dt.set({
second: 0,
millisecond: 0,
});
return dt < maxDate
? dt.set({
second: 0,
millisecond: 0,
})
: maxDate.set({
hour: slotStartHour[0],
minute: 0,
second: 0,
millisecond: 0,
});
};

export const FreeRoomsScreen = ({ navigation }: Props) => {
const { t } = useTranslation();
const { colors } = useTheme();
const { spacing, fontSizes } = useTheme();
const campus = useGetCurrentCampus();
const { setFeedback, isFeedbackVisible } = useFeedbackContext();
const [showFeedback, setShowFeedback] = useState(false);

const today = useMemo(() => DateTime.now().startOf('day'), []);

Expand Down Expand Up @@ -118,6 +137,31 @@ export const FreeRoomsScreen = ({ navigation }: Props) => {
[t, today],
);

useEffect(() => {
if (
startDateTime.equals(
DateTime.now().plus({ day: 1 }).set({
hour: slotStartHour[0],
minute: 0,
second: 0,
millisecond: 0,
}),
)
) {
if (showFeedback) {
setFeedback({
text: t('freeRoomsScreen.maxViewLimitMessage'),
isError: true,
isPersistent: false,
});
} else {
setShowFeedback(true);
}
} else {
setShowFeedback(false);
}
}, [startDateTime, t]);

const { data: searchResult } = useSearchPlaces({ siteId: campus?.id });
const sitePlaces = useGetPlacesFromSearchResult(searchResult);

Expand Down Expand Up @@ -197,7 +241,7 @@ export const FreeRoomsScreen = ({ navigation }: Props) => {
<IconButton
icon={faChevronRight}
color={colors.secondaryText}
disabled={!freeRooms?.data}
disabled={!freeRooms?.data || isFeedbackVisible}
onPress={() =>
setStartDateTime(prevDt =>
findNearestSlotStartHour(prevDt.plus({ hour: 1, minute: 30 })),
Expand Down

0 comments on commit 715e635

Please sign in to comment.