diff --git a/pairent_frontend_react/src/API/IAPIObject.js b/pairent_frontend_react/src/API/IAPIObject.js new file mode 100644 index 0000000..e4a1af8 --- /dev/null +++ b/pairent_frontend_react/src/API/IAPIObject.js @@ -0,0 +1,57 @@ + +/** + * Basic API interaction & local caching interface + */ +class IAPIObject { + /** + * Local storage key used to save data. + * @type {string} + */ + static storage_key = undefined; + + static _checkStorageSupport() { + if (this.storage_key === undefined) { + throw Error('This doesn\'t support local storage'); + } + } + + _getStorageKey() { + return Object.getPrototypeOf(this).constructor.storage_key; + } + + _IcheckStorageSupport() { + if (this._getStorageKey() === undefined) { + throw Error('This doesn\'t support local storage'); + } + } + + /** @returns {ThisType} */ + static restoreFromLocalStorage() { + _checkStorageSupport(); + if (!window.sessionStorage.getItem(this.storage_key)) + return false; + return new APIToken(window.sessionStorage.getItem(this.storage_key)); + } + + /** @returns {boolean} */ + static isCached() { + this._checkStorageSupport(); + if (window.sessionStorage.getItem(this.storage_key)) { + return true; + } + return false; + } + + /** + * Save this object to local storage + * @throws {QuotaExceededError} + * @returns {void} + */ + saveToLocalStorage() { + this._IcheckStorageSupport(); + + window.sessionStorage.setItem(this._getStorageKey(), JSON.stringify(this)); + } +} + +export { IAPIObject }; \ No newline at end of file diff --git a/pairent_frontend_react/src/API/User.js b/pairent_frontend_react/src/API/User.js index 4fc42c4..99b84f7 100644 --- a/pairent_frontend_react/src/API/User.js +++ b/pairent_frontend_react/src/API/User.js @@ -1,6 +1,8 @@ import axios from 'axios'; import constants from '../constants'; +import { IAPIObject } from './IAPIObject'; + const { API_ROOT, api_path } = constants; class UserLoginResponse { @@ -14,58 +16,6 @@ class UserLoginResponse { id; } -class IAPIObject { - /** - * Local storage key used to save data. - * @type {string} - */ - static storage_key = undefined; - - static _checkStorageSupport() { - if (this.storage_key === undefined) { - throw Error('This doesn\'t support local storage'); - } - } - - _getStorageKey() { - return Object.getPrototypeOf(this).constructor.storage_key; - } - - _IcheckStorageSupport() { - if (this._getStorageKey() === undefined) { - throw Error('This doesn\'t support local storage'); - } - } - - /** @returns {ThisType} */ - static restoreFromLocalStorage() { - _checkStorageSupport(); - if (!window.sessionStorage.getItem(this.storage_key)) - return false; - return new APIToken(window.sessionStorage.getItem(this.storage_key)); - } - - /** @returns {boolean} */ - static isCached() { - this._checkStorageSupport(); - if (window.sessionStorage.getItem(this.storage_key)) { - return true; - } - return false; - } - - /** - * Save this object to local storage - * @throws {QuotaExceededError} - * @returns {void} - */ - saveToLocalStorage() { - this._IcheckStorageSupport(); - - window.sessionStorage.setItem(this._getStorageKey(), JSON.stringify(this)); - } -} - class APIToken extends IAPIObject { static storage_key = 'pairent_api_key';