From 91d4e6076e221afcfbc2e637eb4bd09df4417693 Mon Sep 17 00:00:00 2001 From: tmorin Date: Wed, 20 Dec 2017 16:08:50 +0100 Subject: [PATCH] refactor request wizard --- src/back/index.js | 16 +++-- src/front/index.ejs | 9 +-- src/front/index.js | 91 +-------------------------- src/front/request.form.remove.ejs | 2 +- src/front/request.js | 100 ++++++++++++++++++++++++++++++ src/front/requestStep2Dialog.ejs | 16 +++++ 6 files changed, 136 insertions(+), 98 deletions(-) create mode 100644 src/front/request.js diff --git a/src/back/index.js b/src/back/index.js index ea66a1c..c4f9aca 100644 --- a/src/back/index.js +++ b/src/back/index.js @@ -13,8 +13,8 @@ passport.use(new GitHubStrategy({ clientSecret: GITHUB_CLIENT_SECRET, callbackURL: CALLBACK_URL }, - (accessToken, refreshToken, user, done) => { - done(null, {accessToken}); + (accessToken, refreshToken, {username}, done) => { + done(null, {accessToken, username}); } )); @@ -31,10 +31,18 @@ request.defaults({ }); app.get('/auth/github', - passport.authenticate('github', {scope: ['repo'], session: false, failureRedirect: `${HOME_URL}#login-error`})); + passport.authenticate('github', { + scope: ['public_repo'], + session: false, + failureRedirect: `${HOME_URL}#login-error` + })); app.get('/auth/github/callback', - passport.authenticate('github', {scope: ['repo'], session: false, failureRedirect: `${HOME_URL}#login-failure`}), + passport.authenticate('github', { + scope: ['public_repo'], + session: false, + failureRedirect: `${HOME_URL}#login-failure` + }), (req, res) => res.redirect(`${HOME_URL}#login-success=${JSON.stringify(req.user)}`)); app.get('/api/servers', (req, res) => { diff --git a/src/front/index.ejs b/src/front/index.ejs index 233363c..20025ee 100644 --- a/src/front/index.ejs +++ b/src/front/index.ejs @@ -22,7 +22,8 @@ - + + @@ -131,10 +132,10 @@
  • - +
  • diff --git a/src/front/index.js b/src/front/index.js index 6cc9d65..4c07122 100644 --- a/src/front/index.js +++ b/src/front/index.js @@ -2,10 +2,10 @@ import 'bootstrap'; import 'babel-polyfill'; import 'whatwg-fetch'; import $ from 'jquery'; -import GitHub from 'github-api'; import './index.less'; import {fetchServers} from './servers'; -import {restoreAddress} from "./address"; +import {restoreAddress} from './address'; +import './request'; $('body').css({'display': 'block'}); $('[data-toggle="tooltip"]').tooltip(); @@ -19,91 +19,4 @@ function refreshAll() { $(() => { refreshAll(); - - const $requestStep2Dialog = $('#requestStep2Dialog'); - const $formSelection = $requestStep2Dialog.find('.form-selection'); - const $forms = $requestStep2Dialog.find('form'); - const $requestStep3Dialog = $('#requestStep3Dialog'); - - $formSelection.on('change', 'input', evt => { - const formSelector = `form[name="${evt.target.value}Server"]`; - $forms.attr('hidden', 'hidden'); - $requestStep2Dialog.find(formSelector).removeAttr('hidden'); - }); - - $requestStep2Dialog.on('show.bs.modal', () => { - $forms.attr('hidden', 'hidden'); - $forms.find('input, textarea, button').removeAttr('disabled'); - $forms.find('button i').removeClass('fa-spin'); - $forms[0].reset(); - }); - - $forms.on('submit', evt => { - evt.preventDefault(); - - const token = $('meta[property="x:app:accessToken"]').attr('content'); - const repoUser = $('meta[property="x:app:repoUser"]').attr('content'); - const repoName = $('meta[property="x:app:repoName"]').attr('content'); - const host = location.hostname; - const action = evt.target.action.value; - const name = evt.target.name.value; - const comments = evt.target.comments.value; - - const body = []; - - if (action !== 'remove') { - const front = evt.target.front.value; - const back = evt.target.back.value; - const locations = evt.target.locations.value; - const file = {name, front, back, location: locations}; - body.push('```'); - body.push(JSON.stringify(file, null, 2)); - body.push('```'); - } - - if (comments) { - body.push('comments:'); - body.push(comments); - } - - const issueData = { - title: `[${host}] - ${action} - ${name}`, - body: body.join('\n'), - labels: ['servers'] - }; - - $forms.find('input, textarea, button').attr('disabled', ''); - $forms.find('button i').addClass('fa-spin'); - - new GitHub({token}).getIssues(repoUser, repoName).createIssue(issueData) - .then(() => { - $requestStep3Dialog.addClass('success').removeClass('failure'); - }) - .catch(err => { - console.error(err); - $requestStep3Dialog.removeClass('success').addClass('failure'); - }) - .then(() => { - $requestStep2Dialog - .one('hidden.bs.modal', () => $requestStep3Dialog.modal('show')) - .modal('hide'); - }); - }); - - const hash = decodeURI(window.location.hash); - const loginSuccess = hash.indexOf('#login-success') > -1; - if (loginSuccess) { - $(window).one('hashchange', evt => evt.preventDefault()); - location.hash = ''; - const context = JSON.parse(hash.replace('#login-success=', '')); - $('meta[property="x:app:accessToken"]').attr('content', context.accessToken); - $requestStep2Dialog.modal('show'); - } - - const loginFailure = hash.indexOf('#login-failure') > -1; - if (loginFailure) { - $(window).one('hashchange', evt => evt.preventDefault()); - location.hash = ''; - } }); - diff --git a/src/front/request.form.remove.ejs b/src/front/request.form.remove.ejs index 833a026..f194ba1 100644 --- a/src/front/request.form.remove.ejs +++ b/src/front/request.form.remove.ejs @@ -13,7 +13,7 @@

    - diff --git a/src/front/request.js b/src/front/request.js new file mode 100644 index 0000000..bc42661 --- /dev/null +++ b/src/front/request.js @@ -0,0 +1,100 @@ +import GitHub from 'github-api'; +import $ from 'jquery'; + +$(() => { + + const $requestStep2Dialog = $('#requestStep2Dialog'); + const $formSelection = $requestStep2Dialog.find('.form-selection'); + const $forms = $requestStep2Dialog.find('form'); + const $requestStep3Dialog = $('#requestStep3Dialog'); + + $formSelection.on('change', 'input', evt => { + const formSelector = `form[name="${evt.target.value}Server"]`; + $forms.attr('hidden', 'hidden'); + $requestStep2Dialog.find(formSelector).removeAttr('hidden'); + }); + + $requestStep2Dialog.on('show.bs.modal', () => { + const token = $('meta[property="x:github:accessToken"]').attr('content'); + $requestStep2Dialog.find('a[name="github-logout"]').attr('href', `auth/github/logout?accessToken=${token}`); + + const username = $('meta[property="x:github:username"]').attr('content'); + $requestStep2Dialog.find('.github-username').text(username); + + $forms.attr('hidden', 'hidden'); + $forms.find('input, textarea, button').removeAttr('disabled'); + $forms.find('button i').removeClass('fa-spin'); + $forms[0].reset(); + }); + + $forms.on('submit', evt => { + evt.preventDefault(); + + const token = $('meta[property="x:github:accessToken"]').attr('content'); + const repoUser = $('meta[property="x:app:repoUser"]').attr('content'); + const repoName = $('meta[property="x:app:repoName"]').attr('content'); + const host = location.hostname; + const action = evt.target.action.value; + const name = evt.target.name.value; + const comments = evt.target.comments.value; + + const body = []; + + if (action !== 'remove') { + const front = evt.target.front.value; + const back = evt.target.back.value; + const locations = evt.target.locations.value; + const file = {name, front, back, location: locations}; + body.push('```'); + body.push(JSON.stringify(file, null, 2)); + body.push('```'); + } + + if (comments) { + body.push('comments:'); + body.push(comments); + } + + const issueData = { + title: `[${host}] - ${action} - ${name}`, + body: body.join('\n'), + labels: ['servers'] + }; + + $forms.find('input, textarea, button').attr('disabled', ''); + $forms.find('button i').addClass('fa-spin'); + + new GitHub({token}).getIssues(repoUser, repoName).createIssue(issueData) + .then(() => { + $requestStep3Dialog.addClass('success').removeClass('failure'); + }) + .catch(err => { + console.error(err); + $requestStep3Dialog.removeClass('success').addClass('failure'); + }) + .then(() => { + $requestStep2Dialog + .one('hidden.bs.modal', () => $requestStep3Dialog.modal('show')) + .modal('hide'); + }); + }); + + const hash = decodeURI(window.location.hash); + const loginSuccess = hash.indexOf('#login-success') > -1; + if (loginSuccess) { + $(window).one('hashchange', evt => evt.preventDefault()); + location.hash = ''; + const context = JSON.parse(hash.replace('#login-success=', '')); + console.log('context', context); + $('meta[property="x:github:accessToken"]').attr('content', context.accessToken); + $('meta[property="x:github:username"]').attr('content', context.username); + $requestStep2Dialog.modal('show'); + } + + const loginFailure = hash.indexOf('#login-failure') > -1; + if (loginFailure) { + $(window).one('hashchange', evt => evt.preventDefault()); + location.hash = ''; + } + +}); diff --git a/src/front/requestStep2Dialog.ejs b/src/front/requestStep2Dialog.ejs index 5e86465..2e61af4 100644 --- a/src/front/requestStep2Dialog.ejs +++ b/src/front/requestStep2Dialog.ejs @@ -7,7 +7,22 @@ + +