This commit is contained in:
rngsurrounded
2025-03-02 20:14:14 +09:00
41 changed files with 470 additions and 376 deletions
+3 -3
View File
@@ -1,4 +1,4 @@
import { authFetch } from ".";
import { apiFetch } from ".";
interface AuthResponse {
token: string;
@@ -9,14 +9,14 @@ export const signup = async (body: {
username: string;
password: string;
}) => {
return await authFetch<AuthResponse>("/sign-up", {
return await apiFetch<AuthResponse>("/sign-up", {
method: "POST",
body,
});
};
export const login = async (body: { email: string; password: string }) => {
return await authFetch<AuthResponse>("/sign-in", {
return await apiFetch<AuthResponse>("/sign-in", {
method: "POST",
body,
});
@@ -1,83 +1,14 @@
import { apiFetch } from '.';
import { Competition, CompetitionStatus, ParticipationType } from '@/shared/types';
import { userFetch } from ".";
import { Competition } from "../types/competition";
interface ApiCompetition {
id: string;
state: 'started' | 'not_started' | 'finished';
title: string;
description: string;
image_url: string | null;
end_date: string;
start_date: string;
type: string;
participation_type: ParticipationType;
}
const mapStateToStatus = (state: string, isParticipating: boolean): CompetitionStatus => {
if (!isParticipating) {
return CompetitionStatus.NotParticipating;
}
switch (state) {
case 'started':
return CompetitionStatus.InProgress;
case 'finished':
return CompetitionStatus.Completed;
case 'not_started':
return CompetitionStatus.InProgress;
default:
return CompetitionStatus.NotParticipating;
}
export const getCompetitions = async (participating?: boolean) => {
return await userFetch<Competition[]>("/competitions", {
params: {
is_participating: participating,
},
});
};
const transformApiCompetition = (apiComp: ApiCompetition, isParticipating: boolean): Competition => {
return {
id: apiComp.id,
name: apiComp.title,
imageUrl: apiComp.image_url || '/DANO.png',
isOlympics: apiComp.type !== 'edu',
status: mapStateToStatus(apiComp.state, isParticipating),
description: apiComp.description,
startDate: new Date(apiComp.start_date),
endDate: new Date(apiComp.end_date),
participationType: apiComp.participation_type
};
export const getCompetition = async (id: string) => {
return await userFetch<Competition>(`/competition/${id}`);
};
export const getParticipatingCompetitions = async (): Promise<Competition[]> => {
try {
const apiCompetitions: ApiCompetition[] = await apiFetch('/api/v1/competitions', {
query: { is_participating: true }
});
return apiCompetitions.map(comp => transformApiCompetition(comp, true));
} catch (error) {
console.error('Failed to fetch participating competitions:', error);
throw error;
}
};
export const getNonParticipatingCompetitions = async (): Promise<Competition[]> => {
try {
const apiCompetitions: ApiCompetition[] = await apiFetch('/api/v1/competitions', {
query: { is_participating: false }
});
return apiCompetitions.map(comp => transformApiCompetition(comp, false));
} catch (error) {
console.error('Failed to fetch non-participating competitions:', error);
throw error;
}
};
export const getAllCompetitions = async (): Promise<{
participating: Competition[];
nonParticipating: Competition[];
}> => {
const [participating, nonParticipating] = await Promise.all([
getParticipatingCompetitions(),
getNonParticipatingCompetitions()
]);
return { participating, nonParticipating };
};
+2 -2
View File
@@ -14,14 +14,14 @@ export class ApiError extends Error {
}
}
export const authFetch = ofetch.create({
export const apiFetch = ofetch.create({
baseURL: BASE_URL,
async onResponseError({ response }) {
throw new ApiError(response);
},
});
export const apiFetch = ofetch.create({
export const userFetch = ofetch.create({
baseURL: BASE_URL,
async onRequest({ options }) {
options.headers.set("Authorization", "Bearer " + getToken());
@@ -0,0 +1,10 @@
import { apiFetch } from ".";
import { Reviewer } from "../types/review";
export const getReviewer = async (token: string) => {
return await apiFetch<Reviewer>(`/review/${token}`);
};
export const getReviewerSubmissions = async (token: string) => {
return await apiFetch(`/review/${token}/submissions`);
};
+2 -2
View File
@@ -1,6 +1,6 @@
import { apiFetch } from ".";
import { userFetch } from ".";
import { User } from "../types/user";
export const getCurrentUser = async () => {
return await apiFetch<User>("/me");
return await userFetch<User>("/me");
};
@@ -0,0 +1,26 @@
export interface Competition {
id: string;
title: string;
description: string;
state: CompetitionState;
image_url?: string;
start_date?: Date;
end_date?: Date;
type: CompetitionType;
participation_type: CompetitionParticipationType;
}
export enum CompetitionState {
NOT_STARTED = "not_started",
STARTED = "started",
FINISHED = "finished",
}
export enum CompetitionType {
EDU = "edu",
COMPETITIVE = "competitive",
}
export enum CompetitionParticipationType {
SOLO = "solo",
}
@@ -0,0 +1,5 @@
export interface Reviewer {
id: string;
name: string;
surname: string;
}