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'
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 GoalsContext = createContext();
5
export const GoalsContext = createContext()
6
 
6
 
Línea 7... Línea 7...
7
export default function GoalsProvider({ url, children }) {
7
export default function GoalsProvider({ url, children }) {
8
  const { data, loading: 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]
Línea 14... Línea 14...
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) => {
Línea 21... Línea 21...
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) =>
29
      const newGoals = goals.map((goal) =>
Línea 30... Línea 30...
30
        goal.id === updatedGoal.id ? updatedGoal : goal
30
        goal.id === updatedGoal.id ? updatedGoal : goal
31
      );
31
      )
32
      mutate({ ...data, items: newGoals });
32
      mutate({ ...data, items: newGoals })
33
    },
33
    },
Línea 34... Línea 34...
34
    [goals, mutate]
34
    [goals, mutate]
35
  );
35
  )
36
 
36
 
37
  const getGoalById = useCallback(
37
  const getGoalById = useCallback(
Línea 47... Línea 47...
47
        addUrl: link_add,
47
        addUrl: link_add,
48
        total,
48
        total,
49
        addGoal,
49
        addGoal,
50
        deleteGoal,
50
        deleteGoal,
51
        updateGoal,
51
        updateGoal,
52
        getGoalById,
52
        getGoalById
53
      }}
53
      }}
54
    >
54
    >
55
      {children}
55
      {children}
56
    </GoalsContext.Provider>
56
    </GoalsContext.Provider>
57
  );
57
  )
58
}
58
}