|
| 1 | +import { |
| 2 | + CardPart, |
| 3 | + Heading, |
| 4 | + MultiplePartsCard, |
| 5 | + Spacer, |
| 6 | + Text, |
| 7 | +} from "@python-italia/pycon-styleguide"; |
| 8 | +import { eachDayOfInterval, format, parseISO } from "date-fns"; |
| 9 | +import { Fragment } from "react"; |
| 10 | +import { FormattedMessage } from "react-intl"; |
| 11 | +import { useCurrentLanguage } from "~/locale/context"; |
| 12 | +import type { CfpFormQuery } from "~/types"; |
| 13 | + |
| 14 | +const CHOICES = ["available", "preferred", "unavailable"]; |
| 15 | +const RANGES = ["am", "pm"]; |
| 16 | + |
| 17 | +type Props = { |
| 18 | + conferenceData: CfpFormQuery; |
| 19 | + selectedDuration: any; |
| 20 | + speakerAvailabilities: any; |
| 21 | + onChangeAvailability: any; |
| 22 | +}; |
| 23 | +export const AvailabilitySection = ({ |
| 24 | + conferenceData, |
| 25 | + selectedDuration, |
| 26 | + speakerAvailabilities, |
| 27 | + onChangeAvailability, |
| 28 | +}: Props) => { |
| 29 | + const language = useCurrentLanguage(); |
| 30 | + const { |
| 31 | + conference: { start, end }, |
| 32 | + } = conferenceData; |
| 33 | + const parsedStart = parseISO(start); |
| 34 | + const parsedEnd = parseISO(end); |
| 35 | + const daysBetween = eachDayOfInterval({ start: parsedStart, end: parsedEnd }); |
| 36 | + const dateFormatter = new Intl.DateTimeFormat(language, { |
| 37 | + day: "2-digit", |
| 38 | + month: "long", |
| 39 | + }); |
| 40 | + |
| 41 | + return ( |
| 42 | + <MultiplePartsCard> |
| 43 | + <CardPart contentAlign="left"> |
| 44 | + <Heading size={3}> |
| 45 | + <FormattedMessage id="cfp.availability.title" /> |
| 46 | + </Heading> |
| 47 | + </CardPart> |
| 48 | + <CardPart background="milk" contentAlign="left"> |
| 49 | + <Text size={2}> |
| 50 | + <FormattedMessage id="cfp.availability.description" /> |
| 51 | + </Text> |
| 52 | + <Spacer size="small" /> |
| 53 | + <div |
| 54 | + className="grid gap-2 lg:gap-4 select-none" |
| 55 | + style={{ |
| 56 | + gridTemplateColumns: `150px repeat(${daysBetween.length}, 1fr)`, |
| 57 | + }} |
| 58 | + > |
| 59 | + <div /> |
| 60 | + |
| 61 | + {daysBetween.map((day) => ( |
| 62 | + <Text |
| 63 | + key={day.toISOString()} |
| 64 | + weight="strong" |
| 65 | + size={"label3"} |
| 66 | + align="center" |
| 67 | + > |
| 68 | + {dateFormatter.format(day)} |
| 69 | + </Text> |
| 70 | + ))} |
| 71 | + |
| 72 | + {RANGES.map((hour) => ( |
| 73 | + <Fragment key={hour}> |
| 74 | + <div> |
| 75 | + <Text weight="strong" size={"label3"} as="p"> |
| 76 | + {hour === "am" ? ( |
| 77 | + <FormattedMessage id="cfp.availability.table.morning" /> |
| 78 | + ) : ( |
| 79 | + <FormattedMessage id="cfp.availability.table.afternoon" /> |
| 80 | + )} |
| 81 | + </Text> |
| 82 | + <Spacer size="thin" /> |
| 83 | + <Text weight="strong" size={"label3"} as="p"> |
| 84 | + {hour === "am" ? ( |
| 85 | + <FormattedMessage id="cfp.availability.table.morning.range" /> |
| 86 | + ) : ( |
| 87 | + <FormattedMessage id="cfp.availability.table.afternoon.range" /> |
| 88 | + )} |
| 89 | + </Text> |
| 90 | + </div> |
| 91 | + {daysBetween.map((day) => { |
| 92 | + const availabilityDate = `${format(day, "yyyy-MM-dd")}@${hour}`; |
| 93 | + const currentChoice = |
| 94 | + speakerAvailabilities?.[availabilityDate] || "available"; |
| 95 | + |
| 96 | + return ( |
| 97 | + <div |
| 98 | + className="w-full cursor-pointer text-center p-1 bg-grey-50 hover:bg-grey-100" |
| 99 | + onClick={(_) => { |
| 100 | + const nextChoice = |
| 101 | + CHOICES[ |
| 102 | + (CHOICES.indexOf(currentChoice) + 1) % CHOICES.length |
| 103 | + ]; |
| 104 | + onChangeAvailability(availabilityDate, nextChoice); |
| 105 | + }} |
| 106 | + > |
| 107 | + {currentChoice === "available" && <> </>} |
| 108 | + {currentChoice === "preferred" && "✔️"} |
| 109 | + {currentChoice === "unavailable" && "❌"} |
| 110 | + </div> |
| 111 | + ); |
| 112 | + })} |
| 113 | + </Fragment> |
| 114 | + ))} |
| 115 | + </div> |
| 116 | + |
| 117 | + <Spacer size="small" /> |
| 118 | + |
| 119 | + <Text size={3}> |
| 120 | + <FormattedMessage id="cfp.availability.explanation" /> |
| 121 | + </Text> |
| 122 | + </CardPart> |
| 123 | + </MultiplePartsCard> |
| 124 | + ); |
| 125 | +}; |
0 commit comments