Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
import React from "react";
2
import { useState, useEffect } from "react";
3
import { useForm } from "react-hook-form";
4
import { connect } from "react-redux";
5
import {axios} from "../../../../utils";
6
import styled from "styled-components";
7
import Spinner from "../../../../shared/loading-spinner/Spinner";
8
import Setting from "./setting/Setting";
9
import { addNotification } from "../../../../redux/notification/notification.actions";
10
 
11
const StyledSpinnerContainer = styled.div`
12
  position: absolute;
13
  left: 0;
14
  top: 0;
15
  width: 100%;
16
  height: 100%;
17
  background: rgba(255, 255, 255, 0.4);
18
  display: flex;
19
  justify-content: center;
20
  align-items: center;
21
  z-index: 300;
22
`;
23
 
24
const Settings = (props) => {
25
  // https://leaderslinked.com/my-company/NDg2MjM4MDQg/setting/premium
26
  // status_microlearning
27
  // status_self_evaluation
28
  // backendVars Destructuring
29
  const { companyId } = props.backendVars;
30
 
31
  // reduxDestructuring
32
  const { addNotification } = props;
33
 
34
  // React hook form
35
  const { register, errors, handleSubmit } = useForm();
36
 
37
  // states
38
  const [loading, setLoading] = useState(false);
39
 
40
  useEffect(async () => {
41
    setLoading(true);
42
    await axios
43
      .get(`/my-company/${companyId}/setting/premium`)
44
      .then((response) => {
45
        const resData = response.data;
46
         (resData);
47
      });
48
    setLoading(false);
49
  }, []);
50
 
51
  const handleOnSubmit = async (data) => {
52
    setLoading(true);
53
    const formData = new FormData();
54
    Object.entries(data).map(([key, value]) => {
55
      if (value) formData.append(key, value);
56
    });
57
    axios
58
      .post(`/my-company/${companyId}/setting/premium`, formData)
59
      .then((response) => {
60
        const resData = response.data;
61
        if (resData.success) {
62
          addNotification({
63
            style: "success",
64
            msg: resData.data,
65
          });
66
          window.location.replace(`/my-company/${companyId}`);
67
        } else {
68
          const resError = resData.data;
69
          if (resError.constructor.name === "Object") {
70
            Object.entries(resError).map(([key, value]) => {
71
              if (key in getValues()) {
72
                setError(key, {
73
                  type: "manual",
74
                  message: Array.isArray(value) ? value[0] : value,
75
                });
76
              }
77
            });
78
          } else {
79
            addNotification({
80
              style: "danger",
81
              msg: resError,
82
            });
83
          }
84
        }
85
      });
86
    setLoading(false);
87
  };
88
 
89
  return (
90
    <section className="profile-account-setting">
91
      <div className="container">
92
        <div className="account-tabs-setting">
93
          <div className="row">
94
            <div className="col-lg-3">
95
              <div className="acc-leftbar">
96
                <div className="nav nav-tabs" id="nav-tab" role="tablist">
97
                  <a
98
                    className="nav-item nav-link active"
99
                    id="nav-premium-tab"
100
                    data-toggle="tab"
101
                    href="#nav-premium"
102
                    role="tab"
103
                    aria-controls="nav-premium"
104
                    aria-selected="false"
105
                  >
106
                    <i className="fa fa-diamond"></i>Premium
107
                  </a>
108
                </div>
109
              </div>
110
              {/* <!--acc-leftbar end--> */}
111
            </div>
112
 
113
            <div className="col-lg-9">
114
              <div className="tab-content" id="nav-tabContent">
115
                <div
116
                  className="tab-pane fade active show"
117
                  id="nav-premium"
118
                  role="tabpanel"
119
                  aria-labelledby="nav-premium-tab"
120
                >
121
                  <div className="acc-setting">
122
                    <h3>Características Premium</h3>
123
                    <form onSubmit={handleSubmit(handleOnSubmit)}>
124
                      <Setting
125
                        register={register}
126
                        title="Micro aprendizaje"
127
                        inputName="status_microlearning"
128
                      />
129
                      <Setting
130
                        register={register}
131
                        title="Auto Evaluación"
132
                        inputName="status_self_evaluation"
133
                      />
134
                      <div className="save-stngs pd2">
135
                        <ul>
136
                          <li>
137
                            <button
138
                              type="submit"
139
                              className="btn-save-premium-settings"
140
                            >
141
                              Guardar
142
                            </button>
143
                          </li>
144
                        </ul>
145
                      </div>
146
                    </form>
147
                    {/* <!--save-stngs end--> */}
148
                    {loading && (
149
                      <StyledSpinnerContainer>
150
                        <Spinner />
151
                      </StyledSpinnerContainer>
152
                    )}
153
                  </div>
154
                  {/* <!--acc-setting end--> */}
155
                </div>
156
              </div>
157
            </div>
158
            {/* <!--account-tabs-setting end--> */}
159
          </div>
160
        </div>
161
      </div>
162
    </section>
163
  );
164
};
165
 
166
const mapDispatchToProps = {
167
  addNotification: (notification) => addNotification(notification),
168
};
169
 
170
export default connect(null, mapDispatchToProps)(Settings);