mirror of
https://github.com/Radiquum/AniX.git
synced 2025-04-29 01:19:41 +05:00
feat: add history api and page
This commit is contained in:
parent
e73b214045
commit
eb620c0b1d
5 changed files with 186 additions and 22 deletions
19
app/api/history/route.js
Normal file
19
app/api/history/route.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
import { NextResponse } from "next/server";
|
||||
import { fetchDataViaGet } from "../utils";
|
||||
import { ENDPOINTS } from "../config";
|
||||
|
||||
export async function GET(request) {
|
||||
const page = parseInt(request.nextUrl.searchParams.get(["page"])) || 0;
|
||||
const token = request.nextUrl.searchParams.get(["token"]) || null;
|
||||
const sortName = request.nextUrl.searchParams.get(["sort"]) || "adding_descending";
|
||||
|
||||
if (!token || token == "null") {
|
||||
return NextResponse.json({ message: "No token provided" }, { status: 403 });
|
||||
}
|
||||
|
||||
let url = new URL(`${ENDPOINTS.user.history}/${page}`);
|
||||
url.searchParams.set("token", token);
|
||||
|
||||
const response = await fetchDataViaGet(url.toString());
|
||||
return NextResponse.json(response);
|
||||
}
|
|
@ -44,14 +44,17 @@ export const fetchDataViaPost = async (url, body, API_V2) => {
|
|||
|
||||
export const authorize = async (url, data) => {
|
||||
try {
|
||||
const response = await fetch(`${url}?login=${data.login}&password=${data.password}`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"User-Agent": USER_AGENT,
|
||||
Sign: "9aa5c7af74e8cd70c86f7f9587bde23d",
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
});
|
||||
const response = await fetch(
|
||||
`${url}?login=${data.login}&password=${data.password}`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"User-Agent": USER_AGENT,
|
||||
Sign: "9aa5c7af74e8cd70c86f7f9587bde23d",
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
}
|
||||
);
|
||||
if (response.status !== 200) {
|
||||
throw new Error("Error authorizing user");
|
||||
}
|
||||
|
@ -74,9 +77,34 @@ export function removeJWT() {
|
|||
}
|
||||
|
||||
export function numberDeclension(number, one, two, five) {
|
||||
if (number > 10 && [11, 12, 13, 14].includes(number%100)) return five;
|
||||
let last_num = number%10;
|
||||
if (number > 10 && [11, 12, 13, 14].includes(number % 100)) return five;
|
||||
let last_num = number % 10;
|
||||
if (last_num == 1) return one;
|
||||
if ([2,3,4].includes(last_num)) return two;
|
||||
if ([5,6,7,8,9, 0].includes(last_num)) return five;
|
||||
if ([2, 3, 4].includes(last_num)) return two;
|
||||
if ([5, 6, 7, 8, 9, 0].includes(last_num)) return five;
|
||||
}
|
||||
|
||||
export function sinceUnixDate(unixInSeconds) {
|
||||
const unix = Math.floor(unixInSeconds * 1000);
|
||||
const date = new Date(unix);
|
||||
const currentDate = new Date().valueOf();
|
||||
const dateDifferenceSeconds = new Date(currentDate - unix) / 1000;
|
||||
|
||||
const minutes = Math.floor(dateDifferenceSeconds / 60)
|
||||
const hours = Math.floor(dateDifferenceSeconds / 3600);
|
||||
const days = Math.floor(dateDifferenceSeconds / 86400);
|
||||
|
||||
const minutesName = numberDeclension(minutes, "минута", "минуты", "минут");
|
||||
const hoursName = numberDeclension(hours, "час", "часа", "часов");
|
||||
const daysName = numberDeclension(days, "день", "дня", "дней");
|
||||
|
||||
if (dateDifferenceSeconds < 60) return "менее минуты назад";
|
||||
if (dateDifferenceSeconds < 3600)
|
||||
return `${minutes} ${minutesName} назад`;
|
||||
if (dateDifferenceSeconds < 86400)
|
||||
return `${hours} ${hoursName} назад`;
|
||||
if (dateDifferenceSeconds < 2592000)
|
||||
return `${days} ${daysName} назад`;
|
||||
|
||||
return date.toLocaleString("ru-RU").split(",")[0];
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue