| 3736 |
stevensc |
1 |
import { useMemo } from 'react';
|
|
|
2 |
import { api } from '@api';
|
|
|
3 |
import { useApi, useAlert } from '@shared/hooks';
|
|
|
4 |
import { updateBasicSettings } from '@account-settings/services';
|
|
|
5 |
import { parseHelperToSelect } from '@shared/utils';
|
|
|
6 |
|
|
|
7 |
const getTimezones = async () => {
|
|
|
8 |
return await api.get('/helpers/timezones');
|
|
|
9 |
};
|
|
|
10 |
|
|
|
11 |
const getBasicSettings = async () => {
|
|
|
12 |
return await api.get('/account-settings/basic');
|
|
|
13 |
};
|
|
|
14 |
|
|
|
15 |
export function useBasicSettings() {
|
|
|
16 |
const { showSuccess, showError } = useAlert();
|
|
|
17 |
|
|
|
18 |
const { data: timezones, loading: loadingTimezones } = useApi(getTimezones, {
|
|
|
19 |
autoFetch: true
|
|
|
20 |
});
|
|
|
21 |
const { data: formValues, loading: loadingFormValues } = useApi(getBasicSettings, {
|
|
|
22 |
autoFetch: true
|
|
|
23 |
});
|
|
|
24 |
|
|
|
25 |
const timezonesOptions = useMemo(() => parseHelperToSelect(timezones), [timezones]);
|
|
|
26 |
|
|
|
27 |
const { execute: executeUpdate } = useApi(updateBasicSettings, {
|
|
|
28 |
onSuccess: (message) => {
|
|
|
29 |
showSuccess(message);
|
|
|
30 |
},
|
|
|
31 |
onError: (error) => {
|
|
|
32 |
showError(error.message || 'Error al actualizar la información');
|
|
|
33 |
}
|
|
|
34 |
});
|
|
|
35 |
|
|
|
36 |
const updateSettings = async (data) => {
|
|
|
37 |
const parsedData = {
|
|
|
38 |
...data,
|
|
|
39 |
is_adult: data.is_adult ? 'y' : 'n'
|
|
|
40 |
};
|
|
|
41 |
await executeUpdate(parsedData);
|
|
|
42 |
};
|
|
|
43 |
|
|
|
44 |
return {
|
|
|
45 |
formValues: formValues
|
|
|
46 |
? {
|
|
|
47 |
...formValues,
|
|
|
48 |
is_adult: formValues.is_adult === 'y'
|
|
|
49 |
}
|
|
|
50 |
: null,
|
|
|
51 |
timezones: timezonesOptions,
|
|
|
52 |
loading: loadingTimezones || loadingFormValues,
|
|
|
53 |
updateSettings
|
|
|
54 |
};
|
|
|
55 |
}
|