Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3357 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 3357 Rev 3358
Línea 1... Línea 1...
1
import React, { useEffect, useRef, useState } from 'react'
1
import React, { useState } from 'react'
2
import { useDispatch } from 'react-redux'
-
 
3
import { Controller, useForm } from 'react-hook-form'
-
 
4
import { Backdrop, Box, Typography } from '@mui/material'
2
import { Backdrop } from '@mui/material'
5
 
-
 
6
import { useHabitsUrls } from '@hooks'
-
 
7
import { savePreferences } from '@services/habits/daily-log'
-
 
8
import { addNotification } from '@store/notification/notification.actions'
-
 
Línea 9... Línea 3...
9
 
3
 
10
import Widget from '@components/UI/Widget'
4
import Widget from '@components/UI/Widget'
11
import EmojiGroup from '@components/common/emoji-selector'
-
 
12
 
-
 
13
const EMOJI_PICKERS_POSITIONS = [
-
 
14
  { column: '2 / 3', row: 'auto' },
-
 
15
  { column: '1 / 2', row: '3 / 4' },
-
 
16
  { column: '3 / 4', row: '3 / 4' }
-
 
Línea 17... Línea 5...
17
]
5
import DailyLogForm from './daily-log/daily-log-form'
18
 
6
 
19
export default function SelfRating() {
-
 
20
  const [show, setShow] = useState(false)
-
 
21
  const formRef = useRef()
-
 
22
  const dispatch = useDispatch()
-
 
23
 
-
 
24
  const { categories, links, selectEmoji } = useHabitsUrls()
-
 
25
 
-
 
Línea 26... Línea 7...
26
  const { control, watch, handleSubmit } = useForm()
7
export default function SelfRating() {
Línea 27... Línea -...
27
  const formValues = watch()
-
 
28
 
-
 
29
  const toggleShow = () => setShow(!show)
-
 
30
 
-
 
31
  const onSubmit = handleSubmit(async (data) => {
-
 
32
    try {
-
 
33
      const mediaResponse = await savePreferences(
-
 
34
        links.link_aspect_daily_log,
-
 
35
        data
-
 
36
      )
-
 
37
    } catch (error) {
-
 
38
      dispatch(addNotification({ style: 'danger', msg: error.message }))
-
 
39
    }
-
 
40
  })
-
 
41
 
-
 
42
  useEffect(() => {
-
 
43
    const shouldDisplay = categories.every((cat) => !cat.selected)
-
 
44
 
-
 
45
    if (categories.length && shouldDisplay) {
-
 
46
      setShow(true)
-
 
47
    }
-
 
48
  }, [categories])
-
 
49
 
-
 
50
  useEffect(() => {
-
 
51
    const categoriesSelectedValues = Object.values(formValues)
-
 
52
    const areAllCategoriesSelected = categoriesSelectedValues.every(
-
 
53
      (val) => val !== undefined
-
 
54
    )
-
 
55
 
-
 
56
    if (categoriesSelectedValues.length && areAllCategoriesSelected) {
-
 
57
      onSubmit()
8
  const [show, setShow] = useState(false)
58
    }
9
 
59
  }, [formValues])
10
  const toggleShow = () => setShow(!show)
60
 
11
 
61
  return (
12
  return (
62
    <Backdrop
13
    <Backdrop
63
      sx={{ color: '#fff', zIndex: 1250 }}
14
      sx={{ color: '#fff', zIndex: 1250 }}
64
      open={show}
15
      open={show}
65
      onClick={toggleShow}
-
 
66
    >
-
 
67
      <Widget styles={{ maxWidth: '800px' }}>
-
 
68
        <Widget.Body>
-
 
69
          <Box
-
 
70
            component='form'
-
 
71
            sx={{
-
 
72
              display: 'grid',
-
 
73
              gridTemplateColumns: '1fr 1fr 1fr',
-
 
74
              gridTemplateRows: '1fr 1fr 1fr',
-
 
75
              gap: '1rem',
-
 
76
              placeItems: 'center'
-
 
77
            }}
-
 
78
            ref={formRef}
-
 
79
            onSubmit={onSubmit}
-
 
80
          >
-
 
81
            {categories?.map(({ uuid, name, emojis }, index) => (
-
 
82
              <Box
-
 
83
                key={uuid}
-
 
84
                sx={{
-
 
85
                  gridColumn: EMOJI_PICKERS_POSITIONS[index].column,
-
 
86
                  gridRow: EMOJI_PICKERS_POSITIONS[index].row
-
 
87
                }}
-
 
88
              >
-
 
89
                <Typography variant='h2' textAlign='center'>
-
 
90
                  {name}
-
 
91
                </Typography>
-
 
92
 
-
 
93
                <Controller
-
 
94
                  control={control}
-
 
95
                  name={name}
-
 
96
                  render={({ field: { onChange } }) => (
-
 
97
                    <EmojiGroup>
-
 
98
                      {emojis?.map(({ code, image }, index) => (
-
 
99
                        <EmojiGroup.Item
-
 
100
                          key={code}
-
 
101
                          image={image}
-
 
102
                          index={index}
-
 
103
                          onClick={(e) => {
-
 
104
                            e.stopPropagation()
16
      onClick={toggleShow}
105
                            selectEmoji(uuid, code)
-
 
106
                            onChange(code)
-
 
107
                          }}
-
 
108
                        />
-
 
109
                      ))}
-
 
110
                    </EmojiGroup>
-
 
111
                  )}
-
 
112
                />
-
 
113
              </Box>
-
 
114
            ))}
-
 
115
 
-
 
116
            <Box sx={{ gridColumn: '2 / 3', gridRow: '2 / 3' }}>
-
 
117
              <img
-
 
118
                src='/images/logo-app-02.png'
-
 
119
                alt='habits image'
-
 
120
                width={150}
-
 
121
                height={150}
17
    >
122
              />
18
      <Widget styles={{ maxWidth: '800px' }}>
123
            </Box>
19
        <Widget.Body>
124
          </Box>
20
          <DailyLogForm />
125
        </Widget.Body>
21
        </Widget.Body>