fixed sort in profile-reports table and in statistics table, improved error messages, improved metatags by fetching metadata from server

This commit is contained in:
Alibek committed 2024-03-16 02:19:33 +06:00
1 parent 0221a79015
commit 0ba0f1df89
25 files changed
+413 -171

No files matched your search

@@ -29,7 +29,8 @@ const ProfileTable: React.FC<IProfileTableProps> = ({
} = useProfileReportsStore();
const session = useSession();
const router = useRouter();
const [sort, setSort] = useState("date");
const [sort, setSort] = useState("");
const [status, setStatus] = useState<number>(2);
const [activePage, setActivePage] = useState<number>(
+searchParams["страница-обращений"] || 1
);
@@ -37,11 +38,19 @@ const ProfileTable: React.FC<IProfileTableProps> = ({
{
param: "Дата",
handleClick() {
setSort("date");
setSort("");
},
},
{ param: "Адрес" },
{ param: "Статус" },
{
param: "Статус",
handleClick() {
setStatus((prev) =>
prev === 1 ? 2 : prev === 2 ? 3 : prev === 3 ? 1 : 2
);
setSort("status");
},
},
{
param: "Комментарии",
handleClick() {
@@ -51,21 +60,13 @@ const ProfileTable: React.FC<IProfileTableProps> = ({
{
param: "Рейтинг",
handleClick() {
setSort("rating");
setSort("likes");
},
},
];
useEffect(() => {
if (session.status === "loading") return;
getMyReports(
sort,
activePage,
session.data?.access_token as string
);
}, [session]);
useEffect(() => {
router.push(
`/profile/my-reports?страница-обращений=${activePage}`,
{ scroll: false }
@@ -73,9 +74,10 @@ const ProfileTable: React.FC<IProfileTableProps> = ({
getMyReports(
sort,
activePage,
session.data?.access_token as string
session.data?.access_token as string,
status
);
}, [activePage, sort]);
}, [activePage, sort, status, session]);
return (
<div className="profile-table">
@@ -7,17 +7,13 @@ import {
import { AxiosError } from "axios";
import { create } from "zustand";
const filterCategories: Record<string, string> = {
count_reviews: "count_reviews",
total_likes: "total_likes",
};
interface IProfileReportsStore extends IFetch {
data: IMyReportsList;
getMyReports: (
filter: string,
page: number,
access_token: string
access_token: string,
status: number
) => void;
}
@@ -34,7 +30,8 @@ export const useProfileReportsStore = create<IProfileReportsStore>(
getMyReports: async (
sort: string,
page: number,
access_token: string
access_token: string,
status: number
) => {
try {
const Authorization = `Bearer ${access_token}`;
@@ -44,27 +41,21 @@ export const useProfileReportsStore = create<IProfileReportsStore>(
},
};
set({ isLoading: true });
const data = await apiInstance
.get<IMyReportsList>(
`/users/reports/?page=${page}&page_size=8`,
config
)
.then((res) => res.data);
if (sort === "date") {
data.results = data.results.sort((a, b) => {
const dateA = new Date(b.created_at) as unknown as number;
const dateB = new Date(a.created_at) as unknown as number;
return dateA - dateB;
});
} else if (sort === "reviews") {
data.results = data.results.sort(
(a, b) => b.count_reviews - a.count_reviews
);
} else if (sort === "rating") {
data.results = data.results.sort(
(a, b) => b.total_likes - a.total_likes
);
let data;
if (sort === "status") {
data = await apiInstance
.get<IMyReportsList>(
`/users/reports/?page=${page}&page_size=8&status=${status}`,
config
)
.then((res) => res.data);
} else {
data = await apiInstance
.get<IMyReportsList>(
`/users/reports/?page=${page}&page_size=8&sort_by=${sort}`,
config
)
.then((res) => res.data);
}
set({ data: data });
@@ -5,15 +5,6 @@ import { IStatistics } from "@/shared/types/statistics-type";
import { AxiosError } from "axios";
import { create } from "zustand";
const filterCategories: Record<string, string> = {
broken_road_1: "broken_road_1",
hotbed_of_accidents_2: "hotbed_of_accidents_2",
local_defect_3: "local_defect_3",
repair_plans_4: "repair_plans_4",
repaired_5: "repaired_5",
local_defect_fixed_6: "local_defect_fixed_6",
};
interface IStatisticsStore extends IFetch {
data: IStatistics[];
getStatistics: (
@@ -35,16 +26,14 @@ export const useStatisticsStore = create<IStatisticsStore>((set) => ({
try {
set({ isLoading: true });
const response = await apiInstance.get<IStatistics[]>(
`/report/${endpoint}/stats`
`/report/${endpoint}/stats?sort_by=${sort}`
);
let data = response.data.filter((loc) =>
loc.name.toLowerCase().includes(query.toLowerCase())
);
const sorted = data.sort((a: any, b: any) => a[sort] - b[sort]);
set({ data: sorted });
set({ data: data });
} catch (error: unknown) {
if (error instanceof AxiosError) {
set({ error: error.message });