2025-11-25 17:13:16 +07:00
|
|
|
'use server'
|
2025-12-02 17:11:53 +07:00
|
|
|
import { getSessionData } from '@/app/lib/session';
|
2025-11-25 17:13:16 +07:00
|
|
|
|
2025-12-02 17:46:12 +07:00
|
|
|
const API_BASE_URL = process.env.NODE_ENV === 'development'
|
|
|
|
|
? 'http://localhost'
|
|
|
|
|
: '';
|
|
|
|
|
|
2025-11-26 14:01:35 +07:00
|
|
|
const FRUITS_URL = 'https://www.fruityvice.com/api/fruit/';
|
2025-12-02 17:46:12 +07:00
|
|
|
const ALL = 'all';
|
2025-11-26 14:01:35 +07:00
|
|
|
|
|
|
|
|
export async function fetchFruits() {
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch(FRUITS_URL + ALL, { 'method': 'GET' });
|
|
|
|
|
let parsed = await response.json();
|
|
|
|
|
return parsed;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error:', error);
|
|
|
|
|
throw new Error('Failed to fetch fruits data.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-29 14:40:26 +07:00
|
|
|
export async function fetchFruit(id: number) {
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch(FRUITS_URL + id, { 'method': 'GET' });
|
|
|
|
|
let parsed = await response.json();
|
|
|
|
|
return parsed;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error:', error);
|
|
|
|
|
throw new Error('Failed to fetch fruits data.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-02 17:11:53 +07:00
|
|
|
export async function getUserData() {
|
|
|
|
|
const userEmail = await getSessionData('email');
|
|
|
|
|
const token = await getSessionData('token');
|
|
|
|
|
|
2025-11-25 17:13:16 +07:00
|
|
|
try {
|
2025-12-02 17:46:12 +07:00
|
|
|
const response = await fetch(`${API_BASE_URL}/api/user?email=${userEmail}`, {
|
2025-12-02 17:11:53 +07:00
|
|
|
method: 'GET',
|
2025-11-25 17:13:16 +07:00
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
2025-12-02 17:11:53 +07:00
|
|
|
"Accept": "application/json",
|
|
|
|
|
"Authorization": `Bearer ${token}`,
|
2025-11-25 17:13:16 +07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-12-02 17:11:53 +07:00
|
|
|
if (response.ok) {
|
|
|
|
|
return await response.json();
|
|
|
|
|
}
|
2025-11-25 17:13:16 +07:00
|
|
|
} catch (error) {
|
2025-12-02 17:11:53 +07:00
|
|
|
console.error('error');
|
2025-11-25 17:13:16 +07:00
|
|
|
}
|
|
|
|
|
}
|