3719 |
stevensc |
1 |
import { axios } from '@utils';
|
|
|
2 |
|
|
|
3 |
export const saveHabit = async (url, habit) => {
|
|
|
4 |
const formData = new FormData();
|
|
|
5 |
Object.entries(habit).forEach(([key, value]) => value && formData.append(key, value));
|
|
|
6 |
const response = await axios.post(url, formData);
|
|
|
7 |
const { data, message, success } = response.data;
|
|
|
8 |
if (!success) throw new Error('Error al guardar el hábito');
|
|
|
9 |
return { data, message };
|
|
|
10 |
};
|
|
|
11 |
|
|
|
12 |
export const editHabit = async (url, habit) => {
|
|
|
13 |
const formData = new FormData();
|
|
|
14 |
Object.entries(habit).forEach(([key, value]) => formData.append(key, value));
|
|
|
15 |
const response = await axios.post(url, formData);
|
|
|
16 |
const { data, message, success } = response.data;
|
|
|
17 |
if (!success) throw new Error('Error al editar el hábito');
|
|
|
18 |
return { data, message };
|
|
|
19 |
};
|
|
|
20 |
|
|
|
21 |
export const deleteHabit = async (url) => {
|
|
|
22 |
const response = await axios.post(url);
|
|
|
23 |
const { data, success } = response.data;
|
|
|
24 |
if (!success) throw new Error('Error al eliminar el hábito');
|
|
|
25 |
return data;
|
|
|
26 |
};
|
|
|
27 |
|
|
|
28 |
export const getTemplate = async (url) => {
|
|
|
29 |
const response = await axios.get(url);
|
|
|
30 |
const { data, success } = response.data;
|
|
|
31 |
if (!success) throw new Error('Error al guardar el hábito');
|
|
|
32 |
return data;
|
|
|
33 |
};
|
|
|
34 |
|
|
|
35 |
export const saveProgress = async (url, progress) => {
|
|
|
36 |
const formData = new FormData();
|
|
|
37 |
Object.entries(progress).forEach(([key, value]) => formData.append(key, value));
|
|
|
38 |
const response = await axios.post(url, formData);
|
|
|
39 |
const { data, message, success } = response.data;
|
|
|
40 |
if (!success) {
|
|
|
41 |
const errMsg = typeof data === 'string' ? data : 'Error al guardar el progreso del hábito';
|
|
|
42 |
throw new Error(errMsg);
|
|
|
43 |
}
|
|
|
44 |
return { data, message };
|
|
|
45 |
};
|
|
|
46 |
|
|
|
47 |
export const updateProgress = async (url, progress) => {
|
|
|
48 |
const formData = new FormData();
|
|
|
49 |
Object.entries(progress).forEach(([key, value]) => formData.append(key, value));
|
|
|
50 |
const response = await axios.post(url, formData);
|
|
|
51 |
const { data, message, success } = response.data;
|
|
|
52 |
if (!success) {
|
|
|
53 |
const errMsg = typeof data === 'string' ? data : 'Error al actualizar el progreso del hábito';
|
|
|
54 |
throw new Error(errMsg);
|
|
|
55 |
}
|
|
|
56 |
return { data, message };
|
|
|
57 |
};
|
|
|
58 |
|
|
|
59 |
export const deleteProgress = async (url) => {
|
|
|
60 |
const response = await axios.post(url);
|
|
|
61 |
const { data, success } = response.data;
|
|
|
62 |
if (!success) {
|
|
|
63 |
const errMsg = typeof data === 'string' ? data : 'Error al eliminar el progreso del hábito';
|
|
|
64 |
throw new Error(errMsg);
|
|
|
65 |
}
|
|
|
66 |
return data;
|
|
|
67 |
};
|