Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3315 | Ir a la última revisión | | Ultima modificación | Ver Log |

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