add error toasts
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { createSlice, createAsyncThunk, PayloadAction } from '@reduxjs/toolkit';
|
||||
import axios from '../../axios';
|
||||
import { toastError } from '../../lib/toastNotification';
|
||||
|
||||
// =====================
|
||||
// Типы
|
||||
@@ -120,9 +121,7 @@ export const fetchArticles = createAsyncThunk(
|
||||
|
||||
return response.data;
|
||||
} catch (err: any) {
|
||||
return rejectWithValue(
|
||||
err.response?.data?.message || 'Ошибка при получении статей',
|
||||
);
|
||||
return rejectWithValue(err.response?.data);
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -135,10 +134,7 @@ export const fetchMyArticles = createAsyncThunk(
|
||||
const response = await axios.get<Article[]>('/articles/my');
|
||||
return response.data;
|
||||
} catch (err: any) {
|
||||
return rejectWithValue(
|
||||
err.response?.data?.message ||
|
||||
'Ошибка при получении моих статей',
|
||||
);
|
||||
return rejectWithValue(err.response?.data);
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -151,9 +147,7 @@ export const fetchArticleById = createAsyncThunk(
|
||||
const response = await axios.get<Article>(`/articles/${articleId}`);
|
||||
return response.data;
|
||||
} catch (err: any) {
|
||||
return rejectWithValue(
|
||||
err.response?.data?.message || 'Ошибка при получении статьи',
|
||||
);
|
||||
return rejectWithValue(err.response?.data);
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -177,9 +171,7 @@ export const createArticle = createAsyncThunk(
|
||||
});
|
||||
return response.data;
|
||||
} catch (err: any) {
|
||||
return rejectWithValue(
|
||||
err.response?.data?.message || 'Ошибка при создании статьи',
|
||||
);
|
||||
return rejectWithValue(err.response?.data);
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -207,9 +199,7 @@ export const updateArticle = createAsyncThunk(
|
||||
);
|
||||
return response.data;
|
||||
} catch (err: any) {
|
||||
return rejectWithValue(
|
||||
err.response?.data?.message || 'Ошибка при обновлении статьи',
|
||||
);
|
||||
return rejectWithValue(err.response?.data);
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -222,9 +212,7 @@ export const deleteArticle = createAsyncThunk(
|
||||
await axios.delete(`/articles/${articleId}`);
|
||||
return articleId;
|
||||
} catch (err: any) {
|
||||
return rejectWithValue(
|
||||
err.response?.data?.message || 'Ошибка при удалении статьи',
|
||||
);
|
||||
return rejectWithValue(err.response?.data);
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -263,7 +251,12 @@ const articlesSlice = createSlice({
|
||||
);
|
||||
builder.addCase(fetchArticles.rejected, (state, action: any) => {
|
||||
state.fetchArticles.status = 'failed';
|
||||
state.fetchArticles.error = action.payload;
|
||||
const errors = action.payload.errors as Record<string, string[]>;
|
||||
Object.values(errors).forEach((messages) => {
|
||||
messages.forEach((msg) => {
|
||||
toastError(msg);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// fetchMyArticles
|
||||
@@ -280,7 +273,12 @@ const articlesSlice = createSlice({
|
||||
);
|
||||
builder.addCase(fetchMyArticles.rejected, (state, action: any) => {
|
||||
state.fetchMyArticles.status = 'failed';
|
||||
state.fetchMyArticles.error = action.payload;
|
||||
const errors = action.payload.errors as Record<string, string[]>;
|
||||
Object.values(errors).forEach((messages) => {
|
||||
messages.forEach((msg) => {
|
||||
toastError(msg);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// fetchArticleById
|
||||
@@ -297,7 +295,12 @@ const articlesSlice = createSlice({
|
||||
);
|
||||
builder.addCase(fetchArticleById.rejected, (state, action: any) => {
|
||||
state.fetchArticleById.status = 'failed';
|
||||
state.fetchArticleById.error = action.payload;
|
||||
const errors = action.payload.errors as Record<string, string[]>;
|
||||
Object.values(errors).forEach((messages) => {
|
||||
messages.forEach((msg) => {
|
||||
toastError(msg);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// createArticle
|
||||
@@ -314,7 +317,14 @@ const articlesSlice = createSlice({
|
||||
);
|
||||
builder.addCase(createArticle.rejected, (state, action: any) => {
|
||||
state.createArticle.status = 'failed';
|
||||
state.createArticle.error = action.payload;
|
||||
state.createArticle.error = action.payload.title;
|
||||
|
||||
const errors = action.payload.errors as Record<string, string[]>;
|
||||
Object.values(errors).forEach((messages) => {
|
||||
messages.forEach((msg) => {
|
||||
toastError(msg);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// updateArticle
|
||||
@@ -331,7 +341,14 @@ const articlesSlice = createSlice({
|
||||
);
|
||||
builder.addCase(updateArticle.rejected, (state, action: any) => {
|
||||
state.updateArticle.status = 'failed';
|
||||
state.updateArticle.error = action.payload;
|
||||
state.createArticle.error = action.payload.title;
|
||||
|
||||
const errors = action.payload.errors as Record<string, string[]>;
|
||||
Object.values(errors).forEach((messages) => {
|
||||
messages.forEach((msg) => {
|
||||
toastError(msg);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// deleteArticle
|
||||
@@ -355,7 +372,12 @@ const articlesSlice = createSlice({
|
||||
);
|
||||
builder.addCase(deleteArticle.rejected, (state, action: any) => {
|
||||
state.deleteArticle.status = 'failed';
|
||||
state.deleteArticle.error = action.payload;
|
||||
const errors = action.payload.errors as Record<string, string[]>;
|
||||
Object.values(errors).forEach((messages) => {
|
||||
messages.forEach((msg) => {
|
||||
toastError(msg);
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user