Skip to content

Commit

Permalink
integrated delete with backend
Browse files Browse the repository at this point in the history
  • Loading branch information
VaibhavSingh8 committed Oct 10, 2024
1 parent af0bde2 commit 7bd80a8
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 22 deletions.
2 changes: 1 addition & 1 deletion groups/createElements.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const createCard = (
.querySelector('.delete-group')
.addEventListener('click', (e) => {
e.stopPropagation();
onDelete(rawGroup.id);
onDelete(rawGroup.id, rawGroup.roleId);
});
}

Expand Down
70 changes: 51 additions & 19 deletions groups/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
getDiscordGroupIdsFromSearch,
getParamValueFromURL,
setParamValueInURL,
deleteDiscordGroupRole,
} from './utils.js';

const QUERY_PARAM_KEY = {
Expand All @@ -46,10 +47,14 @@ const handler = {
.filter(
(ng) => JSON.stringify(oldGroups?.[ng.id]) !== JSON.stringify(ng),
)
.filter((ng) => dataStore.filteredGroupsIds.includes(ng.id));
.filter((ng) => dataStore.filteredGroupsIds.includes(ng.id))
.filter((ng) => !ng.isDeleted);
diffGroups.forEach((group) =>
renderGroupById({
group,
group: {
...dataStore.groups[group.id],
roleId: dataStore.groups[group.id].roleid,
},
cardOnClick: () => groupCardOnAction(group.id),
isSuperUser: dataStore.isSuperUser,
}),
Expand All @@ -64,7 +69,6 @@ const handler = {
renderAllGroups({
cardOnClick: groupCardOnAction,
});
// if (isDev && (!value || value.length == 0)) renderNoGroupFound();
break;
case 'search':
if (isDev) {
Expand Down Expand Up @@ -179,8 +183,9 @@ const afterAuthentication = async () => {

await Promise.all([getDiscordGroups(), getUserGroupRoles()]).then(
([groups, roleData]) => {
dataStore.filteredGroupsIds = groups.map((group) => group.id);
dataStore.groups = groups.reduce((acc, group) => {
const nonDeletedGroups = groups.filter((group) => !group.isDeleted);
dataStore.filteredGroupsIds = nonDeletedGroups.map((group) => group.id);
dataStore.groups = nonDeletedGroups.reduce((acc, group) => {
let title = group.rolename
.replace('group-', '')
.split('-')
Expand Down Expand Up @@ -257,6 +262,7 @@ function updateGroup(id, group) {
...group,
},
};
console.log(`Updated group ${id}:`, dataStore.groups[id]); // Add this line for debugging
}

function groupCardOnAction(id) {
Expand All @@ -281,30 +287,56 @@ function groupCardOnAction(id) {
function renderAllGroups({ cardOnClick }) {
const mainContainer = document.querySelector('.group-container');
mainContainer.innerHTML = '';
if (dataStore.filteredGroupsIds.length === 0 && isDev) {
const nonDeletedGroups = dataStore.filteredGroupsIds.filter(
(id) => !dataStore.groups[id].isDeleted,
);
if (nonDeletedGroups.length === 0 && isDev) {
renderNoGroupFound();
} else {
dataStore.filteredGroupsIds.forEach((id) =>
renderGroupById({
group: dataStore.groups[id],
cardOnClick: () => cardOnClick(id),
onDelete: isDev ? showDeleteModal : undefined,
isSuperUser: dataStore.isSuperUser && isDev,
}),
);
nonDeletedGroups.forEach((id) => {
const group = dataStore.groups[id];
if (!group.isDeleted) {
renderGroupById({
group: group,
cardOnClick: () => cardOnClick(id),
onDelete: isDev ? showDeleteModal : undefined,
isSuperUser: dataStore.isSuperUser && isDev,
});
}
});
}
}

function showDeleteModal(groupId) {
function showDeleteModal(groupId, roleId) {
if (!isDev) return;
renderDeleteConfirmationModal({
onClose: () => {
removeDeleteConfirmationModal();
},
onConfirm: () => {
console.log(`Confirming delete for group ${groupId}`);
removeDeleteConfirmationModal();
// Will Implement actual delete functionality here
onConfirm: async () => {
try {
await deleteDiscordGroupRole(groupId, roleId);
showToaster('Group deleted successfully');

updateGroup(groupId, { isDeleted: true });

dataStore.filteredGroupsIds = dataStore.filteredGroupsIds.filter(
(id) => id !== groupId,
);
// const { [groupId]: deletedGroup, ...remainingGroups } =
// dataStore.groups;
// dataStore.groups = remainingGroups;
// dataStore.filteredGroupsIds = dataStore.filteredGroupsIds.filter(
// (id) => id !== groupId,
// );
renderAllGroups({
cardOnClick: groupCardOnAction,
});
} catch (error) {
showToaster(error.message || 'Failed to delete group');
} finally {
removeDeleteConfirmationModal();
}
},
});
}
Expand Down
29 changes: 27 additions & 2 deletions groups/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const BASE_URL = window.API_BASE_URL; // REPLACE WITH YOUR LOCALHOST URL FOR TESTING LOCAL BACKEND

// const BASE_URL = window.API_BASE_URL; // REPLACE WITH YOUR LOCALHOST URL FOR TESTING LOCAL BACKEND
const BASE_URL = 'http://localhost:3000';
async function getMembers() {
try {
const res = await fetch(`${BASE_URL}/users/`, {
Expand Down Expand Up @@ -117,6 +117,30 @@ async function removeRoleFromMember(roleId, discordId) {
}
}

async function deleteDiscordGroupRole(groupId, roleId) {
try {
const res = await fetch(`${BASE_URL}/discord-actions/groups/${groupId}`, {
method: 'DELETE',
credentials: 'include',
headers: {
'Content-type': 'application/json',
},
body: JSON.stringify({ roleid: roleId }),
});

if (!res.ok) {
const errorResponse = await res.json();
throw new Error(
`Failed to delete group role: ${JSON.stringify(errorResponse.error)}`,
);
}

return await res.json();
} catch (err) {
throw err;
}
}

function removeGroupKeywordFromDiscordRoleName(groupName) {
if (/^group.*/.test(groupName)) {
const splitNames = groupName.split('-');
Expand Down Expand Up @@ -168,4 +192,5 @@ export {
getDiscordGroupIdsFromSearch,
getParamValueFromURL,
setParamValueInURL,
deleteDiscordGroupRole,
};

0 comments on commit 7bd80a8

Please sign in to comment.