Изменена модалка добавления миссии
All checks were successful
Build and Push Docker Image / build (push) Successful in 53s
All checks were successful
Build and Push Docker Image / build (push) Successful in 53s
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
import { createSlice, createAsyncThunk, PayloadAction } from '@reduxjs/toolkit';
|
||||
import axios from '../../axios';
|
||||
import { toastError } from '../../lib/toastNotification';
|
||||
import {
|
||||
buildUploadMissionFormData,
|
||||
getProblemXmlMissingNameMessage,
|
||||
UploadMissionRequest,
|
||||
} from '../../api/missionsUpload';
|
||||
|
||||
// ─── Типы ────────────────────────────────────────────
|
||||
|
||||
@@ -148,27 +153,45 @@ export const fetchMyMissions = createAsyncThunk(
|
||||
export const uploadMission = createAsyncThunk(
|
||||
'missions/uploadMission',
|
||||
async (
|
||||
{
|
||||
file,
|
||||
name,
|
||||
difficulty,
|
||||
tags,
|
||||
}: { file: File; name: string; difficulty: number; tags: string[] },
|
||||
request: UploadMissionRequest,
|
||||
{ rejectWithValue },
|
||||
) => {
|
||||
try {
|
||||
const formData = new FormData();
|
||||
formData.append('MissionFile', file);
|
||||
formData.append('Name', name);
|
||||
formData.append('Difficulty', difficulty.toString());
|
||||
tags.forEach((tag) => formData.append('Tags', tag));
|
||||
const formData = buildUploadMissionFormData(request);
|
||||
|
||||
const response = await axios.post('/missions/upload', formData, {
|
||||
headers: { 'Content-Type': 'multipart/form-data' },
|
||||
});
|
||||
return response.data; // Mission
|
||||
} catch (err: any) {
|
||||
return rejectWithValue(err.response?.data);
|
||||
const status = err?.response?.status;
|
||||
const responseData = err?.response?.data;
|
||||
|
||||
if (status === 400) {
|
||||
const msg = getProblemXmlMissingNameMessage(responseData);
|
||||
if (msg) {
|
||||
return rejectWithValue({
|
||||
errors: {
|
||||
missionFile: [msg],
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (responseData?.errors) {
|
||||
return rejectWithValue(responseData);
|
||||
}
|
||||
|
||||
const fallback =
|
||||
typeof responseData === 'string'
|
||||
? responseData
|
||||
: 'Не удалось загрузить миссию';
|
||||
|
||||
return rejectWithValue({
|
||||
errors: {
|
||||
general: [fallback],
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -351,17 +374,19 @@ const missionsSlice = createSlice({
|
||||
(state, action: PayloadAction<any>) => {
|
||||
state.statuses.upload = 'failed';
|
||||
|
||||
const errors = action.payload.errors as Record<
|
||||
string,
|
||||
string[]
|
||||
>;
|
||||
Object.values(errors).forEach((messages) => {
|
||||
messages.forEach((msg) => {
|
||||
toastError(msg);
|
||||
const errors = action.payload?.errors as
|
||||
| Record<string, string[]>
|
||||
| undefined;
|
||||
if (errors) {
|
||||
Object.values(errors).forEach((messages) => {
|
||||
messages.forEach((msg) => {
|
||||
toastError(msg);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
state.create.errors = errors;
|
||||
state.create.errors = errors;
|
||||
} else {
|
||||
toastError('Не удалось загрузить миссию');
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user