Skip to content

Commit

Permalink
Added front logic (create, delete and getByUser events/contacts b00tc…
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmePo committed Jan 4, 2025
1 parent 674e9d8 commit 1321a15
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 0 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as Errors from "../../errors";

const createEmergencyContact = async (contactName, phone, relationship, userId) => {
try {
const response = await fetch(`/api/emergency-contacts/users/${userId}`, {
method: "POST",
headers: {
"Content-type": "application/json",
},
body: JSON.stringify({ contactName, phone, relationship }),
});

if (!response.ok) {
const errorData = await response.json();
throw new Errors.ApiError(errorData.message);
}

return await response.json(); // Devuelve la respuesta JSON si fue exitosa
} catch (err) {
if (err instanceof TypeError) {
throw new Errors.ServerError("Server is not connected");
}
throw new Errors.UnexpectedError(err.message || "Unexpected error occurred");
}
};

export default createEmergencyContact;
27 changes: 27 additions & 0 deletions staff/borja-garcia/project/appProject/src/logic/createEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as Errors from "../../errors";

const createEvent = async (eventName, startDateTime, duration, color, userId) => {
try {
const response = await fetch(`/api/events/users/${userId}}`, {
method: "POST",
headers: {
"Content-type": "application/json",
},
body: JSON.stringify({ eventName, startDateTime, duration, color }),
});

if (!response.ok) {
const errorData = await response.json();
throw new Errors.ApiError(errorData.message);
}

return await response.json(); // Devuelve la respuesta JSON si fue exitosa
} catch (err) {
if (err instanceof TypeError) {
throw new Errors.ServerError("Server is not connected");
}
throw new Errors.UnexpectedError(err.message || "Unexpected error occurred");
}
};

export default createEvent;
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as Errors from "../../errors";

const deleteEmergencyContactById = async (contactId) => {
try {
const response = await fetch(`/api/emergency-contacts/${contactId}`, {
method: "DELETE",
headers: {
"Content-type": "application/json",
},
});

if (!response.ok) {
const errorData = await response.json();
throw new Errors.ServerError(errorData.error || "Error al crear el usuario");
}

return await response.json(); // Devuelve la respuesta JSON si fue exitosa
} catch (err) {
if (err instanceof TypeError) {
throw new Errors.ServerError("Server is not connected");
}
throw new Errors.UnexpectedError(err.message || "Unexpected error occurred");
}
};

export default deleteEmergencyContactById;
26 changes: 26 additions & 0 deletions staff/borja-garcia/project/appProject/src/logic/deleteEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as Errors from "../../errors";

const deleteEventById = async (eventId) => {
try {
const response = await fetch(`/api/events/${eventId}`, {
method: "DELETE",
headers: {
"Content-type": "application/json",
},
});

if (!response.ok) {
const errorData = await response.json();
throw new Errors.ServerError(errorData.error || "Error al crear el usuario");
}

return await response.json(); // Devuelve la respuesta JSON si fue exitosa
} catch (err) {
if (err instanceof TypeError) {
throw new Errors.ServerError("Server is not connected");
}
throw new Errors.UnexpectedError(err.message || "Unexpected error occurred");
}
};

export default deleteEventById;
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as Errors from "../../errors";

const getEmerContByUser = async (userId) => {
try {
const response = await fetch(`/api/emergency-contacts/user/${userId}`);
if (!response.ok) {
throw new Errors.ApiError("Error fetching emergency contacts", response.status);
}
return await response.json(); // Devuelve la respuesta JSON si fue exitosa
} catch (err) {
if (err instanceof TypeError) {
throw new Errors.ServerError("Server is not connected");
}
throw new Errors.UnexpectedError(err.message || "Unexpected error occurred");
}
};

export default getEmerContByUser;
18 changes: 18 additions & 0 deletions staff/borja-garcia/project/appProject/src/logic/getEventsByUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as Errors from "../../errors";

const getEventsByUser = async (userId) => {
try {
const response = await fetch(`/api/events/user/${userId}`);
if (!response.ok) {
throw new Errors.ApiError("Error fetching events", response.status);
}
return await response.json(); // Devuelve la respuesta JSON si fue exitosa
} catch (err) {
if (err instanceof TypeError) {
throw new Errors.ServerError("Server is not connected");
}
throw new Errors.UnexpectedError(err.message || "Unexpected error occurred");
}
};

export default getEventsByUser;
Empty file.
Empty file.
Empty file.

0 comments on commit 1321a15

Please sign in to comment.