56 lines
1.0 KiB
TypeScript
56 lines
1.0 KiB
TypeScript
|
|
import * as Yup from "yup";
|
|
import { buildFormData } from "../../api/helper/buildFormData";
|
|
|
|
interface formUtilCommon {
|
|
key:string,
|
|
value:string ,
|
|
}
|
|
|
|
interface ObjectToEdit extends formUtilCommon {
|
|
|
|
id?:number,
|
|
value_type:string
|
|
|
|
}
|
|
|
|
export interface InitialValues extends ObjectToEdit {
|
|
value_type:string
|
|
|
|
}
|
|
interface ValidateSchema extends formUtilCommon{
|
|
|
|
}
|
|
|
|
export const getInitialValues = (objectToEdit: ObjectToEdit | null = null): InitialValues => {
|
|
|
|
|
|
return {
|
|
id:objectToEdit?.id?? 0 ,
|
|
key:objectToEdit?.key ?? "",
|
|
value:objectToEdit?.value?? "",
|
|
value_type:objectToEdit?.value_type?? ""
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
export const getValidationSchema = (editMode: boolean = false): Yup.Schema<ValidateSchema> => {
|
|
// validate input
|
|
return Yup.object().shape({
|
|
key:Yup.string().required('required'),
|
|
value:Yup.string().required("required")
|
|
});
|
|
};
|
|
|
|
export const getDataToSend = (values: any): FormData => {
|
|
const data = { ...values };
|
|
|
|
|
|
const formData = new FormData();
|
|
buildFormData(formData, data);
|
|
return formData;
|
|
};
|
|
|