From 96cd073bda6ecffb4e9b2489af512f05c9f8dd8c Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 13 Feb 2017 07:39:54 +0900 Subject: [PATCH] [Client] LS to JS and some Clean up :v: --- src/web/app/boot.js | 2 +- src/web/app/common/mixins.ls | 2 +- src/web/app/common/scripts/api.js | 44 ++++++++++++++ src/web/app/common/scripts/api.ls | 51 ---------------- src/web/app/desktop/scripts/update-avatar.ls | 2 +- src/web/app/desktop/scripts/update-banner.ls | 2 +- .../app/desktop/scripts/update-wallpaper.ls | 2 +- ...detect-slow-internet-connection-notice.tag | 60 ------------------- src/web/app/desktop/tags/home.tag | 1 - src/web/app/desktop/tags/index.js | 1 - 10 files changed, 49 insertions(+), 118 deletions(-) create mode 100644 src/web/app/common/scripts/api.js delete mode 100644 src/web/app/common/scripts/api.ls delete mode 100644 src/web/app/desktop/tags/detect-slow-internet-connection-notice.tag diff --git a/src/web/app/boot.js b/src/web/app/boot.js index 8c7a2e0fd..b22c5fc5b 100644 --- a/src/web/app/boot.js +++ b/src/web/app/boot.js @@ -4,7 +4,7 @@ const riot = require('riot'); require('velocity-animate'); -const api = require('./common/scripts/api.ls'); +const api = require('./common/scripts/api'); const signout = require('./common/scripts/signout.ls'); const generateDefaultUserdata = require('./common/scripts/generate-default-userdata.ls'); const mixins = require('./common/mixins.ls'); diff --git a/src/web/app/common/mixins.ls b/src/web/app/common/mixins.ls index 50918c68c..0a3802e25 100644 --- a/src/web/app/common/mixins.ls +++ b/src/web/app/common/mixins.ls @@ -6,7 +6,7 @@ module.exports = (me) ~> (require './scripts/i.ls') me riot.mixin \api do - api: (require './scripts/api.ls').bind null i + api: (require './scripts/api').bind null i riot.mixin \cropper do Cropper: require \cropperjs diff --git a/src/web/app/common/scripts/api.js b/src/web/app/common/scripts/api.js new file mode 100644 index 000000000..924d697eb --- /dev/null +++ b/src/web/app/common/scripts/api.js @@ -0,0 +1,44 @@ +/** + * API Request + */ + +let spinner = null; +let pending = 0; + +/** + * Send a request to API + * @param {string|Object} i Credential + * @param {string} endpoint Endpoint + * @param {Object} [data={}] Data + * @return {Promise} Response + */ +module.exports = (i, endpoint, data = {}) => { + if (++pending === 1) { + spinner = document.createElement('div'); + spinner.setAttribute('id', 'wait'); + document.body.appendChild(spinner); + } + + // Append the credential + if (i != null) data.i = typeof i === 'object' ? i.token : i; + + return new Promise((resolve, reject) => { + // Send request + fetch(endpoint.indexOf('://') > -1 ? endpoint : `${CONFIG.api.url}/${endpoint}`, { + method: 'POST', + body: JSON.stringify(data), + credentials: endpoint === 'signin' ? 'include' : 'omit' + }).then(res => { + if (--pending === 0) spinner.parentNode.removeChild(spinner); + if (res.status === 200) { + res.json().then(resolve); + } else if (res.status === 204) { + resolve(); + } else { + res.json().then(err => { + reject(err.error); + }); + } + }).catch(reject); + }); +}; diff --git a/src/web/app/common/scripts/api.ls b/src/web/app/common/scripts/api.ls deleted file mode 100644 index 47182b6a5..000000000 --- a/src/web/app/common/scripts/api.ls +++ /dev/null @@ -1,51 +0,0 @@ -riot = require \riot - -spinner = null -pending = 0 - -net = riot.observable! - -riot.mixin \net do - net: net - -module.exports = (i, endpoint, data = {}) -> - if ++pending == 1 - spinner := document.create-element \div - ..set-attribute \id \wait - document.body.append-child spinner - - if i? and typeof i == \object then i = i.token - - # append user token when signed in - if i? then data.i = i - - opts = - method: \POST - body: JSON.stringify data - - if endpoint == \signin - opts.credentials = \include - - ep = if (endpoint.index-of '://') > -1 - then endpoint - else "#{CONFIG.api.url}/#{endpoint}" - - new Promise (resolve, reject) -> - timer = set-timeout -> - net.trigger \detected-slow-network - , 5000ms - - fetch ep, opts - .then (res) -> - clear-timeout timer - if --pending == 0 - spinner.parent-node.remove-child spinner - - if res.status == 200 - res.json!.then resolve - else if res.status == 204 - resolve! - else - res.json!.then (err) -> - reject err.error - .catch reject diff --git a/src/web/app/desktop/scripts/update-avatar.ls b/src/web/app/desktop/scripts/update-avatar.ls index 513a59074..351e54fe5 100644 --- a/src/web/app/desktop/scripts/update-avatar.ls +++ b/src/web/app/desktop/scripts/update-avatar.ls @@ -3,7 +3,7 @@ riot = require 'riot' dialog = require './dialog.ls' -api = require '../../common/scripts/api.ls' +api = require '../../common/scripts/api' module.exports = (I, cb, file = null) ~> diff --git a/src/web/app/desktop/scripts/update-banner.ls b/src/web/app/desktop/scripts/update-banner.ls index 5754cdcdb..2417b8ab2 100644 --- a/src/web/app/desktop/scripts/update-banner.ls +++ b/src/web/app/desktop/scripts/update-banner.ls @@ -3,7 +3,7 @@ riot = require 'riot' dialog = require './dialog.ls' -api = require '../../common/scripts/api.ls' +api = require '../../common/scripts/api' module.exports = (I, cb, file = null) ~> diff --git a/src/web/app/desktop/scripts/update-wallpaper.ls b/src/web/app/desktop/scripts/update-wallpaper.ls index 49632400c..e7394a0d0 100644 --- a/src/web/app/desktop/scripts/update-wallpaper.ls +++ b/src/web/app/desktop/scripts/update-wallpaper.ls @@ -3,7 +3,7 @@ riot = require 'riot' dialog = require './dialog.ls' -api = require '../../common/scripts/api.ls' +api = require '../../common/scripts/api' module.exports = (I, cb, file = null) ~> diff --git a/src/web/app/desktop/tags/detect-slow-internet-connection-notice.tag b/src/web/app/desktop/tags/detect-slow-internet-connection-notice.tag deleted file mode 100644 index 09a746fb9..000000000 --- a/src/web/app/desktop/tags/detect-slow-internet-connection-notice.tag +++ /dev/null @@ -1,60 +0,0 @@ - -
-

インターネット回線が遅いようです。

-
- - -
diff --git a/src/web/app/desktop/tags/home.tag b/src/web/app/desktop/tags/home.tag index c7dc82efc..b969aec44 100644 --- a/src/web/app/desktop/tags/home.tag +++ b/src/web/app/desktop/tags/home.tag @@ -7,7 +7,6 @@
-