Commit 004fb2d3 authored by Jakob Lund Johannessen's avatar Jakob Lund Johannessen
Browse files

chore: minor refactoring of my coach

parent 2d62f0c9
Pipeline #172078 failed with stages
in 35 seconds
async function displayCurrentCoach() {
let user = await getCurrentUser();
let coach = null;
if (user.coach) {
response = await sendRequest("GET", user.coach);
const response = await sendRequest("GET", user.coach);
if (!response.ok) {
let data = await response.json();
let alert = createAlert("Could not retrieve coach!", data);
......@@ -19,13 +18,12 @@ async function displayCurrentCoach() {
}
async function displayOffers() {
let user = await getCurrentUser();
let templateOffer = document.querySelector("#template-offer");
let listOffers = document.querySelector("#list-offers");
let status = "p"; // pending
let category = "received";
let category = "received";
let response = await sendRequest("GET", `${HOST}/api/offers/?status=${status}&category=${category}`);
if (!response.ok) {
let data = await response.json();
......@@ -38,16 +36,14 @@ async function displayOffers() {
let li = cloneOffer.querySelector("li");
let span = li.querySelector("span");
span.textContent = `${offer.owner} wants to be your coach`;
let buttons = li.querySelectorAll("button");
let acceptButton = buttons[0];
let declineButton = buttons[1];
//acceptButton.id = `btn-accept-${offer.id}`;
acceptButton.addEventListener("click", async (event) => await acceptOffer(event, offer.url, offer.owner));
acceptButton.addEventListener("click", async (event) => acceptOffer(event, offer.url, offer.owner));
//declineButton.id = `btn-decline-${offer.id}`;
declineButton.addEventListener("click", async (event) => await declineOffer(event, offer.url));
declineButton.addEventListener("click", async (event) => declineOffer(event, offer.url));
listOffers.appendChild(li);
}
......@@ -60,25 +56,24 @@ async function displayOffers() {
}
}
async function acceptOffer(event, offerUrl, ownerUsername) {
let button = event.currentTarget;
let body = {"status": "d"};
async function acceptOffer(_event, offerUrl, ownerUsername) {
let body = { "status": "d" };
let response = await sendRequest("PATCH", offerUrl, body);
const response = await sendRequest("PATCH", offerUrl, body);
if (!response.ok) {
let data = await response.json();
let alert = createAlert("Could not accept offer!", data);
document.body.prepend(alert);
} else {
let response = await sendRequest("GET", `${HOST}/api/users/${ownerUsername}/`);
let owner = await response.json();
const ownerResponse = await sendRequest("GET", `${HOST}/api/users/${ownerUsername}/`);
let owner = await ownerResponse.json();
let user = await getCurrentUser();
let body = {'coach': owner.url};
response = await sendRequest("PATCH", user.url, body);
let requestBody = { 'coach': owner.url };
const patchResponse = await sendRequest("PATCH", user.url, requestBody);
if (!response.ok) {
let data = await response.json();
if (!patchResponse.ok) {
let data = await patchResponse.json();
let alert = createAlert("Could not update coach!", data);
document.body.prepend(alert);
} else {
......@@ -89,9 +84,8 @@ async function acceptOffer(event, offerUrl, ownerUsername) {
}
async function declineOffer(event, offerUrl) {
let button = event.currentTarget;
let body = {'status': 'd'};
async function declineOffer(_event, offerUrl) {
let body = { 'status': 'd' };
let response = await sendRequest("PATCH", offerUrl, body);
if (!response.ok) {
......@@ -195,7 +189,7 @@ function cancelCoach() {
return false;
}
async function setCoach() {
async function setCoach(_e) {
// get current user
let user = await getCurrentUser();
let newCoach = document.querySelector("#input-coach").value;
......@@ -234,8 +228,8 @@ window.addEventListener("DOMContentLoaded", async () => {
let buttonSetCoach = document.querySelector("#button-set-coach");
let buttonEditCoach = document.querySelector("#button-edit-coach");
let buttonCancelCoach = document.querySelector("#button-cancel-coach");
buttonSetCoach.addEventListener("click", async (event) => await setCoach(event));
buttonSetCoach.addEventListener("click", async (event) => setCoach(event));
buttonEditCoach.addEventListener("click", editCoach);
buttonCancelCoach.addEventListener("click", cancelCoach);
});
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment