Initial commit

This commit is contained in:
payacom
2026-07-09 15:26:46 +03:30
parent ff2009c402
commit edb094e323
65 changed files with 4083 additions and 803 deletions

View File

@@ -0,0 +1,68 @@
import { getApiBaseUrl } from "@/components/main/BaseUrl";
export type AcademyExploreItem = {
_id: string;
courseId: string;
type: "image" | "video";
files?: { path?: string; type?: string }[];
course_video?: string | null;
course_images?: string[];
course_image?: string;
course_name?: string;
teacher_name?: string;
file_name?: string;
caption?: string;
user_id?: string;
user_name?: string;
first_name?: string;
last_name?: string;
profile_image?: string;
likesCount?: number;
createdAt?: string;
status?: string;
};
type AcademyExploreResponse = {
success?: boolean;
data?: {
items: AcademyExploreItem[];
pagination?: {
currentPage: number;
totalPages: number;
totalItems: number;
hasNextPage: boolean;
};
};
};
export async function fetchAcademyExplore(
page: number,
limit: number,
token: string
): Promise<{
items: AcademyExploreItem[];
hasNextPage: boolean;
}> {
const apiUrl = `${getApiBaseUrl()}/academy/academy/explore/free-content?page=${page}&limit=${limit}`;
const response = await fetch(apiUrl, {
cache: "no-store",
headers: {
Authorization: token ? `Bearer ${token}` : "",
},
});
if (response.status === 404) {
return { items: [], hasNextPage: false };
}
if (!response.ok) {
throw new Error(`Academy explore failed: ${response.status}`);
}
const json = (await response.json()) as AcademyExploreResponse;
const items = json.data?.items ?? [];
const hasNextPage = json.data?.pagination?.hasNextPage ?? false;
return { items, hasNextPage };
}

View File

@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { BASE_URL } from "@/components/main/BaseUrl";
import { getApiBaseUrl } from "@/components/main/BaseUrl";
export async function fetchBillboards(
page: number,
@@ -27,7 +27,7 @@ export async function fetchBillboards(
timestamp: Date.now().toString(), // اضافه کردن timestamp برای جلوگیری از کش
}).toString();
const apiUrl = `${BASE_URL}/advertising/web?${queryParams}`;
const apiUrl = `${getApiBaseUrl()}/advertising/web?${queryParams}`;
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 90_000);

View File

