This commit is contained in:
Iliyan Angelov
2025-10-09 00:44:15 +03:00
parent 18ae8b9f88
commit dd8eb1c7aa
44 changed files with 3240 additions and 137 deletions

View File

@@ -0,0 +1,118 @@
import { API_BASE_URL } from '../config/api';
export interface PolicySection {
id: number;
heading: string;
content: string;
order: number;
}
export interface Policy {
id: number;
type: 'privacy' | 'terms' | 'support';
title: string;
slug: string;
description: string;
last_updated: string;
version: string;
effective_date: string;
sections: PolicySection[];
}
export interface PolicyListItem {
id: number;
type: 'privacy' | 'terms' | 'support';
title: string;
slug: string;
description: string;
last_updated: string;
version: string;
}
class PolicyServiceAPI {
private baseUrl = `${API_BASE_URL}/api/policies`;
/**
* Get all policies
*/
async getPolicies(): Promise<PolicyListItem[]> {
try {
const response = await fetch(`${this.baseUrl}/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data.results || data;
} catch (error) {
console.error('Error fetching policies:', error);
throw error;
}
}
/**
* Get a specific policy by type
*/
async getPolicyByType(type: 'privacy' | 'terms' | 'support'): Promise<Policy> {
try {
const response = await fetch(`${this.baseUrl}/${type}/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.error(`Error fetching policy ${type}:`, error);
throw error;
}
}
/**
* Get a specific policy by ID
*/
async getPolicyById(id: number): Promise<Policy> {
try {
const response = await fetch(`${this.baseUrl}/${id}/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.error(`Error fetching policy ${id}:`, error);
throw error;
}
}
}
// Export a singleton instance
export const policyService = new PolicyServiceAPI();
// Export individual functions for convenience
export const getPolicies = () => policyService.getPolicies();
export const getPolicyByType = (type: 'privacy' | 'terms' | 'support') => policyService.getPolicyByType(type);
export const getPolicyById = (id: number) => policyService.getPolicyById(id);
export default policyService;

View File

@@ -0,0 +1,131 @@
"use client";
import { useState, useEffect } from 'react';
import { Policy, PolicyListItem, getPolicies, getPolicyByType, getPolicyById } from '../api/policyService';
interface UsePoliciesReturn {
data: PolicyListItem[] | null;
loading: boolean;
error: string | null;
refetch: () => Promise<void>;
}
interface UsePolicyReturn {
data: Policy | null;
isLoading: boolean;
error: Error | null;
refetch: () => Promise<void>;
}
/**
* Hook to fetch all policies
*/
export const usePolicies = (): UsePoliciesReturn => {
const [data, setData] = useState<PolicyListItem[] | null>(null);
const [loading, setLoading] = useState<boolean>(true);
const [error, setError] = useState<string | null>(null);
const fetchData = async () => {
try {
setLoading(true);
setError(null);
const result = await getPolicies();
setData(result);
} catch (err) {
setError(err instanceof Error ? err.message : 'An error occurred');
console.error('Error fetching policies:', err);
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchData();
}, []);
return {
data,
loading,
error,
refetch: fetchData,
};
};
/**
* Hook to fetch a policy by type
*/
export const usePolicy = (type: 'privacy' | 'terms' | 'support' | null): UsePolicyReturn => {
const [data, setData] = useState<Policy | null>(null);
const [isLoading, setIsLoading] = useState<boolean>(true);
const [error, setError] = useState<Error | null>(null);
const fetchData = async () => {
if (!type) {
setIsLoading(false);
return;
}
try {
setIsLoading(true);
setError(null);
const result = await getPolicyByType(type);
setData(result);
} catch (err) {
setError(err instanceof Error ? err : new Error('An error occurred'));
console.error('Error fetching policy:', err);
} finally {
setIsLoading(false);
}
};
useEffect(() => {
fetchData();
}, [type]);
return {
data,
isLoading,
error,
refetch: fetchData,
};
};
/**
* Hook to fetch a policy by ID
*/
export const usePolicyById = (id: number | null): UsePolicyReturn => {
const [data, setData] = useState<Policy | null>(null);
const [isLoading, setIsLoading] = useState<boolean>(true);
const [error, setError] = useState<Error | null>(null);
const fetchData = async () => {
if (!id) {
setIsLoading(false);
return;
}
try {
setIsLoading(true);
setError(null);
const result = await getPolicyById(id);
setData(result);
} catch (err) {
setError(err instanceof Error ? err : new Error('An error occurred'));
console.error('Error fetching policy:', err);
} finally {
setIsLoading(false);
}
};
useEffect(() => {
fetchData();
}, [id]);
return {
data,
isLoading,
error,
refetch: fetchData,
};
};