Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev 3319 Rev 3320
Línea 1... Línea 1...
1
import React from 'react'
1
import React from 'react'
2
import { useParams } from 'react-router-dom'
-
 
3
import { useDispatch } from 'react-redux'
2
import { useDispatch } from 'react-redux'
4
import { useForm } from 'react-hook-form'
-
 
5
import { Button } from '@mui/material'
-
 
Línea 6... Línea 3...
6
 
3
 
7
import { useFetch, useMyProgress } from '@hooks'
4
import { useHabitProgress } from '@hooks'
8
import { saveProgress } from '@services/habits/habits'
5
import { saveProgress } from '@services/habits/habits'
Línea 9... Línea 6...
9
import { addNotification } from '@store/notification/notification.actions'
6
import { addNotification } from '@store/notification/notification.actions'
10
 
-
 
11
import PageHeader from '@components/common/page-header'
7
 
12
import Form from '@components/common/form'
-
 
13
import LoadingWrapper from '@components/common/loading-wrapper'
8
import PageHeader from '@components/common/page-header'
Línea 14... Línea 9...
14
import Input from '@components/UI/inputs/Input'
9
import LoadingWrapper from '@components/common/loading-wrapper'
15
import Ckeditor from '@components/common/ckeditor/Ckeditor'
-
 
16
 
10
import ProgressForm from '@components/habits/progress/progress-form'
Línea 17... Línea 11...
17
export default function AddHabitProgress() {
11
 
18
  const { id } = useParams()
-
 
Línea 19... Línea -...
19
  const dispatch = useDispatch()
-
 
20
 
-
 
21
  const { getHabitById } = useMyProgress()
-
 
22
  const currentHabit = getHabitById(id)
-
 
23
 
-
 
24
  const { data, isLoading } = useFetch(currentHabit?.link)
-
 
25
 
-
 
26
  const {
-
 
27
    control,
-
 
28
    formState: { errors, isSubmitting },
-
 
29
    handleSubmit
-
 
30
  } = useForm()
-
 
31
 
-
 
32
  const getCurrentDate = () => {
-
 
33
    const date = new Date()
-
 
34
    const year = date.getFullYear()
-
 
35
    const month = String(date.getMonth() + 1).padStart(2, '0') // Enero es 0
-
 
36
    const day = String(date.getDate()).padStart(2, '0')
-
 
37
    const hours = String(date.getHours()).padStart(2, '0')
-
 
38
    const minutes = String(date.getMinutes()).padStart(2, '0')
12
export default function AddHabitProgress() {
39
    const seconds = String(date.getSeconds()).padStart(2, '0')
13
  const dispatch = useDispatch()
40
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
-
 
41
  }
14
 
42
 
15
  const { addUrl, loading, addItem } = useHabitProgress()
43
  const onSubmit = handleSubmit(async (progress) => {
-
 
44
    try {
-
 
45
      const date = getCurrentDate()
16
 
46
      const response = await saveProgress(data.link_add, {
17
  const onSubmit = async (progress) => {
47
        ...progress,
18
    try {
48
        date
19
      const response = await saveProgress(addUrl, progress)
49
      })
20
      addItem(response.data)
Línea 50... Línea 21...
50
      console.log(response)
21
      dispatch(addNotification({ style: 'success', msg: response.message }))
51
    } catch (error) {
22
    } catch (error) {
52
      dispatch(addNotification({ style: 'danger', msg: error.message }))
-
 
53
    }
23
      dispatch(addNotification({ style: 'danger', msg: error.message }))
54
  })
-
 
55
 
-
 
56
  return (
-
 
57
    <>
-
 
58
      <PageHeader
-
 
59
        title={`${currentHabit?.name ?? ''} - Agregar progreso`}
-
 
60
        goBack
-
 
61
      />
-
 
62
      <LoadingWrapper loading={isLoading}>
-
 
63
        <Form onSubmit={onSubmit}>
-
 
64
          <LoadingWrapper loading={isSubmitting} displayChildren>
-
 
65
            <Input
-
 
66
              control={control}
-
 
67
              label='Valor cuantitativo:'
24
    }
68
              name='quantitative_value'
-
 
69
              type='number'
25
  }
70
              rules={{ required: 'El valor es requerido' }}
-
 
71
              error={errors.quantitative_value?.message}
-
 
72
            />
-
 
73
 
-
 
74
            <Ckeditor
-
 
75
              control={control}
-
 
76
              name='qualitative_description'
26
 
77
              error={errors.qualitative_description?.message}
-
 
78
              rules={{ required: 'La descripción es requerida' }}
-
 
79
              label='Descripción cualitativa:'
-
 
80
            />
-
 
81
 
27
  return (
82
            <Button color='primary' type='submit'>
28
    <>
83
              Enviar
29
      <PageHeader title='Agregar progreso' goBack />
84
            </Button>
30