add error toasts

This commit is contained in:
Виталий Лавшонок
2025-12-10 01:33:16 +03:00
parent 02de330034
commit d1a46435c4
17 changed files with 508 additions and 278 deletions

View File

@@ -1,5 +1,6 @@
import { createSlice, createAsyncThunk, PayloadAction } from '@reduxjs/toolkit';
import axios from '../../axios';
import { toastError } from '../../lib/toastNotification';
// ─── Типы ────────────────────────────────────────────
@@ -29,6 +30,9 @@ interface MissionsState {
missions: Mission[];
currentMission: Mission | null;
hasNextPage: boolean;
create: {
errors?: Record<string, string[]>;
};
statuses: {
fetchList: Status;
fetchById: Status;
@@ -45,6 +49,7 @@ const initialState: MissionsState = {
missions: [],
currentMission: null,
hasNextPage: false,
create: {},
statuses: {
fetchList: 'idle',
fetchById: 'idle',
@@ -79,9 +84,7 @@ export const fetchMissions = createAsyncThunk(
});
return response.data; // { missions, hasNextPage }
} catch (err: any) {
return rejectWithValue(
err.response?.data?.message || 'Ошибка при получении миссий',
);
return rejectWithValue(err.response?.data);
}
},
);
@@ -94,9 +97,7 @@ export const fetchMissionById = createAsyncThunk(
const response = await axios.get(`/missions/${id}`);
return response.data; // Mission
} catch (err: any) {
return rejectWithValue(
err.response?.data?.message || 'Ошибка при получении миссии',
);
return rejectWithValue(err.response?.data);
}
},
);
@@ -109,10 +110,7 @@ export const fetchMyMissions = createAsyncThunk(
const response = await axios.get('/missions/my');
return response.data as Mission[]; // массив миссий пользователя
} catch (err: any) {
return rejectWithValue(
err.response?.data?.message ||
'Ошибка при получении моих миссий',
);
return rejectWithValue(err.response?.data);
}
},
);
@@ -141,9 +139,7 @@ export const uploadMission = createAsyncThunk(
});
return response.data; // Mission
} catch (err: any) {
return rejectWithValue(
err.response?.data?.message || 'Ошибка при загрузке миссии',
);
return rejectWithValue(err.response?.data);
}
},
);
@@ -156,9 +152,7 @@ export const deleteMission = createAsyncThunk(
await axios.delete(`/missions/${id}`);
return id; // возвращаем id удалённой миссии
} catch (err: any) {
return rejectWithValue(
err.response?.data?.message || 'Ошибка при удалении миссии',
);
return rejectWithValue(err.response?.data);
}
},
);
@@ -204,7 +198,16 @@ const missionsSlice = createSlice({
fetchMissions.rejected,
(state, action: PayloadAction<any>) => {
state.statuses.fetchList = 'failed';
state.error = action.payload;
const errors = action.payload.errors as Record<
string,
string[]
>;
Object.values(errors).forEach((messages) => {
messages.forEach((msg) => {
toastError(msg);
});
});
},
);
@@ -224,7 +227,16 @@ const missionsSlice = createSlice({
fetchMissionById.rejected,
(state, action: PayloadAction<any>) => {
state.statuses.fetchById = 'failed';
state.error = action.payload;
const errors = action.payload.errors as Record<
string,
string[]
>;
Object.values(errors).forEach((messages) => {
messages.forEach((msg) => {
toastError(msg);
});
});
},
);
@@ -244,7 +256,16 @@ const missionsSlice = createSlice({
fetchMyMissions.rejected,
(state, action: PayloadAction<any>) => {
state.statuses.fetchMy = 'failed';
state.error = action.payload;
const errors = action.payload.errors as Record<
string,
string[]
>;
Object.values(errors).forEach((messages) => {
messages.forEach((msg) => {
toastError(msg);
});
});
},
);
@@ -264,7 +285,18 @@ const missionsSlice = createSlice({
uploadMission.rejected,
(state, action: PayloadAction<any>) => {
state.statuses.upload = 'failed';
state.error = action.payload;
const errors = action.payload.errors as Record<
string,
string[]
>;
Object.values(errors).forEach((messages) => {
messages.forEach((msg) => {
toastError(msg);
});
});
state.create.errors = errors;
},
);
@@ -290,7 +322,16 @@ const missionsSlice = createSlice({
deleteMission.rejected,
(state, action: PayloadAction<any>) => {
state.statuses.delete = 'failed';
state.error = action.payload;
const errors = action.payload.errors as Record<
string,
string[]
>;
Object.values(errors).forEach((messages) => {
messages.forEach((msg) => {
toastError(msg);
});
});
},
);
},