Skip to content

Commit

Permalink
Merge pull request #70 from City-of-Helsinki/filter-ongoing-plot-sear…
Browse files Browse the repository at this point in the history
…ches

Only show plot searches that are currently ongoing
  • Loading branch information
EmiliaMakelaVincit authored Sep 25, 2023
2 parents 502a60b + b014e07 commit 7099253
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 10 deletions.
24 changes: 22 additions & 2 deletions src/mapSearch/mapSearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import BlockLoader from '../loader/blockLoader';
import {
fetchPlotSearchAttributes,
fetchPlotSearches,
fetchPlotSearchStages,
fetchPlotSearchTypes,
} from '../plotSearch/actions';

import { ApiAttributes } from '../api/types';
import {
PlotSearch,
PlotSearchStage,
PlotSearchSubtype,
PlotSearchTarget,
PlotSearchType,
Expand All @@ -30,9 +32,11 @@ interface State {
isFetchingPlotSearches: boolean;
isFetchingPlotSearchAttributes: boolean;
isFetchingPlotSearchTypes: boolean;
isFetchingPlotSearchStages: boolean;
plotSearches: Array<PlotSearch>;
plotSearchAttributes: ApiAttributes;
plotSearchTypes: Array<PlotSearchType>;
plotSearchStages: Array<PlotSearchStage>;
favourite: Favourite;
}

Expand All @@ -41,12 +45,15 @@ interface Props {
fetchPlotSearches: (payload?: { params: Record<string, string> }) => void;
fetchPlotSearchAttributes: () => void;
fetchPlotSearchTypes: () => void;
fetchPlotSearchStages: () => void;
isFetchingPlotSearches: boolean;
isFetchingPlotSearchAttributes: boolean;
isFetchingPlotSearchTypes: boolean;
isFetchingPlotSearchStages: boolean;
plotSearches: Array<PlotSearch>;
plotSearchAttributes: ApiAttributes;
plotSearchTypes: Array<PlotSearchType>;
plotSearchStages: Array<PlotSearchStage>;
favourite: Favourite;
}

Expand All @@ -69,10 +76,13 @@ const MapSearchPage = (props: Props): JSX.Element => {
fetchPlotSearches,
fetchPlotSearchAttributes,
fetchPlotSearchTypes,
fetchPlotSearchStages,
isFetchingPlotSearches,
isFetchingPlotSearchAttributes,
isFetchingPlotSearchTypes,
isFetchingPlotSearchStages,
plotSearchTypes,
plotSearchStages,
plotSearches,
favourite,
} = props;
Expand Down Expand Up @@ -127,11 +137,17 @@ const MapSearchPage = (props: Props): JSX.Element => {
}, [id, plotSearches]);

useEffect(() => {
fetchPlotSearches({ params: { search_class: searchClass } });
fetchPlotSearchAttributes();
fetchPlotSearchTypes();
fetchPlotSearchStages();
}, []);

useEffect(() => {
if (!isFetchingPlotSearchStages && plotSearchStages.length > 0) {
fetchPlotSearches({ params: { search_class: searchClass } });
}
}, [isFetchingPlotSearchStages]);

