Вернул воронку отмены
This commit is contained in:
parent
4e58236d8f
commit
ccc73506b2
@ -5,9 +5,9 @@ import { useTranslations } from "next-intl";
|
||||
|
||||
import { Skeleton } from "@/components/ui";
|
||||
import {
|
||||
getMyChatSettings,
|
||||
updateMyChatSettings,
|
||||
} from "@/entities/chats/chatSettings.api";
|
||||
fetchMyChatSettings,
|
||||
updateChatSettings,
|
||||
} from "@/entities/chats/actions";
|
||||
import type { IChatSettings } from "@/entities/chats/types";
|
||||
|
||||
import styles from "./AutoTopUpToggle.module.scss";
|
||||
@ -22,9 +22,9 @@ export default function AutoTopUpToggle() {
|
||||
let mounted = true;
|
||||
(async () => {
|
||||
try {
|
||||
const res = await getMyChatSettings();
|
||||
if (mounted) {
|
||||
setSettings(res.settings);
|
||||
const res = await fetchMyChatSettings();
|
||||
if (mounted && res.data) {
|
||||
setSettings(res.data.settings);
|
||||
}
|
||||
} catch (e) {
|
||||
// silent failure
|
||||
@ -45,8 +45,10 @@ export default function AutoTopUpToggle() {
|
||||
setSettings(next); // optimistic
|
||||
setIsUpdating(true);
|
||||
try {
|
||||
const res = await updateMyChatSettings(next);
|
||||
setSettings(res.settings);
|
||||
const res = await updateChatSettings(next);
|
||||
if (res.data) {
|
||||
setSettings(res.data.settings);
|
||||
}
|
||||
} catch (e) {
|
||||
// revert on error silently
|
||||
setSettings(settings);
|
||||
|
||||
7
src/entities/balance/actions.ts
Normal file
7
src/entities/balance/actions.ts
Normal file
@ -0,0 +1,7 @@
|
||||
"use server";
|
||||
|
||||
import { getUserBalance } from "./api";
|
||||
|
||||
export async function fetchUserBalance() {
|
||||
return getUserBalance();
|
||||
}
|
||||
@ -1,39 +1,16 @@
|
||||
"use client";
|
||||
|
||||
import { getClientAccessToken } from "@/shared/auth/clientToken";
|
||||
import { http } from "@/shared/api/httpClient";
|
||||
import { API_ROUTES } from "@/shared/constants/api-routes";
|
||||
|
||||
import { IUserBalanceResponse, UserBalanceSchema } from "./types";
|
||||
|
||||
/**
|
||||
* Fetches the current user balance using client-side authentication
|
||||
* Fetches the current user balance (server-side with httpOnly cookie access)
|
||||
* @returns Promise with user balance information
|
||||
*/
|
||||
export const getUserBalance = async (): Promise<IUserBalanceResponse> => {
|
||||
const accessToken = getClientAccessToken();
|
||||
if (!accessToken) {
|
||||
throw new Error("No access token available");
|
||||
}
|
||||
|
||||
const apiUrl = process.env.NEXT_PUBLIC_API_URL;
|
||||
if (!apiUrl) {
|
||||
throw new Error("API URL not configured");
|
||||
}
|
||||
|
||||
const url = new URL(API_ROUTES.getUserBalance(), apiUrl);
|
||||
|
||||
const response = await fetch(url.toString(), {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
return http.get<IUserBalanceResponse>(API_ROUTES.getUserBalance(), {
|
||||
tags: ["balance"],
|
||||
schema: UserBalanceSchema,
|
||||
revalidate: 0,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch balance: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return UserBalanceSchema.parse(data);
|
||||
};
|
||||
|
||||
@ -6,11 +6,15 @@ import { http } from "@/shared/api/httpClient";
|
||||
import { API_ROUTES } from "@/shared/constants/api-routes";
|
||||
import { ActionResponse } from "@/types";
|
||||
|
||||
import { getMyChatSettings, updateMyChatSettings } from "./chatSettings.api";
|
||||
import {
|
||||
CreateAllChatsResponseSchema,
|
||||
GetChatMessagesResponseSchema,
|
||||
IChatSettings,
|
||||
ICreateAllChatsResponse,
|
||||
IGetChatMessagesResponse,
|
||||
IGetMyChatSettingsResponse,
|
||||
IUpdateMyChatSettingsResponse,
|
||||
} from "./types";
|
||||
|
||||
export async function createAllChats(): Promise<
|
||||
@ -60,6 +64,38 @@ export async function fetchChatMessages(
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchMyChatSettings(): Promise<
|
||||
ActionResponse<IGetMyChatSettingsResponse>
|
||||
> {
|
||||
try {
|
||||
const response = await getMyChatSettings();
|
||||
return { data: response, error: null };
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error("Failed to fetch chat settings:", error);
|
||||
const errorMessage =
|
||||
error instanceof Error ? error.message : "Something went wrong.";
|
||||
return { data: null, error: errorMessage };
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateChatSettings(
|
||||
settings: IChatSettings
|
||||
): Promise<ActionResponse<IUpdateMyChatSettingsResponse>> {
|
||||
try {
|
||||
const response = await updateMyChatSettings(settings);
|
||||
revalidateTag("profile");
|
||||
revalidateTag("chat-settings");
|
||||
return { data: response, error: null };
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error("Failed to update chat settings:", error);
|
||||
const errorMessage =
|
||||
error instanceof Error ? error.message : "Something went wrong.";
|
||||
return { data: null, error: errorMessage };
|
||||
}
|
||||
}
|
||||
|
||||
export async function revalidateChatsPage() {
|
||||
revalidateTag("chats-list");
|
||||
}
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
"use client";
|
||||
|
||||
import { http } from "@/shared/api/httpClient";
|
||||
import { API_ROUTES } from "@/shared/constants/api-routes";
|
||||
|
||||
@ -12,7 +10,7 @@ import {
|
||||
} from "./types";
|
||||
|
||||
/**
|
||||
* Fetch current user's chat settings (client-side)
|
||||
* Fetch current user's chat settings (server-side with httpOnly cookie access)
|
||||
*/
|
||||
export const getMyChatSettings =
|
||||
async (): Promise<IGetMyChatSettingsResponse> => {
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
|
||||
import { getUserBalance } from "@/entities/balance/api";
|
||||
import { fetchUserBalance } from "@/entities/balance/actions";
|
||||
|
||||
export const useUserBalance = () => {
|
||||
const [balance, setBalance] = useState<number | null>(null);
|
||||
@ -13,17 +13,12 @@ export const useUserBalance = () => {
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const response = await getUserBalance();
|
||||
const response = await fetchUserBalance();
|
||||
setBalance(response.balance);
|
||||
} catch (err) {
|
||||
setError(
|
||||
err instanceof Error ? err : new Error("Failed to fetch balance")
|
||||
);
|
||||
// Используем devLogger или другой механизм логирования в продакшене
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error("Error fetching user balance:", err);
|
||||
}
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
|
||||
@ -91,7 +91,13 @@ class HttpClient {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
if (accessToken) headers.set("Authorization", `Bearer ${accessToken}`);
|
||||
if (accessToken) {
|
||||
headers.set("Authorization", `Bearer ${accessToken}`);
|
||||
|
||||
console.log("🔑 Token being sent:", accessToken.substring(0, 20) + "...");
|
||||
} else {
|
||||
console.log("❌ No access token found!");
|
||||
}
|
||||
headers.set("Content-Type", "application/json");
|
||||
|
||||
const res = await fetch(fullUrl, {
|
||||
|
||||
@ -8,10 +8,25 @@ export async function getServerAccessToken() {
|
||||
export function getClientAccessToken(): string | undefined {
|
||||
if (typeof window === "undefined") return undefined;
|
||||
|
||||
// eslint-disable-next-line no-console
|
||||
console.log("🍪 Debug cookies:", document.cookie);
|
||||
|
||||
const cookies = document.cookie.split(";");
|
||||
// eslint-disable-next-line no-console
|
||||
console.log("🍪 All cookies:", cookies);
|
||||
|
||||
const accessTokenCookie = cookies.find(cookie =>
|
||||
cookie.trim().startsWith("accessToken=")
|
||||
);
|
||||
|
||||
return accessTokenCookie?.split("=")[1];
|
||||
// eslint-disable-next-line no-console
|
||||
console.log("🍪 Found accessToken cookie:", accessTokenCookie);
|
||||
|
||||
if (!accessTokenCookie) return undefined;
|
||||
|
||||
// Use substring instead of split to handle tokens with = signs
|
||||
const token = accessTokenCookie.trim().substring("accessToken=".length);
|
||||
// eslint-disable-next-line no-console
|
||||
console.log("🍪 Extracted token:", token.substring(0, 20) + "...");
|
||||
return token;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user