@@ -0,0 +1,101 @@
|
||||
import type { VoteOption, VoteResult } from '../types';
|
||||
|
||||
const API_BASE = '/api';
|
||||
|
||||
export const api = {
|
||||
getVoteLocked: async (): Promise<boolean> => {
|
||||
const res = await fetch(`${API_BASE}/vote/locked`);
|
||||
if (!res.ok) throw new Error('Failed to fetch vote lock state');
|
||||
return res.json();
|
||||
},
|
||||
|
||||
getResultLocked: async (): Promise<boolean> => {
|
||||
const res = await fetch(`${API_BASE}/result/locked`);
|
||||
if (!res.ok) throw new Error('Failed to fetch result lock state');
|
||||
return res.json();
|
||||
},
|
||||
|
||||
getVoteOptions: async (): Promise<VoteOption[]> => {
|
||||
const res = await fetch(`${API_BASE}/vote`);
|
||||
if (res.status === 423) throw new Error('Locked');
|
||||
if (!res.ok) throw new Error('Failed to fetch options');
|
||||
return res.json();
|
||||
},
|
||||
|
||||
incVote: async (id: string): Promise<void> => {
|
||||
const res = await fetch(`${API_BASE}/vote/${id}/inc`, { method: 'PATCH' });
|
||||
if (!res.ok) throw new Error('Failed to submit vote');
|
||||
},
|
||||
|
||||
getResults: async (): Promise<VoteResult[]> => {
|
||||
const res = await fetch(`${API_BASE}/result`);
|
||||
if (res.status === 423) throw new Error('Locked');
|
||||
if (!res.ok) throw new Error('Failed to fetch results');
|
||||
return res.json();
|
||||
},
|
||||
|
||||
// Admin Endpoints
|
||||
setVoteLocked: async (value: boolean): Promise<void> => {
|
||||
const res = await fetch(`${API_BASE}/vote/locked`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ Value: value })
|
||||
});
|
||||
if (!res.ok) throw new Error('Failed to set lock');
|
||||
},
|
||||
|
||||
setResultLocked: async (value: boolean): Promise<void> => {
|
||||
const res = await fetch(`${API_BASE}/result/locked`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ Value: value })
|
||||
});
|
||||
if (!res.ok) throw new Error('Failed to set lock');
|
||||
},
|
||||
|
||||
addVoteEntry: async (name: string, category: string): Promise<VoteOption> => {
|
||||
const res = await fetch(`${API_BASE}/vote`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ Name: name, Category: category })
|
||||
});
|
||||
if (!res.ok) throw new Error('Failed to add entry');
|
||||
return res.json();
|
||||
},
|
||||
|
||||
deleteVoteEntry: async (id: string): Promise<void> => {
|
||||
const res = await fetch(`${API_BASE}/vote/${id}`, { method: 'DELETE' });
|
||||
if (!res.ok) throw new Error('Failed to delete entry');
|
||||
},
|
||||
|
||||
bulkDeleteVoteEntries: async (ids: string[]): Promise<void> => {
|
||||
const res = await fetch(`${API_BASE}/vote/bulk`, {
|
||||
method: 'DELETE',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(ids)
|
||||
});
|
||||
if (!res.ok) throw new Error('Failed to delete entries');
|
||||
},
|
||||
|
||||
setVoteCount: async (id: string, votes: number): Promise<void> => {
|
||||
const res = await fetch(`${API_BASE}/vote/${id}/override`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ Votes: votes })
|
||||
});
|
||||
if (!res.ok) throw new Error('Failed to override vote');
|
||||
},
|
||||
|
||||
getUser: async (): Promise<string | null> => {
|
||||
try {
|
||||
const res = await fetch(`/admin`);
|
||||
return res.headers.get('user');
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
|
||||
logout: async (): Promise<void> => {
|
||||
await fetch(`${API_BASE}/auth/logout`);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user