Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev 3374 Rev 3416
Línea 1... Línea 1...
1
import React from 'react'
1
import React from "react";
2
import { useNavigate, useParams } from 'react-router-dom'
2
import { useNavigate, useParams } from "react-router-dom";
3
import { useDispatch } from 'react-redux'
3
import { useDispatch } from "react-redux";
4
 
4
 
5
import { useFetch, useGoals } from '@hooks'
5
import { useFetch, useGoals } from "@hooks";
6
import { editGoal } from '@services/habits/goals'
6
import { editGoal } from "@services/habits/goals";
7
import { addNotification } from '@store/notification/notification.actions'
7
import { addNotification } from "@store/notification/notification.actions";
8
 
8
 
9
import PageHeader from '@components/common/page-header'
9
import PageHeader from "@components/common/page-header";
10
import GoalForm from '@components/habits/goals/goal-form'
10
import GoalForm from "@components/habits/goals/goal-form";
11
import LoadingWrapper from '@components/common/loading-wrapper'
11
import LoadingWrapper from "@components/common/loading-wrapper";
Línea 12... Línea 12...
12
 
12
 
13
export default function EditGoalPage() {
13
export default function EditGoalPage() {
14
  const { id } = useParams()
14
  const { id } = useParams();
15
  const navigate = useNavigate()
15
  const navigate = useNavigate();
Línea 16... Línea 16...
16
  const dispatch = useDispatch()
16
  const dispatch = useDispatch();
17
 
17
 
Línea 18... Línea 18...
18
  const { addUrl, updateGoal, getGoalById } = useGoals()
18
  const { addUrl, updateGoal, getGoalById } = useGoals();
19
  const currentGoal = getGoalById(id)
19
  const currentGoal = getGoalById(id);
20
 
20
 
21
  const { data: goalValues, isLoading } = useFetch(
21
  const { data: goalValues, loading } = useFetch(
22
    currentGoal?.actions.link_edit
22
    currentGoal?.actions.link_edit
23
  )
23
  );
24
  const { data: habits } = useFetch(addUrl)
24
  const { data: habits } = useFetch(addUrl);
25
  const habitsValues = Object.entries(habits).map(([value, name]) => ({
25
  const habitsValues = Object.entries(habits).map(([value, name]) => ({
Línea 26... Línea 26...
26
    value,
26
    value,
27
    name
27
    name,
28
  }))
28
  }));
29
 
29
 
30
  const onSubmit = async (goal) => {
30
  const onSubmit = async (goal) => {
31
    try {
31
    try {
32
      const response = await editGoal(currentGoal?.actions.link_edit, goal)
32
      const response = await editGoal(currentGoal?.actions.link_edit, goal);
33
      updateGoal(response.data)
33
      updateGoal(response.data);
34
      dispatch(addNotification({ style: 'success', msg: response.message }))
34
      dispatch(addNotification({ style: "success", msg: response.message }));
35
      navigate('/habits/goals')
35
      navigate("/habits/goals");
Línea 36... Línea 36...
36
    } catch (error) {
36
    } catch (error) {
37
      dispatch(addNotification({ style: 'danger', msg: error.message }))
37
      dispatch(addNotification({ style: "danger", msg: error.message }));
38
    }
38
    }
39
  }
39
  };
40
 
40
 
41
  return (
41
  return (
42
    <>
42
    <>
43
      <PageHeader title='Editar Meta' goBack />
43
      <PageHeader title="Editar Meta" goBack />
44
      <LoadingWrapper loading={isLoading}>
44
      <LoadingWrapper loading={loading}>
45
        <GoalForm
45
        <GoalForm
46
          onSubmit={onSubmit}
46
          onSubmit={onSubmit}
47
          habits={habitsValues}
47
          habits={habitsValues}
48
          values={{
48
          values={{
49
            ...goalValues,
49
            ...goalValues,
50
            start_date: new Date(goalValues.start_date + 'T00:00:00'),
50
            start_date: new Date(goalValues.start_date + "T00:00:00"),
51
            end_date: new Date(goalValues.end_date + 'T00:00:00')
51
            end_date: new Date(goalValues.end_date + "T00:00:00"),
52
          }}
52
          }}