Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1011 | Rev 4380 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1011 Rev 4379
Línea -... Línea 1...
-
 
1
/* eslint-disable react/prop-types */
1
import React, { useEffect, useState } from "react";
2
import React, { useEffect, useState } from "react";
2
import styled from "styled-components";
-
 
3
import { useForm } from "react-hook-form";
3
import { useForm } from "react-hook-form";
4
import SwitchInput from "./switch-input/SwitchInput";
4
import { axios } from "../../../utils";
5
import { addNotification } from "../../../redux/notification/notification.actions";
-
 
6
import { connect } from "react-redux";
5
import { connect } from "react-redux";
-
 
6
import { addNotification } from "../../../redux/notification/notification.actions";
-
 
7
 
-
 
8
// Components
-
 
9
import SwitchInput from "./switch-input/SwitchInput";
7
import Spinner from "../../../shared/loading-spinner/Spinner";
10
import Spinner from "../../../shared/loading-spinner/Spinner";
8
import {axios} from "../../../utils";
-
 
Línea 9... Línea 11...
9
 
11
 
10
const StyledSettings = styled.div`
12
const PRIVACY_OPTIONS = [
11
  .notbat {
13
  {
12
    display: flex;
-
 
13
    justify-content: space-between;
14
    label: '',
14
    align-items: center;
15
    input_name: '',
15
    padding: 1rem;
-
 
16
    border-bottom: 1px solid #f2f2f2;
-
 
17
    position: relative;
16
    value: false
18
  }
17
  }
Línea 19... Línea -...
19
`;
-
 
20
 
-
 
21
const StyledSpinnerContainer = styled.div`
-
 
22
  position: absolute;
-
 
23
  left: 0;
-
 
24
  top: 0;
-
 
25
  width: 100%;
-
 
26
  height: 100%;
-
 
27
  background: rgba(255, 255, 255, 0.4);
-
 
28
  display: flex;
-
 
29
  justify-content: center;
-
 
30
  align-items: center;
-
 
31
  z-index: 300;
-
 
32
`;
18
]
33
 
-
 
34
const Privacy = (props) => {
-
 
Línea 35... Línea -...
35
  // redux destructuring
-
 
36
  const { addNotification } = props;
-
 
37
 
-
 
38
  const { register, handleSubmit, setError, setValue } = useForm();
19
 
-
 
20
const Privacy = ({ addNotification }) => {
-
 
21
 
Línea 39... Línea 22...
39
 
22
  const [loading, setLoading] = useState(false);
40
  // states
23
  const [options, setOptions] = useState(PRIVACY_OPTIONS);
41
  const [loading, setLoading] = useState(false);
24
  const { handleSubmit } = useForm();
-
 
25
 
42
 
26
  const handleOnSubmit = () => {
43
  const handleOnSubmit = async (data) => {
27
    setLoading(true);
44
    setLoading(true);
28
    const formData = new FormData();
-
 
29
 
-
 
30
    options.map(({ input_name, value }) => {
45
    const formData = new FormData();
31
      if (value) return formData.append(input_name, value)
46
    Object.entries(data).map(([key, value]) => {
32
    });
47
      if (value) formData.append(key, value);
33
 
48
    });
-
 
49
    await axios.post("/account-settings/privacy", formData).then((response) => {
-
 
50
      const resData = response.data;
-
 
51
      if (resData.success) {
-
 
52
        addNotification({
-
 
53
          style: "success",
34
    axios
54
          msg: resData.data,
-
 
55
        });
-
 
56
      } else {
35
      .post("/account-settings/privacy", formData)
57
        if (typeof resData.data === "object") {
-
 
58
          resData.data;
36
      .then(({ data: response }) => {
59
          Object.entries(resData.data).map(([key, value]) => {
-
 
60
            setError(key, { type: "manual", message: value[0] });
-
 
61
          });
37
        if (!response.success) {
62
        } else {
38
          typeof response.data === "string"
63
          const errorMessage =
-
 
64
            typeof resData.data === "string"
-
 
65
              ? resData.data
-
 
66
              : "Ha ocurrido un error, Por favor intente mas tarde";
39
            ? addNotification({ style: "danger", msg: response.data })
67
          addNotification({
40
            : Object
-
 
41
              .entries(response.data)
68
            style: "danger",
42
              .map(([key, value]) => addNotification({ style: "success", msg: `${key} error: ${value} ` }))
69
            msg: errorMessage,
-
 
70
          });
43
 
71
        }
44
        }
Línea 72... Línea 45...
72
      }
45
        addNotification({ style: "success", msg: response.data })
73
    });
46
      })
-
 
47
      .finally(() => setLoading(false))
74
    setLoading(false);
48
  };
75
  };
49
 
76
 
50
  useEffect(() => {
-
 
51
    setLoading(true)
77
  useEffect(async () => {
52
    axios
78
    setLoading(true);
53
      .get("/account-settings/privacy")
-
 
54
      .then(({ data: response }) => {
-
 
55
        if (response.success) {
-
 
56
          Object
-
 
57
            .entries(response.data)
-
 
58
            .map(([key, value]) =>
79
    await axios.get("/account-settings/privacy").then((response) => {
59
              setOptions((prevNotifications) => prevNotifications
80
      const resData = response.data;
60
                .map((notification) =>
81
      if (resData.success) {
61
                  notification.input_name === key
82
        Object.entries(resData.data).map(([key, value]) => {
62
                    ? { ...notification, value: Boolean(value) }
-
 
63
                    : notification
83
          setValue(key, value);
64
                )))
Línea 84... Línea 65...
84
        });
65
        }
85
      }
66
      })
86
    });
67
      .finally(() => setLoading(false))
87
    setLoading(false);
68
 
88
  }, []);
69
  }, []);
89
 
70
 
90
  return (
-
 
91
    <StyledSettings className="acc-setting">
-
 
92
      <h3>Notificaciones</h3>
-
 
93
      <form onSubmit={handleSubmit(handleOnSubmit)}>
-
 
94
        <div className="notbat">
-
 
95
          Mostrar en la búsqueda
-
 
96
          <SwitchInput register={register} name="show_in_search" />
-
 
97
        </div>
71
  return (
98
        <div className="save-stngs pd2">
-
 
99
          <ul>
-
 
100
            <li>
72
    <div className="acc-setting">
101
              <button type="submit" className="btn-save-basic">
73
      <h3>Notificaciones</h3>
102
                Guardar
74
      <form onSubmit={handleSubmit(handleOnSubmit)}>
-
 
75
        <div className="notbat">
103
              </button>
76
          Mostrar en la búsqueda
104
            </li>
-
 
105
          </ul>
-
 
106
        </div>
77
          <SwitchInput />
107
        {/* <!--save-stngs end--> */}
-
 
108
        {/* <?php echo $this->form()->closeTag($form); ?>	 */}
78
        </div>
109
      </form>
-
 
110
      {loading && (
79
        <button type="submit" className="btn btn-primary">
111
        <StyledSpinnerContainer>
80
          Guardar
Línea 112... Línea -...
112
          <Spinner />
-
 
113
        </StyledSpinnerContainer>
-
 
114
      )}
-
 
115
    </StyledSettings>
-
 
116
  );
81
        </button>
117
};
82
      </form>
118
 
83
      {loading && <Spinner />}
Línea 119... Línea 84...
119
// const mapStateToProps = (state) => ({
84
    </div>