Proyectos de Subversion LeadersLinked - SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3736 stevensc 1
import { useState, useEffect } from 'react';
2
import { useApi, useAlert } from '@shared/hooks';
3
import { getPrivacySettings, updatePrivacySettings } from '@account-settings/services';
4
 
5
const PRIVACY_OPTIONS = [
6
  {
7
    label: 'Mostrar en la búsqueda',
8
    input_name: 'show_in_search',
9
    value: false
10
  }
11
];
12
 
13
export function usePrivacySettings() {
14
  const { showSuccess, showError } = useAlert();
15
  const [options, setOptions] = useState(PRIVACY_OPTIONS);
16
 
17
  const { loading: isLoading, execute: fetchSettings } = useApi(getPrivacySettings, {
18
    autoFetch: true,
19
    onSuccess: (response) => {
20
      if (response.success && response.data) {
21
        Object.entries(response.data).forEach(([key, value]) => {
22
          setOptions((prevOptions) =>
23
            prevOptions.map((option) =>
24
              option.input_name === key ? { ...option, value: Boolean(value) } : option
25
            )
26
          );
27
        });
28
      }
29
    },
30
    onError: (error) => {
31
      showError(error.message || 'Error al cargar las configuraciones de privacidad');
32
    }
33
  });
34
 
35
  const { loading: isUpdating, execute: executeUpdate } = useApi(updatePrivacySettings, {
36
    onSuccess: (response) => {
37
      if (response.success) {
38
        showSuccess(response.data);
39
      } else {
40
        showError(response.data || 'Error al actualizar la privacidad');
41
      }
42
    },
43
    onError: (error) => {
44
      showError(error.message || 'Error al actualizar la privacidad');
45
    }
46
  });
47
 
48
  const handleChecked = (value, inputName) => {
49
    setOptions((prevOptions) =>
50
      prevOptions.map((option) =>
51
        option.input_name === inputName ? { ...option, value: Boolean(value) } : option
52
      )
53
    );
54
  };
55
 
56
  const updateSettings = async () => {
57
    const settingsData = {};
58
    options.forEach(({ input_name, value }) => {
59
      settingsData[input_name] = value;
60
    });
61
    await executeUpdate(settingsData);
62
  };
63
 
64
  return {
65
    options,
66
    isLoading,
67
    isUpdating,
68
    handleChecked,
69
    updateSettings
70
  };
71
}