Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3432 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 3432 Rev 3719
Línea 1... Línea 1...
1
import React, { createContext, useCallback } from 'react'
1
import React, { createContext, useCallback } from 'react';
2
import { useFetch } from '@hooks'
2
import { useFetch } from '@hooks';
3
 
3
 
4
export const ValuesContext = createContext()
4
export const ValuesContext = createContext();
5
 
5
 
6
export default function ValuesProvider({ url, children }) {
6
export default function ValuesProvider({ url, children }) {
7
  const { data, isLoading: loading, mutate } = useFetch(url, { items: [] })
7
  const { data, isLoading: loading, mutate } = useFetch(url, { items: [] });
8
  const { items: values, link_add, total } = data
8
  const { items: values, link_add, total } = data;
9
 
9
 
10
  const addValue = useCallback(
10
  const addValue = useCallback(
11
    (newValue) => {
11
    (newValue) => {
12
      const newValues = [...values, newValue]
12
      const newValues = [...values, newValue];
13
      mutate({ ...data, items: newValues })
13
      mutate({ ...data, items: newValues });
14
    },
14
    },
15
    [values, mutate]
15
    [values, mutate]
16
  )
16
  );
17
 
17
 
18
  const removeValue = useCallback(
18
  const removeValue = useCallback(
19
    (valueId) => {
19
    (valueId) => {
20
      const newValues = values.filter((value) => value.id !== valueId)
20
      const newValues = values.filter((value) => value.id !== valueId);
21
      mutate({ ...data, items: newValues })
21
      mutate({ ...data, items: newValues });
22
    },
22
    },
23
    [values, mutate]
23
    [values, mutate]
24
  )
24
  );
25
 
25
 
26
  const updateValue = useCallback(
26
  const updateValue = useCallback(
27
    (updatedValue) => {
27
    (updatedValue) => {
28
      const newValues = values.map((value) =>
28
      const newValues = values.map((value) =>
29
        value.id === updatedValue.id ? updatedValue : value
29
        value.id === updatedValue.id ? updatedValue : value
30
      )
30
      );
31
      mutate({ ...data, items: newValues })
31
      mutate({ ...data, items: newValues });
32
    },
32
    },
33
    [values, mutate]
33
    [values, mutate]
34
  )
34
  );
35
 
35
 
36
  const getValueById = useCallback(
36
  const getValueById = useCallback(
37
    (valueId) => values.find((value) => value.id === valueId),
37
    (valueId) => values.find((value) => value.id === valueId),
38
    [values]
38
    [values]
39
  )
39
  );
40
 
40
 
41
  return (
41
  return (
42
    <ValuesContext.Provider
42
    <ValuesContext.Provider
43
      value={{
43
      value={{
44
        loading,
44
        loading,
45
        values,
45
        values,
46
        addUrl: link_add,
46
        addUrl: link_add,
47
        total,
47
        total,
48
        addValue,
48
        addValue,
49
        removeValue,
49
        removeValue,
50
        updateValue,
50
        updateValue,
51
        getValueById
51
        getValueById
52
      }}
52
      }}
53
    >
53
    >
54
      {children}
54
      {children}
55
    </ValuesContext.Provider>
55
    </ValuesContext.Provider>
56
  )
56
  );
57
}
57
}