Skip to content

Commit

Permalink
[#124] Fix cascade display name shows code for saved form
Browse files Browse the repository at this point in the history
  • Loading branch information
ifirmawan committed Aug 8, 2023
1 parent bb69b87 commit 845e12c
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions app/src/form/fields/TypeCascade.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React, { useState, useEffect, useMemo } from 'react';
import React, { useState, useEffect, useMemo, useCallback } from 'react';
import { View, Text } from 'react-native';
import { Dropdown } from 'react-native-element-dropdown';
import { FieldLabel } from '../support';
import { styles } from '../styles';
import { FormState, UIState } from '../../store';
import { i18n } from '../../lib';
import { i18n, cascades } from '../../lib';

const TypeCascade = ({
onChange,
Expand Down Expand Up @@ -60,8 +60,8 @@ const TypeCascade = ({
onChange(id, finalValues);
if (finalValues) {
const { options: selectedOptions, value: selectedValue } = dropdownValues.pop();
const findSelected = selectedOptions?.find((o) => o.id === selectedValue) || [];
const cascadeName = findSelected?.name || null;
const findSelected = selectedOptions?.find((o) => o.id === selectedValue);
const cascadeName = findSelected?.name;
FormState.update((s) => {
s.cascades = { ...s.cascades, [id]: cascadeName };
});
Expand All @@ -87,6 +87,27 @@ const TypeCascade = ({
});
}, [dataSource, source, values, id]);

const fetchCascade = useCallback(async () => {
if (source && values?.[id]?.length) {
const cascadeID = values[id].slice(-1)[0];
const { rows } = await cascades.loadDataSource(source, cascadeID);
const { length: rowLength, _array: rowItems } = rows;
const csValue = rowLength ? rowItems[0] : null;
if (csValue) {
FormState.update((s) => {
s.cascades = {
...s.cascades,
[id]: csValue.name,
};
});
}
}
}, [source, values, id]);

useEffect(() => {
fetchCascade();
}, [fetchCascade]);

useEffect(() => {
if (dropdownItems.length === 0 && initialDropdowns.length) {
setDropdownItems(initialDropdowns);
Expand Down

0 comments on commit 845e12c

Please sign in to comment.