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 { connect } from 'react-redux';
3
import { useForm } from 'react-hook-form';
4
import { styled } from '@mui/material';
5
 
6
import { axios } from '@utils';
7
import { addNotification } from '@app/redux/notification/notification.actions';
8
 
9
import Spinner from '@components/UI/Spinner';
10
import UbicationInput from '@components/UI/inputs/UbicationInput';
11
import FormErrorFeedback from '@components/UI/form/FormErrorFeedback';
12
 
13
const StyledSpinnerContainer = styled('div')`
14
  position: absolute;
15
  left: 0;
16
  top: 0;
17
  width: 100%;
18
  height: 100%;
19
  background: rgba(255, 255, 255, 0.4);
20
  display: flex;
21
  justify-content: center;
22
  align-items: center;
23
  z-index: 300;
24
`;
25
 
26
const Location = ({ addNotification }) => {
27
  const [loading, setLoading] = useState(false);
28
 
29
  const {
30
    register,
31
    handleSubmit,
32
    setValue,
33
    clearErrors,
34
    watch,
35
    setError,
36
    formState: { errors }
37
  } = useForm();
38
 
39
  const getLocations = () => {
40
    axios
41
      .get('account-settings/location')
42
      .then((response) => {
43
        const { data, success } = response.data;
44
 
45
        if (!success) {
46
          throw new Error('Error interno. Por favor, intente mas tarde');
47
        }
48
 
49
        Object.entries(data).forEach(([key, value]) => {
50
          setValue(key, value);
51
        });
52
      })
53
      .catch((err) => {
54
        addNotification({ style: 'danger', msg: err.message });
55
      });
56
  };
57
 
58
  const getAddress = (address) => {
59
    Object.entries(address).forEach(([key, value]) => {
60
      if (value) {
61
        register(key);
62
        clearErrors(key);
63
        setValue(key, value);
64
      }
65
    });
66
  };
67
 
68
  const onSubmit = async (data) => {
69
    setLoading(true);
70
    const formData = new FormData();
71
    Object.entries(data).forEach(([key, value]) => formData.append(key, value));
72
 
73
    axios.post('/account-settings/location', formData).then((response) => {
74
      const { data, success } = response.data;
75
 
76
      if (success) {
77
        addNotification({
78
          style: 'success',
79
          msg: data.message
80
        });
81
      } else {
82
        if (typeof data === 'object') {
83
          Object.entries(data).forEach(([key, value]) => {
84
            setError(key, { type: 'manual', message: value[0] });
85
          });
86
        } else {
87
          const errorMessage =
88
            typeof data === 'string' ? data : 'Ha ocurrido un error, Por favor intente mas tarde';
89
          addNotification({
90
            style: 'danger',
91
            msg: errorMessage
92
          });
93
        }
94
      }
95
    });
96
    setLoading(false);
97
  };
98
 
99
  useEffect(() => {
100
    getLocations();
101
 
102
    register('formatted_address', {
103
      required: 'por favor seleccione una ubicación correcta'
104
    });
105
  }, []);
106
 
107
  return (
108
    <div className='settings-container'>
109
      <h2>Cambiar Ubicación</h2>
110
      <div className='acc-setting_content'>
111
        <form onSubmit={handleSubmit(onSubmit)}>
112
          <div className='d-flex flex-wrap' style={{ gap: '1rem' }}>
113
            <div className='cp-field cp-field2'>
114
              <label htmlFor='first_name'>Ubicación</label>
115
              <div className='cpp-fiel'>
116
                <UbicationInput
117
                  onGetAddress={getAddress}
118
                  settedQuery={watch('formatted_address')}
119
                />
120
                <i className='fa fa-map-marker'></i>
121
              </div>
122
              {<FormErrorFeedback>{errors?.formatted_address?.message}</FormErrorFeedback>}
123
            </div>
124
            <div className='col-2 mx-auto d-flex align-items-center justify-content-center'>
125
              <button type='submit' className='btn btn-secondary mt-3'>
126
                Guardar
127
              </button>
128
            </div>
129
          </div>
130
        </form>
131
      </div>
132
      {loading && (
133
        <StyledSpinnerContainer>
134
          <Spinner />
135
        </StyledSpinnerContainer>
136
      )}
137
    </div>
138
  );
139
};
140
 
141
const mapDispatchToProps = {
142
  addNotification: (notification) => addNotification(notification)
143
};
144
 
145
export default connect(null, mapDispatchToProps)(Location);