diff --git a/src/redux/apiPath.ts b/src/redux/apiPath.ts index c3e5ef33628aaac8ff40627aad19462ad87bffcf..77d44039623eda3bb260adbc443ec0a7c28e07da 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 0000000000000000000000000000000000000000..389a12dc6f87d5eee517d7e0a623c28cb1f59363 --- /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 0000000000000000000000000000000000000000..121df19f3c5dfcde599a6213c2f4c49ca756a949 --- /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 0000000000000000000000000000000000000000..e9d5f098382c871c8654e721da794bc8fac19992 --- /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 0000000000000000000000000000000000000000..9524be0572d605432d4760674d58643319d9a31e --- /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 0000000000000000000000000000000000000000..b8ee25a46581d5fb4701d81fc0c98c0a4df74e41 --- /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 0000000000000000000000000000000000000000..08975985f278f1611c09977133f1481641675d1a --- /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 0000000000000000000000000000000000000000..6d13305e71dde323288ea883e5b63445dc0fff9f --- /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 feaaaedffe924b2b295a0e3d2d9590e85e137df0..9c5e6df4433c43af3513df7ed427b22681695bc5 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 891c7cfec500361e496e7d2d53b534f4a79f1bfe..3e957717d60ad91e097b46beca519bbc8f3dab33 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,