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

Fix to move the state into the redux #437

Merged
merged 1 commit into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 33 additions & 24 deletions ui/src/redux/activities.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import { createSlice, createAsyncThunk, PayloadAction } from '@reduxjs/toolkit';

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

interface ActivitiesState {
loading: boolean;
startDate?: Date;
endDate?: Date;
productionOnly: boolean;
deployments: Deployment[];
page: number;
}

const initialState: ActivitiesState = {
loading: false,
productionOnly: false,
deployments: [],
page: 1,
};

export const searchDeployments = createAsyncThunk<
Deployment[],
{
start?: Date;
end?: Date;
productionOnly: boolean;
},
void,
{
state: { activities: ActivitiesState };
}
>(
'activities/searchDeployments',
async ({ start, end, productionOnly }, { getState, rejectWithValue }) => {
const { page } = getState().activities;
try {
return await _searchDeployments(
[],
false,
productionOnly,
start,
end,
page,
perPage
);
} catch (e) {
return rejectWithValue(e);
}
>('activities/searchDeployments', async (_, { getState, rejectWithValue }) => {
const { startDate, endDate, productionOnly, page } = getState().activities;
try {
return await _searchDeployments(
[],
false,
productionOnly,
startDate,
endDate,
page,
perPage
);
} catch (e) {
return rejectWithValue(e);
}
);
});

export const activitiesSlice = createSlice({
name: 'activities',
initialState,
reducers: {
setSearchOptions: (
state: ActivitiesState,
action: PayloadAction<{
startDate: Date;
endDate: Date;
productionOnly: boolean;
}>
) => {
state.startDate = action.payload.startDate;
state.endDate = action.payload.endDate;
state.productionOnly = action.payload.productionOnly;
},
increasePage: (state) => {
state.page = state.page + 1;
},
Expand Down
48 changes: 19 additions & 29 deletions ui/src/views/activities/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react';
import { useEffect } from 'react';
import { shallowEqual } from 'react-redux';
import { Helmet } from 'react-helmet';

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

useEffect(() => {
dispatch(
searchDeployments({
productionOnly: false,
})
);
dispatch(searchDeployments());

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

const [searchOptions, setSearchOptions] = useState<SearchActivitiesValues>(
{}
);

const search = () =>
dispatch(
searchDeployments({
start: searchOptions.period
? searchOptions.period[0].toDate()
: undefined,
end: searchOptions.period
? searchOptions.period[1].toDate()
: undefined,
productionOnly: searchOptions.productionOnly
? searchOptions.productionOnly
: false,
})
);
const onClickSearch = ({
period,
productionOnly,
}: SearchActivitiesValues) => {
if (period) {
console.debug('Set search options.', period, productionOnly);
dispatch(
actions.setSearchOptions({
startDate: period[0].toDate(),
endDate: period[1].toDate(),
productionOnly: productionOnly ? true : false,
})
);
}

const onClickSearch = (values: SearchActivitiesValues) => {
setSearchOptions(values);
search();
dispatch(searchDeployments());
};

const onClickPrev = () => {
dispatch(actions.decreasePage());
search();
dispatch(searchDeployments());
};

const onClickNext = () => {
dispatch(actions.increasePage());
search();
dispatch(searchDeployments());
};

return (
Expand Down