contests
This commit is contained in:
189
src/redux/slices/contests.ts
Normal file
189
src/redux/slices/contests.ts
Normal file
@@ -0,0 +1,189 @@
|
||||
import { createSlice, createAsyncThunk, PayloadAction } from "@reduxjs/toolkit";
|
||||
import axios from "../../axios";
|
||||
|
||||
// =====================
|
||||
// Типы
|
||||
// =====================
|
||||
|
||||
export interface Mission {
|
||||
missionId: number;
|
||||
name: string;
|
||||
sortOrder: number;
|
||||
}
|
||||
|
||||
export interface Member {
|
||||
userId: number;
|
||||
username: string;
|
||||
role: string;
|
||||
}
|
||||
|
||||
export interface Contest {
|
||||
id: number;
|
||||
name: string;
|
||||
description: string;
|
||||
scheduleType: string;
|
||||
startsAt: string;
|
||||
endsAt: string;
|
||||
availableFrom: string | null;
|
||||
availableUntil: string | null;
|
||||
attemptDurationMinutes: number | null;
|
||||
groupId: number | null;
|
||||
groupName: string | null;
|
||||
missions: Mission[];
|
||||
articles: any[];
|
||||
members: Member[];
|
||||
}
|
||||
|
||||
interface ContestsResponse {
|
||||
hasNextPage: boolean;
|
||||
contests: Contest[];
|
||||
}
|
||||
|
||||
export interface CreateContestBody {
|
||||
name: string;
|
||||
description: string;
|
||||
scheduleType: "FixedWindow" | "Flexible";
|
||||
startsAt: string;
|
||||
endsAt: string;
|
||||
availableFrom: string | null;
|
||||
availableUntil: string | null;
|
||||
attemptDurationMinutes: number | null;
|
||||
groupId: number | null;
|
||||
missionIds: number[];
|
||||
articleIds: number[];
|
||||
participantIds: number[];
|
||||
organizerIds: number[];
|
||||
}
|
||||
|
||||
// =====================
|
||||
// Состояние
|
||||
// =====================
|
||||
|
||||
interface ContestsState {
|
||||
contests: Contest[];
|
||||
selectedContest: Contest | null;
|
||||
hasNextPage: boolean;
|
||||
status: "idle" | "loading" | "successful" | "failed";
|
||||
error: string | null;
|
||||
}
|
||||
|
||||
const initialState: ContestsState = {
|
||||
contests: [],
|
||||
selectedContest: null,
|
||||
hasNextPage: false,
|
||||
status: "idle",
|
||||
error: null,
|
||||
};
|
||||
|
||||
// =====================
|
||||
// Async Thunks
|
||||
// =====================
|
||||
|
||||
// Получение списка контестов
|
||||
export const fetchContests = createAsyncThunk(
|
||||
"contests/fetchAll",
|
||||
async (
|
||||
params: { page?: number; pageSize?: number; groupId?: number | null } = {},
|
||||
{ rejectWithValue }
|
||||
) => {
|
||||
try {
|
||||
const { page = 0, pageSize = 10, groupId } = params;
|
||||
const response = await axios.get<ContestsResponse>("/contests", {
|
||||
params: { page, pageSize, groupId },
|
||||
});
|
||||
return response.data;
|
||||
} catch (err: any) {
|
||||
return rejectWithValue(err.response?.data?.message || "Failed to fetch contests");
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// Получение одного контеста по ID
|
||||
export const fetchContestById = createAsyncThunk(
|
||||
"contests/fetchById",
|
||||
async (id: number, { rejectWithValue }) => {
|
||||
try {
|
||||
const response = await axios.get<Contest>(`/contests/${id}`);
|
||||
return response.data;
|
||||
} catch (err: any) {
|
||||
return rejectWithValue(err.response?.data?.message || "Failed to fetch contest");
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// Создание нового контеста
|
||||
export const createContest = createAsyncThunk(
|
||||
"contests/create",
|
||||
async (contestData: CreateContestBody, { rejectWithValue }) => {
|
||||
try {
|
||||
const response = await axios.post<Contest>("/contests", contestData);
|
||||
return response.data;
|
||||
} catch (err: any) {
|
||||
return rejectWithValue(err.response?.data?.message || "Failed to create contest");
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// =====================
|
||||
// Slice
|
||||
// =====================
|
||||
|
||||
const contestsSlice = createSlice({
|
||||
name: "contests",
|
||||
initialState,
|
||||
reducers: {
|
||||
clearSelectedContest: (state) => {
|
||||
state.selectedContest = null;
|
||||
},
|
||||
},
|
||||
extraReducers: (builder) => {
|
||||
// fetchContests
|
||||
builder.addCase(fetchContests.pending, (state) => {
|
||||
state.status = "loading";
|
||||
state.error = null;
|
||||
});
|
||||
builder.addCase(fetchContests.fulfilled, (state, action: PayloadAction<ContestsResponse>) => {
|
||||
state.status = "successful";
|
||||
state.contests = action.payload.contests;
|
||||
state.hasNextPage = action.payload.hasNextPage;
|
||||
});
|
||||
builder.addCase(fetchContests.rejected, (state, action: PayloadAction<any>) => {
|
||||
state.status = "failed";
|
||||
state.error = action.payload;
|
||||
});
|
||||
|
||||
// fetchContestById
|
||||
builder.addCase(fetchContestById.pending, (state) => {
|
||||
state.status = "loading";
|
||||
state.error = null;
|
||||
});
|
||||
builder.addCase(fetchContestById.fulfilled, (state, action: PayloadAction<Contest>) => {
|
||||
state.status = "successful";
|
||||
state.selectedContest = action.payload;
|
||||
});
|
||||
builder.addCase(fetchContestById.rejected, (state, action: PayloadAction<any>) => {
|
||||
state.status = "failed";
|
||||
state.error = action.payload;
|
||||
});
|
||||
|
||||
// createContest
|
||||
builder.addCase(createContest.pending, (state) => {
|
||||
state.status = "loading";
|
||||
state.error = null;
|
||||
});
|
||||
builder.addCase(createContest.fulfilled, (state, action: PayloadAction<Contest>) => {
|
||||
state.status = "successful";
|
||||
state.contests.unshift(action.payload);
|
||||
});
|
||||
builder.addCase(createContest.rejected, (state, action: PayloadAction<any>) => {
|
||||
state.status = "failed";
|
||||
state.error = action.payload;
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
// =====================
|
||||
// Экспорты
|
||||
// =====================
|
||||
export const { clearSelectedContest } = contestsSlice.actions;
|
||||
export const contestsReducer = contestsSlice.reducer;
|
||||
@@ -3,6 +3,7 @@ import { authReducer } from "./slices/auth";
|
||||
import { storeReducer } from "./slices/store";
|
||||
import { missionsReducer } from "./slices/missions";
|
||||
import { submitReducer } from "./slices/submit";
|
||||
import { contestsReducer } from "./slices/contests";
|
||||
|
||||
|
||||
// использование
|
||||
@@ -21,6 +22,7 @@ export const store = configureStore({
|
||||
store: storeReducer,
|
||||
missions: missionsReducer,
|
||||
submin: submitReducer,
|
||||
contests: contestsReducer,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user