Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 420e52e

Browse files
author
Noah Lee
authored
Fix to move the state into the redux (#437)
1 parent ca1fe37 commit 420e52e

File tree

2 files changed

+52
-53
lines changed

2 files changed

+52
-53
lines changed

ui/src/redux/activities.tsx

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
1+
import { createSlice, createAsyncThunk, PayloadAction } from '@reduxjs/toolkit';
22

33
import { searchDeployments as _searchDeployments } from '../apis';
44
import { Deployment } from '../models';
@@ -7,50 +7,59 @@ export const perPage = 30;
77

88
interface ActivitiesState {
99
loading: boolean;
10+
startDate?: Date;
11+
endDate?: Date;
12+
productionOnly: boolean;
1013
deployments: Deployment[];
1114
page: number;
1215
}
1316

1417
const initialState: ActivitiesState = {
1518
loading: false,
19+
productionOnly: false,
1620
deployments: [],
1721
page: 1,
1822
};
1923

2024
export const searchDeployments = createAsyncThunk<
2125
Deployment[],
22-
{
23-
start?: Date;
24-
end?: Date;
25-
productionOnly: boolean;
26-
},
26+
void,
2727
{
2828
state: { activities: ActivitiesState };
2929
}
30-
>(
31-
'activities/searchDeployments',
32-
async ({ start, end, productionOnly }, { getState, rejectWithValue }) => {
33-
const { page } = getState().activities;
34-
try {
35-
return await _searchDeployments(
36-
[],
37-
false,
38-
productionOnly,
39-
start,
40-
end,
41-
page,
42-
perPage
43-
);
44-
} catch (e) {
45-
return rejectWithValue(e);
46-
}
30+
>('activities/searchDeployments', async (_, { getState, rejectWithValue }) => {
31+
const { startDate, endDate, productionOnly, page } = getState().activities;
32+
try {
33+
return await _searchDeployments(
34+
[],
35+
false,
36+
productionOnly,
37+
startDate,
38+
endDate,
39+
page,
40+
perPage
41+
);
42+
} catch (e) {
43+
return rejectWithValue(e);
4744
}
48-
);
45+
});
4946

5047
export const activitiesSlice = createSlice({
5148
name: 'activities',
5249
initialState,
5350
reducers: {
51+
setSearchOptions: (
52+
state: ActivitiesState,
53+
action: PayloadAction<{
54+
startDate: Date;
55+
endDate: Date;
56+
productionOnly: boolean;
57+
}>
58+
) => {
59+
state.startDate = action.payload.startDate;
60+
state.endDate = action.payload.endDate;
61+
state.productionOnly = action.payload.productionOnly;
62+
},
5463
increasePage: (state) => {
5564
state.page = state.page + 1;
5665
},

ui/src/views/activities/index.tsx

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState } from 'react';
1+
import { useEffect } from 'react';
22
import { shallowEqual } from 'react-redux';
33
import { Helmet } from 'react-helmet';
44

@@ -29,47 +29,37 @@ export default (): JSX.Element => {
2929
const dispatch = useAppDispatch();
3030

3131
useEffect(() => {
32-
dispatch(
33-
searchDeployments({
34-
productionOnly: false,
35-
})
36-
);
32+
dispatch(searchDeployments());
3733

3834
// eslint-disable-next-line
3935
}, [dispatch]);
4036

41-
const [searchOptions, setSearchOptions] = useState<SearchActivitiesValues>(
42-
{}
43-
);
44-
45-
const search = () =>
46-
dispatch(
47-
searchDeployments({
48-
start: searchOptions.period
49-
? searchOptions.period[0].toDate()
50-
: undefined,
51-
end: searchOptions.period
52-
? searchOptions.period[1].toDate()
53-
: undefined,
54-
productionOnly: searchOptions.productionOnly
55-
? searchOptions.productionOnly
56-
: false,
57-
})
58-
);
37+
const onClickSearch = ({
38+
period,
39+
productionOnly,
40+
}: SearchActivitiesValues) => {
41+
if (period) {
42+
console.debug('Set search options.', period, productionOnly);
43+
dispatch(
44+
actions.setSearchOptions({
45+
startDate: period[0].toDate(),
46+
endDate: period[1].toDate(),
47+
productionOnly: productionOnly ? true : false,
48+
})
49+
);
50+
}
5951

60-
const onClickSearch = (values: SearchActivitiesValues) => {
61-
setSearchOptions(values);
62-
search();
52+
dispatch(searchDeployments());
6353
};
6454

6555
const onClickPrev = () => {
6656
dispatch(actions.decreasePage());
67-
search();
57+
dispatch(searchDeployments());
6858
};
6959

7060
const onClickNext = () => {
7161
dispatch(actions.increasePage());
72-
search();
62+
dispatch(searchDeployments());
7363
};
7464

7565
return (

0 commit comments

Comments
 (0)