From fdef102baf1abf37a9cd7f3cf4d862150ed26a25 Mon Sep 17 00:00:00 2001 From: nflnkr Date: Mon, 24 Jun 2024 18:11:07 +0300 Subject: [PATCH] show dialog on amo token expiration --- src/api/integration.ts | 12 ++ .../AmoTokenExpiredDialog.tsx | 113 ++++++++++++++++++ src/pages/startPage/EditPage.tsx | 4 + 3 files changed, 129 insertions(+) create mode 100644 src/pages/IntegrationsPage/IntegrationsModal/AmoTokenExpiredDialog.tsx diff --git a/src/api/integration.ts b/src/api/integration.ts index 46af0b05..c2e982ef 100644 --- a/src/api/integration.ts +++ b/src/api/integration.ts @@ -1,6 +1,7 @@ import { QuestionKeys } from "@/pages/IntegrationsPage/IntegrationsModal/types"; import { makeRequest } from "@api/makeRequest"; import { parseAxiosError } from "@utils/parse-error"; +import useSWR from "swr"; export type PaginationRequest = { page: number; @@ -21,6 +22,7 @@ export type AccountResponse = { subdomain: string; country: string; driveURL: string; + stale: boolean; }; export const getAccount = async (): Promise<[AccountResponse | null, string?]> => { @@ -37,6 +39,16 @@ export const getAccount = async (): Promise<[AccountResponse | null, string?]> = } }; +export function useAmoAccount() { + return useSWR("amoAccount", () => + makeRequest({ + method: "GET", + url: `${API_URL}/account`, + useToken: true, + }) + ); +} + // подключить Amo export const connectAmo = async (): Promise<[string | null, string?]> => { diff --git a/src/pages/IntegrationsPage/IntegrationsModal/AmoTokenExpiredDialog.tsx b/src/pages/IntegrationsPage/IntegrationsModal/AmoTokenExpiredDialog.tsx new file mode 100644 index 00000000..f1f2801d --- /dev/null +++ b/src/pages/IntegrationsPage/IntegrationsModal/AmoTokenExpiredDialog.tsx @@ -0,0 +1,113 @@ +import { connectAmo } from "@/api/integration"; +import CustomCheckbox from "@/ui_kit/CustomCheckbox"; +import { Box, Button, Dialog, Typography, useMediaQuery, useTheme } from "@mui/material"; +import { useState } from "react"; + +const HIDE_DIALOG_EXPIRATION_PERIOD = 24 * 60 * 60 * 1000; + +interface Props { + initialOpen: boolean; +} + +export default function AmoTokenExpiredDialog({ initialOpen }: Props) { + const theme = useTheme(); + const [isDialogOpen, setIsDialogOpen] = useState(() => { + const hideExpirationTime = Number(localStorage.getItem("hideAmoTokenExpiredDialogExpirationTime")); + if (hideExpirationTime && hideExpirationTime > Date.now()) return false; + + return initialOpen; + }); + const [isHideDialogForADayChecked, setIsHideDialogForADayChecked] = useState(false); + + const onAmoClick = async () => { + const [url, error] = await connectAmo(); + if (url && !error) { + window.open(url, "_blank"); + } + }; + + function handleDialogClose() { + if (isHideDialogForADayChecked) { + const expirationDate = Date.now() + HIDE_DIALOG_EXPIRATION_PERIOD; + localStorage.setItem("hideAmoTokenExpiredDialogExpirationTime", expirationDate.toString()); + } + + setIsDialogOpen(false); + } + + return ( + + + + Ваш amo-токен не работает + + + + + Amo отозвал ваш токен. Зайдите заново в свой аккаунт, чтобы вам снова начали приходить сделки. + + + + + + { + setIsHideDialogForADayChecked(target.checked); + }} + /> + + + ); +} diff --git a/src/pages/startPage/EditPage.tsx b/src/pages/startPage/EditPage.tsx index b288dd61..57169e8d 100755 --- a/src/pages/startPage/EditPage.tsx +++ b/src/pages/startPage/EditPage.tsx @@ -21,6 +21,8 @@ import { AnyTypedQuizQuestion } from "@frontend/squzanswerer"; import { ModalInfoWhyCantCreate } from "./ModalInfoWhyCantCreate"; import { ConfirmLeaveModal } from "./ConfirmLeaveModal"; import { checkQuestionHint } from "@utils/checkQuestionHint"; +import AmoTokenExpiredDialog from "../IntegrationsPage/IntegrationsModal/AmoTokenExpiredDialog"; +import { useAmoAccount } from "@/api/integration"; interface Props { openBranchingPage: boolean; @@ -41,6 +43,7 @@ export default function EditPage({ setScrollDown, }: Props) { const quiz = useCurrentQuiz(); + const { data: amoAccount } = useAmoAccount(); const { editQuizId } = useQuizStore(); const { questions } = useQuestionsStore(); const { showConfirmLeaveModal, nextStep } = useUiTools(); @@ -105,6 +108,7 @@ export default function EditPage({ return ( <> + {amoAccount && }