Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restrict the visibility of available rooms to a limited number of days in the future #554

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
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
Loading