Skip to content

Commit

Permalink
integrate api to approve / reject
Browse files Browse the repository at this point in the history
  • Loading branch information
yesyash committed Aug 18, 2024
1 parent 396649b commit 3d78242
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 55 deletions.
5 changes: 5 additions & 0 deletions app/constants/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const APPLICATION_STATUS_TYPES = {
accepted: 'accepted',
rejected: 'rejected',
pending: 'pending',
};
84 changes: 35 additions & 49 deletions app/controllers/intro.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,60 @@
import Controller from '@ember/controller';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { APPS } from '../constants/urls';
import { validateApplicationDetails } from '../utils/validate-application-details';
import { APPLICATION_STATUS_TYPES } from '../constants/application';

export default class IntroController extends Controller {
@service login;
@tracked remarks = '';
statusTypes = APPLICATION_STATUS_TYPES;

@tracked rejectionNote = localStorage.getItem('rejectionNote') || '';
@tracked isRejected = localStorage.getItem('isRejected') === 'true';
@action
updateRemarks(e) {
const value = e.currentTarget.value;
this.remarks = value;
}

@action
async approveAction() {
const userId = this.model.userId;
async approveRejectAction(status) {
const initialApplicationDetails = this.model?.[0];

if (!userId) {
alert('User ID is missing. Unable to generate invite link.');
const { isValid, data } = validateApplicationDetails(
initialApplicationDetails,
);

if (!isValid || !data) {
alert('Invalid application details.');
return;
}

const applicationId = data.id;
const body = { status, feedback: this.remarks };

try {
const response = await fetch(
`https://api.realdevsquad.com/discord/invites?userId=${userId}`,
`${APPS.API_BACKEND}/applications/${applicationId}`,
{
method: 'POST',
method: 'PATCH',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
},
);

if (response.ok) {
const result = await response.json();
const inviteLink = result.inviteLink;

this.model.inviteLink = inviteLink;

alert('User approved and invite link generated.');
} else if (response.status === 409) {
alert('Invite link is already present for this user.');
} else if (response.status === 403 || response.status === 404) {
const errorData = await response.json();
alert(`Failed to generate invite link: ${errorData.message}`);
} else {
const errorData = await response.json();
alert(`Failed to generate invite link: ${errorData.message}`);
if (!response.ok || response.status >= 400) {
alert('Failed to update application status.');
return;
}

alert(`Application ${status.toLowerCase()} successfully.`);
} catch (error) {
console.error('Error generating invite link:', error);
alert('An error occurred. Please try again later.');
alert('Something went wrong, please try again later.');
console.error('Error :', error);
}
}

@action
copyToClipboard(link) {
navigator.clipboard
.writeText(link)
.then(() => {
alert('Invite link copied to clipboard!');
})
.catch((err) => {
console.error('Failed to copy the text: ', err);
});
}

@action
rejectAction() {
const feedbackNote = document.querySelector('.comment-box').value.trim();
this.rejectionNote = feedbackNote;
this.isRejected = true;

// Store in local storage
localStorage.setItem('rejectionNote', feedbackNote);
localStorage.setItem('isRejected', 'true');
}
}
21 changes: 15 additions & 6 deletions app/templates/intro.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,29 @@

<label for="comment-box">Your Comment:</label>
<textarea
id="comment-box"
aria-label="Application remarks"
class="comment-box"
placeholder="Enter your comments here..."
{{on "change" this.updateRemarks}}
></textarea>

<div class="action-buttons">
<button
type="button"
class="approve-button"
{{on "click" this.approveAction}}
>Approve</button>
{{on
"click"
(fn this.approveRejectAction this.statusTypes.accepted)
}}
>Accept</button>

<button
type="button"
class="reject-button"
{{on "click" this.rejectAction}}
{{on
"click"
(fn this.approveRejectAction this.statusTypes.rejected)
}}
>Reject</button>
</div>

Expand Down Expand Up @@ -64,9 +72,10 @@
</div>
{{else}}
<div class="under-review-message">
<p>Superusers are reviewing your application. Please check again after a few days.</p>
<p>Superusers are reviewing your application. Please check again after
a few days.</p>
</div>
{{/if}}
{{/if}}
{{/if}}
</section>
</section>
36 changes: 36 additions & 0 deletions app/utils/validate-application-details.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Generate application details
* ---
* @param {Object} applicationDetails
* @param {string} applicationDetails.id
* @param {Object} applicationDetails.intro
* @param {string} applicationDetails.intro.funFact
* @param {string} applicationDetails.intro.forFun
* @param {number} applicationDetails.intro.numberOfHours
* @param {string} applicationDetails.intro.whyRds
* @param {string} applicationDetails.intro.introduction
* @param {Object} applicationDetails.biodata
* @param {string} applicationDetails.biodata.firstName
* @param {string} applicationDetails.biodata.lastName
* @param {Object} applicationDetails.location
* @param {string} applicationDetails.location.country
* @param {string} applicationDetails.location.city
* @param {string} applicationDetails.location.state
* @param {string} applicationDetails.foundFrom
* @param {string} applicationDetails.userId
* @param {Object} applicationDetails.professional
* @param {string} applicationDetails.professional.skills
* @param {string} applicationDetails.professional.institution
* @param {string} applicationDetails.status - "accepted" | "rejected" | "pending"
*
* @returns {Object} validateApplicationDetails
* @returns {boolean} validateApplicationDetails.isValid
* @returns {applicationDetails} validateApplicationDetails.data
*/
export const validateApplicationDetails = (applicationDetails) => {
if (!applicationDetails || !applicationDetails.userId) {
return { isValid: false, data: null };
}

return { isValid: true, data: applicationDetails };
};

0 comments on commit 3d78242

Please sign in to comment.