Skip to content

Commit

Permalink
Merge branch 'main' into feat/FSADT1-1058
Browse files Browse the repository at this point in the history
  • Loading branch information
paulushcgcj authored Dec 14, 2023
2 parents 2ed1efe + 34e53e2 commit 01f168b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 36 deletions.
1 change: 0 additions & 1 deletion frontend/src/helpers/validators/BCeIDFormValidations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
isGreaterThan,
} from "@/helpers/validators/GlobalValidators";


// Step 1: Business Information
formFieldValidations["businessInformation.businessName"] = [
isNotEmpty("Business Name cannot be empty"),
Expand Down
24 changes: 15 additions & 9 deletions frontend/src/helpers/validators/GlobalValidators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export const isUniqueDescriptive = (): {
if (
values.some(
(entry: string) => entry.toLowerCase() === value.toLowerCase()
)
) && value.trim() !== ""
) {
return message;
}
Expand Down Expand Up @@ -288,14 +288,14 @@ export const isWithinRange =
/**
* Checks if the value is a possibly valid day for the specified month.
* Note: February 29 will always be considered valid, since this validation does not consider the year.
*
*
* @param validMonth a valid month
* @param message the error message to be returned if the validation fails.
*/
export const isValidDayOfMonth =
(
validMonth: string,
message = "Value is not a valid day in the selected month",
message = "Value is not a valid day in the selected month"
) =>
(value: string): string => {
const arbitraryLeapYear = 2000;
Expand All @@ -310,7 +310,7 @@ export const isValidDayOfMonth =
/**
* Checks if the value is a valid day for the specified year and month.
* i.e. it tells if the date formed by the provided year, month and day exists.
*
*
* @param validYear a valid year
* @param validMonth a valid month
* @param message the error message to be returned if the validation fails.
Expand All @@ -319,7 +319,7 @@ export const isValidDayOfMonthYear =
(
validYear: string,
validMonth: string,
message = "Value is not a valid day in the selected month and year",
message = "Value is not a valid day in the selected month and year"
) =>
(value: string): string => {
const dateString = `${validYear}-${validMonth}-${value}`;
Expand All @@ -334,7 +334,7 @@ export const isMinimumYearsAgo =
(
years: number,
message: string | ((years: number) => string) = (years) =>
`Value must be at least ${years} years ago`,
`Value must be at least ${years} years ago`
) =>
(value: string): string => {
const maximumDate = subYears(startOfToday(), years);
Expand All @@ -349,7 +349,10 @@ export const isMinimumYearsAgo =
};

export const isGreaterThan =
(compareTo: number, message: string = `Value must be greater than ${compareTo}`) =>
(
compareTo: number,
message: string = `Value must be greater than ${compareTo}`
) =>
(value: string): string => {
if (Number(value) > compareTo) {
return "";
Expand All @@ -358,15 +361,18 @@ export const isGreaterThan =
};

export const isLessThan =
(compareTo: number, message: string = `Value must be less than ${compareTo}`) =>
(
compareTo: number,
message: string = `Value must be less than ${compareTo}`
) =>
(value: string): string => {
if (Number(value) < compareTo) {
return "";
}
return message;
};

export const isDateInThePast = (message: "Value must be in the past") => (value: string) => {
export const isDateInThePast = (message: string) => (value: string) => {
const dateValue = parseISO(value);
if (!isBefore(dateValue, startOfToday())) {
return message;
Expand Down
56 changes: 32 additions & 24 deletions frontend/src/helpers/validators/SubmissionValidators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,44 @@
*
* @see {@link SubmissionValidators.spec.ts}
*/
import { useEventBus } from '@vueuse/core'
import type { ValidationMessageType } from '@/dto/CommonTypesDto'
import { useEventBus } from "@vueuse/core";
import type { ValidationMessageType } from "@/dto/CommonTypesDto";

/**
* Event bus for submission error notifications.
*/
const errorBus = useEventBus<ValidationMessageType[]>('submission-error-notification')
const errorBus = useEventBus<ValidationMessageType[]>(
"submission-error-notification"
);

/**
* Event bus for revalidating the submission.
*/
const revalidateBus = useEventBus<void>('revalidate-bus')
const revalidateBus = useEventBus<void>("revalidate-bus");

/**
* Event bus for error notifications bar.
*/
const notificationBus = useEventBus<ValidationMessageType|undefined>("error-notification");
const notificationBus = useEventBus<ValidationMessageType | undefined>(
"error-notification"
);

/**
* Array of submission validators.
*/
let submissionValidators : ValidationMessageType[] = []
let submissionValidators: ValidationMessageType[] = [];

/**
* Register a listener for submission errors on the error bus.
* When an error is received, update the submission validators array.
*/
errorBus.on((errors) => {
submissionValidators = errors.map((error: ValidationMessageType) => {
notificationBus.emit(error)
return { ...error, originalValue: '' }
})
revalidateBus.emit()
})
notificationBus.emit(error);
return { ...error, originalValue: "" };
});
revalidateBus.emit();
});

/**
* Update the submission validators array with the provided fieldId and value.
Expand All @@ -60,13 +64,15 @@ errorBus.on((errors) => {
* @param value - The new value for the validator's originalValue property.
*/
const updateValidators = (fieldId: string, value: string): void => {
submissionValidators = submissionValidators.map((validator : ValidationMessageType) => {
if (validator.fieldId === fieldId) {
return { ...validator, originalValue: value }
submissionValidators = submissionValidators.map(
(validator: ValidationMessageType) => {
if (validator.fieldId === fieldId) {
return { ...validator, originalValue: value };
}
return validator;
}
return validator
})
}
);
};

/**
* Create a submission validation function for the specified fieldName.
Expand All @@ -78,14 +84,16 @@ export const submissionValidation = (
fieldName: string
): ((value: string) => string) => {
return (value: string) => {
const foundError = submissionValidators.find((validator: ValidationMessageType) => validator.fieldId === fieldName)
const foundError = submissionValidators.find(
(validator: ValidationMessageType) => validator.fieldId === fieldName
);
if (
foundError &&
(foundError.originalValue === value || foundError.originalValue === '')
(foundError.originalValue === value || foundError.originalValue === "")
) {
updateValidators(fieldName, value)
return foundError.errorMsg
updateValidators(fieldName, value);
return foundError.errorMsg;
}
return ''
}
}
return "";
};
};
3 changes: 2 additions & 1 deletion frontend/src/pages/FormBCSCPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ const handleRemove = (index: number) => {
? `${formData.location.contacts[index].firstName} ${formData.location.contacts[index].lastName}`
: "Contact #" + index;
bus.emit({
message: selectedContact,
name: selectedContact,
message: `“${selectedContact}” additional contact was deleted`,
kind: "Contact deleted",
toastTitle: "The additional contact was deleted",
handler: removeContact(index),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useFetchTo } from "@/composables/useFetch";
import {
BusinessSearchResult,
ClientTypeEnum,
ProgressNotification
ProgressNotification,
} from "@/dto/CommonTypesDto";
import { BusinessTypeEnum } from "@/dto/CommonTypesDto";
import type {
Expand Down

0 comments on commit 01f168b

Please sign in to comment.