fleet/frontend/pages/SoftwarePage/components/AddPackageForm/helpers.ts
Gabriel Hernandez cedcadbb36
Add UI for Apple VPP apps feature on the software pages. (#20504)
relates to #19869

> NOTE: API integration will be included in a separate PR

This adds the UI updates to support the new VPP feature on the software
pages. This includes the software titles page and software titles
details page. The new UI includes:

**Add Vpp apps tab in Add software modal:**


![image](https://github.com/user-attachments/assets/f1751b5d-1393-45b7-894c-fa6d067d27d2)

**Various updates to the SoftwareIcon component to support icons from an
external source:**


![image](https://github.com/user-attachments/assets/c860d776-170c-4f51-813e-34f83e479471)

**Various updates to the SoftwarePackageCard compont to support app
store apps.**


![image](https://github.com/user-attachments/assets/7bdd369d-6c95-47a6-8942-63ac3c694ffe)

- [x] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://fleetdm.com/docs/contributing/committing-changes#changes-files)
for more information.
- [x] Added/updated testss:
- [x] Manual QA for all new/changed functionality
2024-07-18 10:20:17 +01:00

161 lines
4.2 KiB
TypeScript

import validator from "validator";
// @ts-ignore
import validateQuery from "components/forms/validators/validate_query";
import { IAddSoftwareFormData, IFormValidation } from "./AddSoftwareForm";
type IAddSoftwareFormValidatorKey = Exclude<
keyof IAddSoftwareFormData,
"installScript"
>;
type IMessageFunc = (formData: IAddSoftwareFormData) => string;
type IValidationMessage = string | IMessageFunc;
interface IValidation {
name: string;
isValid: (
formData: IAddSoftwareFormData,
enabledPreInstallCondition?: boolean,
enabledPostInstallScript?: boolean
) => boolean;
message?: IValidationMessage;
}
/** configuration defines validations for each filed in the form. It defines rules
* to determine if a field is valid, and rules for generating an error message.
*/
const FORM_VALIDATION_CONFIG: Record<
IAddSoftwareFormValidatorKey,
{ validations: IValidation[] }
> = {
software: {
validations: [
{
name: "required",
isValid: (formData) => formData.software !== null,
},
],
},
preInstallCondition: {
validations: [
{
name: "required",
isValid: (
formData: IAddSoftwareFormData,
enabledPreInstallCondition
) => {
if (!enabledPreInstallCondition) {
return true;
}
return (
formData.preInstallCondition !== undefined &&
!validator.isEmpty(formData.preInstallCondition)
);
},
message: (formData) => {
// we dont want an error message until the user has interacted with
// the field. This is why we check for undefined here.
if (formData.preInstallCondition === undefined) {
return "";
}
return "Pre-install condition is required when enabled.";
},
},
{
name: "invalidQuery",
isValid: (formData, enabledPreInstallCondition) => {
if (!enabledPreInstallCondition) {
return true;
}
return (
formData.preInstallCondition !== undefined &&
validateQuery(formData.preInstallCondition).valid
);
},
message: (formData) =>
validateQuery(formData.preInstallCondition).error,
},
],
},
postInstallScript: {
validations: [
{
name: "required",
message: (formData) => {
// we dont want an error message until the user has interacted with
// the field. This is why we check for undefined here.
if (formData.postInstallScript === undefined) {
return "";
}
return "Post-install script is required when enabled.";
},
isValid: (formData, _, enabledPostInstallScript) => {
if (!enabledPostInstallScript) {
return true;
}
return (
formData.postInstallScript !== undefined &&
!validator.isEmpty(formData.postInstallScript)
);
},
},
],
},
selfService: {
// no validations related to self service
validations: [],
},
};
const getErrorMessage = (
formData: IAddSoftwareFormData,
message?: IValidationMessage
) => {
if (message === undefined || typeof message === "string") {
return message;
}
return message(formData);
};
export const generateFormValidation = (
formData: IAddSoftwareFormData,
showingPreInstallCondition: boolean,
showingPostInstallScript: boolean
) => {
const formValidation: IFormValidation = {
isValid: true,
software: {
isValid: false,
},
};
Object.keys(FORM_VALIDATION_CONFIG).forEach((key) => {
const objKey = key as keyof typeof FORM_VALIDATION_CONFIG;
const failedValidation = FORM_VALIDATION_CONFIG[objKey].validations.find(
(validation) =>
!validation.isValid(
formData,
showingPreInstallCondition,
showingPostInstallScript
)
);
if (!failedValidation) {
formValidation[objKey] = {
isValid: true,
};
} else {
formValidation.isValid = false;
formValidation[objKey] = {
isValid: false,
message: getErrorMessage(formData, failedValidation.message),
};
}
});
return formValidation;
};
export default generateFormValidation;