From 36eeb6c16920f97b41cbcb81c7115777fd948d03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B8=85?= Date: Sun, 5 Aug 2018 21:56:29 +0800 Subject: [PATCH] localStorage -> sessionStorage --- .stylelintrc.json | 28 ++++++++++++++++ src/pages/Account/Center/Center.js | 6 ++-- src/utils/request.js | 52 ++++++++++++++++-------------- stylelint.config.js | 28 ---------------- 4 files changed, 58 insertions(+), 56 deletions(-) create mode 100644 .stylelintrc.json delete mode 100644 stylelint.config.js diff --git a/.stylelintrc.json b/.stylelintrc.json new file mode 100644 index 00000000..c4d3198f --- /dev/null +++ b/.stylelintrc.json @@ -0,0 +1,28 @@ +{ + "extends": ["stylelint-config-standard", "stylelint-config-prettier"], + "rules": { + "selector-pseudo-class-no-unknown": null, + "shorthand-property-no-redundant-values": null, + "at-rule-empty-line-before": null, + "at-rule-name-space-after": null, + "comment-empty-line-before": null, + "declaration-bang-space-before": null, + "declaration-empty-line-before": null, + "function-comma-newline-after": null, + "function-name-case": null, + "function-parentheses-newline-inside": null, + "function-max-empty-lines": null, + "function-whitespace-after": null, + "number-leading-zero": null, + "number-no-trailing-zeros": null, + "rule-empty-line-before": null, + "selector-combinator-space-after": null, + "selector-descendant-combinator-no-non-space": null, + "selector-list-comma-newline-after": null, + "selector-pseudo-element-colon-notation": null, + "unit-no-unknown": null, + "no-descending-specificity": null, + "value-list-max-empty-lines": null, + "no-missing-end-of-source-newline": null + } +} diff --git a/src/pages/Account/Center/Center.js b/src/pages/Account/Center/Center.js index 6e579524..f61aff9b 100644 --- a/src/pages/Account/Center/Center.js +++ b/src/pages/Account/Center/Center.js @@ -148,9 +148,9 @@ export default class Center extends PureComponent {
标签
- {currentUser.tags - .concat(newTags) - .map(item => {item.label})} + {currentUser.tags.concat(newTags).map(item => ( + {item.label} + ))} {inputVisible && ( { if (response.status >= 200 && response.status < 300) { return response; } @@ -32,7 +32,26 @@ function checkStatus(response) { error.name = response.status; error.response = response; throw error; -} +}; + +const cachedSave = (response, hashcode) => { + /** + * Clone a response data and store it in sessionStorage + * Does not support data other than json, Cache only json + */ + let contentType = response.headers.get('Content-Type'); + if (contentType && contentType.match(/application\/json/i)) { + // All data is saved as text + response + .clone() + .text() + .then(content => { + sessionStorage.setItem(hashcode, content); + sessionStorage.setItem(hashcode + ':timestamp', Date.now()); + }); + } + return response; +}; /** * Requests a URL, returning a promise. @@ -47,7 +66,7 @@ export default function request(url, options = {}) { * Produce fingerprints based on url and parameters * Maybe url has the same parameters */ - const fingerprint = url + options.body ? JSON.stringify(options.body) : ''; + const fingerprint = url + (options.body ? JSON.stringify(options.body) : ''); const hashcode = hash .sha256() .update(fingerprint) @@ -77,8 +96,8 @@ export default function request(url, options = {}) { }; } } - let cached = localStorage.getItem(hashcode); - let whenCached = localStorage.getItem(hashcode + ':timestamp'); + let cached = sessionStorage.getItem(hashcode); + let whenCached = sessionStorage.getItem(hashcode + ':timestamp'); const expirys = options.expirys || 60; if (cached !== null && whenCached !== null && expirys !== false) { let age = (Date.now() - whenCached) / 1000; @@ -86,30 +105,13 @@ export default function request(url, options = {}) { let response = new Response(new Blob([cached])); return response.json(); } else { - localStorage.removeItem(hashcode); - localStorage.removeItem(hashcode + ':timestamp'); + sessionStorage.removeItem(hashcode); + sessionStorage.removeItem(hashcode + ':timestamp'); } } return fetch(url, newOptions) .then(checkStatus) - .then(response => { - /** - * Clone a response data and store it in localStorage - * Does not support data other than json, Cache only json - */ - let contentType = response.headers.get('Content-Type'); - if (contentType && contentType.match(/application\/json/i)) { - // All data is saved as text - response - .clone() - .text() - .then(content => { - localStorage.setItem(hashcode, content); - localStorage.setItem(hashcode + ':timestamp', Date.now()); - }); - } - return response; - }) + .then(cachedSave) .then(response => { // DELETE and 204 do not return data by default // using .json will report an error. diff --git a/stylelint.config.js b/stylelint.config.js deleted file mode 100644 index 2363deec..00000000 --- a/stylelint.config.js +++ /dev/null @@ -1,28 +0,0 @@ -export default { - extends: ['stylelint-config-standard', 'stylelint-config-prettier'], - rules: { - 'selector-pseudo-class-no-unknown': null, - 'shorthand-property-no-redundant-values': null, - 'at-rule-empty-line-before': null, - 'at-rule-name-space-after': null, - 'comment-empty-line-before': null, - 'declaration-bang-space-before': null, - 'declaration-empty-line-before': null, - 'function-comma-newline-after': null, - 'function-name-case': null, - 'function-parentheses-newline-inside': null, - 'function-max-empty-lines': null, - 'function-whitespace-after': null, - 'number-leading-zero': null, - 'number-no-trailing-zeros': null, - 'rule-empty-line-before': null, - 'selector-combinator-space-after': null, - 'selector-descendant-combinator-no-non-space': null, - 'selector-list-comma-newline-after': null, - 'selector-pseudo-element-colon-notation': null, - 'unit-no-unknown': null, - 'no-descending-specificity': null, - 'value-list-max-empty-lines': null, - 'no-missing-end-of-source-newline': null, - }, -}; -- GitLab