Answer the question
In order to leave comments, you need to log in
How to get the settings right?
The task is this:
I get the settings from api (consists of a key and value), then I save it in a cookie (an alternative to hashing, I save it for 5 minutes), I created an action in vuex with settings loaded
async initOptions() {
if (!Cookies.get('options')) {
await Option.$query().get().then(response => {
Cookies.set('options', JSON.stringify(response.map(function (item) {
return [item.$attributes.option_key, item.$attributes.option_value]
})), {expires: 1/24/60*5});
})
}
}
getOptionByKey: () => (key, def) => {
return (new Map(JSON.parse(Cookies.get('options'))).get(key) ?? def);
}
Answer the question
In order to leave comments, you need to log in
Why is Vuex here? Make a normal function that would take data from cookies or load via api. More or less like this:
async function getOption(key, def) {
let options = Cookies.get('options')
if (!options) {
options = await Option.$query().get()
Cookies.set('options', options)
}
return new Map(JSON.parse(options)).get(key) ?? def
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question