/
index.js
218 lines (206 loc) · 9.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { Loader } from "@egovernments/digit-ui-react-components";
import React, { useEffect } from "react";
import { useTranslation } from "react-i18next";
import { useQueryClient } from "react-query";
import { Redirect, Route, Switch, useHistory, useLocation, useParams, useRouteMatch } from "react-router-dom";
import { newConfig } from "../../../config/config";
import { getCommencementDataFormat } from "../../../utils/index";
const getPath = (path, params) => {
params && Object.keys(params).map(key => {
path = path.replace(`:${key}`, params[key]);
})
return path;
}
const getTradeEditDetails = (data) => {
const gettradeaccessories = (tradeacceserioies) => {
let acc = [];
tradeacceserioies && tradeacceserioies.map((ob) => {
acc.push({
accessory: { code: `${ob.accessoryCategory}`, i18nKey: `TRADELICENSE_ACCESSORIESCATEGORY_${ob.accessoryCategory.replaceAll("-", "_")}` },
accessorycount: ob.count,
unit: `${ob.uom}`,
uom: `${ob.uomValue}`,
id: ob.id,
})
})
return acc;
}
const gettradeunits = (tradeunits) => {
let units = [];
tradeunits && tradeunits.map((ob) => {
units.push({
tradecategory: { i18nKey: `TRADELICENSE_TRADETYPE_${ob.tradeType.split(".")[0]}`, code: `${ob.tradeType.split(".")[0]}` },
tradesubtype: { i18nKey: `TL_${ob.tradeType}`, code: `${ob.tradeType}` },
tradetype: { i18nKey: `TRADELICENSE_TRADETYPE_${ob.tradeType.split(".")[1]}`, code: `${ob.tradeType.split(".")[1]}` },
unit: ob.uom,
uom: ob.uomValue,
id : ob.id,
});
})
return units;
};
const gettradedocuments = (docs) => {
let documents = [];
docs && docs.map((ob) => {
if (ob.documentType.includes("OWNERPHOTO")) {
documents["OwnerPhotoProof"] = ob;
}
else if (ob.documentType.includes("OWNERIDPROOF")) {
documents["ProofOfIdentity"] = ob;
}
else if (ob.documentType.includes("OWNERSHIPPROOF")) {
documents["ProofOfOwnership"] = ob;
}
})
return documents;
}
const gettradeowners = (owner) => {
let ownerarray = [];
owner && owner.map((ob) => {
ownerarray.push({
gender: { code: `${ob.gender}`, name: `${!ob?.gender.includes("FEMALE") ? "Male" : "Female"}`, value: `${!ob?.gender.includes("FEMALE") ? "Male" : "Female"}`, i18nKey:`TL_GENDER_${ob.gender}` },
isprimaryowner: false,
name: ob.name,
mobilenumber: ob.mobileNumber,
permanentAddress: ob.permanentAddress,
id: ob.id,
})
})
// ownerarray["permanentAddress"]=owner.permanentAddress;
return ownerarray;
}
data.TradeDetails = {
BuildingType: { code: `${data?.tradeLicenseDetail?.structureType}`, i18nKey: `COMMON_MASTERS_STRUCTURETYPE_${data.tradeLicenseDetail?.structureType.replaceAll(".", "_")}` },
CommencementDate: getCommencementDataFormat(data?.commencementDate),
StructureType: { code: `${data.tradeLicenseDetail?.structureType.split(".")[0]}`, i18nKey: `${data.tradeLicenseDetail?.structureType.includes("IMMOVABLE") ? "TL_COMMON_YES" : "TL_COMMON_NO"}` },
TradeName: data?.tradeName,
accessories: gettradeaccessories(data?.tradeLicenseDetail?.accessories),
isAccessories: gettradeaccessories(data?.tradeLicenseDetail?.accessories).length > 0 ? { code: `ACCESSORY`, i18nKey: "TL_COMMON_YES" } : { code: `NONACCESSORY`, i18nKey: "TL_COMMON_NO" },
units: gettradeunits(data?.tradeLicenseDetail?.tradeUnits),
}
data.address = {};
if (data?.tradeLicenseDetail?.address?.geoLocation?.latitude && data?.tradeLicenseDetail?.address?.geoLocation?.longitude) {
data.address.geoLocation = {
latitude: data?.tradeLicenseDetail?.address?.geoLocation?.latitude,
longitude: data?.tradeLicenseDetail?.address?.geoLocation?.longitude,
};
} else {
data.address.geoLocation = {};
}
data.address.doorNo = data?.tradeLicenseDetail?.address?.doorNo;
data.address.street = data?.tradeLicenseDetail?.address?.street;
data.address.landmark = data?.tradeLicenseDetail?.address?.landmark;
data.address.pincode = data?.tradeLicenseDetail?.address?.pincode;
data.address.city = { code: data?.tradeLicenseDetail?.address?.tenantId };
data.address.locality = data?.tradeLicenseDetail?.address?.locality;
data.address.locality.i18nkey = data?.tenantId.replace(".", "_").toUpperCase() + "_" + "REVENUE" + "_" + data?.address?.locality?.code;
data.address.locality.doorNo = data?.tradeLicenseDetail?.address?.doorNo;
data.address.locality.landmark = data?.tradeLicenseDetail?.address?.landmark;
data.owners = {
documents: gettradedocuments(data?.tradeLicenseDetail?.applicationDocuments),
owners: gettradeowners(data?.tradeLicenseDetail?.owners),
permanentAddress: data?.tradeLicenseDetail?.owners[0].permanentAddress,
isCorrespondenceAddress: false,
}
data.ownershipCategory = { code: `${data?.tradeLicenseDetail?.subOwnerShipCategory}`, i18nKey: `PT_OWNERSHIP_${data?.tradeLicenseDetail?.subOwnerShipCategory.split(".")[1]}`, value: `${data?.tradeLicenseDetail?.subOwnerShipCategory}` };
return data;
}
const EditTrade = ({ parentRoute }) => {
const queryClient = useQueryClient();
const match = useRouteMatch();
const { t } = useTranslation();
const { id: licenseNo, tenantId } = useParams();
const { pathname } = useLocation();
const history = useHistory();
let config = [];
let application = {};
const [params, setParams, clearParams] = Digit.Hooks.useSessionStorage("TL_EDIT_TRADE", {});
let filter1 = {};
if (licenseNo) filter1.applicationNumber = licenseNo;
if (tenantId) filter1.tenantId = tenantId;
const { isLoading, isError, error, data } = Digit.Hooks.tl.useTradeLicenseSearch({ filters: filter1 }, { filters: filter1 });
const editProperty = window.location.href.includes("edit");
const tlTrade = JSON.parse(sessionStorage.getItem("tl-trade")) || {};
useEffect(() => {
application = data?.Licenses && data.Licenses[0] && data.Licenses[0];
if (data && application) {
application = data?.Licenses[0];
if (editProperty) {
application.isEditProperty = true;
}
sessionStorage.setItem("tradeInitialObject", JSON.stringify({ ...application }));
let tradeEditDetails = getTradeEditDetails(application);
setParams({ ...params, ...tradeEditDetails });
}
}, [data]);
const goNext = (skipStep, index, isAddMultiple, key) => {
let currentPath = pathname.split("/").pop(),
lastchar = currentPath.charAt(currentPath.length - 1),
isMultiple = false,
nextPage;
let { nextStep = {} } = config.find((routeObj) => routeObj.route === currentPath);
if (typeof nextStep == "object" && nextStep != null) {
if (nextStep[sessionStorage.getItem("isAccessories")]) {
nextStep = `${nextStep[sessionStorage.getItem("isAccessories")]}`;
} else if (nextStep[sessionStorage.getItem("StructureType")]) {
nextStep = `${nextStep[sessionStorage.getItem("StructureType")]}`;
}
}
let redirectWithHistory = history.push;
if (skipStep) {
redirectWithHistory = history.replace;
}
if (isAddMultiple) {
nextStep = key;
}
if (nextStep === null) {
return redirectWithHistory(`${getPath(match.path, match.params)}/check`);
}
nextPage = `${getPath(match.path, match.params)}/${nextStep}`;
redirectWithHistory(nextPage);
};
const createProperty = async () => {
history.push(`${getPath(match.path, match.params)}/acknowledgement`);
};
function handleSelect(key, data, skipStep, index, isAddMultiple = false) {
setParams({ ...params, ...{ [key]: { ...params[key], ...data } } });
goNext(skipStep, index, isAddMultiple, key);
}
const handleSkip = () => { };
const handleMultiple = () => { };
const onSuccess = () => {
queryClient.invalidateQueries("TL_EDIT_TRADE");
};
newConfig.forEach((obj) => {
config = config.concat(obj.body.filter((a) => !a.hideInCitizen));
});
config.indexRoute = "check";
if (isLoading) {
return <Loader />;
}
const CheckPage = Digit?.ComponentRegistryService?.getComponent('TLCheckPage') ;
const TLAcknowledgement = Digit?.ComponentRegistryService?.getComponent('TLAcknowledgement');
return (
<Switch>
{config.map((routeObj, index) => {
const { component, texts, inputs, key } = routeObj;
const Component = typeof component === "string" ? Digit.ComponentRegistryService.getComponent(component) : component;
return (
<Route path={`${getPath(match.path, match.params)}/${routeObj.route}`} key={index}>
<Component config={{ texts, inputs, key }} onSelect={handleSelect} onSkip={handleSkip} t={t} formData={params} onAdd={handleMultiple} />
</Route>
);
})}
<Route path={`${getPath(match.path, match.params)}/check`}>
<CheckPage onSubmit={createProperty} value={params} />
</Route>
<Route path={`${getPath(match.path, match.params)}/acknowledgement`}>
<TLAcknowledgement data={params} onSuccess={onSuccess} />
</Route>
<Route>
<Redirect to={`${getPath(match.path, match.params)}/${config.indexRoute}`} />
</Route>
</Switch>
);
};
export default EditTrade;