|
| 1 | +import { createSlice, createAsyncThunk } from "@reduxjs/toolkit" |
| 2 | + |
| 3 | +import { |
| 4 | + searchDeployments as _searchDeployments, |
| 5 | +} from "../apis" |
| 6 | +import { Deployment, } from "../models" |
| 7 | + |
| 8 | +export const perPage = 30 |
| 9 | + |
| 10 | +interface ActivitiesState { |
| 11 | + loading: boolean |
| 12 | + deployments: Deployment[] |
| 13 | + page: number |
| 14 | +} |
| 15 | + |
| 16 | +const initialState: ActivitiesState = { |
| 17 | + loading: false, |
| 18 | + deployments: [], |
| 19 | + page: 1, |
| 20 | +} |
| 21 | + |
| 22 | +export const searchDeployments = createAsyncThunk< |
| 23 | + Deployment[], { |
| 24 | + start?: Date, |
| 25 | + end?: Date, |
| 26 | + productionOnly: boolean, |
| 27 | + }, { |
| 28 | + state: { activities: ActivitiesState } |
| 29 | +}>( |
| 30 | + "activities/searchDeployments", |
| 31 | + async ({start, end, productionOnly}, { getState, rejectWithValue }) => { |
| 32 | + const {page} = getState().activities |
| 33 | + try { |
| 34 | + return await _searchDeployments([], false, productionOnly, start, end, page, perPage) |
| 35 | + } catch (e) { |
| 36 | + return rejectWithValue(e) |
| 37 | + } |
| 38 | + } |
| 39 | +) |
| 40 | + |
| 41 | +export const activitiesSlice = createSlice({ |
| 42 | + name: "activities", |
| 43 | + initialState, |
| 44 | + reducers: { |
| 45 | + increasePage: (state) => { |
| 46 | + state.page = state.page + 1 |
| 47 | + }, |
| 48 | + decreasePage: (state) => { |
| 49 | + state.page = state.page - 1 |
| 50 | + }, |
| 51 | + }, |
| 52 | + extraReducers: builder => { |
| 53 | + builder |
| 54 | + .addCase(searchDeployments.pending, (state) => { |
| 55 | + state.loading = true |
| 56 | + state.deployments = [] |
| 57 | + }) |
| 58 | + .addCase(searchDeployments.fulfilled, (state, action) => { |
| 59 | + state.loading = false |
| 60 | + state.deployments = action.payload |
| 61 | + }) |
| 62 | + .addCase(searchDeployments.rejected, (state) => { |
| 63 | + state.loading = false |
| 64 | + }) |
| 65 | + } |
| 66 | +}) |
0 commit comments