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

use availableDates from itemsAPI with NewRequestingDayPicker #10848

Merged
merged 6 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 53 additions & 0 deletions content/webapp/components/CalendarSelect/NewCalendarSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { FunctionComponent } from 'react';
import Select from '@weco/content/components/Select';
import { dateAsValue } from '../ItemRequestModal/format-date';
import { AvailabilitySlot } from '@weco/content/services/wellcome/catalogue/types';
import { formatDayName, formatDayMonth } from '@weco/common/utils/format-date';

type Props = {
availableDates: AvailabilitySlot[];
chosenDate?: string;
setChosenDate: (value: string) => void;
};

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 for consistency
rcantin-w marked this conversation as resolved.
Show resolved Hide resolved
.slice(0, 12)
);
};

const NewCalendarSelect: FunctionComponent<Props> = ({
availableDates,
chosenDate,
setChosenDate,
}) => {
return availableDates.length ? (
<Select
name="calendar_dates"
label="Select a date"
hideLabel={true}
options={availabilitySlotsToSelectOptions(availableDates)}
value={chosenDate || 'Select a date'}
onChange={e => setChosenDate(e.target.value)}
/>
) : (
<>Error: Available dates could not be found.</>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In case there is an error in the itemsAPI when it fetches the opening times from the contentAPI, the itemsAPI returns an empty list of dates for that item, rather than than failing the entire request.
This means there is a small, yet non-null possibility that the list of available dates will be empty for one/all of the work's items. I'm choosing to wait for the last step to surface this error because it doesn't mean the item is not requestable, we just couldn't get the dates.
Is this the right way to go? and if it is, how can we make it look better? eg. add the library or digital email address?

Copy link
Contributor

Choose a reason for hiding this comment

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

Does this need a designer or can we figure it out on a call tomorrow maybe?

Copy link
Contributor

@rcantin-w rcantin-w May 9, 2024

Choose a reason for hiding this comment

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

);
};

export default NewCalendarSelect;
31 changes: 23 additions & 8 deletions content/webapp/components/ItemRequestModal/RequestDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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 @@ -14,6 +15,7 @@ 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 @@ -77,6 +79,8 @@ const RequestDialog: FunctionComponent<RequestDialogProps> = ({
setIsActive,
currentHoldNumber,
}) => {
const { offsiteRequesting } = useToggles();

const availableDates = useAvailableDates();
const [pickUpDate, setPickUpDate] = useState<string | undefined>(
availableDates.nextAvailable && dateAsValue(availableDates.nextAvailable)
Expand Down Expand Up @@ -140,14 +144,25 @@ const RequestDialog: FunctionComponent<RequestDialogProps> = ({
</PickupDeadline>
</PickUpDateDescription>
<PickUpDateInputWrapper>
<RequestingDayPicker
startDate={availableDates.nextAvailable}
endDate={availableDates.lastAvailable}
exceptionalClosedDates={availableDates.exceptionalClosedDates}
regularClosedDays={availableDates.regularClosedDays}
pickUpDate={pickUpDate}
setPickUpDate={setPickUpDate}
/>
<>
{offsiteRequesting && (
<NewRequestingDayPicker
availableDates={item?.availableDates || []}
pickUpDate={pickUpDate}
setPickUpDate={setPickUpDate}
/>
)}
{!offsiteRequesting && (
<RequestingDayPicker
startDate={availableDates.nextAvailable}
endDate={availableDates.lastAvailable}
exceptionalClosedDates={availableDates.exceptionalClosedDates}
regularClosedDays={availableDates.regularClosedDays}
pickUpDate={pickUpDate}
setPickUpDate={setPickUpDate}
/>
)}
</>
</PickUpDateInputWrapper>
</PickUpDate>
</Space>
Expand Down
4 changes: 2 additions & 2 deletions content/webapp/components/PhysicalItems/PhysicalItems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ const PhysicalItems: FunctionComponent<Props> = ({
const [itemsState, setItemsState] = useItemsState(workItems);

useEffect(() => {
setPhysicalItems(workItems);
}, [workItems]);
setPhysicalItems(physicalItems);
}, [physicalItems]);
rcantin-w marked this conversation as resolved.
Show resolved Hide resolved

useAbortSignalEffect(
signal => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { FunctionComponent } from 'react';
import NewCalendarSelect from '../CalendarSelect/NewCalendarSelect';
import { AvailabilitySlot } from '@weco/content/services/wellcome/catalogue/types';

type Props = {
availableDates: AvailabilitySlot[];
pickUpDate?: string;
setPickUpDate: (date: string) => void;
};

const NewRequestingDayPicker: FunctionComponent<Props> = ({
availableDates,
pickUpDate,
setPickUpDate,
}: Props) => {
return (
<div style={{ position: 'relative' }}>
<NewCalendarSelect
availableDates={availableDates}
chosenDate={pickUpDate}
setChosenDate={setPickUpDate}
/>
</div>
);
};

export default NewRequestingDayPicker;
6 changes: 6 additions & 0 deletions content/webapp/services/wellcome/catalogue/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export type Item<LocationType> = {
locations: LocationType[];
type: 'Item';
note?: string;
availableDates?: AvailabilitySlot[];
};

export type PhysicalItem = Item<PhysicalLocation> & {
Expand All @@ -159,6 +160,11 @@ export type PhysicalItem = Item<PhysicalLocation> & {
};
};

export type AvailabilitySlot = {
from: string;
to: string;
};

type Date = {
label: string;
type: 'Period';
Expand Down
4 changes: 4 additions & 0 deletions content/webapp/test/fixtures/catalogueApi/work.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,10 @@ export const workWithPartOf: Work = {
type: 'DigitalLocation',
},
],
availableDates: [
{ from: '2022-05-23T09:00:00.000Z', to: '2022-05-23T09:00:00.000Z' },
{ from: '2022-05-24T09:00:00.000Z', to: '2022-05-24T09:00:00.000Z' },
],
type: 'Item',
},
],
Expand Down