| 3308 |
stevensc |
1 |
import React from 'react'
|
|
|
2 |
import { useDispatch } from 'react-redux'
|
|
|
3 |
import { useForm } from 'react-hook-form'
|
|
|
4 |
import {
|
|
|
5 |
Accordion,
|
|
|
6 |
AccordionDetails,
|
|
|
7 |
AccordionSummary,
|
|
|
8 |
Button,
|
|
|
9 |
Typography
|
|
|
10 |
} from '@mui/material'
|
|
|
11 |
import { ExpandMore } from '@mui/icons-material'
|
|
|
12 |
|
|
|
13 |
import { saveProgress } from '@services/habits/habits'
|
|
|
14 |
import { addNotification } from '@store/notification/notification.actions'
|
|
|
15 |
|
|
|
16 |
import Widget from '@components/UI/Widget'
|
|
|
17 |
import Form from '@components/common/form'
|
|
|
18 |
import Input from '@components/UI/inputs/Input'
|
|
|
19 |
import Ckeditor from '@components/common/ckeditor/Ckeditor'
|
|
|
20 |
import LoadingWrapper from '@components/common/loading-wrapper'
|
|
|
21 |
|
|
|
22 |
export default function ProgressItem({ habit }) {
|
|
|
23 |
const dispatch = useDispatch()
|
|
|
24 |
|
|
|
25 |
const { uuid, name, link } = habit
|
|
|
26 |
|
|
|
27 |
const {
|
|
|
28 |
handleSubmit,
|
|
|
29 |
control,
|
|
|
30 |
formState: { errors, isSubmitting }
|
|
|
31 |
} = useForm({
|
|
|
32 |
defaultValues: {}
|
|
|
33 |
})
|
|
|
34 |
|
|
|
35 |
function getCurrentDate() {
|
|
|
36 |
const date = new Date()
|
|
|
37 |
const year = date.getFullYear()
|
|
|
38 |
const month = String(date.getMonth() + 1).padStart(2, '0') // Enero es 0
|
|
|
39 |
const day = String(date.getDate()).padStart(2, '0')
|
|
|
40 |
const hours = String(date.getHours()).padStart(2, '0')
|
|
|
41 |
const minutes = String(date.getMinutes()).padStart(2, '0')
|
|
|
42 |
const seconds = String(date.getSeconds()).padStart(2, '0')
|
|
|
43 |
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
const onSubmit = handleSubmit(async (data) => {
|
|
|
47 |
try {
|
|
|
48 |
const date = getCurrentDate()
|
|
|
49 |
const response = await saveProgress(link, { ...data, date })
|
|
|
50 |
console.log(response)
|
|
|
51 |
} catch (error) {
|
|
|
52 |
dispatch(addNotification({ style: 'danger', msg: error.message }))
|
|
|
53 |
}
|
|
|
54 |
})
|
|
|
55 |
|
|
|
56 |
return (
|
|
|
57 |
<Widget>
|
|
|
58 |
<Widget.Body>
|
|
|
59 |
<Accordion
|
|
|
60 |
sx={{
|
|
|
61 |
border: 'none',
|
|
|
62 |
width: '100%',
|
|
|
63 |
boxShadow: 'none',
|
|
|
64 |
padding: 0,
|
|
|
65 |
'&::before': {
|
|
|
66 |
display: 'none'
|
|
|
67 |
}
|
|
|
68 |
}}
|
|
|
69 |
>
|
|
|
70 |
<AccordionSummary
|
|
|
71 |
sx={{ padding: '0.5rem 0 !important' }}
|
|
|
72 |
expandIcon={<ExpandMore />}
|
|
|
73 |
>
|
|
|
74 |
<Typography variant='h2'>{name}</Typography>
|
|
|
75 |
</AccordionSummary>
|
|
|
76 |
|
|
|
77 |
<AccordionDetails sx={{ padding: 0 }}>
|
|
|
78 |
<Form onSubmit={onSubmit} id={`${uuid}_progress-form`}>
|
|
|
79 |
<LoadingWrapper loading={isSubmitting} displayChildren>
|
|
|
80 |
<Input
|
|
|
81 |
control={control}
|
|
|
82 |
label='Valor cuantitativo:'
|
|
|
83 |
name='quantitative_value'
|
|
|
84 |
type='number'
|
|
|
85 |
rules={{ required: 'El valor es requerido' }}
|
|
|
86 |
error={errors.quantitative_value?.message}
|
|
|
87 |
/>
|
|
|
88 |
|
|
|
89 |
<Ckeditor
|
|
|
90 |
control={control}
|
|
|
91 |
name='qualitative_description'
|
|
|
92 |
error={errors.qualitative_description?.message}
|
|
|
93 |
rules={{ required: 'La descripción es requerida' }}
|
|
|
94 |
label='Descripción cualitativa:'
|
|
|
95 |
/>
|
|
|
96 |
|
|
|
97 |
<Button color='primary' form={`${uuid}_progress-form`}>
|
|
|
98 |
Enviar
|
|
|
99 |
</Button>
|
|
|
100 |
</LoadingWrapper>
|
|
|
101 |
</Form>
|
|
|
102 |
</AccordionDetails>
|
|
|
103 |
</Accordion>
|
|
|
104 |
</Widget.Body>
|
|
|
105 |
</Widget>
|
|
|
106 |
)
|
|
|
107 |
}
|