Public Access
48 lines
1.9 KiB
JavaScript
48 lines
1.9 KiB
JavaScript
export const GAME_STATUSES = ['BACKLOG', 'PLAYING', 'FINISHED', 'COMPLETED', 'DROPPED'];
|
|
export const GAME_PRIORITIES = ['HIGH', 'MEDIUM', 'LOW'];
|
|
|
|
// Radix Select has no empty value, so "no priority" needs its own value in the form.
|
|
export const NO_PRIORITY = 'NONE';
|
|
|
|
export const parseStatus = (value) => (GAME_STATUSES.includes(value) ? value : null);
|
|
|
|
export const toBacklogRequest = ({status, rating, notes, priority}) => ({
|
|
status,
|
|
rating: rating || null,
|
|
notes: notes?.trim() ? notes : null,
|
|
priority: priority === NO_PRIORITY ? null : priority,
|
|
});
|
|
|
|
/**
|
|
* PUT /api/library/games/{appid}/backlog. The caller handles the response status.
|
|
*
|
|
* @param entry a backlog entry in request form, see toBacklogRequest
|
|
*/
|
|
export const saveBacklogEntry = (appid, entry) =>
|
|
fetch(`${import.meta.env.VITE_CORE_API_URL}/api/library/games/${appid}/backlog`, {
|
|
method: 'PUT',
|
|
headers: {'Authorization': `Bearer ${localStorage.getItem('tbp_jwt')}`, 'Content-Type': 'application/json'},
|
|
body: JSON.stringify(entry),
|
|
});
|
|
|
|
/**
|
|
* PUT /api/library/games/{appid}/status. The caller handles the response status.
|
|
*/
|
|
export const saveGameStatus = (appid, status) =>
|
|
fetch(`${import.meta.env.VITE_CORE_API_URL}/api/library/games/${appid}/status`, {
|
|
method: 'PUT',
|
|
headers: {'Authorization': `Bearer ${localStorage.getItem('tbp_jwt')}`, 'Content-Type': 'application/json'},
|
|
body: JSON.stringify({status}),
|
|
});
|
|
|
|
export const libraryQuery = ({page, size, sort, status, q, maxMinutes}) => {
|
|
const params = new URLSearchParams({page: String(page), size: String(size), sort});
|
|
if (status) params.set('status', status);
|
|
if (q?.trim()) params.set('q', q.trim());
|
|
if (maxMinutes) params.set('maxMinutes', String(maxMinutes));
|
|
return params.toString();
|
|
};
|
|
|
|
export const backlogShare = (summary) =>
|
|
summary?.total ? Math.round(((summary.byStatus?.BACKLOG ?? 0) / summary.total) * 100) : 0;
|