-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #982 from Real-Dev-Squad/feat/discord-page
- Loading branch information
Showing
6 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import Controller from '@ember/controller'; | ||
import { TOAST_OPTIONS } from '../constants/toast-options'; | ||
import { APPS } from '../constants/urls'; | ||
import { inject as service } from '@ember/service'; | ||
import { tracked } from '@glimmer/tracking'; | ||
import { action } from '@ember/object'; | ||
|
||
export default class DiscordController extends Controller { | ||
queryParams = ['token']; | ||
@service router; | ||
@service toast; | ||
@tracked discordId = | ||
this.model.externalAccountData.attributes.discordId || ''; | ||
@tracked linkStatus = 'not-linked'; | ||
@tracked isLinkingInProgress = false; | ||
@tracked consent = false; | ||
@tracked token = ''; | ||
|
||
async model() { | ||
this.token = this.paramsFor('discord').token; | ||
} | ||
@action setConsent() { | ||
this.consent = !this.consent; | ||
} | ||
|
||
@action async linkDiscordAccount() { | ||
if (!this.consent) { | ||
this.toast.error( | ||
'Please provide consent by checking the checkbox', | ||
'ERROR', | ||
TOAST_OPTIONS, | ||
); | ||
return; | ||
} | ||
|
||
try { | ||
this.isLinkingInProgress = true; | ||
|
||
const response = await fetch( | ||
`${APPS.API_BACKEND}/external-accounts/link/${this.token}`, | ||
{ | ||
method: 'PATCH', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
credentials: 'include', | ||
}, | ||
); | ||
|
||
this.linkStatus = response.status === 204 ? 'linked' : 'failure'; | ||
} catch (error) { | ||
this.linkStatus = 'failure'; | ||
console.error('Failed to link Discord account:', error.message); | ||
} finally { | ||
this.isLinkingInProgress = false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import Route from '@ember/routing/route'; | ||
import { APPS } from '../constants/urls'; | ||
import { inject as service } from '@ember/service'; | ||
import redirectAuth from '../utils/redirect-auth'; | ||
import { TOAST_OPTIONS } from '../constants/toast-options'; | ||
|
||
export default class DiscordRoute extends Route { | ||
@service router; | ||
@service toast; | ||
queryParams = { | ||
token: { refreshModel: true }, | ||
}; | ||
|
||
beforeModel(transition) { | ||
if (!transition.to.queryParams.dev) { | ||
this.router.transitionTo('page-not-found'); | ||
} | ||
} | ||
|
||
async model() { | ||
try { | ||
const token = this.paramsFor('discord').token; | ||
const externalAccountResponse = await fetch( | ||
`${APPS.API_BACKEND}/external-accounts/${token}`, | ||
{ | ||
credentials: 'include', | ||
}, | ||
); | ||
const userResponse = await fetch( | ||
`${APPS.API_BACKEND}/users?profile=true`, | ||
{ | ||
credentials: 'include', | ||
}, | ||
); | ||
|
||
if (userResponse.status === 401) { | ||
const userData = await userResponse.json(); | ||
this.toast.error(userData.message, '', TOAST_OPTIONS); | ||
setTimeout(redirectAuth, 2000); | ||
return { isTokenExpired: true }; | ||
} | ||
|
||
if (externalAccountResponse.status === 401) { | ||
const externalAccountData = await externalAccountResponse.json(); | ||
this.toast.error(externalAccountData.message, '', TOAST_OPTIONS); | ||
return { isTokenExpired: true }; | ||
} | ||
|
||
if ( | ||
userResponse.status === 200 && | ||
externalAccountResponse.status === 200 | ||
) { | ||
const externalAccountData = await externalAccountResponse.json(); | ||
const userData = await userResponse.json(); | ||
return { externalAccountData, userData, isTokenExpired: false }; | ||
} | ||
|
||
throw new Error('Unexpected response status'); | ||
} catch (error) { | ||
this.toast.error(error.message, '', TOAST_OPTIONS); | ||
console.error(error.message); | ||
return { isTokenExpired: true, error: error.message }; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<div> | ||
{{!-- TODO: add discord hbs file in next PRs--}} | ||
<h1>Discord Page</h1> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { AUTH } from '../constants/urls'; | ||
|
||
export default function redirectAuth() { | ||
let authUrl = AUTH.GITHUB_SIGN_IN; | ||
if (typeof window !== 'undefined') { | ||
const url = new URL(authUrl); | ||
const searchParams = new URLSearchParams(url.search); | ||
searchParams.set('redirectURL', window.location.href); | ||
url.search = searchParams.toString(); | ||
window.location.href = url.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { module, test } from 'qunit'; | ||
import { setupTest } from 'website-www/tests/helpers'; | ||
|
||
module('Unit | Route | discord', function (hooks) { | ||
setupTest(hooks); | ||
|
||
test('it exists', function (assert) { | ||
let route = this.owner.lookup('route:discord'); | ||
assert.ok(route); | ||
}); | ||
}); |