Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • It fetches data from AddressNode table on basis of address hierarchy setup, and returns data for current and higher nodes to populate.

  • It also handle the logic to either disbale the higher level nodes based on whether their value is fetched and allow_override_hierarchy is false.

  • It also update (auto populate) the value of higher nodes.

  • Drop down only on those fields which are in hierarchy

Expand
titlesaveAddressUsingAddressHierarchy code snippet called on onChange
Code Block
const saveAddressUsingAddressHierarchy = async (nodeValue: string, nodeType: string) => {
        if (!isKeyOfNewAddress(nodeType) || !nodeValue) return;
        setLoading(true);

        const fetchLocality = await fetchAddressUsingAddressHierarchy({ queryString: nodeValue, searchType: nodeType });

        if (!fetchLocality.isSuccess) {
            setNewAddress({
                ...newAddress,
                city: '',
                state: '',
                country: '',
            });
            setDisableLocality(false);
        } else {
            let nodeFound = false;
            const parentLevelNodes = [];
            for (let i = 0; i < hierarchyData?.length; i += 1) {
                const curNode = hierarchyData[i].location_key.toLowerCase();
                if ((nodeFound || curNode === nodeType.toLocaleLowerCase()) && curNode in fetchLocality?.data) {
                    nodeFound = true;
                    parentLevelNodes.push({ key: hierarchyData[i].location_key, value: fetchLocality?.data[curNode] });
                }
            }
            const fetchedAddressFieldsObj = parentLevelNodes?.reduce((acc, curr) => {
                if (isKeyOfNewAddress(curr.key)) {
                    acc[curr.key] = curr.value || null;
                    if (curr.key !== nodeType && (!isAddressMappingAllowed && curr.value)) {
                        setDisableLocality(true, curr.key);
                    } else {
                        setDisableLocality(false, curr.key);
                    }
                }
                return acc;
            }, {} as { [key: string]: string | null });
            setNewAddress({
                ...newAddress,
                ...fetchedAddressFieldsObj,
            });
        }

        setLoading(false);
    };

...