Skip to content

Commit

Permalink
Added support for UI Autogenerate the formatted id if schema has ui s…
Browse files Browse the repository at this point in the history
…chema option (#10)
  • Loading branch information
jagankumar-egov authored Oct 12, 2023
1 parent fa24719 commit 003f78f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,9 @@ const DigitJSONForm = ({
useEffect(() => {
onFormChange({ formData: Digit.Utils.workbench.postProcessData(formData, inputUiSchema) });
}, []);
const onSubmitV2 = ({ formData }) => {
onSubmit(Digit.Utils.workbench.preProcessData(formData, inputUiSchema));
const onSubmitV2 = async({ formData }) => {
const updatedData=await Digit.Utils.workbench.preProcessData(formData, inputUiSchema);
onSubmit(updatedData);
};

const customWidgets = { SelectWidget: CustomDropdown, CheckboxWidget: CustomCheckbox };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ const MDMSAdd = ({ defaultFormData, updatesToUISchema, screenType = "add", onVie
const [formSchema, setFormSchema] = useState({});
const [uiSchema, setUiSchema] = useState({});
const [api, setAPI] = useState(false);
const [autogenerate, setAutoGenerate] = useState(false);

const [noSchema, setNoSchema] = useState(false);
const [loadDependent, setLoadDependent] = useState([]);
const [showErrorToast, setShowErrorToast] = useState(false);
const [disableForm, setDisableForm] = useState(false);

Expand Down Expand Up @@ -59,46 +57,11 @@ const MDMSAdd = ({ defaultFormData, updatesToUISchema, screenType = "add", onVie
if (data?.SchemaDefinitions?.[0]?.definition?.["x-ui-schema"]?.["ui-apidetails"]) {
setAPI(data?.SchemaDefinitions?.[0]?.definition?.["x-ui-schema"]?.["ui-apidetails"]);
}
if (data?.SchemaDefinitions?.[0]?.definition?.["x-ui-schema"]?.["ui-autogenerate"]) {
const autogenerate = data?.SchemaDefinitions?.[0]?.definition?.["x-ui-schema"]?.["ui-autogenerate"];
setAutoGenerate(Object.keys(autogenerate).map((key) => ({ id: autogenerate[key], path: key })));
}
return data?.SchemaDefinitions?.[0] || {};
},
},
changeQueryName: "schema",
};
const { isLoading1, data: generatedNo, error } = Digit.Hooks.useCustomAPIHook({
url: "/egov-idgen/id/_generate",
body: {
idRequests: [
{
tenantId: tenantId,
idName: autogenerate?.[0]?.id,
},
],
},
config: {
enabled: autogenerate?.[0]?.id && screenType == "add" && true,
select: (data) => {
if (data?.idResponses?.[0]?.id) {
setAutoGenerate([{ ...autogenerate?.[0], value: data?.idResponses?.[0]?.id }]);
session[autogenerate?.[0]?.path] = data?.idResponses?.[0]?.id;
setSession({ ...session });
uiSchema[autogenerate?.[0]?.path] = {
...uiSchema[autogenerate?.[0]?.path],
"ui:readonly": true,
};
setUiSchema({ ...uiSchema });
setDisableForm(true);
setTimeout(() => {
setDisableForm(false);
});
}
return data?.idResponses?.[0]?.id || "";
},
},
});

const { isLoading, data: schema, isFetching } = Digit.Hooks.useCustomAPIHook(reqCriteria);
const body = api?.requestBody
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ const getConfig = (type = "text") => {
};
};

const getMDMSLabel = (code = "",masterName="",moduleName="",ignore=[]) => {
let ignoreThis = ignore?.some(url => url===code)
if(ignoreThis) {
return null
const getMDMSLabel = (code = "", masterName = "", moduleName = "", ignore = []) => {
let ignoreThis = ignore?.some((url) => url === code);
if (ignoreThis) {
return null;
}
//enable this flag to get the localisation enabled for the mdms forms
let flag = true;
Expand All @@ -134,7 +134,7 @@ const getMDMSLabel = (code = "",masterName="",moduleName="",ignore=[]) => {
.reduce((acc, curr) => acc + curr.charAt(0).toUpperCase() + curr.slice(1) + " ", "")
.trim();
}
if(masterName && moduleName){
if (masterName && moduleName) {
return Digit.Utils.locale.getTransformedLocale(`SCHEMA_${moduleName}_${masterName}`);
}
return Digit.Utils.locale.getTransformedLocale(code);
Expand Down Expand Up @@ -188,54 +188,83 @@ const updateTitleToLocalisationCodeForObject = (definition, schemaCode) => {
});
return definition;
};
const formatDates= (value,type)=>{
if(type!="EPOC" && (!value || Number.isNaN(value))){
value= new Date();
const formatDates = (value, type) => {
if (type != "EPOC" && (!value || Number.isNaN(value))) {
value = new Date();
}
switch(type){
switch (type) {
case "date":
return new Date(value)?.toISOString?.()?.split?.("T")?.[0];
case "datetime":
return new Date(value).toISOString();
case "EPOC":
return String(new Date(value)?.getTime());
}
}
};

const generateId = (format,tenant)=>{
return null;
}
const generateId = async (format, tenantId = Digit.ULBService.getCurrentTenantId()) => {
const requestCriteria = {
url: "/egov-idgen/id/_generate",
body: {
idRequests: [
{
tenantId: tenantId,
idName: format,
},
],
},
};
const response = await Digit.CustomService.getResponse({ url: requestCriteria?.url, body: requestCriteria?.body });
return response?.idResponses?.[0]?.id;
};

const formatData = (value,type,schema)=>{
switch(type){
const formatData = (value, type, schema) => {
switch (type) {
case "EPOC":
return formatDates(value,type);
return formatDates(value, type);
case "REVERT-EPOC":
return formatDates(typeof value=="string"&& value?.endsWith?.("Z")?value:parseInt(value),schema?.["ui:widget"]);
return formatDates(typeof value == "string" && value?.endsWith?.("Z") ? value : parseInt(value), schema?.["ui:widget"]);
case "REVERT-EPOC":
return new Date(typeof value=="string"&& value?.endsWith?.("Z")?value:parseInt(value)).toISOString();
return new Date(typeof value == "string" && value?.endsWith?.("Z") ? value : parseInt(value)).toISOString();
default:
return value;
}
}
};
/* preprocess the data before sending the data to mdms create api */
const preProcessData = (data={},schema={})=>{
Object.keys(schema).map((key)=>{
if(typeof schema[key]=="object" && schema[key]?.["format"] && schema[key]?.["format"]?.includes?.("preprocess") && schema?.[key]?.formatType){
data[key]= formatData(data?.[key],schema?.[key]?.formatType,schema?.[key]);
const preProcessData = async (data = {}, schema = {}) => {
let fieldKey = "";
let autoGenerateFormat = "";

Object.keys(schema).map((key) => {
if (typeof schema[key] == "object" && schema[key]?.["format"] && schema[key]?.["format"]?.includes?.("preprocess") && schema?.[key]?.formatType) {
/* this autogenerate format logic can be removed once we have the mdms v2 support to geenrate formatted id */
if (schema?.[key]?.formatType == "autogenerate" && schema?.[key]?.autogenerate) {
autoGenerateFormat = schema?.[key]?.autogenerate;
fieldKey = key;
} else {
data[key] = formatData(data?.[key], schema?.[key]?.formatType, schema?.[key]);
}
}
})
return {...data};
}
});
if (fieldKey != "" && autoGenerateFormat != "") {
data[fieldKey] = await generateId(autoGenerateFormat);
}
return { ...data };
};

/* postprocess the data received from mdms search api to the form */
const postProcessData = (data={},schema={})=>{
Object.keys(schema).map((key)=>{
if(typeof schema[key]=="object" && schema[key]?.["format"] && schema[key]?.["format"]?.includes?.("postprocess") && schema?.[key]?.formatType){
data[key]= formatData(data?.[key],`REVERT-${schema?.[key]?.formatType}`,schema?.[key]);
const postProcessData = (data = {}, schema = {}) => {
Object.keys(schema).map((key) => {
if (
typeof schema[key] == "object" &&
schema[key]?.["format"] &&
schema[key]?.["format"]?.includes?.("postprocess") &&
schema?.[key]?.formatType
) {
data[key] = formatData(data?.[key], `REVERT-${schema?.[key]?.formatType}`, schema?.[key]);
}
})
return {...data};
}
});
return { ...data };
};

export default { getConfig, getMDMSLabel, getFormattedData, getUpdatedPath, updateTitleToLocalisationCodeForObject ,preProcessData,postProcessData};
export default { getConfig, getMDMSLabel, getFormattedData, getUpdatedPath, updateTitleToLocalisationCodeForObject, preProcessData, postProcessData };

0 comments on commit 003f78f

Please sign in to comment.