Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restrict approve and reject buttons on the Task Request detail page to super_user only #896

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions mock-data/taskRequests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ const githubIssue = {
performed_via_github_app: null,
state_reason: 'completed',
};

const individualTaskDetail = {
message: 'task returned successfully',
taskData: {
Expand Down Expand Up @@ -306,7 +305,6 @@ const userInformationTaskCreation = {
},
},
};

const userInformation = {
message: 'User returned successfully!',
user: {
Expand Down Expand Up @@ -339,7 +337,29 @@ const userInformation = {
created_at: 1698684157040,
},
};

const superUserData = {
AnujChhikara marked this conversation as resolved.
Show resolved Hide resolved
id: 'V4rqL1aDecNGoa1IxiCu',
incompleteUserDetails: false,
discordId: '12345',
roles: {
archived: false,
super_user: true,
},
linkedin_id: 'kotesh',
last_name: 'Mudila',
yoe: '5',
github_display_name: 'kotesh Mudila',
company_name: 'Juniper networks ',
github_id: 'kotesh-arya',
designation: 'Front end engineer',
twitter_id: 'Codesh_',
first_name: 'Kotesh',
username: 'kotesh',
picture: {
publicId: 'profile/w2XR9Gkid6Kg5xCJ5Elm/rzh3cwff7hh7srvg63mb',
url: 'https://res.cloudinary.com/realdevsquad/image/upload/v1692990078/profile/w2XR9Gkid6Kg5xCJ5Elm/rzh3cwff7hh7srvg63mb.png',
},
};
vinit717 marked this conversation as resolved.
Show resolved Hide resolved
const defaultMockResponseHeaders = {
status: 200,
contentType: 'application/json',
Expand All @@ -351,6 +371,7 @@ const defaultMockResponseHeaders = {
};

const urlMappings = {
'https://staging-api.realdevsquad.com/users/self': superUserData,
'https://api.realdevsquad.com/taskRequests/dM5wwD9QsiTzi7eG7Oq5':
individualTaskReqDetail,
'https://api.realdevsquad.com/taskRequests/dM5wwD9QsiTzi7eG7Oq6':
Expand Down
5 changes: 4 additions & 1 deletion task-requests/details/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ <h4 class="requestors__container__title">Requestors</h4>
</div>
</div>
<div class="reject__container">
<button id="reject-button" class="request-details__reject__button">
<button
id="reject-button"
class="request-details__reject__button hidden"
>
AnujChhikara marked this conversation as resolved.
Show resolved Hide resolved
Reject
</button>
</div>
Expand Down
62 changes: 46 additions & 16 deletions task-requests/details/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const API_BASE_URL =
: window.API_BASE_URL;

let taskRequest;
let self_user;
Achintya-Chatterjee marked this conversation as resolved.
Show resolved Hide resolved

const taskRequestSkeleton = document.querySelector('.taskRequest__skeleton');
const container = document.querySelector('.container');
Expand All @@ -23,6 +24,19 @@ const errorMessage =
'The requested operation could not be completed. Please try again later.';
let taskId;

async function getSelfUser() {
const res = await fetch(`${API_BASE_URL}/users/self`, {
method: 'GET',
credentials: 'include',
headers: {
'Content-type': 'application/json',
},
});

const self_user = await res.json();
return self_user;
}
AnujChhikara marked this conversation as resolved.
Show resolved Hide resolved

function renderTaskRequestDetails(taskRequest) {
taskRequestContainer.append(
createCustomElement({
Expand Down Expand Up @@ -200,17 +214,26 @@ async function updateTaskRequest(action, userId) {
}
}

function getActionButton(requestor) {
function getActionButton(requestor, taskRequest) {
const isSuperUser = self_user.roles['super_user'];
Achintya-Chatterjee marked this conversation as resolved.
Show resolved Hide resolved
if (!isSuperUser) {
return createCustomElement({
tagName: 'p',
textContent:
taskRequest.status[0].toUpperCase() +
taskRequest.status.slice(1).toLowerCase(),
class: ['requestors__container__list__status'],
});
}

Achintya-Chatterjee marked this conversation as resolved.
Show resolved Hide resolved
if (taskRequest?.status === taskRequestStatus.APPROVED) {
if (taskRequest.approvedTo === requestor?.user?.id) {
return createCustomElement({
tagName: 'p',
textContent: 'Approved',
class: ['requestors__container__list__approved'],
});
} else {
return '';
}
return taskRequest.approvedTo === requestor?.user?.id
? createCustomElement({
tagName: 'p',
textContent: 'Approved',
class: ['requestors__container__list__status'],
})
: '';
}
return createCustomElement({
tagName: 'button',
Expand Down Expand Up @@ -279,14 +302,17 @@ async function renderRequestors(taskRequest) {
createCustomElement({
tagName: 'div',
child: [
taskRequest.status !== 'DENIED' ? getActionButton(requestor) : '',
taskRequest.status !== 'DENIED'
? getActionButton(requestor, taskRequest)
: createCustomElement({
tagName: 'p',
textContent: 'Denied',
class: ['requestors__container__list__status'],
}),
],
}),
],
});
const avatarDiv = userDetailsDiv.querySelector(
'.requestors__container__list__userDetails__avatar',
);
requestorsContainer.append(userDetailsDiv);
});
}
Expand Down Expand Up @@ -410,14 +436,17 @@ const renderGithubIssue = async () => {
);
};
const renderRejectButton = (taskRequest) => {
if (self_user.roles['super_user']) {
rejectButton.classList.remove('hidden');
}
Achintya-Chatterjee marked this conversation as resolved.
Show resolved Hide resolved
if (taskRequest?.status !== 'PENDING') {
rejectButton.disabled = true;
rejectButton.classList.add('hidden');
}

rejectButton.addEventListener('click', async () => {
const res = await updateTaskRequest(TaskRequestAction.REJECT);
if (res?.ok) {
rejectButton.disabled = true;
rejectButton.classList.add('hidden');
}
});
};
Expand All @@ -426,6 +455,7 @@ const renderTaskRequest = async () => {
taskContainer.classList.remove('hidden');
try {
taskRequest = await fetchTaskRequest();
self_user = await getSelfUser();
taskRequestSkeleton.classList.add('hidden');
renderRejectButton(taskRequest);
renderTaskRequestDetails(taskRequest);
Expand Down
2 changes: 1 addition & 1 deletion task-requests/details/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ body {
background: #19805e;
transition: 0.3s ease-in-out;
}
.requestors__container__list__approved {
.requestors__container__list__status {
background: transparent;
border: none;
color: #c3c3c3;
Expand Down
Loading