From 1db5e46fcf5ec993e1243a3226ce90a683761db0 Mon Sep 17 00:00:00 2001 From: Ai Ling <50992674+ailing35@users.noreply.github.com> Date: Wed, 9 Nov 2022 13:02:32 +0800 Subject: [PATCH] [offers][fix] Fix year options (#548) --- apps/portal/src/components/offers/Years.ts | 18 +++++++++++------- .../offersSubmission/OffersSubmissionForm.tsx | 2 +- .../submissionForm/OfferDetailsForm.tsx | 7 +++++-- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/apps/portal/src/components/offers/Years.ts b/apps/portal/src/components/offers/Years.ts index da9ab8e3..e6ececb1 100644 --- a/apps/portal/src/components/offers/Years.ts +++ b/apps/portal/src/components/offers/Years.ts @@ -1,8 +1,12 @@ const NUM_YEARS = 5; -export const FutureYearsOptions = Array.from({ length: NUM_YEARS }, (_, i) => { - const year = new Date().getFullYear() + i; - return { - label: String(year), - value: year, - }; -}); +const OFFSET = 2; +export const YearsOptions = (yearSelected?: number) => + Array.from({ length: NUM_YEARS }, (_, i) => { + const year = yearSelected + ? yearSelected - OFFSET + i + : new Date().getFullYear() - OFFSET + i; + return { + label: String(year), + value: year, + }; + }); diff --git a/apps/portal/src/components/offers/offersSubmission/OffersSubmissionForm.tsx b/apps/portal/src/components/offers/offersSubmission/OffersSubmissionForm.tsx index 51700a52..ca3beed3 100644 --- a/apps/portal/src/components/offers/offersSubmission/OffersSubmissionForm.tsx +++ b/apps/portal/src/components/offers/offersSubmission/OffersSubmissionForm.tsx @@ -73,7 +73,7 @@ export const defaultInternshipOfferValues = { monthlySalary: { currency: DEFAULT_CURRENCY, }, - startYear: null, + startYear: getCurrentYear(), }, }; diff --git a/apps/portal/src/components/offers/offersSubmission/submissionForm/OfferDetailsForm.tsx b/apps/portal/src/components/offers/offersSubmission/submissionForm/OfferDetailsForm.tsx index 9f1f5f43..859526da 100644 --- a/apps/portal/src/components/offers/offersSubmission/submissionForm/OfferDetailsForm.tsx +++ b/apps/portal/src/components/offers/offersSubmission/submissionForm/OfferDetailsForm.tsx @@ -29,7 +29,7 @@ import FormTextInput from '../../forms/FormTextInput'; import { InternshipCycleOptions } from '../../InternshipCycles'; import JobTypeTabs from '../../JobTypeTabs'; import type { OfferFormData } from '../../types'; -import { FutureYearsOptions } from '../../Years'; +import { YearsOptions } from '../../Years'; import { CURRENCY_OPTIONS } from '../../../../utils/offers/currency/CurrencyEnum'; type FullTimeOfferDetailsFormProps = Readonly<{ @@ -287,6 +287,9 @@ function InternshipOfferDetailsForm({ const { register, formState, control } = useFormContext<{ offers: Array; }>(); + const watchStartYear = useWatch({ + name: `offers.${index}.offersIntern.startYear`, + }); const offerFields = formState.errors.offers?.[index]; return ( @@ -351,7 +354,7 @@ function InternshipOfferDetailsForm({ display="block" errorMessage={offerFields?.offersIntern?.startYear?.message} label="Internship Year" - options={FutureYearsOptions} + options={YearsOptions(watchStartYear)} required={true} {...register(`offers.${index}.offersIntern.startYear`, { required: FieldError.REQUIRED,