Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
3719 stevensc 1
import React, { useEffect, useState } from 'react';
2
import { useForm } from 'react-hook-form';
3
import { connect } from 'react-redux';
4
 
5
import { addNotification } from '@app/redux/notification/notification.actions';
6
 
7
import { axios } from '@utils';
8
import Widget from '@components/UI/Widget';
9
import Spinner from '@components/UI/Spinner';
10
import SwitchInput from '@components/UI/SwitchInput';
11
 
12
const PRIVACY_OPTIONS = [
13
  {
14
    label: 'Mostrar en la búsqueda',
15
    input_name: 'show_in_search',
16
    value: false
17
  }
18
];
19
 
20
const Privacy = ({ addNotification }) => {
21
  const [loading, setLoading] = useState(false);
22
  const [options, setOptions] = useState(PRIVACY_OPTIONS);
23
  const { handleSubmit } = useForm();
24
 
25
  const handleOnSubmit = () => {
26
    setLoading(true);
27
    const formData = new FormData();
28
 
29
    options.forEach(({ input_name, value }) => formData.append(input_name, value ? 'y' : 'n'));
30
 
31
    axios
32
      .post('/account-settings/privacy', formData)
33
      .then((response) => {
34
        const { success, data } = response.data;
35
        if (!success) {
36
          typeof data === 'string'
37
            ? addNotification({ style: 'danger', msg: data })
38
            : Object.entries(data).map(([key, value]) =>
39
                addNotification({
40
                  style: 'success',
41
                  msg: `${key} error: ${value} `
42
                })
43
              );
44
        }
45
        addNotification({ style: 'success', msg: data });
46
      })
47
      .finally(() => setLoading(false));
48
  };
49
 
50
  const handleChecked = (value, element) => {
51
    setOptions((prevNotifications) =>
52
      prevNotifications.map((notification) =>
53
        notification.input_name === element
54
          ? { ...notification, value: Boolean(value) }
55
          : notification
56
      )
57
    );
58
  };
59
 
60
  useEffect(() => {
61
    setLoading(true);
62
    axios
63
      .get('/account-settings/privacy')
64
      .then((response) => {
65
        const { success, data } = response.data;
66
        if (success) {
67
          Object.entries(data).map(([key, value]) =>
68
            setOptions((prevOption) =>
69
              prevOption.map((option) =>
70
                option.input_name === key ? { ...option, value: Boolean(value) } : option
71
              )
72
            )
73
          );
74
        }
75
      })
76
      .finally(() => setLoading(false));
77
  }, []);
78
 
79
  if (loading) {
80
    return <Spinner />;
81
  }
82
 
83
  return (
84
    <Widget>
85
      <Widget.Header title='Privacidad' />
86
 
87
      <Widget.Body>
88
        <form onSubmit={handleSubmit(handleOnSubmit)}>
89
          {options.map((option, index) => {
90
            return (
91
              <div className='notbat' key={index}>
92
                <span>{option.label}</span>
93
                <SwitchInput
94
                  isChecked={option.value}
95
                  setValue={(value) => handleChecked(value, option.input_name)}
96
                />
97
              </div>
98
            );
99
          })}
100
 
101
          <button type='submit' className='btn btn-primary mt-3'>
102
            Guardar
103
          </button>
104
        </form>
105
      </Widget.Body>
106
    </Widget>
107
  );
108
};
109
 
110
const mapDispatchToProps = {
111
  addNotification: (notification) => addNotification(notification)
112
};
113
 
114
export default connect(null, mapDispatchToProps)(Privacy);