Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3379 | Rev 3432 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3416 stevensc 1
import React, { useState } from "react";
2
import { useDispatch } from "react-redux";
3
import { Typography } from "@mui/material";
5 stevensc 4
 
3416 stevensc 5
import { axios } from "@utils";
6
import { useFetch } from "@hooks";
7
import { addNotification } from "@store/notification/notification.actions";
5 stevensc 8
 
3416 stevensc 9
import Widget from "@components/UI/Widget";
3374 stevensc 10
 
3416 stevensc 11
import LoadingWrapper from "@components/common/loading-wrapper";
12
import EmojiGroup from "@components/common/emoji-selector";
2268 stevensc 13
 
3416 stevensc 14
const DailyPulse = ({ dailyPulseUrl = "" }) => {
15
  const [isSubmitting, setIsSubmitting] = useState(false);
16
  const dispatch = useDispatch();
41 stevensc 17
 
3416 stevensc 18
  const { data, loading, refetch } = useFetch(dailyPulseUrl, {
3331 stevensc 19
    emojis_how_are_you_feel: [],
3416 stevensc 20
    emojis_climate_on_your_organization: [],
21
  });
5 stevensc 22
 
3416 stevensc 23
  const saveEmoji = async (link_save = "") => {
3330 stevensc 24
    try {
3416 stevensc 25
      setIsSubmitting(true);
26
      const response = await axios.post(link_save);
27
      const { data, success } = response.data;
691 stevensc 28
 
3330 stevensc 29
      if (!success) {
3416 stevensc 30
        const errMsg = typeof data === "string" ? data : "Error al guardar";
31
        throw new Error(errMsg);
3330 stevensc 32
      }
33
 
3416 stevensc 34
      refetch();
3330 stevensc 35
    } catch (error) {
3416 stevensc 36
      dispatch(addNotification({ style: "danger", msg: error.message }));
3330 stevensc 37
    } finally {
3416 stevensc 38
      setIsSubmitting(false);
3330 stevensc 39
    }
3416 stevensc 40
  };
2281 stevensc 41
 
3378 stevensc 42
  if (
3379 stevensc 43
    data.emojis_how_are_you_feel.length <= 1 &&
44
    data.emojis_climate_on_your_organization.length <= 1
3378 stevensc 45
  ) {
3416 stevensc 46
    return null;
3378 stevensc 47
  }
48
 
2268 stevensc 49
  return (
2281 stevensc 50
    <Widget>
3416 stevensc 51
      <Widget.Header title="Pulso Diario" />
5 stevensc 52
 
3330 stevensc 53
      <Widget.Body>
54
        <LoadingWrapper
3416 stevensc 55
          loading={loading || isSubmitting}
3330 stevensc 56
          displayChildren={isSubmitting}
57
        >
3416 stevensc 58
          <Typography variant="h4" textAlign="center">
3331 stevensc 59
            ¿Como te sientes hoy?
60
          </Typography>
3330 stevensc 61
 
3374 stevensc 62
          <EmojiGroup>
63
            {data.emojis_how_are_you_feel?.map(
64
              ({ id, image, link_save }, index) => (
65
                <EmojiGroup.Item
66
                  key={id}
67
                  image={image}
68
                  index={index}
69
                  onClick={() => saveEmoji(link_save)}
70
                />
71
              )
72
            )}
73
          </EmojiGroup>
74
 
3416 stevensc 75
          <Typography variant="h4" textAlign="center">
3330 stevensc 76
            ¿Como esta el clima en la organización?
77
          </Typography>
3374 stevensc 78
 
79
          <EmojiGroup>
80
            {data.emojis_climate_on_your_organization?.map(
81
              ({ id, image, link_save }, index) => (
82
                <EmojiGroup.Item
83
                  key={id}
84
                  image={image}
85
                  index={index}
86
                  onClick={() => saveEmoji(link_save)}
87
                />
88
              )
89
            )}
90
          </EmojiGroup>
3330 stevensc 91
        </LoadingWrapper>
2268 stevensc 92
      </Widget.Body>
93
    </Widget>
3416 stevensc 94
  );
95
};
5 stevensc 96
 
3416 stevensc 97
export default DailyPulse;