Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev 3358 Rev 3364
Línea 1... Línea 1...
1
import React, { createContext } from 'react'
1
import React, { createContext, useEffect } 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 {
5
export const HabitsUrlsContext = createContext()
9
    data,
6
 
10
    isLoading: loading,
7
export default function HabitsUrlsProvider({ children }) {
11
    mutate
8
  const { data, isLoading: loading } = useFetch('/habits', {
12
  } = useFetch('/habits', {
9
    categories: [],
13
    categories: [],
Línea -... Línea 14...
-
 
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
    })
10
    emojis: []
34
 
11
  })
35
    mutate({ ...data, categories: newCategories })
12
  const { categories, emojis, ...links } = data
36
  }
13
 
37
 
14
  return (
38
  return (
15
    <HabitsUrlsContext.Provider
39
    <HabitsUrlsContext.Provider
16
      value={{
40
      value={{
17
        loading,
41
        loading,
18
        categories,
42
        categories,
19
        links,
43
        links,
20
        emojis
44
        selectEmoji
21
      }}
45
      }}