Commit dbd37c47 authored by Aron Fiechter's avatar Aron Fiechter
Browse files

Handle case with no results in querySlice

Set noResultModalOpen to true, do not clear entity or anything else.
parent a8dc7970
......@@ -11,6 +11,7 @@ export const querySlice = createSlice({
result: stubData,
error: null,
loading: false,
noResultModalOpen: false,
},
reducers: {
// Redux Toolkit allows us to write "mutating" logic in reducers. It
......@@ -28,10 +29,22 @@ export const querySlice = createSlice({
setLoading: (state, action) => {
state.loading = action.payload;
},
openNoResultModal: (state) => {
state.noResultModalOpen = true;
},
closeNoResultModal: (state) => {
state.noResultModalOpen = false;
},
},
});
export const { getQueryResultSuccess, getQueryResultFailure, setLoading } = querySlice.actions;
export const {
getQueryResultSuccess,
getQueryResultFailure,
setLoading,
openNoResultModal,
closeNoResultModal,
} = querySlice.actions;
// The function below is called a thunk and allows us to perform async logic. It
// can be dispatched like a regular action: `dispatch(performQuery(<query>))`. This
......@@ -39,22 +52,27 @@ export const { getQueryResultSuccess, getQueryResultFailure, setLoading } = quer
// code can then be executed and other actions can be dispatched
export const performQuery = (query) => async (dispatch) => {
dispatch(setLoading(true));
try {
const data = await sendQuery(query);
dispatch(getQueryResultSuccess(data));
dispatch(clearEntity());
dispatch(resetRange());
} catch (err) {
dispatch(getQueryResultFailure(err.toString()));
} finally {
dispatch(setLoading(false));
}
return sendQuery(query)
.then((data) => {
if (data.nResults === 0) {
return dispatch(openNoResultModal());
}
dispatch(clearEntity());
dispatch(resetRange());
return dispatch(getQueryResultSuccess(data));
})
.catch((err) => {
return dispatch(getQueryResultFailure(err.toString()));
})
.finally(() => dispatch(setLoading(false)));
};
export const selectQuery = (state) => state.query.result.query;
export const selectResult = (state) => state.query.result;
export const selectError = (state) => state.query.error;
export const selectResultsById = (state) => state.query.result?.mapById;
export const selectLoading = (state) => state.query.loading;
export const selectNodes = (state) => state.query.result?.nodes;
export const selectNoResultModalOpen = (state) => state.query.noResultModalOpen;
export default querySlice.reducer;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment