grafana/public/app/features/org/state/reducers.ts
Uchechukwu Obasi d03aef1951
SelectOrgPage: migrate API function calls to Redux (#43133)
* SelectOrgPage: migrate API function calls to Redux

* used a much better approach

* writes test for getUserOrganizations action

* writes test for userOrganizationsLoaded reducer

* change userOrg to plural
2021-12-16 09:57:18 +01:00

33 lines
1.1 KiB
TypeScript

import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { Organization, OrganizationState, UserOrg } from 'app/types';
export const initialState: OrganizationState = {
organization: {} as Organization,
userOrgs: [] as UserOrg[],
};
const organizationSlice = createSlice({
name: 'organization',
initialState,
reducers: {
organizationLoaded: (state, action: PayloadAction<Organization>): OrganizationState => {
return { ...state, organization: action.payload };
},
setOrganizationName: (state, action: PayloadAction<string>): OrganizationState => {
return { ...state, organization: { ...state.organization, name: action.payload } };
},
userOrganizationsLoaded: (state, action: PayloadAction<UserOrg[]>): OrganizationState => {
return { ...state, userOrgs: action.payload };
},
},
});
export const { setOrganizationName, organizationLoaded, userOrganizationsLoaded } = organizationSlice.actions;
export const organizationReducer = organizationSlice.reducer;
export default {
organization: organizationReducer,
};