Merge branch 'main' of http://45.93.137.91:3000/Rahaf/SweetHome
Some checks failed
Build frontend / build (push) Failing after 1m18s

This commit is contained in:
Beilin-b
2026-06-14 08:33:02 -07:00
3 changed files with 242 additions and 1 deletions

View File

@ -454,13 +454,43 @@
import AuthService from '../services/AuthService';
const API_BASE = process.env.NEXT_PUBLIC_API_URL || 'https://45.93.137.91.nip.io/api';
const REPORT_API_BASE = process.env.NEXT_PUBLIC_REPORT_API_URL || 'http://45.93.137.91/api';
function isFormData(value) {
return typeof FormData !== 'undefined' && value instanceof FormData;
}
class ApiBlockedError extends Error {
constructor(message = 'Your account is blocked') {
super(message);
this.name = 'ApiBlockedError';
this.status = 451;
}
}
export function isApiBlockedError(error) {
return error instanceof ApiBlockedError || error?.status === 451;
}
function redirectToBlockedPage() {
if (typeof window !== 'undefined' && window.location.pathname !== '/blocked') {
window.location.replace('/blocked');
}
}
function assertNotBlocked(response) {
if (response.status === 451) {
redirectToBlockedPage();
throw new ApiBlockedError();
}
}
function buildApiUrl(base, endpoint) {
return `${base.replace(/\/$/, '')}${endpoint.startsWith('/') ? endpoint : `/${endpoint}`}`;
}
/**
* Generic API fetch — attaches auth token, unwraps { data } envelope
*/
@ -496,6 +526,7 @@ async function apiFetch(endpoint, options = {}) {
console.log('API Response Status:', res.status);
console.log('API Response OK:', res.ok);
assertNotBlocked(res);
if (!res.ok && res.status !== 206) {
const text = await res.text().catch(() => '');
@ -538,6 +569,36 @@ async function authFetch(endpoint, body, token = null) {
body: bodyIsFormData ? body : JSON.stringify(body),
});
assertNotBlocked(res);
const text = await res.text();
let data = null;
try {
data = text ? JSON.parse(text) : null;
if (data && typeof data === 'object' && 'data' in data) {
data = data.data;
}
} catch {
data = text;
}
const message = typeof data === 'object' && data?.message ? data.message : null;
return { status: res.status, data, ok: res.ok || res.status === 206, message };
}
async function reportFetch(endpoint, body) {
const res = await fetch(buildApiUrl(REPORT_API_BASE, endpoint), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
});
assertNotBlocked(res);
const text = await res.text();
let data = null;
@ -735,6 +796,8 @@ export async function uploadPicture(file) {
body: formData,
});
assertNotBlocked(res);
const text = await res.text();
if (!res.ok) throw new Error(`Upload failed: ${res.status} ${text}`);
@ -760,6 +823,8 @@ async function multipartAuthFetch(endpoint, formData) {
body: formData,
});
assertNotBlocked(res);
const text = await res.text();
let data = null;
@ -962,6 +1027,8 @@ export async function registerRealEstateAgent(formData) {
body: formData,
});
assertNotBlocked(res);
const text = await res.text();
let data = null;
@ -1042,6 +1109,13 @@ export async function filterRentProperties(params = {}) {
// ─── Reports ───
export async function sendGeneralReport(subject, reportBody) {
return reportFetch('/Reports/SendGeneralReport', {
subject,
body: reportBody,
});
}
export async function submitReport(subject, body) {
return apiFetch('/Reports/SendGeneralReport', {
method: 'POST',