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
 
2
 
3
import { useFetch } from '@hooks'
3
import { useFetch } from '@hooks';
4
 
4
 
5
export const GoalsContext = createContext()
5
export const GoalsContext = createContext();
6
 
6
 
7
export default function GoalsProvider({ url, children }) {
7
export default function GoalsProvider({ url, children }) {
8
  const { data, isLoading: loading, mutate } = useFetch(url, { items: [] })
8
  const { data, isLoading: loading, mutate } = useFetch(url, { items: [] });
9
  const { items: goals, link_add, total } = data
9
  const { items: goals, link_add, total } = data;
10
 
10
 
11
  const addGoal = useCallback(
11
  const addGoal = useCallback(
12
    (newGoal) => {
12
    (newGoal) => {
13
      const newGoals = [newGoal, ...goals]
13
      const newGoals = [newGoal, ...goals];
14
      mutate({ ...data, items: newGoals })
14
      mutate({ ...data, items: newGoals });
15
    },
15
    },
16
    [goals, mutate]
16
    [goals, mutate]
17
  )
17
  );
18
 
18
 
19
  const deleteGoal = useCallback(
19
  const deleteGoal = useCallback(
20
    (goalId) => {
20
    (goalId) => {
21
      const newGoals = goals.filter((goal) => goal.id !== goalId)
21
      const newGoals = goals.filter((goal) => goal.id !== goalId);
22
      mutate({ ...data, items: newGoals })
22
      mutate({ ...data, items: newGoals });
23
    },
23
    },
24
    [goals, mutate]
24
    [goals, mutate]
25
  )
25
  );
26
 
26
 
27
  const updateGoal = useCallback(
27
  const updateGoal = useCallback(
28
    (updatedGoal) => {
28
    (updatedGoal) => {
29
      const newGoals = goals.map((goal) =>
-
 
30
        goal.id === updatedGoal.id ? updatedGoal : goal
29
      const newGoals = goals.map((goal) => (goal.id === updatedGoal.id ? updatedGoal : goal));
31
      )
-
 
32
      mutate({ ...data, items: newGoals })
30
      mutate({ ...data, items: newGoals });
33
    },
31
    },
34
    [goals, mutate]
32
    [goals, mutate]
35
  )
33
  );
36
 
34
 
37
  const getGoalById = useCallback(
-
 
38
    (goalId) => goals.find((goal) => goal.id === goalId),
35
  const getGoalById = useCallback((goalId) => goals.find((goal) => goal.id === goalId), [goals]);
39
    [goals]
-
 
40
  )
-
 
41
 
36
 
42
  return (
37
  return (
43
    <GoalsContext.Provider
38
    <GoalsContext.Provider
44
      value={{
39
      value={{
45
        loading,
40
        loading,
46
        goals,
41
        goals,
47
        addUrl: link_add,
42
        addUrl: link_add,
48
        total,
43
        total,
49
        addGoal,
44
        addGoal,
50
        deleteGoal,
45
        deleteGoal,
51
        updateGoal,
46
        updateGoal,
52
        getGoalById
47
        getGoalById
53
      }}
48
      }}
54
    >
49
    >
55
      {children}
50
      {children}
56
    </GoalsContext.Provider>
51
    </GoalsContext.Provider>
57
  )
52
  );
58
}
53
}