refactor: requests decomposed

This commit is contained in:
IlyaDoronin 2024-05-21 17:10:02 +03:00
parent 6c29900ee4
commit dc23829a49
8 changed files with 106 additions and 63 deletions

@ -30,12 +30,12 @@ export type Account = {
wallet: Wallet;
};
const baseUrl = process.env.REACT_APP_DOMAIN + "/customer";
const API_URL = process.env.REACT_APP_DOMAIN + "/customer";
export const getAccountInfo = async (id: string): Promise<[Account | null, string?]> => {
try {
const accountInfoResponse = await makeRequest<never, Account>({
url: `${baseUrl}/account/${id}`,
url: `${API_URL}/account/${id}`,
method: "GET",
useToken: true,
});
@ -47,3 +47,20 @@ export const getAccountInfo = async (id: string): Promise<[Account | null, strin
return [null, `Не удалось получить информацию об аккаунте. ${error}`];
}
};
export const editAccount = async (userId: string, status: "org" | "nko"): Promise<[unknown | null, string?]> => {
try {
const editResponse = await makeRequest<{ status: "org" | "nko" }, unknown>({
method: "patch",
useToken: true,
url: `${API_URL}/account/${userId}`,
body: { status },
});
return [editResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Не удалось отредактировать информацию. ${error}`];
}
};

26
src/api/quiz.ts Normal file

@ -0,0 +1,26 @@
import makeRequest from "@root/api/makeRequest";
import { parseAxiosError } from "@root/utils/parse-error";
const API_URL = `${process.env.REACT_APP_DOMAIN}/squiz/quiz`;
type MoveQuizBody = {
Qid: string;
AccountID: string;
};
export const moveQuiz = async (quizId: string, userId: string): Promise<[unknown | null, string?]> => {
try {
const moveResponse = await makeRequest<MoveQuizBody, unknown>({
method: "post",
url: `${API_URL}/move`,
body: { Qid: quizId, AccountID: userId },
});
return [moveResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Ошибка перемещения квиза. ${error}`];
}
};

@ -47,7 +47,7 @@ export const getRoles_mock = (): Promise<TMockData> => {
export const deleteRole = async (id: string): Promise<[unknown, string?]> => {
try {
const deleteRoleResponse = await makeRequest({
const deleteRoleResponse = await makeRequest<never, unknown>({
url: `${baseUrl}/${id}`,
method: "delete",
});

@ -4,12 +4,12 @@ import { parseAxiosError } from "@root/utils/parse-error";
import type { SendTicketMessageRequest } from "@root/model/ticket";
const baseUrl = process.env.REACT_APP_DOMAIN + "/heruvym";
const API_URL = process.env.REACT_APP_DOMAIN + "/heruvym";
export const sendTicketMessage = async (body: SendTicketMessageRequest): Promise<[unknown, string?]> => {
try {
const sendTicketMessageResponse = await makeRequest<SendTicketMessageRequest, unknown>({
url: `${baseUrl}/send`,
url: `${API_URL}/send`,
method: "POST",
useToken: true,
body,
@ -22,3 +22,40 @@ export const sendTicketMessage = async (body: SendTicketMessageRequest): Promise
return [null, `Ошибка отправки сообщения. ${error}`];
}
};
export const closeTicket = async (ticketId: string): Promise<[unknown, string?]> => {
try {
const ticketCloseResponse = await makeRequest<unknown, unknown>({
url: `${API_URL}/close`,
method: "post",
useToken: true,
body: { ticket: ticketId },
});
return [ticketCloseResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Не удалось закрыть тикет. ${error}`];
}
};
export const sendFile = async (file: File, ticketId: string): Promise<[unknown | null, string?]> => {
try {
const body = new FormData();
body.append(file.name, file);
body.append("ticket", ticketId);
const sendFileRespose = await makeRequest<FormData, unknown>({
method: "POST",
url: `${API_URL}/sendFiles`,
body,
});
return [sendFileRespose];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Не удалось отправить файл. ${error}`];
}
};

@ -30,6 +30,7 @@ import ChatDocument from "./ChatDocument";
import ChatVideo from "./ChatVideo";
import ChatMessage from "./ChatMessage";
import { ACCEPT_SEND_MEDIA_TYPES_MAP, MAX_FILE_SIZE, MAX_PHOTO_SIZE, MAX_VIDEO_SIZE } from "./fileUpload";
import { sendFile as sendFileRequest } from "@root/api/tickets";
const tooLarge = "Файл слишком большой";
const checkAcceptableMediaType = (file: File) => {
@ -164,24 +165,18 @@ export default function Chat() {
const sendFile = async (file: File) => {
if (file === undefined) return true;
let data;
const ticketId = ticket?.id;
if (ticketId !== undefined) {
try {
const body = new FormData();
const [_, sendFileError] = await sendFileRequest(file, ticketId);
body.append(file.name, file);
body.append("ticket", ticketId);
await makeRequest({
url: process.env.REACT_APP_DOMAIN + "/heruvym/sendFiles",
body: body,
method: "POST",
});
} catch (error: any) {
const errorMessage = getMessageFromFetchError(error);
if (errorMessage) enqueueSnackbar(errorMessage);
if (sendFileError) {
const errorMessage = getMessageFromFetchError(sendFileError);
if (errorMessage) {
enqueueSnackbar(errorMessage);
}
}
return true;
}
};

@ -5,6 +5,7 @@ import Typography from "@mui/material/Typography";
import Button from "@mui/material/Button";
import makeRequest from "@root/api/makeRequest";
import { parseAxiosError } from "@root/utils/parse-error";
import { closeTicket } from "@root/api/tickets";
interface Props {
ticketId: string | undefined;
openModal: boolean;
@ -12,22 +13,10 @@ interface Props {
}
export default function CloseTicketModal({ ticketId, openModal, setOpenModal }: Props) {
const CloseTicket = async () => {
try {
const ticketCloseResponse = await makeRequest<unknown, unknown>({
url: process.env.REACT_APP_DOMAIN + "/heruvym/close",
method: "post",
useToken: true,
body: {
ticket: ticketId,
},
});
return [ticketCloseResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Не удалось закрыть тикет. ${error}`];
const onCloseTicket = async () => {
if (ticketId) {
await closeTicket(ticketId);
setOpenModal(false);
}
};
@ -64,13 +53,7 @@ export default function CloseTicketModal({ ticketId, openModal, setOpenModal }:
alignItems: "center",
}}
>
<Button
onClick={async () => {
CloseTicket();
setOpenModal(false);
}}
sx={{ width: "40px", height: "25px" }}
>
<Button onClick={onCloseTicket} sx={{ width: "40px", height: "25px" }}>
Да
</Button>
<Button onClick={() => setOpenModal(false)} sx={{ width: "40px", height: "25px" }}>

@ -1,6 +1,6 @@
import { Box, Button, TextField, Typography } from "@mui/material";
import { ChangeEvent, useState } from "react";
import makeRequest from "@root/api/makeRequest";
import { moveQuiz } from "@root/api/quiz";
type QuizTabProps = {
userId: string;
@ -25,18 +25,7 @@ export default function QuizTab({ userId }: QuizTabProps) {
setQuizId(event.target.value.split("link/")[1]);
}}
/>
<Button
variant="text"
sx={{ background: "#9A9AAF" }}
onClick={async () => {
await makeRequest({
method: "post",
//useToken: true,
url: process.env.REACT_APP_DOMAIN + "/squiz/quiz/move",
body: { Qid: quizId, AccountID: userId },
});
}}
>
<Button variant="text" sx={{ background: "#9A9AAF" }} onClick={() => moveQuiz(quizId, userId)}>
Ок
</Button>
</Box>

@ -6,7 +6,7 @@ import { useDebouncedCallback } from "use-debounce";
import type { ChangeEvent } from "react";
import type { Verification } from "@root/api/verification";
import makeRequest from "@root/api/makeRequest";
import { editAccount } from "@root/api/account";
type VerificationTabProps = {
userId: string;
@ -66,13 +66,9 @@ export const VerificationTab = ({ userId }: VerificationTabProps) => {
status: verificationInfo.status,
});
if (accepted && _ === "OK")
await makeRequest({
method: "patch",
useToken: true,
url: process.env.REACT_APP_DOMAIN + `/customer/account/${userId}`,
body: { status: verificationInfo.status },
});
if (accepted && _ === "OK") {
await editAccount(userId, verificationInfo.status);
}
if (patchVerificationError) {
return console.error("Error verifying:", patchVerificationError);