import { useAuthStore } from './auth.store.js' export function getInventaireImagePathOrDefault(path) { return getImagePathOrGivenDefault(path, '../../image/defaultinventairebook.png') } export function getImagePathOrDefault(path) { return getImagePathOrGivenDefault(path, '../image/defaultbook.png') } export function getImagePathOrGivenDefault(path, defaultpath) { if (path == '' || typeof path === 'undefined') { return defaultpath } else if (path.startsWith('https://')) { return path } else { return path } } function useFetch(data, error, url) { const { user } = useAuthStore() if (user != null) { fetch(url, { method: 'GET', headers: { Authorization: 'Bearer ' + user.token, }, }) .then((res) => { if (res.status === 401) { const authStore = useAuthStore() authStore.logout() } return res.json() }) .then((json) => (data.value = json)) .catch((err) => (error.value = err)) } } export async function getAppInfo(appInfo, appInfoErr) { return fetch('/ws/appinfo', { method: 'GET', }) .then((res) => res.json()) .then((json) => (appInfo.value = json)) .catch((err) => (appInfoErr.value = err)) } export function getMyBooks(data, error, arg, limit, offset) { const queryParams = new URLSearchParams({ limit: limit, offset: offset }) return useFetch(data, error, '/ws/mybooks/' + arg + '?' + queryParams.toString()) } export function getSearchBooks(data, error, searchterm, lang, searchInventaire, limit, offset) { const queryParams = new URLSearchParams({ lang: lang, inventaire: searchInventaire, limit: limit, offset: offset, }) return useFetch( data, error, '/ws/search/' + encodeURIComponent(searchterm) + '?' + queryParams.toString(), ) } export function getInventaireEditionBooks(data, error, inventaireId, lang, limit, offset) { const queryParams = new URLSearchParams({ lang: lang, limit: limit, offset: offset }) return useFetch( data, error, '/ws/inventaire/books/' + encodeURIComponent(inventaireId) + '?' + queryParams.toString(), ) } export function getAuthor(data, error, id) { return useFetch(data, error, '/ws/author/' + id) } export function getAuthorBooks(data, error, id, limit, offset) { const queryParams = new URLSearchParams({ limit: limit, offset: offset }) return useFetch(data, error, '/ws/author/' + id + '/books' + '?' + queryParams.toString()) } export function getBook(data, error, id) { return useFetch(data, error, '/ws/book/' + id) } export function postBook(book) { return genericPayloadCall('/ws/book', book.value, 'POST') } export async function postImportBook(id, language) { return genericPayloadCall('/ws/importbook', { inventaireid: id, lang: language }, 'POST') } export async function putReadBook(bookId) { return genericPayloadCall('/ws/book/' + bookId, { read: true }, 'PUT') } export async function putUnreadBook(bookId) { return genericPayloadCall('/ws/book/' + bookId, { read: false }, 'PUT') } export async function putEndReadDate(bookId, enddate) { return genericPayloadCall('/ws/book/' + bookId, { read: true, endDate: enddate }, 'PUT') } export async function putEndReadDateUnset(bookId) { return genericPayloadCall('/ws/book/' + bookId, { read: true, endDate: 'null' }, 'PUT') } export async function putStartReadDateUnset(bookId) { return genericPayloadCall('/ws/book/' + bookId, { startDate: 'null' }, 'PUT') } export async function putStartReadDate(bookId, startdate) { return genericPayloadCall('/ws/book/' + bookId, { startDate: startdate }, 'PUT') } export async function putUpdateBook(bookId, payload) { return genericPayloadCall('/ws/book/' + bookId, payload, 'PUT') } export function postLogin(user) { return genericPostCallNoAuth('/ws/auth/login', user.value) } export function postSignUp(user) { return genericPostCallNoAuth('/ws/auth/signup', user.value) } export function postImage(file) { const { user } = useAuthStore() const formData = new FormData() formData.append('file', file) if (user != null) { return fetch('/ws/upload/cover', { method: 'POST', headers: { Authorization: 'Bearer ' + user.token, }, body: formData, }) } else { return Promise.resolve() } } export function genericPostCallNoAuth(apiRoute, object) { return fetch(apiRoute, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(object), }) } export function genericPayloadCall(apiRoute, object, method) { const { user } = useAuthStore() if (user != null) { return fetch(apiRoute, { method: method, headers: { 'Content-Type': 'application/json', Authorization: 'Bearer ' + user.token, }, body: JSON.stringify(object), }) } else { return Promise.resolve() } } export function extractFormErrorFromField(fieldName, errors) { if (errors === null) { return '' } if (errors.value == null) { return '' } console.log(errors.value) const titleErr = errors.find((e) => e['field'] === fieldName) if (typeof titleErr !== 'undefined') { return titleErr.error } else { return '' } } export function extractGlobalFormError(errors) { if (errors !== null && 'error' in errors) { return errors['error'] } else { return '' } }