-
Notifications
You must be signed in to change notification settings - Fork 143
/
environments.ts
77 lines (68 loc) · 2.16 KB
/
environments.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import {
createAsyncThunk,
createEntityAdapter,
createSlice,
Dictionary,
EntityId,
} from "@reduxjs/toolkit";
import { Environment } from "pipe/pkg/app/web/model/environment_pb";
import type { AppState } from "../store";
import * as envsApi from "../api/environments";
const MODULE_NAME = "environments";
export const environmentsAdapter = createEntityAdapter<Environment.AsObject>(
{}
);
const {
selectById,
selectAll,
selectEntities,
selectIds,
} = environmentsAdapter.getSelectors();
export const selectAllEnvs = (state: AppState): Environment.AsObject[] =>
selectAll(state.environments);
export const selectEnvIds = (state: AppState): EntityId[] =>
selectIds(state.environments);
export const selectEnvById = (id: EntityId | undefined) => (
state: AppState
): Environment.AsObject | undefined =>
id ? selectById(state.environments, id) : undefined;
export const selectEnvEntities = (
state: AppState
): Dictionary<Environment.AsObject> => selectEntities(state.environments);
export const fetchEnvironments = createAsyncThunk<Environment.AsObject[], void>(
"environments/fetchList",
async () => {
const { environmentsList } = await envsApi.getEnvironments();
return environmentsList;
}
);
export const addEnvironment = createAsyncThunk<
void,
{ name: string; desc: string }
>(`${MODULE_NAME}/add`, async (props) => {
await envsApi.addEnvironment(props);
});
export const deleteEnvironment = createAsyncThunk<
void,
{ environmentId: string }
>(`${MODULE_NAME}/delete`, async (props) => {
await envsApi.deleteEnvironment(props);
});
export const environmentsSlice = createSlice({
name: MODULE_NAME,
initialState: environmentsAdapter.getInitialState(),
reducers: {},
extraReducers: (builder) => {
builder
.addCase(fetchEnvironments.fulfilled, (state, action) => {
environmentsAdapter.addMany(
state,
action.payload.filter((env) => env.deleted === false)
);
})
.addCase(deleteEnvironment.fulfilled, (state, action) => {
environmentsAdapter.removeOne(state, action.meta.arg.environmentId);
});
},
});
export { Environment } from "pipe/pkg/app/web/model/environment_pb";