Skip to content

Commit

Permalink
enforce dietary requirements on booking creation (#676)
Browse files Browse the repository at this point in the history
  • Loading branch information
choden-dev authored Jul 22, 2024
1 parent dd6896a commit 9fcb92c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,19 @@ describe("RequirementCheckBoxes", () => {
"agreed-to-general-policy-checkbox"
)

const dietaryRequirementsInput = getByTestId("dietary-requirements-input")

fireEvent.click(nightPolicyCheckbox)
fireEvent.click(bookingPolicyCheckbox)
fireEvent.change(dietaryRequirementsInput, {
target: { value: "i" }
})

expect(mockOnValidityChange).toHaveBeenCalledWith(false)

fireEvent.change(dietaryRequirementsInput, {
target: { value: "i3" }
})

expect(mockOnValidityChange).toHaveBeenCalledWith(true)
})
Expand All @@ -67,8 +78,14 @@ describe("RequirementCheckBoxes", () => {
"agreed-to-general-policy-checkbox"
)

const dietaryRequirementsInput = getByTestId("dietary-requirements-input")

fireEvent.click(nightPolicyCheckbox)

fireEvent.change(dietaryRequirementsInput, {
target: { value: "ii" }
})

expect(mockOnValidityChange).toHaveBeenCalledWith(false)

fireEvent.click(bookingPolicyCheckbox)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ export const CreateBookingSection = ({
variant="default"
onClick={() => {
if (!isValidForCreation) {
alert("Please check all the required acknowledgements")
alert(
"Please check all the required acknowledgements and enter your dietary requirements"
)
return
}
if (
Expand Down Expand Up @@ -312,12 +314,7 @@ export const CreateBookingSection = ({
onValidityChange={(newValid) => {
setIsValidForCreation(newValid)
}}
/>

<TextInput
onChange={(e) => handleAllergyChange?.(e.target.value)}
label="Please describe your dietary requirements"
placeholder="Enter dietary requirements here"
handleAllergyChange={handleAllergyChange}
/>

{hasExistingSession ? (
Expand All @@ -338,47 +335,78 @@ interface IRequirementCheckBoxes {
* @param newValid if the current state of the checkboxes is valid
*/
onValidityChange: (newValid: boolean) => void

/**
* @param newAllergies
*/
handleAllergyChange?: (newAllergies: string) => void
}

/**
* To allow users to enter "no"
*/
const DIETARY_REQUIREMENTS_MIN_LENGTH = 2 as const

/**
* Provides a way to see if the user has agreed to all required policy
* @deprecated only for internal use in `BookingCreation`, exported for testing purposes
*/
export const RequirementCheckBoxes = ({
onValidityChange
onValidityChange,
handleAllergyChange
}: IRequirementCheckBoxes) => {
const [acceptedRequirements, setAcceptedRequirements] = useState<{
nightPolicy?: boolean
bookingPolicy?: boolean
dietaryRequirements?: boolean
}>({})
useEffect(() => {
onValidityChange(
!!acceptedRequirements.nightPolicy && !!acceptedRequirements.bookingPolicy
!!acceptedRequirements.nightPolicy &&
!!acceptedRequirements.bookingPolicy &&
!!acceptedRequirements.dietaryRequirements
)
}, [acceptedRequirements, onValidityChange])

return (
<span className="mb-3 flex w-full flex-col gap-1">
<Checkbox
data-testid="agreed-to-night-policy-checkbox"
onChange={(e) => {
setAcceptedRequirements({
...acceptedRequirements,
nightPolicy: e.target.checked
})
}}
label="I understand that each date corresponds to one night's stay"
/>
<Checkbox
data-testid="agreed-to-general-policy-checkbox"
label="I have read and acknowledged the booking policy"
<>
<span className="mb-3 flex w-full flex-col gap-1">
<Checkbox
data-testid="agreed-to-night-policy-checkbox"
onChange={(e) => {
setAcceptedRequirements({
...acceptedRequirements,
nightPolicy: e.target.checked
})
}}
label="I understand that each date corresponds to one night's stay"
/>
<Checkbox
data-testid="agreed-to-general-policy-checkbox"
label="I have read and acknowledged the booking policy"
onChange={(e) => {
setAcceptedRequirements({
...acceptedRequirements,
bookingPolicy: e.target.checked
})
}}
/>
</span>

<TextInput
onChange={(e) => {
handleAllergyChange?.(e.target.value)

setAcceptedRequirements({
...acceptedRequirements,
bookingPolicy: e.target.checked
dietaryRequirements:
e.target.value.length >= DIETARY_REQUIREMENTS_MIN_LENGTH
})
}}
data-testid="dietary-requirements-input"
label="Please describe your dietary requirements"
placeholder="Enter dietary requirements here"
/>
</span>
</>
)
}

0 comments on commit 9fcb92c

Please sign in to comment.