Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev 3416 Rev 3432
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'
Línea 3... Línea 3...
3
 
3
 
Línea 4... Línea 4...
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 }) {
Línea 7... Línea 7...
7
  const { data, loading: 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 })
Línea 14... Línea 14...
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)
Línea 21... Línea 21...
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
Línea 30... Línea 30...
30
      );
30
      )
31
      mutate({ ...data, items: newValues });
31
      mutate({ ...data, items: newValues })
32
    },
32
    },
33
    [values, mutate]
33
    [values, mutate]
Línea 34... Línea 34...
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),
Línea 46... Línea 46...
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
}