Skip to content

Commit

Permalink
fix: incorrect selector usage in redux
Browse files Browse the repository at this point in the history
  • Loading branch information
markphelps committed Nov 25, 2023
1 parent ce0609f commit 51dc09a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
4 changes: 2 additions & 2 deletions ui/src/app/flags/Flag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default function Flag() {
const readOnly = useSelector(selectReadonly);

const flag = useSelector((state: RootState) =>
selectFlag(state, namespace.key, flagKey || '')
selectFlag(state, flagKey || '')
);

const [showDeleteFlagModal, setShowDeleteFlagModal] = useState(false);
Expand All @@ -71,7 +71,7 @@ export default function Flag() {
.catch((err) => {
setError(err);
});
}, [flagKey, namespace.key, clearError, setError]);
}, [flagKey, namespace.key, clearError, setError, dispatch]);

if (!flag || flag.key != flagKey) return <Loading />;

Expand Down
17 changes: 8 additions & 9 deletions ui/src/app/flags/flagsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,17 @@ const setFlag = (
state.flags[namespaceKey][flag.key] = flag;
};

export const selectFlag = createSelector(
[
(state: RootState, namespaceKey: string, key: string) => {
const flags = state.flags.flags[namespaceKey];
if (flags) {
return flags[key] || ({} as IFlag);
}
const selectNamespaceFlags = (state: RootState) =>
state.flags.flags[state.namespaces.currentNamespace];

export const selectFlag = createSelector(
[selectNamespaceFlags, (state: RootState, key: string) => key],
(flags, key) => {
if (flags === undefined) {
return {} as IFlag;
}
],
(flag) => flag
return flags[key];
}
);

interface FlagIdentifier {
Expand Down
26 changes: 10 additions & 16 deletions ui/src/app/namespaces/namespacesSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,19 @@ export const namespacesSlice = createSlice({
export const { currentNamespaceChanged } = namespacesSlice.actions;

export const selectNamespaces = createSelector(
[
(state: RootState) =>
Object.entries(state.namespaces.namespaces).map(
([_, value]) => value
) as INamespace[]
],
(namespaces) => namespaces
[(state: RootState) => state.namespaces.namespaces],
(namespaces) => {
return Object.entries(namespaces).map(
([_, value]) => value
) as INamespace[];
}
);

export const selectCurrentNamespace = createSelector(
[
(state: RootState) => {
if (state.namespaces.namespaces[state.namespaces.currentNamespace]) {
return state.namespaces.namespaces[state.namespaces.currentNamespace];
}
return { key: 'default', name: 'Default', description: '' } as INamespace;
}
],
(currentNamespace) => currentNamespace
[(state: RootState) => state.namespaces],
(namespaces) =>
namespaces.namespaces[namespaces.currentNamespace] ||
({ key: 'default', name: 'Default', description: '' } as INamespace)
);

export const fetchNamespacesAsync = createAsyncThunk(
Expand Down

0 comments on commit 51dc09a

Please sign in to comment.