38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
|
import { Action } from "redux";
|
||
|
import { User } from "../../types/user";
|
||
|
import { SET_CURRENT_USER, setCurrentUserAction } from "../actions/auth";
|
||
|
import { FETCH_PROFILE_SUCCESS, fetchProfileSuccessAction, updateProfileSuccessAction, UPDATE_PROFILE_SUCCESS, updateProfileRequestAction } from "../actions/profile";
|
||
|
import { Workgroup } from "../../types/workgroup";
|
||
|
import { FETCH_WORKGROUPS_SUCCESS, fetchWorkgroupsSuccessAction } from "../actions/workgroups";
|
||
|
|
||
|
export interface WorkgroupsState {
|
||
|
workgroupsById: { [id in string]: Workgroup }
|
||
|
}
|
||
|
|
||
|
const defaultState = {
|
||
|
workgroupsById: {}
|
||
|
};
|
||
|
|
||
|
export function workgroupsReducer(state = defaultState, action: Action): WorkgroupsState {
|
||
|
switch (action.type) {
|
||
|
case FETCH_WORKGROUPS_SUCCESS:
|
||
|
return handleFetchWorkgroups(state, action as fetchWorkgroupsSuccessAction);
|
||
|
|
||
|
}
|
||
|
return state;
|
||
|
}
|
||
|
|
||
|
function handleFetchWorkgroups(state: WorkgroupsState, { workgroups }: fetchWorkgroupsSuccessAction): WorkgroupsState {
|
||
|
const workgroupsById = {
|
||
|
...state.workgroupsById,
|
||
|
};
|
||
|
|
||
|
workgroups.forEach(wg => {
|
||
|
workgroupsById[wg.id] = wg;
|
||
|
});
|
||
|
|
||
|
return {
|
||
|
...state,
|
||
|
workgroupsById,
|
||
|
};
|
||
|
};
|