Skip to content

Commit

Permalink
chore: movesrc/ui/store/reducers/groups/table to typescript [#634]
Browse files Browse the repository at this point in the history
  • Loading branch information
vrozaev committed Nov 14, 2024
1 parent 1e14cd3 commit e54a4ac
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 60 deletions.
12 changes: 5 additions & 7 deletions packages/ui/src/ui/constants/groups.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import createActionTypes, {createPrefix} from '../constants/utils';
import createActionTypes from '../constants/utils';

const PREFIX = createPrefix('GROUPS');

export const GROUPS_TABLE = createActionTypes(PREFIX + 'GROUPS_TABLE');
export const GROUPS_TABLE_DATA_FIELDS = PREFIX + 'GROUPS_TABLE_DATA_FIELDS';
export const GROUPS_TABLE = createActionTypes('GROUPS:GROUPS_TABLE');
export const GROUPS_TABLE_DATA_FIELDS = 'GROUPS:GROUPS_TABLE_DATA_FIELDS';

export const ROOT_GROUP_NAME = '<Root>';

export const GROUP_EDITOR_ACTION = createActionTypes(PREFIX + 'GROUP_EDITOR_ACTION');
export const GROUP_EDITOR_ACTION_DATA_FIELDS = PREFIX + 'GROUP_EDITOR_ACTION_DATA_FIELDS';
export const GROUP_EDITOR_ACTION = createActionTypes('GROUPS:GROUP_EDITOR_ACTION');
export const GROUP_EDITOR_ACTION_DATA_FIELDS = 'GROUPS:GROUP_EDITOR_ACTION_DATA_FIELDS';
53 changes: 0 additions & 53 deletions packages/ui/src/ui/store/reducers/groups/table.js

This file was deleted.

105 changes: 105 additions & 0 deletions packages/ui/src/ui/store/reducers/groups/table.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import {GROUPS_TABLE, GROUPS_TABLE_DATA_FIELDS} from '../../../constants/groups';
import {mergeStateOnClusterChange} from '../../../store/reducers/utils';
import type {Action} from 'redux';
import type {ActionD, YTError} from '../../../types';

type Group = {
name: string;
members: string[];
memberOf: string[];
idm: boolean;
};

type EphemeralStateType = {
loaded: boolean;
loading: boolean;
error: YTError | null;
groups: Group[];
sort: {
column: string;
order: string;
};
expanded: Record<string, boolean>;
};

const ephemeralState: EphemeralStateType = {
loaded: false,
loading: false,
error: null,
groups: [],
sort: {
column: 'name',
order: 'asc',
},
expanded: {},
};

type PersistantStateType = {
nameFilter: string;
};

const persistantState = {
nameFilter: '',
};

type GroupsTableStateType = EphemeralStateType & PersistantStateType;

export const groupsTableState: GroupsTableStateType = {
...ephemeralState,
...persistantState,
};

function reducer(state: GroupsTableStateType = groupsTableState, action: GroupsTableAction) {
switch (action.type) {
case GROUPS_TABLE.REQUEST: {
return {...state, loading: true};
}
case GROUPS_TABLE.SUCCESS: {
return {
...state,
loading: false,
loaded: true,
error: null,
...action.data,
};
}
case GROUPS_TABLE.FAILURE: {
return {...state, loading: false, loaded: false};
}
case GROUPS_TABLE.CANCELLED: {
return {...state, loading: false, loaded: false, error: null};
}
case GROUPS_TABLE_DATA_FIELDS: {
return {...state, ...action.data};
}
default:
return state;
}
}

export type GroupsTableAction =
| Action<typeof GROUPS_TABLE.REQUEST>
| ActionD<
typeof GROUPS_TABLE.SUCCESS,
{
data: {
groups: Group[];
};
}
>
| ActionD<typeof GROUPS_TABLE.FAILURE, {error: YTError}>
| Action<typeof GROUPS_TABLE.CANCELLED>
| ActionD<
typeof GROUPS_TABLE_DATA_FIELDS,
{
data: {
sort?: {
column: string;
order: string;
};
expanded?: Record<string, boolean>;
};
}
>;

export default mergeStateOnClusterChange(ephemeralState, persistantState, reducer);

0 comments on commit e54a4ac

Please sign in to comment.