-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseFetchLatestLookup.js
101 lines (90 loc) · 3.77 KB
/
useFetchLatestLookup.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
import { useState, useEffect } from "react";
import { decryptData } from "../auth/Crypto";
import { isValidUUID } from "./utilities";
import axiosInstance from "./api";
import { useDispatch, useSelector } from "react-redux";
import { selectLookups, setLookups } from "../Redux/features/lookups/lookupSlice";
export function useFetchLatestLookup(lookupId, isResultObject = false) {
const dispatch = useDispatch();
const lookups = useSelector(selectLookups);
const [data, setData] = useState([]);
useEffect(() => {
const fetchData = async () => {
try {
const tenantId = decryptData(
sessionStorage.getItem("tenant_id")
);
const paramType = isValidUUID(lookupId)
? "lookup_id"
: "prefix";
const url = `reference-lookup/latest-version/${tenantId}?${paramType}=${encodeURIComponent(
lookupId
)}`;
const { data } = lookups?.[lookupId]
? { data: lookups?.[lookupId] }
: await axiosInstance.get(url);
if (dispatch) {
dispatch(setLookups({ ...lookups, [lookupId]: data }));
}
const reqData = {
lookup_values:
data?.data?.[0]?.properties?.[0]?.lookup_values.map(
(valueObject) =>
isResultObject ? valueObject : valueObject.value
) ?? [],
isDynamic: data?.data?.[0]?.isDynamic,
};
setData(reqData);
} catch (error) {
console.error(error);
}
};
fetchData();
}, [lookupId, isResultObject]);
return data;
}
// export function useFetchLatestLookups(lookupIds, isResultObject = false) {
// const dispatch = useDispatch();
// const allLookup = useSelector(selectLookups);
// const [data, setData] = useState({});
// useEffect(() => {
// const fetchData = async () => {
// try {
// const tenantId = decryptData(
// sessionStorage.getItem("tenant_id")
// );
// const reqData = lookupIds.map(async (lookupId) => {
// const paramType = isValidUUID(lookupId)
// ? "lookup_id"
// : "prefix";
// const url = `reference-lookup/latest-version/${tenantId}?${paramType}=${encodeURIComponent(
// lookupId
// )}`;
// const { data } = allLookup?.[lookupId]
// ? { data: allLookup?.[lookupId] }
// : await axiosInstance.get(url);
// if (dispatch) {
// dispatch(
// setAllLookup({ ...allLookup, [lookupId]: data })
// );
// }
// return {
// lookup_values:
// data?.data?.[0]?.properties?.[0]?.lookup_values.map(
// (valueObject) =>
// isResultObject
// ? valueObject
// : valueObject.value
// ) ?? [],
// isDynamic: data?.data?.[0]?.isDynamic,
// };
// });
// setData(reqData);
// } catch (error) {
// console.error(error);
// }
// };
// fetchData();
// }, [lookupIds, isResultObject]);
// return data;
// }