feat!(front): migrate to Hey API
This commit is contained in:
parent
128a33824a
commit
6e802d2402
47 changed files with 2865 additions and 1209 deletions
|
|
@ -1,7 +1,8 @@
|
|||
import { useEffect, useState } from "react";
|
||||
import { DefaultService } from "../../api";
|
||||
// import { DefaultService } from "../../api";
|
||||
import { addUserTitle, deleteUserTitle, getUserTitle, updateUserTitle } from "../../api";
|
||||
import type { UserTitleStatus } from "../../api";
|
||||
// import { useCookies } from 'react-cookie';
|
||||
import { useCookies } from 'react-cookie';
|
||||
|
||||
import {
|
||||
ClockIcon,
|
||||
|
|
@ -9,6 +10,7 @@ import {
|
|||
PlayCircleIcon,
|
||||
XCircleIcon,
|
||||
} from "@heroicons/react/24/solid";
|
||||
// import { stat } from "fs";
|
||||
|
||||
// Статусы с иконками и подписью
|
||||
const STATUS_BUTTONS: { status: UserTitleStatus; icon: React.ReactNode; label: string }[] = [
|
||||
|
|
@ -19,8 +21,8 @@ const STATUS_BUTTONS: { status: UserTitleStatus; icon: React.ReactNode; label: s
|
|||
];
|
||||
|
||||
export function TitleStatusControls({ titleId }: { titleId: number }) {
|
||||
// const [cookies] = useCookies(['xsrf_token']);
|
||||
// const xsrfToken = cookies['xsrf_token'] || null;
|
||||
const [cookies] = useCookies(['xsrf_token']);
|
||||
const xsrfToken = cookies['xsrf_token'] || null;
|
||||
|
||||
const [currentStatus, setCurrentStatus] = useState<UserTitleStatus | null>(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
|
@ -31,10 +33,13 @@ export function TitleStatusControls({ titleId }: { titleId: number }) {
|
|||
// --- Load initial status ---
|
||||
useEffect(() => {
|
||||
if (!userId) return;
|
||||
getUserTitle({ path: { user_id: userId, title_id: titleId } })
|
||||
.then(res => setCurrentStatus(res.data?.status ?? null))
|
||||
.catch(() => setCurrentStatus(null)); // 404 = not assigned
|
||||
|
||||
DefaultService.getUserTitle(userId, titleId)
|
||||
.then((res) => setCurrentStatus(res.status))
|
||||
.catch(() => setCurrentStatus(null)); // 404 = user title does not exist
|
||||
// DefaultService.getUserTitle(userId, titleId)
|
||||
// .then((res) => setCurrentStatus(res.status))
|
||||
// .catch(() => setCurrentStatus(null)); // 404 = user title does not exist
|
||||
}, [titleId, userId]);
|
||||
|
||||
// --- Handle click ---
|
||||
|
|
@ -46,7 +51,11 @@ export function TitleStatusControls({ titleId }: { titleId: number }) {
|
|||
try {
|
||||
// 1) Если кликнули на текущий статус — DELETE
|
||||
if (currentStatus === status) {
|
||||
await DefaultService.deleteUserTitle(userId, titleId);
|
||||
// await DefaultService.deleteUserTitle(userId, titleId);
|
||||
await deleteUserTitle({path: {
|
||||
user_id: userId,
|
||||
title_id: titleId,
|
||||
}})
|
||||
setCurrentStatus(null);
|
||||
return;
|
||||
}
|
||||
|
|
@ -54,15 +63,28 @@ export function TitleStatusControls({ titleId }: { titleId: number }) {
|
|||
// 2) Если другой статус — POST или PATCH
|
||||
if (!currentStatus) {
|
||||
// ещё нет записи — POST
|
||||
const added = await DefaultService.addUserTitle(userId, {
|
||||
// const added = await DefaultService.addUserTitle(userId, {
|
||||
// title_id: titleId,
|
||||
// status,
|
||||
// });
|
||||
const added = await addUserTitle({
|
||||
body: {
|
||||
title_id: titleId,
|
||||
status,
|
||||
status: status,
|
||||
},
|
||||
path: {user_id: userId}
|
||||
});
|
||||
setCurrentStatus(added.status);
|
||||
|
||||
setCurrentStatus(added.data?.status ?? null);
|
||||
} else {
|
||||
// уже есть запись — PATCH
|
||||
const updated = await DefaultService.updateUserTitle(userId, titleId, { status });
|
||||
setCurrentStatus(updated.status);
|
||||
//const updated = await DefaultService.updateUserTitle(userId, titleId, { status });
|
||||
const updated = await updateUserTitle({
|
||||
path: { user_id: userId, title_id: titleId },
|
||||
body: { status },
|
||||
headers: { "X-XSRF-TOKEN": xsrfToken },
|
||||
});
|
||||
setCurrentStatus(updated.data?.status ?? null);
|
||||
}
|
||||
} finally {
|
||||
setLoading(false);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue