Добавлено использование токена авторизации из куки в компонентах: AgentsBarChart, AgentsTable, BillingMetricCards, BillingPieChart, BillingStatChart, MetricCards, PayoutsTransactionsTable, ReferralsTable, RevenueChart и SalesTable. Реализована проверка наличия токена перед выполнением запросов к API, добавлены соответствующие сообщения об ошибках при его отсутствии.
This commit is contained in:
parent
4b17da42e8
commit
410410bc85
@ -1,6 +1,7 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import { ResponsiveContainer, BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, LabelList, Label } from "recharts";
|
import { ResponsiveContainer, BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, LabelList, Label } from "recharts";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
import Cookies from 'js-cookie';
|
||||||
|
|
||||||
interface AgentBarData {
|
interface AgentBarData {
|
||||||
name: string;
|
name: string;
|
||||||
@ -28,7 +29,19 @@ const AgentsBarChart: React.FC = () => {
|
|||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetch("/api/dashboard/chart/agent")
|
const token = Cookies.get("access_token");
|
||||||
|
if (!token) {
|
||||||
|
console.warn("Токен авторизации не найден.");
|
||||||
|
setError("Токен авторизации не найден.");
|
||||||
|
setLoading(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch("/api/dashboard/chart/agent", {
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${token}`
|
||||||
|
}
|
||||||
|
})
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
if (!res.ok) throw new Error("Ошибка загрузки данных");
|
if (!res.ok) throw new Error("Ошибка загрузки данных");
|
||||||
return res.json();
|
return res.json();
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import styles from "../styles/stat.module.css";
|
|||||||
import { Box, Button } from '@mui/material';
|
import { Box, Button } from '@mui/material';
|
||||||
import FileDownloadIcon from '@mui/icons-material/FileDownload';
|
import FileDownloadIcon from '@mui/icons-material/FileDownload';
|
||||||
import { mkConfig, generateCsv, download } from 'export-to-csv';
|
import { mkConfig, generateCsv, download } from 'export-to-csv';
|
||||||
|
import Cookies from 'js-cookie';
|
||||||
|
|
||||||
function formatCurrency(amount: number) {
|
function formatCurrency(amount: number) {
|
||||||
return amount?.toLocaleString("ru-RU", {
|
return amount?.toLocaleString("ru-RU", {
|
||||||
@ -20,7 +21,19 @@ export default function AgentsTable({ filters, reloadKey }: { filters: { dateSta
|
|||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
if (filters.dateStart) params.append('date_start', filters.dateStart);
|
if (filters.dateStart) params.append('date_start', filters.dateStart);
|
||||||
if (filters.dateEnd) params.append('date_end', filters.dateEnd);
|
if (filters.dateEnd) params.append('date_end', filters.dateEnd);
|
||||||
fetch(`/api/stat/agents?${params.toString()}`)
|
|
||||||
|
const token = Cookies.get("access_token");
|
||||||
|
if (!token) {
|
||||||
|
console.warn("Токен авторизации не найден.");
|
||||||
|
setData([]); // Очистить данные, если токен отсутствует
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch(`/api/stat/agents?${params.toString()}`, {
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${token}`
|
||||||
|
}
|
||||||
|
})
|
||||||
.then(res => res.json())
|
.then(res => res.json())
|
||||||
.then(setData)
|
.then(setData)
|
||||||
.catch(() => setData([]));
|
.catch(() => setData([]));
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import MetricCard from "./MetricCard";
|
import MetricCard from "./MetricCard";
|
||||||
import styles from "../styles/billing.module.css";
|
import styles from "../styles/billing.module.css";
|
||||||
import React, { useEffect, useState } from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
|
import Cookies from 'js-cookie';
|
||||||
|
|
||||||
interface BillingCardsData {
|
interface BillingCardsData {
|
||||||
cost: number;
|
cost: number;
|
||||||
@ -22,7 +23,19 @@ const BillingMetricCards: React.FC = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetch("/api/billing/cards")
|
const token = Cookies.get("access_token");
|
||||||
|
if (!token) {
|
||||||
|
console.warn("Токен авторизации не найден.");
|
||||||
|
setError("Токен авторизации не найден.");
|
||||||
|
setLoading(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch("/api/billing/cards", {
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${token}`
|
||||||
|
}
|
||||||
|
})
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
if (!res.ok) throw new Error("Ошибка загрузки данных");
|
if (!res.ok) throw new Error("Ошибка загрузки данных");
|
||||||
return res.json();
|
return res.json();
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
import React, { useEffect, useState } from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
import { PieChart, Pie, Cell, Tooltip, ResponsiveContainer } from "recharts";
|
import { PieChart, Pie, Cell, Tooltip, ResponsiveContainer } from "recharts";
|
||||||
import styles from "../styles/billing.module.css";
|
import styles from "../styles/billing.module.css";
|
||||||
|
import Cookies from 'js-cookie';
|
||||||
|
|
||||||
const STATUS_COLORS: Record<string, string> = {
|
const STATUS_COLORS: Record<string, string> = {
|
||||||
"done": "#10B981",
|
"done": "#10B981",
|
||||||
@ -15,7 +16,17 @@ const STATUS_COLORS: Record<string, string> = {
|
|||||||
const BillingPieChart: React.FC = () => {
|
const BillingPieChart: React.FC = () => {
|
||||||
const [data, setData] = useState<{ name: string; value: number; fill: string }[]>([]);
|
const [data, setData] = useState<{ name: string; value: number; fill: string }[]>([]);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetch("/api/billing/chart/pie")
|
const token = Cookies.get("access_token");
|
||||||
|
if (!token) {
|
||||||
|
console.warn("Токен авторизации не найден.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch("/api/billing/chart/pie", {
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${token}`
|
||||||
|
}
|
||||||
|
})
|
||||||
.then((res) => res.json())
|
.then((res) => res.json())
|
||||||
.then((apiData) => {
|
.then((apiData) => {
|
||||||
const mapped = apiData.map((item: { status: string; count: number }) => ({
|
const mapped = apiData.map((item: { status: string; count: number }) => ({
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import { useEffect, useState } from "react";
|
|||||||
import styles from "../styles/billing.module.css";
|
import styles from "../styles/billing.module.css";
|
||||||
import { TooltipProps } from "recharts";
|
import { TooltipProps } from "recharts";
|
||||||
import { ValueType, NameType } from "recharts/types/component/DefaultTooltipContent";
|
import { ValueType, NameType } from "recharts/types/component/DefaultTooltipContent";
|
||||||
|
import Cookies from 'js-cookie';
|
||||||
|
|
||||||
const statusColors: Record<string, string> = {
|
const statusColors: Record<string, string> = {
|
||||||
done: "#10B981",
|
done: "#10B981",
|
||||||
@ -19,7 +20,17 @@ const BillingStatChart: React.FC = () => {
|
|||||||
const [statuses, setStatuses] = useState<string[]>([]);
|
const [statuses, setStatuses] = useState<string[]>([]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetch("/api/billing/chart/stat")
|
const token = Cookies.get("access_token");
|
||||||
|
if (!token) {
|
||||||
|
console.warn("Токен авторизации не найден.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch("/api/billing/chart/stat", {
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${token}`
|
||||||
|
}
|
||||||
|
})
|
||||||
.then(res => res.json())
|
.then(res => res.json())
|
||||||
.then((apiData) => {
|
.then((apiData) => {
|
||||||
// Собираем все уникальные даты и статусы
|
// Собираем все уникальные даты и статусы
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
import MetricCard from "./MetricCard";
|
import MetricCard from "./MetricCard";
|
||||||
import styles from "../styles/dashboard.module.css";
|
import styles from "../styles/dashboard.module.css";
|
||||||
import React, { useEffect, useState } from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
|
import Cookies from 'js-cookie';
|
||||||
|
|
||||||
function formatCurrency(amount: number) {
|
function formatCurrency(amount: number) {
|
||||||
return amount.toLocaleString("ru-RU", {
|
return amount.toLocaleString("ru-RU", {
|
||||||
@ -25,7 +26,19 @@ export default function MetricCards() {
|
|||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetch("/api/dashboard/cards")
|
const token = Cookies.get("access_token");
|
||||||
|
if (!token) {
|
||||||
|
console.warn("Токен авторизации не найден.");
|
||||||
|
setError("Токен авторизации не найден.");
|
||||||
|
setLoading(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch("/api/dashboard/cards", {
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${token}`
|
||||||
|
}
|
||||||
|
})
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
if (!res.ok) throw new Error("Ошибка загрузки данных");
|
if (!res.ok) throw new Error("Ошибка загрузки данных");
|
||||||
return res.json();
|
return res.json();
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import styles from "../styles/billing.module.css";
|
|||||||
import { Box, Button } from '@mui/material';
|
import { Box, Button } from '@mui/material';
|
||||||
import FileDownloadIcon from '@mui/icons-material/FileDownload';
|
import FileDownloadIcon from '@mui/icons-material/FileDownload';
|
||||||
import { mkConfig, generateCsv, download } from 'export-to-csv';
|
import { mkConfig, generateCsv, download } from 'export-to-csv';
|
||||||
|
import Cookies from 'js-cookie';
|
||||||
|
|
||||||
|
|
||||||
function formatCurrency(amount: number) {
|
function formatCurrency(amount: number) {
|
||||||
@ -29,7 +30,19 @@ export default function PayoutsTransactionsTable({ filters, reloadKey }: { filte
|
|||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
if (filters.dateStart) params.append('date_start', filters.dateStart);
|
if (filters.dateStart) params.append('date_start', filters.dateStart);
|
||||||
if (filters.dateEnd) params.append('date_end', filters.dateEnd);
|
if (filters.dateEnd) params.append('date_end', filters.dateEnd);
|
||||||
fetch(`/api/billing/payouts/transactions?${params.toString()}`)
|
|
||||||
|
const token = Cookies.get("access_token");
|
||||||
|
if (!token) {
|
||||||
|
console.warn("Токен авторизации не найден.");
|
||||||
|
setData([]); // Очистить данные, если токен отсутствует
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch(`/api/billing/payouts/transactions?${params.toString()}`, {
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${token}`
|
||||||
|
}
|
||||||
|
})
|
||||||
.then(res => res.json())
|
.then(res => res.json())
|
||||||
.then(setData)
|
.then(setData)
|
||||||
.catch(() => setData([]));
|
.catch(() => setData([]));
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import styles from "../styles/stat.module.css";
|
|||||||
import { Box, Button } from '@mui/material';
|
import { Box, Button } from '@mui/material';
|
||||||
import FileDownloadIcon from '@mui/icons-material/FileDownload';
|
import FileDownloadIcon from '@mui/icons-material/FileDownload';
|
||||||
import { mkConfig, generateCsv, download } from 'export-to-csv';
|
import { mkConfig, generateCsv, download } from 'export-to-csv';
|
||||||
|
import Cookies from 'js-cookie';
|
||||||
|
|
||||||
function formatCurrency(amount: number) {
|
function formatCurrency(amount: number) {
|
||||||
return amount?.toLocaleString("ru-RU", {
|
return amount?.toLocaleString("ru-RU", {
|
||||||
@ -20,7 +21,19 @@ export default function ReferralsTable({ filters, reloadKey }: { filters: { date
|
|||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
if (filters.dateStart) params.append('date_start', filters.dateStart);
|
if (filters.dateStart) params.append('date_start', filters.dateStart);
|
||||||
if (filters.dateEnd) params.append('date_end', filters.dateEnd);
|
if (filters.dateEnd) params.append('date_end', filters.dateEnd);
|
||||||
fetch(`/api/stat/referrals?${params.toString()}`)
|
|
||||||
|
const token = Cookies.get("access_token");
|
||||||
|
if (!token) {
|
||||||
|
console.warn("Токен авторизации не найден.");
|
||||||
|
setData([]); // Очистить данные, если токен отсутствует
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch(`/api/stat/referrals?${params.toString()}`, {
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${token}`
|
||||||
|
}
|
||||||
|
})
|
||||||
.then(res => res.json())
|
.then(res => res.json())
|
||||||
.then(setData)
|
.then(setData)
|
||||||
.catch(() => setData([]));
|
.catch(() => setData([]));
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import { ResponsiveContainer, LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Label } from "recharts";
|
import { ResponsiveContainer, LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Label } from "recharts";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
import Cookies from 'js-cookie';
|
||||||
|
|
||||||
const formatCurrency = (amount: number) => {
|
const formatCurrency = (amount: number) => {
|
||||||
return amount.toLocaleString("ru-RU", {
|
return amount.toLocaleString("ru-RU", {
|
||||||
@ -22,7 +23,19 @@ const RevenueChart: React.FC = () => {
|
|||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetch("/api/dashboard/chart/total")
|
const token = Cookies.get("access_token");
|
||||||
|
if (!token) {
|
||||||
|
console.warn("Токен авторизации не найден.");
|
||||||
|
setError("Токен авторизации не найден.");
|
||||||
|
setLoading(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch("/api/dashboard/chart/total", {
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${token}`
|
||||||
|
}
|
||||||
|
})
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
if (!res.ok) throw new Error("Ошибка загрузки данных");
|
if (!res.ok) throw new Error("Ошибка загрузки данных");
|
||||||
return res.json();
|
return res.json();
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import styles from "../styles/stat.module.css";
|
|||||||
import { Box, Button } from '@mui/material';
|
import { Box, Button } from '@mui/material';
|
||||||
import FileDownloadIcon from '@mui/icons-material/FileDownload';
|
import FileDownloadIcon from '@mui/icons-material/FileDownload';
|
||||||
import { mkConfig, generateCsv, download } from 'export-to-csv';
|
import { mkConfig, generateCsv, download } from 'export-to-csv';
|
||||||
|
import Cookies from 'js-cookie';
|
||||||
|
|
||||||
function formatCurrency(amount: number) {
|
function formatCurrency(amount: number) {
|
||||||
return amount?.toLocaleString("ru-RU", {
|
return amount?.toLocaleString("ru-RU", {
|
||||||
@ -20,7 +21,19 @@ export default function SalesTable({ filters, reloadKey }: { filters: { dateStar
|
|||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
if (filters.dateStart) params.append('date_start', filters.dateStart);
|
if (filters.dateStart) params.append('date_start', filters.dateStart);
|
||||||
if (filters.dateEnd) params.append('date_end', filters.dateEnd);
|
if (filters.dateEnd) params.append('date_end', filters.dateEnd);
|
||||||
fetch(`/api/stat/sales?${params.toString()}`)
|
|
||||||
|
const token = Cookies.get("access_token");
|
||||||
|
if (!token) {
|
||||||
|
console.warn("Токен авторизации не найден.");
|
||||||
|
setData([]); // Очистить данные, если токен отсутствует
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch(`/api/stat/sales?${params.toString()}`, {
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${token}`
|
||||||
|
}
|
||||||
|
})
|
||||||
.then(res => res.json())
|
.then(res => res.json())
|
||||||
.then(setData)
|
.then(setData)
|
||||||
.catch(() => setData([]));
|
.catch(() => setData([]));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user