Вернул воронку отмены
This commit is contained in:
parent
4e58236d8f
commit
ccc73506b2
@ -5,9 +5,9 @@ import { useTranslations } from "next-intl";
|
|||||||
|
|
||||||
import { Skeleton } from "@/components/ui";
|
import { Skeleton } from "@/components/ui";
|
||||||
import {
|
import {
|
||||||
getMyChatSettings,
|
fetchMyChatSettings,
|
||||||
updateMyChatSettings,
|
updateChatSettings,
|
||||||
} from "@/entities/chats/chatSettings.api";
|
} from "@/entities/chats/actions";
|
||||||
import type { IChatSettings } from "@/entities/chats/types";
|
import type { IChatSettings } from "@/entities/chats/types";
|
||||||
|
|
||||||
import styles from "./AutoTopUpToggle.module.scss";
|
import styles from "./AutoTopUpToggle.module.scss";
|
||||||
@ -22,9 +22,9 @@ export default function AutoTopUpToggle() {
|
|||||||
let mounted = true;
|
let mounted = true;
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
const res = await getMyChatSettings();
|
const res = await fetchMyChatSettings();
|
||||||
if (mounted) {
|
if (mounted && res.data) {
|
||||||
setSettings(res.settings);
|
setSettings(res.data.settings);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// silent failure
|
// silent failure
|
||||||
@ -45,8 +45,10 @@ export default function AutoTopUpToggle() {
|
|||||||
setSettings(next); // optimistic
|
setSettings(next); // optimistic
|
||||||
setIsUpdating(true);
|
setIsUpdating(true);
|
||||||
try {
|
try {
|
||||||
const res = await updateMyChatSettings(next);
|
const res = await updateChatSettings(next);
|
||||||
setSettings(res.settings);
|
if (res.data) {
|
||||||
|
setSettings(res.data.settings);
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// revert on error silently
|
// revert on error silently
|
||||||
setSettings(settings);
|
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 { http } from "@/shared/api/httpClient";
|
||||||
|
|
||||||
import { getClientAccessToken } from "@/shared/auth/clientToken";
|
|
||||||
import { API_ROUTES } from "@/shared/constants/api-routes";
|
import { API_ROUTES } from "@/shared/constants/api-routes";
|
||||||
|
|
||||||
import { IUserBalanceResponse, UserBalanceSchema } from "./types";
|
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
|
* @returns Promise with user balance information
|
||||||
*/
|
*/
|
||||||
export const getUserBalance = async (): Promise<IUserBalanceResponse> => {
|
export const getUserBalance = async (): Promise<IUserBalanceResponse> => {
|
||||||
const accessToken = getClientAccessToken();
|
return http.get<IUserBalanceResponse>(API_ROUTES.getUserBalance(), {
|
||||||
if (!accessToken) {
|
tags: ["balance"],
|
||||||
throw new Error("No access token available");
|
schema: UserBalanceSchema,
|
||||||
}
|
revalidate: 0,
|
||||||
|
|
||||||
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",
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
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 { API_ROUTES } from "@/shared/constants/api-routes";
|
||||||
import { ActionResponse } from "@/types";
|
import { ActionResponse } from "@/types";
|
||||||
|
|
||||||
|
import { getMyChatSettings, updateMyChatSettings } from "./chatSettings.api";
|
||||||
import {
|
import {
|
||||||
CreateAllChatsResponseSchema,
|
CreateAllChatsResponseSchema,
|
||||||
GetChatMessagesResponseSchema,
|
GetChatMessagesResponseSchema,
|
||||||
|
IChatSettings,
|
||||||
ICreateAllChatsResponse,
|
ICreateAllChatsResponse,
|
||||||
IGetChatMessagesResponse,
|
IGetChatMessagesResponse,
|
||||||
|
IGetMyChatSettingsResponse,
|
||||||
|
IUpdateMyChatSettingsResponse,
|
||||||
} from "./types";
|
} from "./types";
|
||||||
|
|
||||||
export async function createAllChats(): Promise<
|
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() {
|
export async function revalidateChatsPage() {
|
||||||
revalidateTag("chats-list");
|
revalidateTag("chats-list");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,3 @@
|
|||||||
"use client";
|
|
||||||
|
|
||||||
import { http } from "@/shared/api/httpClient";
|
import { http } from "@/shared/api/httpClient";
|
||||||
import { API_ROUTES } from "@/shared/constants/api-routes";
|
import { API_ROUTES } from "@/shared/constants/api-routes";
|
||||||
|
|
||||||
@ -12,7 +10,7 @@ import {
|
|||||||
} from "./types";
|
} 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 =
|
export const getMyChatSettings =
|
||||||
async (): Promise<IGetMyChatSettingsResponse> => {
|
async (): Promise<IGetMyChatSettingsResponse> => {
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { useCallback, useEffect, useState } from "react";
|
import { useCallback, useEffect, useState } from "react";
|
||||||
|
|
||||||
import { getUserBalance } from "@/entities/balance/api";
|
import { fetchUserBalance } from "@/entities/balance/actions";
|
||||||
|
|
||||||
export const useUserBalance = () => {
|
export const useUserBalance = () => {
|
||||||
const [balance, setBalance] = useState<number | null>(null);
|
const [balance, setBalance] = useState<number | null>(null);
|
||||||
@ -13,17 +13,12 @@ export const useUserBalance = () => {
|
|||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
try {
|
try {
|
||||||
const response = await getUserBalance();
|
const response = await fetchUserBalance();
|
||||||
setBalance(response.balance);
|
setBalance(response.balance);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setError(
|
setError(
|
||||||
err instanceof Error ? err : new Error("Failed to fetch balance")
|
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 {
|
} finally {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -91,7 +91,13 @@ class HttpClient {
|
|||||||
// ignore
|
// 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");
|
headers.set("Content-Type", "application/json");
|
||||||
|
|
||||||
const res = await fetch(fullUrl, {
|
const res = await fetch(fullUrl, {
|
||||||
|
|||||||
@ -8,10 +8,25 @@ export async function getServerAccessToken() {
|
|||||||
export function getClientAccessToken(): string | undefined {
|
export function getClientAccessToken(): string | undefined {
|
||||||
if (typeof window === "undefined") return undefined;
|
if (typeof window === "undefined") return undefined;
|
||||||
|
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.log("🍪 Debug cookies:", document.cookie);
|
||||||
|
|
||||||
const cookies = document.cookie.split(";");
|
const cookies = document.cookie.split(";");
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.log("🍪 All cookies:", cookies);
|
||||||
|
|
||||||
const accessTokenCookie = cookies.find(cookie =>
|
const accessTokenCookie = cookies.find(cookie =>
|
||||||
cookie.trim().startsWith("accessToken=")
|
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