Last active
February 18, 2020 13:30
-
-
Save matgargano/143f816157b2369a1331b8a6595da709 to your computer and use it in GitHub Desktop.
React Persisters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Persister from './Persister'; | |
import { HttpClient } from '../HttpClient'; | |
export default class API extends Persister { | |
constructor(keyObject) { | |
super(keyObject); | |
this.LS_KEY = 'api'; | |
} | |
async get() { | |
const url = `${process.env.REACT_APP_API_ENDPOINT}/api/assets/get_asset/${this.keyObject.getObject().linkType}/${this.keyObject.getObject().linkKey}/${this.keyObject.getObject().assetTypeCode}`; | |
const response = await HttpClient.request({ url }).then((e) => e); | |
const { assets } = response.data; | |
return assets; | |
} | |
static async getLock(assetId) { | |
const url = `${process.env.REACT_APP_API_ENDPOINT}/api/assets/lock_asset/${assetId}`; | |
const response = await HttpClient.request({ url }).then((e) => e); | |
const { data } = response; | |
return data; | |
} | |
async releaseLock(assetId, force = false) { | |
const response = await this.lockAction(assetId, 'unlock', force); | |
return response; | |
} | |
async setLock(assetId, force = false) { | |
const response = await this.lockAction(assetId, 'lock', force); | |
return response; | |
} | |
// eslint-disable-next-line class-methods-use-this | |
async lockAction(assetId, action = 'lock', force = false) { | |
const url = `${process.env.REACT_APP_API_ENDPOINT}/api/assets/lock_asset/`; | |
const payload = { asset_id: assetId, action }; | |
if (force) { | |
payload.override = true; | |
} | |
const response = await HttpClient.request({ url, method: 'post', data: payload }).then((e) => e); | |
const { data } = response; | |
return data; | |
} | |
async set(value, assetId = null) { | |
const url = `${process.env.REACT_APP_API_ENDPOINT}/api/assets/set_asset/`; | |
const data = { | |
html: value, | |
link_type: this.keyObject.getObject().linkType, | |
link_key: this.keyObject.getObject().linkKey, | |
asset_type_code: this.keyObject.getObject().assetTypeCode, | |
}; | |
if (assetId) { | |
data.asset_id = assetId; | |
} | |
try { | |
await HttpClient.request({ url, method: 'post', data }); | |
} catch (e) { | |
console.log(e); | |
} | |
return value; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class KeyObject { | |
constructor({ | |
linkType, linkKey, assetTypeCode, | |
}) { | |
if (!linkType || !linkKey || !assetTypeCode) { | |
throw Error('KeyObject missing linkType, linkKey and/or assetTypeCode'); | |
} | |
this.linkType = linkType; | |
this.linkKey = linkKey; | |
this.assetTypeCode = assetTypeCode; | |
} | |
getObject() { | |
const { | |
linkType, linkKey, assetTypeCode, | |
} = this; | |
return { | |
linkType, | |
linkKey, | |
assetTypeCode, | |
}; | |
} | |
getKey() { | |
const { linkType, linkKey, assetTypeCode } = this; | |
return linkType.concat(linkKey, assetTypeCode); | |
} | |
} | |
export default KeyObject; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lscache from 'lscache'; | |
import Persister from './Persister'; | |
export default class LocalStorage extends Persister { | |
constructor(keyObject) { | |
super(keyObject); | |
this.LS_KEY = 'localstorage'; | |
} | |
async get(what = null) { | |
let whatToGet = what; | |
if (this.isArray && !whatToGet) { | |
return []; | |
} if (!whatToGet) { | |
whatToGet = this.keyObject.getObject().assetTypeCode; | |
} | |
let data = await lscache.get(this.getKey()); | |
data = data || {}; | |
return data[whatToGet]; | |
} | |
async set(value) { | |
try { | |
let data = await lscache.get(this.getKey()); | |
data = data || {}; | |
data[this.keyObject.getObject().assetTypeCode] = value; | |
await lscache.set(this.getKey(), data); | |
return true; | |
} catch (e) { | |
console.log(e); | |
return 'ERROR'; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Persister { | |
constructor(keyObject = null) { | |
this.setKeyObject(keyObject); | |
} | |
setKeyObject(keyObject) { | |
this.keyObject = keyObject; | |
this.checkKeyObjectType(); | |
this.actualKeyObjectToCheckAgainst = this.keyObject; | |
this.isArray = Array.isArray(this.keyObject); | |
if (this.isArray) { | |
[this.actualKeyObjectToCheckAgainst] = this.keyObject; | |
} | |
this.LS_KEY = ''; | |
} | |
// eslint-disable-next-line class-methods-use-this | |
async get() { | |
throw new Error('must implement get'); | |
} | |
// eslint-disable-next-line class-methods-use-this | |
async set() { | |
throw new Error('must implement set'); | |
} | |
checkKeyObjectType() { | |
if ( | |
!((typeof this.actualKeyObjectToCheckAgainst).toString() !== 'KeyObject') | |
) { | |
throw new Error('Must pass in a keyObject to setEditorDataInAPI'); | |
} | |
return true; | |
} | |
getKey(prepend = null) { | |
let prependArgument = prepend; | |
if (!prependArgument) { | |
prependArgument = this.LS_KEY; | |
} | |
return prependArgument.concat( | |
this.actualKeyObjectToCheckAgainst.getObject().linkType, | |
this.actualKeyObjectToCheckAgainst.getObject().linkKey, | |
); | |
} | |
getKeyObject() { | |
return this.keyObject; | |
} | |
} | |
export default Persister; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment