Skip to content

Commit

Permalink
Merge pull request #10875 from wellcomecollection/remove-available-da…
Browse files Browse the repository at this point in the history
…te-code-not-needed-anymore

Remove available date code not needed anymore
  • Loading branch information
agnesgaroux authored May 21, 2024
2 parents 9f63ee3 + c1be23f commit 55f2f32
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 1,034 deletions.
89 changes: 35 additions & 54 deletions content/webapp/components/CalendarSelect/CalendarSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,68 +1,49 @@
import { FunctionComponent } from 'react';
import Select, { SelectOption } from '@weco/content/components/Select';
import { isRequestableDate } from '../../utils/dates';
import { getDatesBetween } from '@weco/common/utils/dates';
import Select from '@weco/content/components/Select';
import { dateAsValue } from '../ItemRequestModal/format-date';
import {
formatDayName,
formatDayMonth,
DayOfWeek,
} from '@weco/common/utils/format-date';
import { AvailabilitySlot } from '@weco/content/services/wellcome/catalogue/types';
import { formatDayName, formatDayMonth } from '@weco/common/utils/format-date';

type Props = {
startDate?: Date;
endDate?: Date;
excludedDates: Date[];
excludedDays: DayOfWeek[];
availableDates: AvailabilitySlot[];
chosenDate?: string;
setChosenDate: (value: string) => void;
};

function getAvailableDates(
startDate: Date,
endDate: Date,
excludedDates: Date[],
excludedDays: DayOfWeek[]
): SelectOption[] {
return getDatesBetween({ startDate, endDate })
.filter(date =>
isRequestableDate({
date,
startDate,
endDate,
excludedDates,
excludedDays,
})
)
.map(date => ({
value: dateAsValue(date),
text: `${formatDayName(date)} ${formatDayMonth(date)}`,
}));
}
const availabilitySlotsToSelectOptions = (
availableDates: AvailabilitySlot[]
) => {
// AvailabilitySlots have open and close dateTimestamps
// right now we only care about the day, not the time
// so we're only using "from" = opening date/time
return (
availableDates
.map(availabilitySlot => new Date(availabilitySlot.from))
.map(availableDate => ({
value: dateAsValue(availableDate),
text: `${formatDayName(availableDate)} ${formatDayMonth(
availableDate
)}`,
}))
// the list of available dates is returned from the itemsAPI in various lenghts
// trimming down to 12
.slice(0, 12)
);
};

const CalendarSelect: FunctionComponent<Props> = ({
startDate,
endDate,
excludedDates,
excludedDays,
availableDates,
chosenDate,
setChosenDate,
}) => {
const availableDates =
startDate &&
endDate &&
getAvailableDates(startDate, endDate, excludedDates, excludedDays);

return availableDates ? (
<Select
name="calendar_dates"
label="Select a date"
hideLabel={true}
options={availableDates}
value={chosenDate || 'Select a date'}
onChange={e => setChosenDate(e.target.value)}
/>
) : null;
};
}) => (
<Select
name="calendar_dates"
label="Select a date"
hideLabel={true}
options={availabilitySlotsToSelectOptions(availableDates)}
value={chosenDate || 'Select a date'}
onChange={e => setChosenDate(e.target.value)}
/>
);

export default CalendarSelect;
49 changes: 0 additions & 49 deletions content/webapp/components/CalendarSelect/NewCalendarSelect.tsx

This file was deleted.

70 changes: 22 additions & 48 deletions content/webapp/components/ItemRequestModal/RequestDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { FunctionComponent, FormEvent, useState } from 'react';
import { useAvailableDates } from './useAvailableDates';
import { isRequestableDate } from '../../utils/dates';
import { allowedRequests } from '@weco/common/values/requests';
import { font } from '@weco/common/utils/classnames';
import Space from '@weco/common/views/components/styled/Space';
import RequestingDayPicker from '../RequestingDayPicker/RequestingDayPicker';
import NewRequestingDayPicker from '../RequestingDayPicker/NewRequestingDayPicker';
import Button, { ButtonTypes } from '@weco/common/views/components/Buttons';
import {
PhysicalItem,
Expand All @@ -15,7 +12,6 @@ import styled from 'styled-components';
import { CTAs, CurrentRequests, Header } from './common';
import { themeValues } from '@weco/common/views/themes/config';
import { dateAsValue, dateFromValue } from './format-date';
import { useToggles } from '@weco/common/server-data/Context';

const PickUpDate = styled(Space).attrs({
$v: { size: 'l', properties: ['padding-top', 'padding-bottom'] },
Expand Down Expand Up @@ -86,11 +82,11 @@ const RequestDialog: FunctionComponent<RequestDialogProps> = ({
setIsActive,
currentHoldNumber,
}) => {
const { offsiteRequesting } = useToggles();

const availableDates = useAvailableDates();
const firstAvailableDate = item.availableDates
? item.availableDates[0]
: undefined;
const [pickUpDate, setPickUpDate] = useState<string | undefined>(
availableDates.nextAvailable && dateAsValue(availableDates.nextAvailable)
firstAvailableDate && dateAsValue(new Date(firstAvailableDate.from))
);

function handleConfirmRequest(event: FormEvent<HTMLFormElement>) {
Expand All @@ -104,16 +100,7 @@ const RequestDialog: FunctionComponent<RequestDialogProps> = ({
// these DD-MM-YYYY values, and hopefully they've kept those bugs at bay.
const pickUpDateValue = pickUpDate ? dateFromValue(pickUpDate) : undefined;

if (
pickUpDateValue &&
isRequestableDate({
date: pickUpDateValue,
startDate: availableDates.nextAvailable,
endDate: availableDates.lastAvailable,
excludedDates: availableDates.exceptionalClosedDates,
excludedDays: availableDates.regularClosedDays,
})
) {
if (pickUpDateValue) {
confirmRequest(pickUpDateValue);
}
}
Expand Down Expand Up @@ -151,36 +138,23 @@ const RequestDialog: FunctionComponent<RequestDialogProps> = ({
</PickupDeadline>
</PickUpDateDescription>
<PickUpDateInputWrapper>
<>
{offsiteRequesting &&
(item.availableDates?.length ? (
<NewRequestingDayPicker
availableDates={item.availableDates}
pickUpDate={pickUpDate}
setPickUpDate={setPickUpDate}
/>
) : (
<ErrorMessage>
Error fetching available dates.
<br />
Try again later or email us at
<br />
<a href="mailto:[email protected]">
[email protected]
</a>
</ErrorMessage>
))}
{!offsiteRequesting && (
<RequestingDayPicker
startDate={availableDates.nextAvailable}
endDate={availableDates.lastAvailable}
exceptionalClosedDates={availableDates.exceptionalClosedDates}
regularClosedDays={availableDates.regularClosedDays}
pickUpDate={pickUpDate}
setPickUpDate={setPickUpDate}
/>
)}
</>
{item.availableDates?.length ? (
<RequestingDayPicker
availableDates={item.availableDates}
pickUpDate={pickUpDate}
setPickUpDate={setPickUpDate}
/>
) : (
<ErrorMessage>
Error fetching available dates.
<br />
Try again later or email us at
<br />
<a href="mailto:[email protected]">
[email protected]
</a>
</ErrorMessage>
)}
</PickUpDateInputWrapper>
</PickUpDate>
</Space>
Expand Down
57 changes: 0 additions & 57 deletions content/webapp/components/ItemRequestModal/useAvailableDates.ts

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
import { DayOfWeek } from '@weco/common/utils/format-date';
import { FunctionComponent } from 'react';
import CalendarSelect from '../CalendarSelect/CalendarSelect';
import { AvailabilitySlot } from '@weco/content/services/wellcome/catalogue/types';

type Props = {
startDate?: Date;
endDate?: Date;
exceptionalClosedDates: Date[];
regularClosedDays: DayOfWeek[];
availableDates: AvailabilitySlot[];
pickUpDate?: string;
setPickUpDate: (date: string) => void;
};

const RequestingDayPicker: FunctionComponent<Props> = ({
startDate,
endDate,
exceptionalClosedDates,
regularClosedDays,
availableDates,
pickUpDate,
setPickUpDate,
}: Props) => {
return (
<div style={{ position: 'relative' }}>
<CalendarSelect
startDate={startDate}
endDate={endDate}
excludedDates={exceptionalClosedDates}
excludedDays={regularClosedDays}
availableDates={availableDates}
chosenDate={pickUpDate}
setChosenDate={setPickUpDate}
/>
Expand Down
Loading

0 comments on commit 55f2f32

Please sign in to comment.