delete mission
This commit is contained in:
@@ -34,6 +34,7 @@ interface MissionsState {
|
||||
fetchById: Status;
|
||||
upload: Status;
|
||||
fetchMy: Status;
|
||||
delete: Status;
|
||||
};
|
||||
error: string | null;
|
||||
}
|
||||
@@ -49,6 +50,7 @@ const initialState: MissionsState = {
|
||||
fetchById: 'idle',
|
||||
upload: 'idle',
|
||||
fetchMy: 'idle',
|
||||
delete: 'idle',
|
||||
},
|
||||
error: null,
|
||||
};
|
||||
@@ -141,6 +143,21 @@ export const uploadMission = createAsyncThunk(
|
||||
},
|
||||
);
|
||||
|
||||
// DELETE /missions/{id}
|
||||
export const deleteMission = createAsyncThunk(
|
||||
'missions/deleteMission',
|
||||
async (id: number, { rejectWithValue }) => {
|
||||
try {
|
||||
await axios.delete(`/missions/${id}`);
|
||||
return id; // возвращаем id удалённой миссии
|
||||
} catch (err: any) {
|
||||
return rejectWithValue(
|
||||
err.response?.data?.message || 'Ошибка при удалении миссии',
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
// ─── Slice ────────────────────────────────────────────
|
||||
|
||||
const missionsSlice = createSlice({
|
||||
@@ -245,6 +262,32 @@ const missionsSlice = createSlice({
|
||||
state.error = action.payload;
|
||||
},
|
||||
);
|
||||
|
||||
// ─── DELETE MISSION ───
|
||||
builder.addCase(deleteMission.pending, (state) => {
|
||||
state.statuses.delete = 'loading';
|
||||
state.error = null;
|
||||
});
|
||||
builder.addCase(
|
||||
deleteMission.fulfilled,
|
||||
(state, action: PayloadAction<number>) => {
|
||||
state.statuses.delete = 'successful';
|
||||
state.missions = state.missions.filter(
|
||||
(m) => m.id !== action.payload,
|
||||
);
|
||||
|
||||
if (state.currentMission?.id === action.payload) {
|
||||
state.currentMission = null;
|
||||
}
|
||||
},
|
||||
);
|
||||
builder.addCase(
|
||||
deleteMission.rejected,
|
||||
(state, action: PayloadAction<any>) => {
|
||||
state.statuses.delete = 'failed';
|
||||
state.error = action.payload;
|
||||
},
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user