Skip to content

Commit

Permalink
Merge pull request #61 from AplinkosMinisterija/59-laiko-pasirinkimo-…
Browse files Browse the repository at this point in the history
…tvarkymas

validate if selected event time is after minTime
  • Loading branch information
dovilemely authored Apr 30, 2024
2 parents e5f5bb2 + 55f6fbd commit b06d0a9
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 68 deletions.
5 changes: 4 additions & 1 deletion src/components/forms/Registration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ const RegistrationForm = ({

const deleteInfo = getDeleteInfo();

const validationSchema = isFreelancer ? validateFreelancerFishStocking : validateFishStocking;
const validationSchema = () => {
const applySchema = isFreelancer ? validateFreelancerFishStocking : validateFishStocking;
return applySchema(minTime);
};

const filterFishTypes = (batches: any[]) => {
const batchesFishTypesIds = batches.filter((b) => !!b.fishType?.id).map((b) => b.fishType?.id);
Expand Down
6 changes: 6 additions & 0 deletions src/utils/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,9 @@ export const compressImageSize = (file: File) =>
'blob',
);
});

export const checkIfDateIsAfter = (value: Date | undefined, minTime: number) => {
const selectedDate = value && new Date(value);
const minDate = new Date(new Date().setDate(new Date().getDate() + minTime));
return selectedDate && selectedDate > minDate;
};
1 change: 1 addition & 0 deletions src/utils/texts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const validationTexts: any = {
positiveNumber: 'Reikšmė turi būti didesnė už nulį',
requireFiles: 'Privalote įkelti dokumentus',
atLeastOneColumn: 'Turi būti pasirinktas bent vienas stulpelis',
invalidEventTime: 'Pasirinkite teisingą datą',
};

export const inputLabels = {
Expand Down
141 changes: 74 additions & 67 deletions src/utils/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as Yup from 'yup';
import { FishOriginTypes } from './constants';
import { validationTexts } from './texts';
import { FishBatch, FishType } from './types';
import { checkIfDateIsAfter } from './functions';

export const loginSchema = Yup.object().shape({
email: Yup.string().required(validationTexts.requireText).email(validationTexts.badEmailFormat),
Expand Down Expand Up @@ -47,74 +48,80 @@ export const validateMyProfile = Yup.object().shape({
.matches(/^(86|\+3706)\d{7}$/, validationTexts.badPhoneFormat),
});

export const validateFishStocking = Yup.object().shape({
location: Yup.object().required(validationTexts.requireText),
eventTime: Yup.date()
.required(validationTexts.requireText)
.typeError(validationTexts.requireText),
assignedTo: Yup.object().required(validationTexts.requireText).nullable(),
phone: Yup.string()
.required(validationTexts.requireText)
.trim()
.matches(/^(86|\+3706)\d{7}$/, validationTexts.badPhoneFormat),
batches: Yup.array().of(
Yup.object().shape({
fishType: Yup.object()
.required(validationTexts.requireSelect)
.shape({
id: Yup.number().required(validationTexts.requireText),
}),
fishAge: Yup.object()
.required(validationTexts.requireSelect)
.shape({
id: Yup.number().required(validationTexts.requireText),
}),
amount: Yup.string().required(validationTexts.requireText),
}),
),
fishOriginCompanyName: Yup.string().when('fishOrigin', (fishOrigin: any, schema: any) =>
fishOrigin?.[0] === FishOriginTypes.GROWN
? schema.required(validationTexts.requireText)
: schema,
),
fishOriginReservoir: Yup.object().when('fishOrigin', (fishOrigin: any, schema: any) =>
fishOrigin?.[0] === FishOriginTypes.CAUGHT
? schema.required(validationTexts.requireText)
: schema,
),
});
export const validateFishStocking = (minTime: number) =>
Yup.object().shape({
location: Yup.object().required(validationTexts.requireText),
eventTime: Yup.date()
.test('valid eventTime', validationTexts.invalidEventTime, (value) => {
return checkIfDateIsAfter(value, minTime);
})
.required(validationTexts.requireText),
assignedTo: Yup.object().required(validationTexts.requireText).nullable(),
phone: Yup.string()
.required(validationTexts.requireText)
.trim()
.matches(/^(86|\+3706)\d{7}$/, validationTexts.badPhoneFormat),
batches: Yup.array().of(
Yup.object().shape({
fishType: Yup.object()
.required(validationTexts.requireSelect)
.shape({
id: Yup.number().required(validationTexts.requireText),
}),
fishAge: Yup.object()
.required(validationTexts.requireSelect)
.shape({
id: Yup.number().required(validationTexts.requireText),
}),
amount: Yup.string().required(validationTexts.requireText),
}),
),
fishOriginCompanyName: Yup.string().when('fishOrigin', (fishOrigin: any, schema: any) =>
fishOrigin?.[0] === FishOriginTypes.GROWN
? schema.required(validationTexts.requireText)
: schema,
),
fishOriginReservoir: Yup.object().when('fishOrigin', (fishOrigin: any, schema: any) =>
fishOrigin?.[0] === FishOriginTypes.CAUGHT
? schema.required(validationTexts.requireText)
: schema,
),
});

export const validateFreelancerFishStocking = Yup.object().shape({
location: Yup.object().required(validationTexts.requireText),
eventTime: Yup.date()
.required(validationTexts.requireText)
.typeError(validationTexts.requireText),
batches: Yup.array().of(
Yup.object().shape({
fishType: Yup.object()
.required(validationTexts.requireSelect)
.shape({
id: Yup.number().required(validationTexts.requireText),
}),
fishAge: Yup.object()
.required(validationTexts.requireSelect)
.shape({
id: Yup.number().required(validationTexts.requireText),
}),
amount: Yup.string().required(validationTexts.requireText),
}),
),
fishOriginCompanyName: Yup.string().when('fishOrigin', (fishOrigin: any, schema: any) =>
fishOrigin?.[0] === FishOriginTypes.GROWN
? schema.required(validationTexts.requireText)
: schema,
),
fishOriginReservoir: Yup.object().when('fishOrigin', (fishOrigin: any, schema: any) =>
fishOrigin?.[0] === FishOriginTypes.CAUGHT
? schema.required(validationTexts.requireText)
: schema,
),
});
export const validateFreelancerFishStocking = (minTime: number) =>
Yup.object().shape({
location: Yup.object().required(validationTexts.requireText),
eventTime: Yup.date()
.test('valid eventTime', validationTexts.invalidEventTime, (value) => {
return checkIfDateIsAfter(value, minTime);
})
.required(validationTexts.requireText),
batches: Yup.array().of(
Yup.object().shape({
fishType: Yup.object()
.required(validationTexts.requireSelect)
.shape({
id: Yup.number().required(validationTexts.requireText),
}),
fishAge: Yup.object()
.required(validationTexts.requireSelect)
.shape({
id: Yup.number().required(validationTexts.requireText),
}),
amount: Yup.string().required(validationTexts.requireText),
}),
),
fishOriginCompanyName: Yup.string().when('fishOrigin', (fishOrigin: any, schema: any) =>
fishOrigin?.[0] === FishOriginTypes.GROWN
? schema.required(validationTexts.requireText)
: schema,
),
fishOriginReservoir: Yup.object().when('fishOrigin', (fishOrigin: any, schema: any) =>
fishOrigin?.[0] === FishOriginTypes.CAUGHT
? schema.required(validationTexts.requireText)
: schema,
),
});

export const validateFishStockingReview = Yup.object().shape({
waybillNo: Yup.string().required(validationTexts.requireText),
Expand Down

0 comments on commit b06d0a9

Please sign in to comment.