2021-03-18 09:52:36 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
import { getJSON } from '../../../infrastructure/fetch-json'
|
|
|
|
import { fileCollator } from '../util/file-collator'
|
2021-06-23 08:09:23 +00:00
|
|
|
import useAbortController from '../../../shared/hooks/use-abort-controller'
|
2021-03-18 09:52:36 +00:00
|
|
|
|
|
|
|
const alphabetical = (a, b) => fileCollator.compare(a.name, b.name)
|
|
|
|
|
|
|
|
export function useUserProjects() {
|
|
|
|
const [loading, setLoading] = useState(true)
|
|
|
|
const [data, setData] = useState(null)
|
|
|
|
const [error, setError] = useState(false)
|
|
|
|
|
2021-06-23 08:09:23 +00:00
|
|
|
const { signal } = useAbortController()
|
|
|
|
|
2021-03-18 09:52:36 +00:00
|
|
|
useEffect(() => {
|
2021-06-23 08:09:23 +00:00
|
|
|
getJSON('/user/projects', { signal })
|
2021-03-18 09:52:36 +00:00
|
|
|
.then(data => {
|
|
|
|
setData(data.projects.sort(alphabetical))
|
|
|
|
})
|
|
|
|
.catch(error => setError(error))
|
|
|
|
.finally(() => setLoading(false))
|
2021-06-23 08:09:23 +00:00
|
|
|
}, [signal])
|
2021-03-18 09:52:36 +00:00
|
|
|
|
|
|
|
return { loading, data, error }
|
|
|
|
}
|