refactor: отчёт сессии загружается одним hook useSessionReport с повтором 409
This commit is contained in:
parent
93c5f063f3
commit
f6d0653e68
7 changed files with 106 additions and 79 deletions
|
|
@ -3,7 +3,9 @@
|
|||
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
import type { Level, SessionMode, StationSnapshot } from "@/shared/types/generated";
|
||||
import type { Level, SessionMode, SessionReport, StationSnapshot } from "@/shared/types/generated";
|
||||
|
||||
import { reportErrorMessage, reportRetryDelay, shouldRetryReport } from "./report.mjs";
|
||||
|
||||
export class ApiError extends Error {
|
||||
constructor(
|
||||
|
|
@ -328,6 +330,30 @@ export const useProfile = (traineeId: string | null) =>
|
|||
enabled: Boolean(traineeId),
|
||||
});
|
||||
|
||||
const errorStatus = (error: unknown) => (error instanceof ApiError ? error.status : null);
|
||||
|
||||
/**
|
||||
* Отчёт сессии. `ready = false` — запроса нет и отчёт не показывается: его
|
||||
* просят после `score.ready` или у уже завершённой сессии. 409 (оценка ещё
|
||||
* считается) повторяется сам с ограниченным числом попыток.
|
||||
*/
|
||||
export function useSessionReport(sessionId: string | null, ready: boolean) {
|
||||
const enabled = Boolean(sessionId) && ready;
|
||||
const query = useQuery({
|
||||
queryKey: ["session-report", sessionId],
|
||||
queryFn: () => request<SessionReport>(`/api/sessions/${sessionId}/report`),
|
||||
enabled,
|
||||
retry: (failureCount, error) => shouldRetryReport(failureCount, errorStatus(error)),
|
||||
retryDelay: reportRetryDelay,
|
||||
});
|
||||
return {
|
||||
report: enabled ? query.data ?? null : null,
|
||||
loading: enabled && query.isFetching,
|
||||
error: enabled && query.isError ? reportErrorMessage(errorStatus(query.error)) : "",
|
||||
refetch: query.refetch,
|
||||
};
|
||||
}
|
||||
|
||||
export const useCreateSession = () =>
|
||||
useMutation({
|
||||
mutationFn: (body: { scenario_id: string; mode: SessionMode; trainee?: string; group?: string }) =>
|
||||
|
|
|
|||
Loading…
Reference in a new issue