Skip to content

Instantly share code, notes, and snippets.

@jsturgis
Last active July 20, 2018 05:48
Show Gist options
  • Save jsturgis/b391224a05b143aa21640b7b2fadbdc9 to your computer and use it in GitHub Desktop.
Save jsturgis/b391224a05b143aa21640b7b2fadbdc9 to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
import EligibilityCheck from '../eligibility-check-for-third-party';
export default Ember.Component.extend({
init() {
this._super(...arguments);
const config = {
pollTask: () => {console.log('called polltask')},
runTask: () => {console.log('called runtask')},
whenStatusIsEligible: this.get('whenStatusIsEligible').bind(this),
};
this.set('eligibilityCheck', new EligibilityCheck(config))
},
whenStatusIsEligible() {
const redirectUrl = this.get('cookieStore').getItem(
THIRD_PARTY_BIND.AUTH_INTERSTITIAL_COOKIE_NAME
);
if (redirectUrl) {
jSecure.redirect(redirectUrl);
}
},
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
//import csrfAjax from 'shared/utils/csrf-ajax';
const STATUS_ELIGIBLE = 'ELIGIBLE';
const ELIGIBILITY_CHECK_POLLING = {
TASK_ID: 'ELIGIBILITY_CHECK_POLLING_TASK_ID',
INTERVAL_IN_MS: 5000,
MAX_TRY: 20,
API: '/voyager/api/voyagerOnboardingThirdPartyBindEligibilityStatus',
API_DATA_PARAM: {
checkHasPositionOrEducation: true,
},
};
let pollingCount = 0;
export default class {
constructor({
pollTask,
runTask,
pollingInterval=ELIGIBILITY_CHECK_POLLING.INTERVAL_IN_MS,
pollingMaxTry=ELIGIBILITY_CHECK_POLLING.MAX_TRY,
whenStatusIsEligible=() => {}
}) {
this.pollTask = pollTask.bind(this);
this.runTask = runTask;
// Interval in milliseconds at which the pollEligibilityCheck will be called
// can be overridden
this.pollingInterval = pollingInterval;
// Max try the pollEligibilityCheck will be called
// can be overridden
this.pollingMaxTry=pollingMaxTry;
// It will be called when eligibility status becomes eligible
// should be overridden
this.whenStatusIsEligible=whenStatusIsEligible;
this.pollTask('pollEligibilityCheck', ELIGIBILITY_CHECK_POLLING.TASK_ID);
}
pollEligibilityCheck(next) {
if (pollingCount++ < this.pollingMaxTry) {
csrfAjax({
method: 'GET',
url: ELIGIBILITY_CHECK_POLLING.API,
data: ELIGIBILITY_CHECK_POLLING.API_DATA_PARAM,
}).then(({ status }) => {
if (status === STATUS_ELIGIBLE) {
this.whenStatusIsEligible();
this._stopPolling();
} else {
this.runTask(next, this.pollingInterval);
}
});
} else {
this._stopPolling();
}
}
_stopPolling() {
this.cancelPoll(ELIGIBILITY_CHECK_POLLING.TASK_ID);
pollingCount = 0;
}
};
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
{{my-component}}
<br>
<br>
{
"version": "0.15.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.2.2",
"ember-template-compiler": "3.2.2",
"ember-testing": "3.2.2"
},
"addons": {
"ember-data": "3.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment