Add prettier dependency to format frontend code
This commit is contained in:
133
front/src/api.js
133
front/src/api.js
@@ -1,79 +1,93 @@
|
||||
import { useAuthStore } from './auth.store.js'
|
||||
|
||||
export function getInventaireImagePathOrDefault(path) {
|
||||
return getImagePathOrGivenDefault(path, "../../image/defaultinventairebook.png")
|
||||
return getImagePathOrGivenDefault(path, '../../image/defaultinventairebook.png')
|
||||
}
|
||||
|
||||
export function getImagePathOrDefault(path) {
|
||||
return getImagePathOrGivenDefault(path, "../image/defaultbook.png")
|
||||
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;
|
||||
if (path == '' || typeof path === 'undefined') {
|
||||
return defaultpath
|
||||
} else if (path.startsWith('https://')) {
|
||||
return path
|
||||
} else {
|
||||
return path;
|
||||
return path
|
||||
}
|
||||
}
|
||||
|
||||
function useFetch(data, error, url) {
|
||||
const { user } = useAuthStore();
|
||||
const { user } = useAuthStore()
|
||||
|
||||
if (user != null) {
|
||||
fetch(url, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + user.token
|
||||
}
|
||||
Authorization: 'Bearer ' + user.token,
|
||||
},
|
||||
})
|
||||
.then((res) => {
|
||||
if (res.status === 401) {
|
||||
const authStore = useAuthStore();
|
||||
authStore.logout();
|
||||
const authStore = useAuthStore()
|
||||
authStore.logout()
|
||||
}
|
||||
return res.json();
|
||||
return res.json()
|
||||
})
|
||||
.then((json) => (data.value = json))
|
||||
.catch((err) => (error.value = err));
|
||||
.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))
|
||||
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());
|
||||
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());
|
||||
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());
|
||||
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);
|
||||
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());
|
||||
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);
|
||||
return useFetch(data, error, '/ws/book/' + id)
|
||||
}
|
||||
|
||||
export function postBook(book) {
|
||||
@@ -81,39 +95,39 @@ export function postBook(book) {
|
||||
}
|
||||
|
||||
export async function postImportBook(id, language) {
|
||||
return genericPayloadCall('/ws/importbook', {inventaireid: id, lang: language}, 'POST');
|
||||
return genericPayloadCall('/ws/importbook', { inventaireid: id, lang: language }, 'POST')
|
||||
}
|
||||
|
||||
export async function putReadBook(bookId) {
|
||||
return genericPayloadCall('/ws/book/' + bookId + "/read", {read: true}, 'PUT')
|
||||
return genericPayloadCall('/ws/book/' + bookId + '/read', { read: true }, 'PUT')
|
||||
}
|
||||
|
||||
export async function putUnreadBook(bookId) {
|
||||
return genericPayloadCall('/ws/book/' + bookId + "/read", {read: false}, 'PUT')
|
||||
return genericPayloadCall('/ws/book/' + bookId + '/read', { read: false }, 'PUT')
|
||||
}
|
||||
|
||||
export async function putEndReadDate(bookId, enddate) {
|
||||
return genericPayloadCall('/ws/book/' + bookId + "/read", {read: true, endDate: enddate}, 'PUT')
|
||||
return genericPayloadCall('/ws/book/' + bookId + '/read', { read: true, endDate: enddate }, 'PUT')
|
||||
}
|
||||
|
||||
export async function putEndReadDateUnset(bookId) {
|
||||
return genericPayloadCall('/ws/book/' + bookId + "/read", {read: true, endDate: "null"}, 'PUT')
|
||||
return genericPayloadCall('/ws/book/' + bookId + '/read', { read: true, endDate: 'null' }, 'PUT')
|
||||
}
|
||||
|
||||
export async function putStartReadDateUnset(bookId) {
|
||||
return genericPayloadCall('/ws/book/' + bookId + "/startread", {startDate: "null"}, 'PUT')
|
||||
return genericPayloadCall('/ws/book/' + bookId + '/startread', { startDate: 'null' }, 'PUT')
|
||||
}
|
||||
|
||||
export async function putStartReadDate(bookId, startdate) {
|
||||
return genericPayloadCall('/ws/book/' + bookId + "/startread", {startDate: startdate}, 'PUT')
|
||||
return genericPayloadCall('/ws/book/' + bookId + '/startread', { startDate: startdate }, 'PUT')
|
||||
}
|
||||
|
||||
export async function putWantReadBook(bookId, payload) {
|
||||
return genericPayloadCall('/ws/book/' + bookId + "/wantread", payload, 'PUT')
|
||||
return genericPayloadCall('/ws/book/' + bookId + '/wantread', payload, 'PUT')
|
||||
}
|
||||
|
||||
export async function putRateBook(bookId, payload) {
|
||||
return genericPayloadCall('/ws/book/' + bookId + "/rate", payload, 'PUT')
|
||||
return genericPayloadCall('/ws/book/' + bookId + '/rate', payload, 'PUT')
|
||||
}
|
||||
|
||||
export function postLogin(user) {
|
||||
@@ -125,19 +139,19 @@ export function postSignUp(user) {
|
||||
}
|
||||
|
||||
export function postImage(file) {
|
||||
const { user } = useAuthStore();
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
const { user } = useAuthStore()
|
||||
const formData = new FormData()
|
||||
formData.append('file', file)
|
||||
if (user != null) {
|
||||
return fetch("/ws/upload/cover", {
|
||||
return fetch('/ws/upload/cover', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + user.token
|
||||
Authorization: 'Bearer ' + user.token,
|
||||
},
|
||||
body: formData
|
||||
body: formData,
|
||||
})
|
||||
} else {
|
||||
return Promise.resolve();
|
||||
return Promise.resolve()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,48 +161,47 @@ export function genericPostCallNoAuth(apiRoute, object) {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(object)
|
||||
body: JSON.stringify(object),
|
||||
})
|
||||
}
|
||||
|
||||
export function genericPayloadCall(apiRoute, object, method) {
|
||||
const { user } = useAuthStore();
|
||||
const { user } = useAuthStore()
|
||||
|
||||
if (user != null) {
|
||||
return fetch(apiRoute, {
|
||||
method: method,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + user.token
|
||||
Authorization: 'Bearer ' + user.token,
|
||||
},
|
||||
body: JSON.stringify(object)
|
||||
body: JSON.stringify(object),
|
||||
})
|
||||
}
|
||||
else {
|
||||
return Promise.resolve();
|
||||
} else {
|
||||
return Promise.resolve()
|
||||
}
|
||||
}
|
||||
|
||||
export function extractFormErrorFromField(fieldName, errors) {
|
||||
if (errors === null) {
|
||||
return "";
|
||||
return ''
|
||||
}
|
||||
if (errors.value == null) {
|
||||
return "";
|
||||
return ''
|
||||
}
|
||||
console.log(errors.value);
|
||||
const titleErr = errors.find((e) => e["field"] === fieldName);
|
||||
console.log(errors.value)
|
||||
const titleErr = errors.find((e) => e['field'] === fieldName)
|
||||
if (typeof titleErr !== 'undefined') {
|
||||
return titleErr.error;
|
||||
return titleErr.error
|
||||
} else {
|
||||
return "";
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
export function extractGlobalFormError(errors) {
|
||||
if (errors !== null && "error" in errors) {
|
||||
return errors["error"];
|
||||
if (errors !== null && 'error' in errors) {
|
||||
return errors['error']
|
||||
} else {
|
||||
return "";
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user