Skip to content

Commit

Permalink
Added rooms sorted by favorites first
Browse files Browse the repository at this point in the history
  • Loading branch information
villepynttari committed Jul 8, 2024
1 parent 34073c7 commit 6d0684e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 23 deletions.
43 changes: 20 additions & 23 deletions frontend/src/components/AvailableRoomList/AvailableRoomList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import RoomCard from '../RoomCard/RoomCard';
import NoRoomsCard from '../RoomCard/NoRoomsCard';
import BookingDrawer from '../BookingDrawer/BookingDrawer';
import TimePickerDrawer from '../TimePickerDrawer/TimePickerDrawer';
import { sortByFavoritedAndName } from '../../util/arrayUtils';

const SKIP_CONFIRMATION = true;

Expand Down Expand Up @@ -317,29 +318,25 @@ const AvailableRoomList = (props: BookingListProps) => {
{noAvailableRooms(rooms) ? (
<NoRoomsCard />
) : (
rooms
.sort((a, b) => (a.name < b.name ? -1 : 1))
.map((room) =>
isAvailableFor(bookingDuration, room, startingTime)
? (isFavorited(room, preferences),
(
<li key={room.id}>
<RoomCard
room={room}
onClick={handleCardClick}
bookingLoading={bookingLoading}
disableBooking={false}
isSelected={selectedRoom === room}
expandFeatures={
expandedFeaturesAll
}
preferences={preferences}
setPreferences={setPreferences}
/>
</li>
))
: null
)
sortByFavoritedAndName<Room>(rooms).map((room) =>
isAvailableFor(bookingDuration, room, startingTime)
? (isFavorited(room, preferences),
(
<li key={room.id}>
<RoomCard
room={room}
onClick={handleCardClick}
bookingLoading={bookingLoading}
disableBooking={false}
isSelected={selectedRoom === room}
expandFeatures={expandedFeaturesAll}
preferences={preferences}
setPreferences={setPreferences}
/>
</li>
))
: null
)
)}
</List>
</Box>
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/BookingView/BookingView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
UserIcon
} from '../../theme_2024';
import { useUserSettings } from '../../contexts/UserSettingsContext';
import { sortByFavoritedAndName } from '../../util/arrayUtils';

const UPDATE_FREQUENCY = 30000;
const GET_RESERVED = true;
Expand Down
16 changes: 16 additions & 0 deletions frontend/src/util/arrayUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { sortByFavoritedAndName } from './arrayUtils';

const testArray = [
{ name: 'a', favorited: false },
{ name: 'b', favorited: true },
{ name: 'c', favorited: false }
];
describe('isNonEmptyArray', () => {
test('Should return false for null', () => {
expect(sortByFavoritedAndName(testArray)).toEqual([
{ name: 'b', favorited: true },
{ name: 'a', favorited: false },
{ name: 'c', favorited: false }
]);
});
});
10 changes: 10 additions & 0 deletions frontend/src/util/arrayUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function sortByFavoritedAndName<T>(
array: { name: string; favorited: boolean }[]
): T[] {
return array.sort((a, b) => {
if (a.favorited === b.favorited) {
return a.name.localeCompare(b.name);
}
return a.favorited ? -1 : 1;
}) as T[];
}

0 comments on commit 6d0684e

Please sign in to comment.