Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3368 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3719 stevensc 1
import React, { useEffect } from 'react';
2
import { Controller, useForm } from 'react-hook-form';
3
import { Box, Typography } from '@mui/material';
4
 
5
import EmojiGroup from '@components/common/emoji-selector';
6
 
7
const EMOJI_PICKERS_POSITIONS = [
8
  { column: { xs: '1 / 3', md: '2 / 3' }, row: 'auto' },
9
  { column: { xs: '1 / 2', md: '1 / 2' }, row: '3 / 4' },
10
  { column: { xs: '2 / 3', md: '3 / 4' }, row: '3 / 4' }
11
];
12
 
13
export default function DailyLogForm({ categories, onSubmit, onSelect }) {
14
  const { control, watch, handleSubmit } = useForm();
15
  const formValues = watch();
16
 
17
  const submitHandler = handleSubmit(async (data) => onSubmit(data));
18
 
19
  useEffect(() => {
20
    const categoriesSelectedValues = Object.values(formValues);
21
    const areAllCategoriesSelected = categoriesSelectedValues.every((val) => val !== undefined);
22
 
23
    if (categoriesSelectedValues.length && areAllCategoriesSelected) {
24
      submitHandler();
25
    }
26
  }, [formValues]);
27
 
28
  return (
29
    <Box
30
      component='form'
31
      sx={{
32
        display: 'grid',
33
        gridTemplateColumns: { xs: 'repeat(2,1fr)', md: 'repeat(3,1fr)' },
34
        gridTemplateRows: 'repeat(3,1fr)',
35
        columnGap: '1rem',
36
        placeItems: 'center'
37
      }}
38
    >
39
      {categories?.map(({ uuid, name, emojis }, index) => (
40
        <Box
41
          key={uuid}
42
          sx={{
43
            gridColumn: EMOJI_PICKERS_POSITIONS[index].column,
44
            gridRow: EMOJI_PICKERS_POSITIONS[index].row
45
          }}
46
        >
47
          <Typography variant='h2' textAlign='center'>
48
            {name}
49
          </Typography>
50
 
51
          <Controller
52
            control={control}
53
            name={uuid}
54
            render={({ field: { onChange } }) => (
55
              <EmojiGroup>
56
                {emojis?.map(({ code, image }, index) => (
57
                  <EmojiGroup.Item
58
                    key={code}
59
                    image={image}
60
                    index={index}
61
                    onClick={(e) => {
62
                      e.stopPropagation();
63
                      onSelect(uuid, code);
64
                      onChange(code);
65
                    }}
66
                  />
67
                ))}
68
              </EmojiGroup>
69
            )}
70
          />
71
        </Box>
72
      ))}
73
 
74
      <Box
75
        sx={{
76
          gridColumn: { xs: '1 / 3', md: '2 / 3' },
77
          gridRow: '2 / 3'
78
        }}
79
      >
80
        <img src='/images/logo-app-02.png' alt='habits image' width={150} height={150} />
81
      </Box>
82
    </Box>
83
  );
84
}