@@ -1,22 +1,38 @@
import { BASE_URL } from "@/components/main/BaseUrl";
import { getApiBaseUrl } from "@/components/main/BaseUrl";
import { Post } from "@/types/types";
/** Fetch a single post by id (tries dedicated endpoint, then list fallback) */
/** Fetch a single post by id */
export async function fetchPostById(
id: string,
token?: string
): Promise<Post | null> {
const base = getApiBaseUrl();
const headers: HeadersInit = token
? { Authorization: `Bearer ${token}` }
: {};
const tryUrls = [
`${BASE_URL}/posts/${id}`,
`${BASE_URL}/posts/web/${id}`,
`${BASE_URL}/posts/get/${id}`,
const primaryUrl = `${base}/posts/web/${id}`;
try {
const res = await fetch(primaryUrl, {
cache: "no-store",
headers,
});
if (res.ok) {
const data = await res.json();
const post = data?.post ?? data?.data ?? data;
if (post?._id) return post as Post;
}
} catch {
/* try fallbacks */
}
const fallbackUrls = [
`${base}/posts/${id}`,
`${base}/posts/get/${id}`,
];
for (const url of tryUrls) {
for (const url of fallbackUrls) {
try {
const res = await fetch(url, {
cache: "no-store",
@@ -31,19 +47,5 @@ export async function fetchPostById(
}
}
try {
const res = await fetch(
`${BASE_URL}/users/web?page=1&limit=50&_id=${id}`,
{ cache: "no-store", headers }
);
if (res.ok) {
const data = await res.json();
const found = (data?.posts as Post[])?.find((p) => p._id === id);
if (found) return found;
}
} catch {
/* ignore */
}
return null;
}

View File

@@ -1,4 +1,4 @@
import { BASE_URL } from "@/components/main/BaseUrl";
import { getApiBaseUrl } from "@/components/main/BaseUrl";
import { normalizeUserLevel } from "@/lib/userLevel";
export async function fetchPosts(
@@ -12,6 +12,12 @@ export async function fetchPosts(
rateFilter?: string;
_id?: string;
type?: string;
exploreFilter?: string;
subExpertise?: string;
lat?: string;
lng?: string;
feedMode?: "grid" | "reels";
seedPostId?: string;
},
token: string
) {
@@ -33,7 +39,7 @@ export async function fetchPosts(
...validFilters,
}).toString();
const apiUrl = `${BASE_URL}/users/web?${queryParams}`;
const apiUrl = `${getApiBaseUrl()}/users/web?${queryParams}`;
const response = await fetch(apiUrl, {
cache: "no-store",

View File

@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { BASE_URL } from "@/components/main/BaseUrl";
import { getApiBaseUrl } from "@/components/main/BaseUrl";
export async function fetchProjects(
page: number,
@@ -28,7 +28,7 @@ export async function fetchProjects(
timestamp: Date.now().toString(), // اضافه کردن timestamp برای جلوگیری از کش
}).toString();
const apiUrl = `${BASE_URL}/projects?${queryParams}`;
const apiUrl = `${getApiBaseUrl()}/projects?${queryParams}`;
const response = await fetch(apiUrl, {
cache: "no-store", // غیرفعال کردن کش

View File

@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { BASE_URL } from "@/components/main/BaseUrl";
import { getApiBaseUrl } from "@/components/main/BaseUrl";
export async function fetchSearch(
page: number,
@@ -25,7 +25,7 @@ export async function fetchSearch(
timestamp: Date.now().toString(), // اضافه کردن timestamp برای جلوگیری از کش
}).toString();
const apiUrl = `${BASE_URL}/search-web?${queryParams}`;
const apiUrl = `${getApiBaseUrl()}/search-web?${queryParams}`;
const response = await fetch(apiUrl, {
cache: 'no-store', // غیرفعال کردن کش

73
src/api/fetchStories.ts Normal file
View File

@@ -0,0 +1,73 @@
import { getApiBaseUrl } from "@/components/main/BaseUrl";
export type StoryItem = {
_id: string;
media_path: string;
media_type: "image" | "video";
createdAt?: string;
expires_at?: string;
viewed?: boolean;
};
export type StoryFeedUser = {
user: {
_id: string;
user_name: string;
first_name: string;
last_name: string;
profile_image?: string;
};
stories: StoryItem[];
has_unviewed: boolean;
};
export type StoriesFeedResponse = {
feed: StoryFeedUser[];
my_story: StoryFeedUser | null;
viewer_id: string | null;
};
export async function fetchStoriesFeed(
token: string
): Promise<StoriesFeedResponse> {
const res = await fetch(`${getApiBaseUrl()}/stories/feed`, {
cache: "no-store",
headers: {
Authorization: token ? `Bearer ${token}` : "",
},
});
if (!res.ok) throw new Error("Failed to load stories");
return res.json();
}
export async function markStoryViewed(
storyId: string,
token: string
): Promise<void> {
await fetch(`${getApiBaseUrl()}/stories/view`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({ storyId }),
});
}
export async function createStoryBase64(
file: { name: string; type: string; data: string },
token: string
): Promise<void> {
const res = await fetch(`${getApiBaseUrl()}/stories/create-base64`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({ file }),
});
if (!res.ok) {
const json = await res.json().catch(() => ({}));
throw new Error(json?.message || "خطا در انتشار استوری");
}
}

View File

@@ -0,0 +1,39 @@
import { getApiBaseUrl } from "@/components/main/BaseUrl";
export type ExploreInteractionPayload = {
targetType: "post" | "profile" | "academy";
targetId: string;
authorId?: string;
contentType?: "image" | "video" | "academy";
action?:
| "view"
| "profile_visit"
| "share"
| "like"
| "comment"
| "offer"
| "watch_complete"
| "skip"
| "dwell";
dwellMs?: number;
};
export async function trackExploreInteraction(
payload: ExploreInteractionPayload,
token: string
): Promise<void> {
if (!token) return;
try {
await fetch(`${getApiBaseUrl()}/users/explore/interaction`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(payload),
});
} catch {
/* non-blocking */
}
}