feat(org): Ported components to mjs and named exports
This commit is contained in:
parent
37f7833983
commit
595417a23b
118 changed files with 836 additions and 852 deletions
109
sites/org/hooks/useBackend.mjs
Normal file
109
sites/org/hooks/useBackend.mjs
Normal file
|
@ -0,0 +1,109 @@
|
|||
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,
|
||||
})
|
||||
|
||||
export 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 }) => {
|
||||
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 }) => {
|
||||
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
|
||||
}
|
||||
|
||||
/*
|
||||
* Checks whether a username is available
|
||||
*/
|
||||
backend.isUsernameAvailable = async (username) => {
|
||||
try {
|
||||
app.startLoading()
|
||||
await api.post(`/available/username/jwt`, { username }, auth)
|
||||
} catch (err) {
|
||||
// 404 means user is not found, so the username is available
|
||||
if (err.response?.status === 404) return true
|
||||
return false
|
||||
} finally {
|
||||
app.stopLoading()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
return backend
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue