diff --git a/app/constants/application.js b/app/constants/application.js new file mode 100644 index 00000000..68b23897 --- /dev/null +++ b/app/constants/application.js @@ -0,0 +1,5 @@ +export const APPLICATION_STATUS_TYPES = { + accepted: 'accepted', + rejected: 'rejected', + pending: 'pending', +}; diff --git a/app/controllers/intro.js b/app/controllers/intro.js index 2173b27a..6c102632 100644 --- a/app/controllers/intro.js +++ b/app/controllers/intro.js @@ -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'); - } } diff --git a/app/templates/intro.hbs b/app/templates/intro.hbs index 4ebb620f..7b13f813 100644 --- a/app/templates/intro.hbs +++ b/app/templates/intro.hbs @@ -21,21 +21,29 @@
@@ -64,9 +72,10 @@ {{else}} {{/if}} {{/if}} {{/if}} - + \ No newline at end of file diff --git a/app/utils/validate-application-details.js b/app/utils/validate-application-details.js new file mode 100644 index 00000000..3a47fa42 --- /dev/null +++ b/app/utils/validate-application-details.js @@ -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 }; +};