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

add connection for first part of reservation system for lyche #947

Open
wants to merge 2 commits into
base: master
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
13 changes: 12 additions & 1 deletion backend/root/utils/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
DO NOT WRITE IN THIS FILE, AS IT WILL BE OVERWRITTEN ON NEXT UPDATE.

THIS FILE WAS GENERATED BY: root.management.commands.generate_routes
LAST UPDATE: 2023-12-18 15:49:32.799909+00:00
LAST UPDATE: 2024-01-27 14:29:54.437785+00:00
"""

############################################################
Expand Down Expand Up @@ -98,6 +98,15 @@
admin__guardian_userobjectpermission_delete = 'admin:guardian_userobjectpermission_delete'
admin__guardian_userobjectpermission_change = 'admin:guardian_userobjectpermission_change'
adminguardianuserobjectpermission__objectId = ''
admin__samfundet_campus_permissions = 'admin:samfundet_campus_permissions'
admin__samfundet_campus_permissions_manage_user = 'admin:samfundet_campus_permissions_manage_user'
admin__samfundet_campus_permissions_manage_group = 'admin:samfundet_campus_permissions_manage_group'
admin__samfundet_campus_changelist = 'admin:samfundet_campus_changelist'
admin__samfundet_campus_add = 'admin:samfundet_campus_add'
admin__samfundet_campus_history = 'admin:samfundet_campus_history'
admin__samfundet_campus_delete = 'admin:samfundet_campus_delete'
admin__samfundet_campus_change = 'admin:samfundet_campus_change'
adminsamfundetcampus__objectId = ''
admin__samfundet_userpreference_permissions = 'admin:samfundet_userpreference_permissions'
admin__samfundet_userpreference_permissions_manage_user = 'admin:samfundet_userpreference_permissions_manage_user'
admin__samfundet_userpreference_permissions_manage_group = 'admin:samfundet_userpreference_permissions_manage_group'
Expand Down Expand Up @@ -463,6 +472,8 @@
samfundet__home = 'samfundet:home'
samfundet__assign_group = 'samfundet:assign_group'
samfundet__webhook = 'samfundet:webhook'
samfundet__check_reservation = 'samfundet:check_reservation'
samfundet__reservation_form = 'samfundet:reservation-form'
samfundet__recruitment_positions = 'samfundet:recruitment_positions'
samfundet__recruitment_positions_gang = 'samfundet:recruitment_positions_gang'
samfundet__active_recruitment_positions = 'samfundet:active_recruitment_positions'
Expand Down
1 change: 1 addition & 0 deletions backend/samfundet/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@

########## Lyche ##########
path('check-reservation/', views.ReservationCheckAvailabilityView.as_view(), name='check_reservation'),
path('reservation-form/', views.ReservationFormView.as_view(), name='reservation-form'),

########## Recruitment ##########
path('recruitment-positions/', views.RecruitmentPositionsPerRecruitmentView.as_view(), name='recruitment_positions'),
Expand Down
10 changes: 10 additions & 0 deletions backend/samfundet/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
RecruitmentAdmissionForGangSerializer,
RecruitmentAdmissionForApplicantSerializer,
)
from .models.model_choices import ReservationOccasion
from .utils import event_query

# =============================== #
Expand Down Expand Up @@ -310,6 +311,15 @@ class TableView(ModelViewSet):
queryset = Table.objects.all()


class ReservationFormView(APIView):
permission_classes = [AllowAny]

def get(self, request: Request) -> Response:
venue = self.request.query_params.get('venue', Venue.objects.get(slug='lyche').id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disse burde vi samle som konstanter et sted (lyche etc)

biggest_table = Table.objects.filter(venue=venue).order_by('-seating').first()
return Response({'occasion': ReservationOccasion.choices, 'biggest_table': biggest_table.seating if biggest_table else 0}, status=status.HTTP_200_OK)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Response({'occasion': ReservationOccasion.choices, 'biggest_table': biggest_table.seating if biggest_table else 0}, status=status.HTTP_200_OK)
biggest_seating = getattr(biggest_table, 'seating', 0)
return Response({'occasion': ReservationOccasion.choices, 'biggest_table': biggest_seating}, status=status.HTTP_200_OK)



class ReservationCheckAvailabilityView(APIView):
permission_classes = [AllowAny]
serializer_class = ReservationCheckSerializer
Expand Down
71 changes: 48 additions & 23 deletions frontend/src/Pages/LycheReservationPage/LycheReservationPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,75 @@ import { Link } from '~/Components/Link/Link';
import { SultenPage } from '~/Components/SultenPage';
import { TextItem } from '~/constants/TextItems';
import styles from './LycheReservationPage.module.scss';
import { toast } from 'react-toastify';
import { KV } from '~/constants';
import { useKeyValue, useTextItem } from '~/hooks';
import { ReservationDto } from '~/dto';
import { FetchAvailableTimesDto, ReservationDto } from '~/dto';
import { KEY } from '~/i18n/constants';
import { useTranslation } from 'react-i18next';
import { useState } from 'react';
import { useEffect, useState } from 'react';
import { SamfForm } from '~/Forms/SamfForm';
import { SamfFormField } from '~/Forms/SamfFormField';
import { DropDownOption } from '~/Components/Dropdown/Dropdown';
import { ReservationFormLine } from './Components';
import { getAvailableTimes, getReservationForm } from '~/api';
import { dbT } from '~/utils';
import { STATUS } from '~/http_status_codes';

export function LycheReservationPage() {
const { t } = useTranslation();
const sultenMail = useKeyValue(KV.SULTEN_MAIL);
const [reservation, setReservation] = useState<ReservationDto>();
const [hoursOptions, setHoursOptions] = useState<DropDownOption<string>[]>();
const [occasionOptions, setOccasionOptions] = useState<DropDownOption<string>[]>();
const [occupancyOptions, setOccupancyOptions] = useState<DropDownOption<number>[]>();
const [availableDate, setAvailableDate] = useState<boolean>(false);

const occasionOptions: DropDownOption<string>[] = [
{ value: 'DRINK', label: 'drikke' },
{ value: 'EAT', label: 'spise' },
];

const hoursOptions: DropDownOption<string>[] = [
{ value: '12:00', label: '12:00' },
{ value: '13:00', label: '13:00' },
{ value: '14:00', label: '14:00' },
{ value: '15:00', label: '15:00' },
];

const occupancyOptions: DropDownOption<number>[] = [
{ value: 1, label: '1' },
{ value: 2, label: '2' },
{ value: 3, label: '3' },
{ value: 4, label: '4' },
];
useEffect(() => {
getReservationForm()
.then((data) => {
setOccasionOptions(data.occasion.map((d: string[]) => ({ value: d[0], label: d[1] })));
if (data.biggest_table == 0) {
toast.error('Det eksisterer ingen bord i backend');
} else {
Comment on lines +33 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Antar dette bare er er for testing?

setOccupancyOptions(
new Array(data.biggest_table).fill(null).map((_, i) => ({ value: i + 1, label: (i + 1).toString() })),
);
}
})
.catch((error) => {
toast.error(t(KEY.common_something_went_wrong));
console.error(error);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vi burde egt ikke ignorere disse

}, []);

function checkAvailableDate(data: ReservationDto) {
setReservation({ ...reservation, ...data } as ReservationDto);
setAvailableDate(true);
getAvailableTimes(data as FetchAvailableTimesDto)
.then((dateData: string[]) => {
const transformed_hours = dateData.map((d: string) => ({ value: d, label: d }));
setHoursOptions(transformed_hours);
if (transformed_hours.length > 0) {
setReservation(data);
setAvailableDate(true);
} else {
toast.error(t(KEY.sulten_no_available_tables));
}
})
.catch((e) => {
if (e.response.status == STATUS.HTTP_406_NOT_ACCEPTABLE) {
toast.error(dbT(e.response.data, 'error'));
} else {
toast.error(t(KEY.common_something_went_wrong));
}
setAvailableDate(false);
});
}

function submit(data: ReservationDto) {
console.log({ ...reservation, data });
}

const findAvailableDateStage = (
<SamfForm
className={styles.formContainer}
Expand Down Expand Up @@ -87,7 +112,7 @@ export function LycheReservationPage() {
{t(KEY.common_date)} {reservation?.reservation_date as string}
</p>
<p className={styles.text}>
{t(KEY.common_guests)} {reservation?.guest_count}
{t(KEY.common_guests)}: {reservation?.guest_count}
</p>
</div>
<ReservationFormLine label={t(KEY.common_time) + '*'}>
Expand Down
16 changes: 16 additions & 0 deletions frontend/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ClosedPeriodDto,
EventDto,
EventGroupDto,
FetchAvailableTimesDto,
FoodCategoryDto,
FoodPreferenceDto,
GangDto,
Expand All @@ -20,6 +21,7 @@ import {
RecruitmentAdmissionDto,
RecruitmentDto,
RecruitmentPositionDto,
ReservationFormDto,
SaksdokumentDto,
TextItemDto,
UserDto,
Expand Down Expand Up @@ -236,6 +238,20 @@ export async function getEventGroups(): Promise<EventGroupDto[]> {
return response.data;
}

export async function getAvailableTimes(data: FetchAvailableTimesDto, pk?: number): Promise<string[]> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pk først ser bedre ut

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Skal ikke optional parametre være etter non-optional?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hvorfor er den optional i det hele tatt?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Det er noe som ikke stemmer med dette endepunktet

const url = BACKEND_DOMAIN + reverse({ pattern: ROUTES.backend.samfundet__check_reservation, urlParams: { pk: pk } });
const response = await axios.post<string[]>(url, data, { withCredentials: true });

return response.data;
}

export async function getReservationForm(): Promise<ReservationFormDto> {
const url = BACKEND_DOMAIN + ROUTES.backend.samfundet__reservation_form;
const response = await axios.get<ReservationFormDto>(url, { withCredentials: true });

return response.data;
}

export async function getMenus(): Promise<MenuDto[]> {
const url = BACKEND_DOMAIN + ROUTES.backend.samfundet__menu_list;
const response = await axios.get<MenuDto[]>(url, { withCredentials: true });
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,16 @@ export type MenuDto = {
menu_items?: MenuItemDto[];
};

export type FetchAvailableTimesDto = {
guest_count: number;
reservation_date: string;
};

export type ReservationFormDto = {
occasion: string[][];
biggest_table: number;
};

export type ReservationDto = {
name?: string;
email?: string;
Expand Down
1 change: 1 addition & 0 deletions frontend/src/i18n/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export const KEY = {
sulten_reservation_form_more_than_8_help: 'sulten_reservation_form_more_than_8_help',
sulten_reservation_form_remember_closing: 'sulten_reservation_form_remember_closing',
sulten_reservation_form_find_times: 'sulten_reservation_form_find_times',
sulten_no_available_tables: 'sulten_no_available_tables',
// Recruitment:
recruitment_tags: 'recruitment_tags',
recruitment_position: 'recruitment_position',
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/i18n/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export const nb: Record<KeyValues, string> = {
[KEY.sulten_reservation_form_remember_closing]: 'Husk at Lyche stenger',
[KEY.sulten_reservation_form_find_times]: 'Finn Ledige Tidspunkt',
[KEY.sulten_dishes]: 'Retter',
[KEY.sulten_no_available_tables]: 'Ingen ledige bord for denne dagen eller antall gjester',

// Occupied Recruitment
[KEY.occupied_title]: 'Tilgjenglighet',
Expand Down Expand Up @@ -490,7 +491,7 @@ export const en: Record<KeyValues, string> = {
[KEY.sulten_reservation_form_more_than_8_help]: 'More than 8 people? send us an',
[KEY.sulten_reservation_form_remember_closing]: 'Remember that Lyche closes at',
[KEY.sulten_reservation_form_find_times]: 'Find Available Times',

[KEY.sulten_no_available_tables]: 'No available tables for this date and guest count',
// Occupied Recruitment
[KEY.occupied_title]: 'Availability',
[KEY.occupied_help_text]: 'Please indicate times when you are unavailable',
Expand Down
13 changes: 12 additions & 1 deletion frontend/src/routes/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ THIS FILE IS AUTOGENERATED.
DO NOT WRITE IN THIS FILE, AS IT WILL BE OVERWRITTEN ON NEXT UPDATE.

THIS FILE WAS GENERATED BY: root.management.commands.generate_routes
LAST UPDATE: 2023-12-18 15:49:32.799909+00:00
LAST UPDATE: 2024-01-27 14:29:54.437785+00:00
"""
*/
// ############################################################
Expand Down Expand Up @@ -97,6 +97,15 @@ export const ROUTES_BACKEND = {
admin__guardian_userobjectpermission_delete: '/admin/guardian/userobjectpermission/:objectId/delete/',
admin__guardian_userobjectpermission_change: '/admin/guardian/userobjectpermission/:objectId/change/',
adminguardianuserobjectpermission__objectId: '/admin/guardian/userobjectpermission/:objectId/',
admin__samfundet_campus_permissions: '/admin/samfundet/campus/:objectPk/permissions/',
admin__samfundet_campus_permissions_manage_user: '/admin/samfundet/campus/:objectPk/permissions/user-manage/:userId/',
admin__samfundet_campus_permissions_manage_group: '/admin/samfundet/campus/:objectPk/permissions/group-manage/:groupId/',
admin__samfundet_campus_changelist: '/admin/samfundet/campus/',
admin__samfundet_campus_add: '/admin/samfundet/campus/add/',
admin__samfundet_campus_history: '/admin/samfundet/campus/:objectId/history/',
admin__samfundet_campus_delete: '/admin/samfundet/campus/:objectId/delete/',
admin__samfundet_campus_change: '/admin/samfundet/campus/:objectId/change/',
adminsamfundetcampus__objectId: '/admin/samfundet/campus/:objectId/',
admin__samfundet_userpreference_permissions: '/admin/samfundet/userpreference/:objectPk/permissions/',
admin__samfundet_userpreference_permissions_manage_user: '/admin/samfundet/userpreference/:objectPk/permissions/user-manage/:userId/',
admin__samfundet_userpreference_permissions_manage_group: '/admin/samfundet/userpreference/:objectPk/permissions/group-manage/:groupId/',
Expand Down Expand Up @@ -462,6 +471,8 @@ export const ROUTES_BACKEND = {
samfundet__home: '/home/',
samfundet__assign_group: '/assign_group/',
samfundet__webhook: '/webhook/',
samfundet__check_reservation: '/check-reservation/',
samfundet__reservation_form: '/reservation-form/',
samfundet__recruitment_positions: '/recruitment-positions/',
samfundet__recruitment_positions_gang: '/recruitment-positions-gang/',
samfundet__active_recruitment_positions: '/active-recruitment-positions/',
Expand Down
Loading