Skip to content

Commit

Permalink
fix: hide deny / edit actions for cancelled reservations
Browse files Browse the repository at this point in the history
Use same check rules for action button visibility for both series and
single reservations.
  • Loading branch information
joonatank committed Jan 9, 2025
1 parent 6446df2 commit a2c3fc3
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 58 deletions.
69 changes: 35 additions & 34 deletions apps/admin-ui/src/component/RecurringReservationsView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import { base64encode, filterNonNullable } from "common/src/helpers";
import { LoadingSpinner } from "hds-react";
import { errorToast } from "common/src/common/toast";
import { useCheckPermission } from "@/hooks";
import {
isPossibleToDeny,
isPossibleToEdit,
} from "@/modules/reservationModificationRules";

type RecurringReservationType = NonNullable<
RecurringReservationQuery["recurringReservation"]
Expand Down Expand Up @@ -126,40 +130,37 @@ export function RecurringReservationsView({
const buttons = [];
const startDate = new Date(x.begin);
const endDate = new Date(x.end);
const now = new Date();

if (x.state !== ReservationStateChoice.Denied) {
if (hasPermission && startDate > now && onChange) {
buttons.push(
<ReservationListButton
key="change"
callback={() => handleChange(x)}
type="change"
t={t}
/>
);
}

if (onSelect) {
buttons.push(
<ReservationListButton
key="show"
callback={() => onSelect(x)}
type="show"
t={t}
/>
);
}
if (hasPermission && endDate > now) {
buttons.push(
<ReservationListButton
key="deny"
callback={() => handleRemove(x)}
type="deny"
t={t}
/>
);
}

if (hasPermission && onChange && isPossibleToDeny(x.state, endDate)) {
buttons.push(
<ReservationListButton
key="change"
callback={() => handleChange(x)}
type="change"
t={t}
/>
);
}

if (onSelect && x.state === ReservationStateChoice.Confirmed) {
buttons.push(
<ReservationListButton
key="show"
callback={() => onSelect(x)}
type="show"
t={t}
/>
);
}
if (hasPermission && isPossibleToEdit(x.state, endDate)) {
buttons.push(
<ReservationListButton
key="deny"
callback={() => handleRemove(x)}
type="deny"
t={t}
/>
);
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,46 @@ import { addHours, isToday } from "date-fns";
* Allowed to change state (except deny unconfirmed) only till it's ended.
* Allowed to modify the reservation after ending as long as it's the same date or within one hour.
*/
export const isPossibleToApprove = (
export function isPossibleToApprove(
state: ReservationStateChoice,
end: Date
): boolean =>
state === ReservationStateChoice.RequiresHandling && end > new Date();
): boolean {
return state === ReservationStateChoice.RequiresHandling && end > new Date();
}

export const isPossibleToDeny = (
state: ReservationStateChoice,
/// for regular reservations they can be denied until the end
/// a full series of reservations can only be denied till the start of the last one
export function isPossibleToDeny(
state: Maybe<ReservationStateChoice> | undefined,
end: Date
): boolean => {
): boolean {
if (state === ReservationStateChoice.RequiresHandling) {
return true;
}
return state === ReservationStateChoice.Confirmed && end > new Date();
};
}

export const isPossibleToReturn = (
export function isPossibleToReturn(
state: ReservationStateChoice,
end: Date
): boolean =>
(state === ReservationStateChoice.Denied ||
state === ReservationStateChoice.Confirmed) &&
end > new Date();
): boolean {
if (
state !== ReservationStateChoice.Denied &&
state !== ReservationStateChoice.Confirmed
) {
return false;
}

return end > new Date();
}

export const isPossibleToEdit = (
export function isPossibleToEdit(
state: Maybe<ReservationStateChoice> | undefined | null,
end: Date
): boolean => {
): boolean {
if (state !== ReservationStateChoice.Confirmed) {
return false;
}
const now = new Date();
return end > addHours(now, -1) || isToday(end);
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
isPossibleToDeny,
isPossibleToEdit,
isPossibleToReturn,
} from "./reservationModificationRules";
} from "@/modules/reservationModificationRules";

type ReservationType = NonNullable<ReservationQuery["reservation"]>;
type Props = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React from "react";
import { ReservationStateChoice, type ReservationQuery } from "@gql/gql-types";
import { type ReservationQuery } from "@gql/gql-types";
import { useTranslation } from "next-i18next";
import { Button } from "hds-react";
import { ButtonLikeLink } from "@/component/ButtonLikeLink";
import { DenyDialogSeries } from "@/component/DenyDialog";
import { useModal } from "@/context/ModalContext";
import { useRecurringReservations } from "@/hooks";
import { isPossibleToDeny } from "@/modules/reservationModificationRules";

// TODO use a fragment
type ReservationType = NonNullable<ReservationQuery["reservation"]>;
Expand Down Expand Up @@ -40,15 +41,14 @@ export function ApprovalButtonsRecurring({
handleAccept();
};

const now = new Date();
// TODO don't need to do this anymore we can just pass the first reservation here
// need to do get all data here otherwise totalCount is incorrect (filter here instead of in the query)
const reservationsPossibleToDelete = reservations
.filter((x) => new Date(x.begin) > now)
.filter((x) => x.state === ReservationStateChoice.Confirmed);
const reservationsPossibleToDeny = reservations.filter((x) =>
isPossibleToDeny(x.state, new Date(x.begin))
);

const handleDenyClick = () => {
const reservation = reservationsPossibleToDelete.find(() => true);
const reservation = reservationsPossibleToDeny.find(() => true);
if (reservation == null) {
return;
}
Expand All @@ -74,7 +74,7 @@ export function ApprovalButtonsRecurring({
disabled: false,
} as const;

if (reservationsPossibleToDelete.length === 0) {
if (reservationsPossibleToDeny.length === 0) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion apps/admin-ui/src/spa/reservations/[id]/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { useModal } from "@/context/ModalContext";
import eventStyleGetter, { legend } from "./eventStyleGetter";
import { Legend, LegendsWrapper } from "@/component/Legend";
import { EditTimeModal } from "@/component/EditTimeModal";
import { isPossibleToEdit } from "./reservationModificationRules";
import { isPossibleToEdit } from "@/modules/reservationModificationRules";
import { getEventBuffers } from "common/src/calendar/util";
import { filterNonNullable } from "common/src/helpers";
import VisibleIfPermission from "@/component/VisibleIfPermission";
Expand Down

0 comments on commit a2c3fc3

Please sign in to comment.