|
@@ -0,0 +1,62 @@
|
|
|
+import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
|
|
|
+import { fetchBodyParts, BodyPart, BodyPartParams } from '../API/bodyPart';
|
|
|
+import { AxiosError } from 'axios';
|
|
|
+
|
|
|
+interface BodyPartState {
|
|
|
+ items: BodyPart[];
|
|
|
+ loading: boolean;
|
|
|
+ error: string | null;
|
|
|
+}
|
|
|
+
|
|
|
+const initialState: BodyPartState = {
|
|
|
+ items: [],
|
|
|
+ loading: false,
|
|
|
+ error: null,
|
|
|
+};
|
|
|
+
|
|
|
+export const getBodyParts = createAsyncThunk(
|
|
|
+ 'bodyPart/getBodyParts',
|
|
|
+ async (
|
|
|
+ { params, token }: { params: BodyPartParams; token: string },
|
|
|
+ { rejectWithValue }
|
|
|
+ ) => {
|
|
|
+ try {
|
|
|
+ const data = await fetchBodyParts(params, token);
|
|
|
+ return data;
|
|
|
+ } catch (err: unknown) {
|
|
|
+ let errorMessage = 'Failed to fetch body parts';
|
|
|
+ if (
|
|
|
+ err &&
|
|
|
+ typeof err === 'object' &&
|
|
|
+ 'message' in err &&
|
|
|
+ typeof (err as AxiosError).message === 'string'
|
|
|
+ ) {
|
|
|
+ errorMessage = (err as AxiosError).message;
|
|
|
+ }
|
|
|
+ return rejectWithValue(errorMessage);
|
|
|
+ }
|
|
|
+ }
|
|
|
+);
|
|
|
+
|
|
|
+const bodyPartSlice = createSlice({
|
|
|
+ name: 'bodyPart',
|
|
|
+ initialState,
|
|
|
+ reducers: {},
|
|
|
+ extraReducers: (builder) => {
|
|
|
+ builder
|
|
|
+ .addCase(getBodyParts.pending, (state) => {
|
|
|
+ state.loading = true;
|
|
|
+ state.error = null;
|
|
|
+ })
|
|
|
+ .addCase(getBodyParts.fulfilled, (state, action) => {
|
|
|
+ state.loading = false;
|
|
|
+ state.items = action.payload;
|
|
|
+ })
|
|
|
+ .addCase(getBodyParts.rejected, (state, action) => {
|
|
|
+ state.loading = false;
|
|
|
+ state.error = action.payload as string;
|
|
|
+ });
|
|
|
+ },
|
|
|
+});
|
|
|
+
|
|
|
+export default bodyPartSlice.reducer;
|