Skip to content

Commit

Permalink
Merge pull request #10916 from wellcomecollection/fix-item-request-modal
Browse files Browse the repository at this point in the history
useEffect when item.availableDates becomes defined
  • Loading branch information
agnesgaroux authored Jun 5, 2024
2 parents 3cb4941 + 36dda94 commit 1a8ac69
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions content/webapp/components/ItemRequestModal/RequestDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FunctionComponent, FormEvent, useState } from 'react';
import { FunctionComponent, FormEvent, useState, useEffect } from 'react';
import { allowedRequests } from '@weco/common/values/requests';
import { font } from '@weco/common/utils/classnames';
import Space from '@weco/common/views/components/styled/Space';
Expand Down Expand Up @@ -82,13 +82,19 @@ const RequestDialog: FunctionComponent<RequestDialogProps> = ({
setIsActive,
currentHoldNumber,
}) => {
const firstAvailableDate = item.availableDates
? item.availableDates[0]
: undefined;
const [pickUpDate, setPickUpDate] = useState<string | undefined>(
firstAvailableDate && dateAsValue(new Date(firstAvailableDate.from))
item.availableDates && dateAsValue(new Date(item.availableDates[0].from))
);

// the pickUpDate's state sometimes get set as undefined before the availableDates have been fetched
// as a result the user can't confirm the request unless they interact with the RequestingDayPicker in some way to trigger a state update
// here we set pickUpDate when item.availableDates becomes defined
useEffect(() => {
if (!pickUpDate && item.availableDates) {
setPickUpDate(dateAsValue(new Date(item.availableDates[0].from)));
}
}, [item.availableDates]);

function handleConfirmRequest(event: FormEvent<HTMLFormElement>) {
event.preventDefault();

Expand Down

0 comments on commit 1a8ac69

Please sign in to comment.