useEffect(() => {
let newOptions = [...(plotSearchTypes || [])];

Expand Down Expand Up @@ -186,7 +202,8 @@ const MapSearchPage = (props: Props): JSX.Element => {
if (
isFetchingPlotSearches ||
isFetchingPlotSearchAttributes ||
isFetchingPlotSearchTypes
isFetchingPlotSearchTypes ||
isFetchingPlotSearchStages
) {
return <BlockLoader />;
}
Expand Down Expand Up @@ -235,11 +252,14 @@ const mapStateToProps = (state: RootState): State => ({
isFetchingPlotSearchAttributes:
state.plotSearch.isFetchingPlotSearchAttributes,
isFetchingPlotSearchTypes: state.plotSearch.isFetchingPlotSearchTypes,
isFetchingPlotSearchStages: state.plotSearch.isFetchingPlotSearchStages,
plotSearchStages: state.plotSearch.plotSearchStages,
favourite: state.favourite.favourite,
});

export default connect(mapStateToProps, {
fetchPlotSearches,
fetchPlotSearchAttributes,
fetchPlotSearchTypes,
fetchPlotSearchStages,
})(MapSearchPage);
14 changes: 14 additions & 0 deletions src/plotSearch/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ import { Action } from 'redux';

import {
FETCH_PLOT_SEARCH_ATTRIBUTES,
FETCH_PLOT_SEARCH_STAGES,
FETCH_PLOT_SEARCH_TYPES,
FETCH_PLOT_SEARCHES,
PLOT_SEARCH_ATTRIBUTES_NOT_FOUND,
PLOT_SEARCH_STAGES_NOT_FOUND,
PLOT_SEARCH_TYPES_NOT_FOUND,
PLOT_SEARCHES_NOT_FOUND,
PlotSearch,
PlotSearchStage,
PlotSearchType,
RECEIVE_PLOT_SEARCH_ATTRIBUTES,
RECEIVE_PLOT_SEARCH_STAGES,
RECEIVE_PLOT_SEARCH_TYPES,
RECEIVE_PLOT_SEARCHES,
} from './types';
Expand Down Expand Up @@ -46,3 +50,13 @@ export const receivePlotSearchTypes = (

export const plotSearchTypesNotFound = (): Action<string> =>
createAction(PLOT_SEARCH_TYPES_NOT_FOUND)();

export const fetchPlotSearchStages = (): Action<string> =>
createAction(FETCH_PLOT_SEARCH_STAGES)();

export const receivePlotSearchStages = (
payload: Array<PlotSearchStage>
): Action<string> => createAction(RECEIVE_PLOT_SEARCH_STAGES)(payload);

export const plotSearchStagesNotFound = (): Action<string> =>
createAction(PLOT_SEARCH_STAGES_NOT_FOUND)();
18 changes: 18 additions & 0 deletions src/plotSearch/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { createSlice } from '@reduxjs/toolkit';
import {
FETCH_PLOT_SEARCH_ATTRIBUTES,
FETCH_PLOT_SEARCH_STAGES,
FETCH_PLOT_SEARCH_TYPES,
FETCH_PLOT_SEARCHES,
PLOT_SEARCHES_NOT_FOUND,
PlotSearch,
PlotSearchStage,
PlotSearchType,
RECEIVE_PLOT_SEARCH_ATTRIBUTES,
RECEIVE_PLOT_SEARCH_STAGES,
RECEIVE_PLOT_SEARCH_TYPES,
RECEIVE_PLOT_SEARCHES,
ReceivePlotSearchAttributesAction,
ReceivePlotSearchesAction,
ReceivePlotSearchStagesAction,
ReceivePlotSearchTypesAction,
} from './types';
import { ApiAttributes } from '../api/types';
Expand All @@ -19,18 +23,22 @@ type CurrentDisplayState = {
plotSearches: Array<PlotSearch>;
plotSearchAttributes: ApiAttributes;
plotSearchTypes: Array<PlotSearchType>;
plotSearchStages: Array<PlotSearchStage>;
isFetchingPlotSearches: boolean;
isFetchingPlotSearchAttributes: boolean;
isFetchingPlotSearchTypes: boolean;
isFetchingPlotSearchStages: boolean;
};

const initialState: CurrentDisplayState = {
plotSearches: [],
plotSearchAttributes: {},
plotSearchTypes: [],
plotSearchStages: [],
isFetchingPlotSearches: false,
isFetchingPlotSearchAttributes: false,
isFetchingPlotSearchTypes: false,
isFetchingPlotSearchStages: false,
};

const plotSearchSlice = createSlice({
Expand Down Expand Up @@ -71,6 +79,16 @@ const plotSearchSlice = createSlice({
[FETCH_PLOT_SEARCH_TYPES]: (state) => {
state.isFetchingPlotSearchTypes = true;
},
[RECEIVE_PLOT_SEARCH_STAGES]: (
state,
{ payload }: ReceivePlotSearchStagesAction
) => {
state.plotSearchStages = payload;
state.isFetchingPlotSearchStages = false;
},
[FETCH_PLOT_SEARCH_STAGES]: (state) => {
state.isFetchingPlotSearchStages = true;
},
},
});

Expand Down
8 changes: 8 additions & 0 deletions src/plotSearch/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ export const fetchPlotSearchTypesRequest = (): Generator<
> => {
return callApi(new Request(createUrl('plot_search_type/')));
};

export const fetchPlotSearchStagesRequest = (): Generator<
Effect,
ApiCallResult,
Response
> => {
return callApi(new Request(createUrl('plot_search_stage/')));
};
58 changes: 53 additions & 5 deletions src/plotSearch/saga.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { all, call, Effect, fork, put, takeLatest } from 'redux-saga/effects';
import {
all,
call,
Effect,
fork,
put,
select,
takeLatest,
} from 'redux-saga/effects';
import {
fetchPlotSearchAttributesRequest,
fetchPlotSearchesRequest,
fetchPlotSearchStagesRequest,
fetchPlotSearchTypesRequest,
} from './requests';
import {
Expand All @@ -11,28 +20,45 @@ import {
FETCH_PLOT_SEARCH_TYPES,
PlotSearchType,
PlotSearchFromBackend,
PlotSearchStage,
FETCH_PLOT_SEARCH_STAGES,
} from './types';
import {
plotSearchAttributesNotFound,
plotSearchesNotFound,
plotSearchStagesNotFound,
plotSearchTypesNotFound,
receivePlotSearchAttributes,
receivePlotSearches,
receivePlotSearchStages,
receivePlotSearchTypes,
} from './actions';
import { parsePlotSearches } from './helpers';
import { ApiCallResult } from '../api/callApi';
import { ApiAttributes } from '../api/types';
import { logError } from '../root/helpers';
import { RootState } from '../root/rootReducer';

function* fetchPlotSearchesSaga({
payload,
}: FetchPlotSearchesAction): Generator<Effect, void, ApiCallResult> {
}: FetchPlotSearchesAction): Generator<Effect, void, never> {
try {
const { response, bodyAsJson } = yield call(
fetchPlotSearchesRequest,
payload?.params
const stages: Array<PlotSearchStage> = yield select(
(state: RootState) => state.plotSearch.plotSearchStages
);
const ongoingId = stages.find((stage) => stage.stage === 'in_action')?.id;

if (!ongoingId) {
logError('Could not find ongoing stage ID');
yield put(plotSearchesNotFound());
return;
}

const result: ApiCallResult = yield call(fetchPlotSearchesRequest, {
stage: '' + ongoingId,
...(payload?.params || {}),
});
const { response, bodyAsJson } = result;

switch (response.status) {
case 200:
Expand Down Expand Up @@ -102,6 +128,27 @@ function* fetchPlotSearchTypesSaga(): Generator<Effect, void, ApiCallResult> {
}
}

function* fetchPlotSearchStagesSaga(): Generator<Effect, void, ApiCallResult> {
try {
const { response, bodyAsJson } = yield call(fetchPlotSearchStagesRequest);

switch (response.status) {
case 200:
yield put(
receivePlotSearchStages(bodyAsJson?.results as Array<PlotSearchStage>)
);
break;
default:
yield put(plotSearchStagesNotFound());
break;
}
} catch (e) {
logError(e);
yield put(plotSearchStagesNotFound());
throw e;
}
}

export default function* plotSearchSaga(): Generator {
yield all([
fork(function* (): Generator {
Expand All @@ -111,6 +158,7 @@ export default function* plotSearchSaga(): Generator {
fetchPlotSearchAttributesSaga
);
yield takeLatest(FETCH_PLOT_SEARCH_TYPES, fetchPlotSearchTypesSaga);
yield takeLatest(FETCH_PLOT_SEARCH_STAGES, fetchPlotSearchStagesSaga);
}),
]);
}
30 changes: 27 additions & 3 deletions src/plotSearch/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ export interface PlotSearchTypesNotFoundAction {
type: typeof PLOT_SEARCH_TYPES_NOT_FOUND;
}

export const FETCH_PLOT_SEARCH_STAGES = 'plotSearch/FETCH_PLOT_SEARCH_STAGES';
export interface FetchPlotSearchStagesAction {
type: typeof FETCH_PLOT_SEARCH_STAGES;
}

export const RECEIVE_PLOT_SEARCH_STAGES =
'plotSearch/RECEIVE_PLOT_SEARCH_STAGES';
export interface ReceivePlotSearchStagesAction {
type: typeof RECEIVE_PLOT_SEARCH_STAGES;
payload: Array<PlotSearchStage>;
}

export const PLOT_SEARCH_STAGES_NOT_FOUND =
'plotSearch/PLOT_SEARCH_STAGES_NOT_FOUND';
export interface PlotSearchStagesNotFoundAction {
type: typeof PLOT_SEARCH_STAGES_NOT_FOUND;
}

export type PlotSearchType = {
id: number;
name: string;
Expand All @@ -70,6 +88,12 @@ export type PlotSearchSubtype = {
show_district: boolean;
};

export type PlotSearchStage = {
id: number;
name: string;
stage: string;
};

export type PlotSearchTypeReference = {
id: number;
name: string;
Expand All @@ -81,7 +105,7 @@ export type PlotSearchSubtypeReference = {
plot_search_type: number;
};

export type PlotSearchStage = {
export type PlotSearchStageReference = {
id: number;
name: string;
};
Expand Down Expand Up @@ -268,7 +292,7 @@ export type PlotSearchFromBackend = {
id: number;
type: PlotSearchTypeReference;
subtype: PlotSearchSubtypeReference;
stage: PlotSearchStage;
stage: PlotSearchStageReference;
search_class: string;
form: Form;
decisions: Array<Decision>;
Expand All @@ -285,7 +309,7 @@ export type PlotSearch = {
id: number;
type: PlotSearchTypeReference;
subtype: PlotSearchSubtypeReference;
stage: PlotSearchStage;
stage: PlotSearchStageReference;
search_class: string;
form: Form;
decisions: Array<Decision>;
Expand Down

0 comments on commit 7099253

Please sign in to comment.