Skip to content

Commit

Permalink
fix(StateDropdown): default state is always set
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisVorop committed Dec 25, 2023
1 parent 36bcda8 commit 8510668
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/components/StateDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,15 @@ export const StateDropdown = React.forwardRef<HTMLDivElement, StateDropdownProps
enabled: !!flowId,
});

const { data: defaultFlow = [] } = trpc.flow.recommedations.useQuery(undefined, {
const { data: defaultFlow } = trpc.flow.recommedations.useQuery(undefined, {
enabled: defaultFlowEnabled,
refetchOnMount: 'always',
select([flow]) {
return flow;
},
});

const flow = defaultFlowEnabled ? defaultFlow[0] : flowById;

useEffect(() => {
const defaultState = flow?.states?.filter((s) => s?.default)[0];
if (!value && defaultState) {
setState(defaultState);
onChange?.(defaultState);
}
}, [value, onChange, flow]);
const flow = defaultFlowEnabled ? defaultFlow : flowById;

const onStateChange = useCallback(
(s: Partial<State>) => {
Expand All @@ -51,6 +47,12 @@ export const StateDropdown = React.forwardRef<HTMLDivElement, StateDropdownProps
[onChange],
);

useEffect(() => {
if (value) return;
const defaultState = flow?.states.find((state) => state.default === true);
defaultState && onStateChange(defaultState);
}, [flow, onStateChange, value]);

return (
<Dropdown
ref={ref}
Expand Down

0 comments on commit 8510668

Please sign in to comment.