Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3364 | Rev 3416 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 3364 Rev 3365
Línea 1... Línea 1...
1
import React, { createContext, useEffect } from 'react'
1
import React, { createContext } from 'react'
Línea 2... Línea 2...
2
 
2
 
Línea 3... Línea 3...
3
import { useFetch } from '@hooks'
3
import { useFetch } from '@hooks'
Línea 4... Línea 4...
4
 
4
 
5
export const HabitsUrlsContext = createContext()
-
 
6
 
-
 
7
export default function HabitsUrlsProvider({ children }) {
-
 
8
  const {
-
 
9
    data,
5
export const HabitsUrlsContext = createContext()
10
    isLoading: loading,
6
 
11
    mutate
7
export default function HabitsUrlsProvider({ children }) {
12
  } = useFetch('/habits', {
8
  const { data, isLoading: loading } = useFetch('/habits', {
13
    categories: [],
9
    categories: [],
Línea 14... Línea -...
14
    emojis: []
-
 
15
  })
-
 
16
  const { categories, emojis, ...links } = data
-
 
17
 
-
 
18
  useEffect(() => {
-
 
19
    if (!categories.length || !emojis.length) return
-
 
20
 
-
 
21
    const categoriesWithEmojis = categories.map((c) => ({ ...c, emojis }))
-
 
22
    mutate({ ...data, categories: categoriesWithEmojis })
-
 
23
  }, [loading])
-
 
24
 
-
 
25
  const selectEmoji = (categoryUuid, emojiCode) => {
-
 
26
    const newCategories = categories.map((category) => {
-
 
27
      if (category.uuid === categoryUuid) {
-
 
28
        const emojis = category.emojis.filter((e) => e.code === emojiCode)
-
 
29
        return { ...category, emojis }
-
 
30
      }
-
 
31
 
-
 
32
      return category
-
 
33
    })
-
 
34
 
10
    emojis: []
35
    mutate({ ...data, categories: newCategories })
11
  })
36
  }
12
  const { categories, emojis, ...links } = data
37
 
13
 
38
  return (
14
  return (
39
    <HabitsUrlsContext.Provider
15
    <HabitsUrlsContext.Provider
40
      value={{
16
      value={{
41
        loading,
17
        loading,
42
        categories,
18
        categories,
43
        links,
19
        links,
44
        selectEmoji
20
        emojis
45
      }}
21
      }}