Skip to content

Commit

Permalink
Delete Recipe, Delete User and Discard Recipe fetch functions completed
Browse files Browse the repository at this point in the history
  • Loading branch information
Noxtrah committed May 25, 2024
1 parent 5ffd3e2 commit 945bd65
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
48 changes: 41 additions & 7 deletions scripts/adminPanelReportsTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ async function createSingleReportItem(report) {

const discardButton = createButton('Discard');
discardButton.classList.add('discard-button');
discardButton.addEventListener('click', () => discardRecipe(report.recipe.id));
discardButton.addEventListener('click', () => discardRecipe(report.userRecipeResponse.id));
actionButtons.appendChild(discardButton);

const detailButton = createButton('Detail');
Expand All @@ -137,7 +137,7 @@ async function createSingleReportItem(report) {
const deleteButton = createButton('Delete');
deleteButton.classList.add('delete-button');
deleteButton.addEventListener('click', function() {
deleteRecipe(report);
createWarningPopup(report);
});
actionButtons.appendChild(deleteButton);

Expand All @@ -159,6 +159,26 @@ function createButton(text) {

function discardRecipe(recipeId) {
// Implement discard logic
apiUrl = `https://recipiebeckend.azurewebsites.net/admin/discard-report?id=${recipeId}`
try{
fetch(apiUrl, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json'
}
})
.then(response => response.json())
.then(data => {
console.log('Discard recipe response:', data);
})
.catch(error => {
console.error('Error:', error);
});
}
catch(error){
console.log(error);
}

console.log('Discard recipe with ID:', recipeId);
}

Expand Down Expand Up @@ -285,10 +305,6 @@ function closeDetailSidebar() {
detailSideBarWrapper.remove();
}

function deleteRecipe(report) {
createWarningPopup(report);
}

function createWarningPopup(reportedItem) {
// Create overlay element
const reportRecipeResponse = reportedItem.userRecipeResponse;
Expand Down Expand Up @@ -332,7 +348,8 @@ function createWarningPopup(reportedItem) {
sendReportButton.classList.add('popup-delete-button');

sendReportButton.addEventListener('click', function() {
//Delete Fetch
deleteRecipe(reportRecipeResponse.id);
overlay.remove();
});


Expand All @@ -348,4 +365,21 @@ function createWarningPopup(reportedItem) {

// Append overlay to body
document.body.appendChild(overlay);
}

async function deleteRecipe(recipeID){
apiUrl = `https://recipiebeckend.azurewebsites.net/admin/delete-recipe?id=${recipeID}`
try {
const response = await fetch(apiUrl, {
method: 'DELETE'
});
if (!response.ok) {
throw new Error(`Network response was not ok (status: ${response.status})`);
}
const data = await response.json();
console.log('User deleted successfully:', data);
location.reload();
} catch (error) {
console.error('Error deleting user:', error);
}
}
13 changes: 7 additions & 6 deletions scripts/adminPanelUsersTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ function displayUsers(users) {

userRow.appendChild(nameCell);

// Create cell for title
var nameLastnameCell = document.createElement('td');
nameLastnameCell.classList.add('people-des');
var nameLastnameHeader = document.createElement('p');
nameLastnameHeader.textContent = user.name;
var subnameLastnameParagraph = document.createElement('p');
subnameLastnameParagraph.textContent = user.lastName;
var nameSpan = document.createElement('span');
nameSpan.textContent = user.name;
var lastNameSpan = document.createElement('span');
lastNameSpan.textContent = ' ' + user.lastName; // Add a space between name and last name
nameLastnameHeader.appendChild(nameSpan);
nameLastnameHeader.appendChild(lastNameSpan);
nameLastnameCell.appendChild(nameLastnameHeader);
nameLastnameCell.appendChild(subnameLastnameParagraph);
userRow.appendChild(nameLastnameCell);

// Create cell for status
Expand Down Expand Up @@ -308,6 +308,7 @@ function userDeleteWarnPopup(user){
// Append overlay to body
document.body.appendChild(overlay);
deleteUser(user.id);
overlay.remove();
}

async function deleteUser(userID) {
Expand Down

0 comments on commit 945bd65

Please sign in to comment.