Skip to content
Snippets Groups Projects
Commit 4d971ccb authored by Piotr Gawron's avatar Piotr Gawron
Browse files

reducer for comments

parent 2e1e8ce1
No related branches found
No related tags found
2 merge requests!223reset the pin numbers before search results are fetch (so the results will be...,!200Resolve "[MIN-113] Show comments on the map"
......@@ -94,4 +94,5 @@ export const apiPath = {
getSubmapConnections: (): string => `projects/${PROJECT_ID}/submapConnections/`,
logout: (): string => `doLogout`,
userPrivileges: (login: string): string => `users/${login}?columns=privileges`,
getComments: (): string => `projects/${PROJECT_ID}/comments/models/*/`,
};
import { FetchDataState } from '@/types/fetchDataState';
import { CommentsState } from '@/redux/comment/comment.types';
export const COMMENT_SUBMAP_CONNECTIONS_INITIAL_STATE: FetchDataState<Comment[]> = {
data: [],
loading: 'idle',
error: { name: '', message: '' },
};
export const COMMENT_INITIAL_STATE: CommentsState = {
data: [],
loading: 'idle',
error: { name: '', message: '' },
};
import { DEFAULT_ERROR } from '@/constants/errors';
import { CommentsState } from '@/redux/comment/comment.types';
export const COMMENT_INITIAL_STATE_MOCK: CommentsState = {
data: [],
loading: 'idle',
error: DEFAULT_ERROR,
};
import { ActionReducerMapBuilder } from '@reduxjs/toolkit';
import { CommentsState } from '@/redux/comment/comment.types';
import { getComments } from '@/redux/comment/thunks/getComments';
export const getCommentsReducer = (builder: ActionReducerMapBuilder<CommentsState>): void => {
builder.addCase(getComments.pending, state => {
state.loading = 'pending';
});
builder.addCase(getComments.fulfilled, (state, action) => {
state.loading = 'succeeded';
state.data = action.payload;
});
builder.addCase(getComments.rejected, state => {
state.loading = 'failed';
});
};
import { createSlice } from '@reduxjs/toolkit';
import { COMMENT_INITIAL_STATE } from '@/redux/comment/comment.constants';
import { getCommentsReducer } from '@/redux/comment/comment.reducers';
export const commentsSlice = createSlice({
name: 'comments',
initialState: COMMENT_INITIAL_STATE,
reducers: {},
extraReducers: builder => {
getCommentsReducer(builder);
},
});
export default commentsSlice.reducer;
import { getComments } from './thunks/getComments';
export { getComments };
import { FetchDataState } from '@/types/fetchDataState';
import { Comment } from '@/types/models';
export type CommentsState = FetchDataState<Comment[], []>;
import { bioEntityResponseSchema } from '@/models/bioEntityResponseSchema';
import { apiPath } from '@/redux/apiPath';
import { axiosInstanceNewAPI } from '@/services/api/utils/axiosInstance';
import { ThunkConfig } from '@/types/store';
import { validateDataUsingZodSchema } from '@/utils/validateDataUsingZodSchema';
import { createAsyncThunk } from '@reduxjs/toolkit';
import { Comment } from '@/types/models';
export const getComments = createAsyncThunk<Comment[], object, ThunkConfig>(
'project/getComments',
async () => {
try {
const response = await axiosInstanceNewAPI.get<Comment[]>(apiPath.getComments());
const isDataValid = validateDataUsingZodSchema(response.data, bioEntityResponseSchema);
return isDataValid ? response.data : [];
} catch (error) {
return Promise.reject(error);
}
},
);
import { CONSTANT_INITIAL_STATE } from '@/redux/constant/constant.adapter';
import { COMMENT_INITIAL_STATE_MOCK } from '@/redux/comment/comment.mock';
import { BACKGROUND_INITIAL_STATE_MOCK } from '../backgrounds/background.mock';
import { BIOENTITY_INITIAL_STATE_MOCK } from '../bioEntity/bioEntity.mock';
import { CHEMICALS_INITIAL_STATE_MOCK } from '../chemicals/chemicals.mock';
......@@ -53,4 +54,5 @@ export const INITIAL_STORE_STATE_MOCK: RootState = {
plugins: PLUGINS_INITIAL_STATE_MOCK,
markers: MARKERS_INITIAL_STATE_MOCK,
entityNumber: ENTITY_NUMBER_INITIAL_STATE_MOCK,
comment: COMMENT_INITIAL_STATE_MOCK,
};
......@@ -23,6 +23,7 @@ import {
TypedStartListening,
configureStore,
} from '@reduxjs/toolkit';
import commentReducer from '@/redux/comment/comment.slice';
import compartmentPathwaysReducer from './compartmentPathways/compartmentPathways.slice';
import entityNumberReducer from './entityNumber/entityNumber.slice';
import exportReducer from './export/export.slice';
......@@ -40,6 +41,7 @@ export const reducers = {
drugs: drugsReducer,
chemicals: chemicalsReducer,
bioEntity: bioEntityReducer,
comment: commentReducer,
drawer: drawerReducer,
modal: modalReducer,
map: mapReducer,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment