use-user-projects.js 653 B

12345678910111213141516171819202122
  1. import { useEffect, useState } from 'react'
  2. import { getJSON } from '../../../infrastructure/fetch-json'
  3. import { fileCollator } from '../util/file-collator'
  4. const alphabetical = (a, b) => fileCollator.compare(a.name, b.name)
  5. export function useUserProjects() {
  6. const [loading, setLoading] = useState(true)
  7. const [data, setData] = useState(null)
  8. const [error, setError] = useState(false)
  9. useEffect(() => {
  10. getJSON('/user/projects')
  11. .then(data => {
  12. setData(data.projects.sort(alphabetical))
  13. })
  14. .catch(error => setError(error))
  15. .finally(() => setLoading(false))
  16. }, [])
  17. return { loading, data, error }
  18. }