From 4d971ccba7396a412f5444ba7be8c3ae0ef1fdda Mon Sep 17 00:00:00 2001 From: Piotr Gawron <p.gawron@atcomp.pl> Date: Fri, 24 May 2024 15:17:58 +0200 Subject: [PATCH] reducer for comments --- src/redux/apiPath.ts | 1 + src/redux/comment/comment.constants.ts | 14 ++++++++++++++ src/redux/comment/comment.mock.ts | 8 ++++++++ src/redux/comment/comment.reducers.ts | 17 +++++++++++++++++ src/redux/comment/comment.slice.ts | 14 ++++++++++++++ src/redux/comment/comment.thunks.ts | 3 +++ src/redux/comment/comment.types.ts | 4 ++++ src/redux/comment/thunks/getComments.ts | 22 ++++++++++++++++++++++ src/redux/root/root.fixtures.ts | 2 ++ src/redux/store.ts | 2 ++ 10 files changed, 87 insertions(+) create mode 100644 src/redux/comment/comment.constants.ts create mode 100644 src/redux/comment/comment.mock.ts create mode 100644 src/redux/comment/comment.reducers.ts create mode 100644 src/redux/comment/comment.slice.ts create mode 100644 src/redux/comment/comment.thunks.ts create mode 100644 src/redux/comment/comment.types.ts create mode 100644 src/redux/comment/thunks/getComments.ts diff --git a/src/redux/apiPath.ts b/src/redux/apiPath.ts index c3e5ef33..77d44039 100644 --- a/src/redux/apiPath.ts +++ b/src/redux/apiPath.ts @@ -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/*/`, }; diff --git a/src/redux/comment/comment.constants.ts b/src/redux/comment/comment.constants.ts new file mode 100644 index 00000000..389a12dc --- /dev/null +++ b/src/redux/comment/comment.constants.ts @@ -0,0 +1,14 @@ +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: '' }, +}; diff --git a/src/redux/comment/comment.mock.ts b/src/redux/comment/comment.mock.ts new file mode 100644 index 00000000..121df19f --- /dev/null +++ b/src/redux/comment/comment.mock.ts @@ -0,0 +1,8 @@ +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, +}; diff --git a/src/redux/comment/comment.reducers.ts b/src/redux/comment/comment.reducers.ts new file mode 100644 index 00000000..e9d5f098 --- /dev/null +++ b/src/redux/comment/comment.reducers.ts @@ -0,0 +1,17 @@ +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'; + }); +}; diff --git a/src/redux/comment/comment.slice.ts b/src/redux/comment/comment.slice.ts new file mode 100644 index 00000000..9524be05 --- /dev/null +++ b/src/redux/comment/comment.slice.ts @@ -0,0 +1,14 @@ +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; diff --git a/src/redux/comment/comment.thunks.ts b/src/redux/comment/comment.thunks.ts new file mode 100644 index 00000000..b8ee25a4 --- /dev/null +++ b/src/redux/comment/comment.thunks.ts @@ -0,0 +1,3 @@ +import { getComments } from './thunks/getComments'; + +export { getComments }; diff --git a/src/redux/comment/comment.types.ts b/src/redux/comment/comment.types.ts new file mode 100644 index 00000000..08975985 --- /dev/null +++ b/src/redux/comment/comment.types.ts @@ -0,0 +1,4 @@ +import { FetchDataState } from '@/types/fetchDataState'; +import { Comment } from '@/types/models'; + +export type CommentsState = FetchDataState<Comment[], []>; diff --git a/src/redux/comment/thunks/getComments.ts b/src/redux/comment/thunks/getComments.ts new file mode 100644 index 00000000..6d13305e --- /dev/null +++ b/src/redux/comment/thunks/getComments.ts @@ -0,0 +1,22 @@ +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); + } + }, +); diff --git a/src/redux/root/root.fixtures.ts b/src/redux/root/root.fixtures.ts index feaaaedf..9c5e6df4 100644 --- a/src/redux/root/root.fixtures.ts +++ b/src/redux/root/root.fixtures.ts @@ -1,4 +1,5 @@ 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, }; diff --git a/src/redux/store.ts b/src/redux/store.ts index 891c7cfe..3e957717 100644 --- a/src/redux/store.ts +++ b/src/redux/store.ts @@ -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, -- GitLab