1
0
Fork 0
freesewing/sites/org/hooks/useBackend.js
2023-01-22 19:46:56 +01:00

97 lines
2.1 KiB
JavaScript

import axios from 'axios'
import process from 'process'
/*
* Helper methods to interact with the FreeSewing backend
*/
const api = axios.create({
baseURL: process.env.NEXT_PUBLIC_BACKEND || 'https://backend.freesewing.org',
timeout: 3000,
})
function useBackend(app) {
const auth = {
headers: { Authorization: 'Bearer ' + app.token },
}
const backend = {}
/*
* User signup
*/
backend.signUp = async ({ email, language }) => {
let result
try {
app.startLoading()
result = await api.post('/signup', { email, language })
} catch (err) {
return err
} finally {
app.stopLoading()
}
if (result && result.status === 201 && result.data) return result.data
return null
}
/*
* Load confirmation
*/
backend.loadConfirmation = async ({ id, check, startLoading, stopLoading }) => {
let result
try {
app.startLoading()
result = await api.get(`/confirmations/${id}/${check}`)
} catch (err) {
return err
} finally {
app.stopLoading()
}
if (result && result.status === 201 && result.data) return result.data
return null
}
/*
* Confirm signup
*/
backend.confirmSignup = async ({ id, consent, startLoading, stopLoading }) => {
let result
try {
app.startLoading()
result = await api.post(`/confirm/signup/${id}`, { consent })
} catch (err) {
return err
} finally {
app.stopLoading()
}
if (result && result.status === 200 && result.data) return result.data
return null
}
/*
* Generic update account method
*/
backend.updateAccount = async (data) => {
let result
try {
app.startLoading()
result = await api.patch(`/account/jwt`, data, auth)
} catch (err) {
return err
} finally {
app.stopLoading()
}
if (result && result.status === 200 && result.data?.account) {
app.setAccount(result.data.account)
return true
}
return false
}
/* Set control */
backend.setControl = (control) => updateAccount({ control })
return backend
}
export default useBackend