store refactor

This commit is contained in:
Dmitry Belyaev 2021-06-03 14:10:44 +03:00
parent 52c5dfd64a
commit 1bb3bdb24a
Signed by: b4tman
GPG Key ID: 41A00BF15EA7E5F3

View File

@ -9,19 +9,22 @@ const LoadingStatus = Object.freeze({
Error: { loading: false, error: true },
});
let new_infobase = (name) => ({
const new_infobase = (name) => ({
name,
url: "",
publicated: false,
});
const infobaseByName = (state) => (name) =>
state.infobases.find((infobase) => name === infobase.name);
function setInfobaseLock(state, { name, lock }) {
let locked = state.locked_bases.includes(name);
const locked = state.locked_bases.includes(name);
if (locked === lock) return;
if (lock) {
state.locked_bases.push(name);
} else {
let idx = state.locked_bases.findIndex((x) => name === x);
const idx = state.locked_bases.findIndex((x) => name === x);
delete state.locked_bases[idx];
}
}
@ -57,7 +60,9 @@ export default {
setInfobaseLock(state, { name, lock: false });
},
setInfobase(state, { name, infobase_new }) {
let idx = state.infobases.findIndex((infobase) => name === infobase.name);
const idx = state.infobases.findIndex(
(infobase) => name === infobase.name
);
if (-1 === idx) {
state.infobases.push(infobase_new);
} else {
@ -69,12 +74,12 @@ export default {
}
},
setInfobaseURL(state, { name, url }) {
let infobase = state.infobases.find((infobase) => name === infobase.name);
const infobase = infobaseByName(state)(name);
if (infobase === undefined) return;
infobase.url = url;
},
setInfobasePublication(state, { name, publicated }) {
let infobase = state.infobases.find((infobase) => name === infobase.name);
const infobase = infobaseByName(state)(name);
if (infobase === undefined) return;
infobase.publicated = publicated;
},
@ -138,10 +143,10 @@ export default {
}
},
async updateInfobaseURL(ctx, { name, url }) {
let data = {
...new_infobase(name),
const infobase = ctx.getters.getInfobaseByName(name);
const data = {
...infobase,
url,
publicated: true,
};
ctx.commit("lockInfobase", name);
@ -149,6 +154,7 @@ export default {
try {
await axios.post(`${api_base}/publications`, data);
ctx.commit("setInfobaseURL", { name, url });
ctx.commit("setErrorMessage", "");
} catch (err) {
ctx.commit("setErrorMessage", `Ошибка обновления URL для базы ${name}`);
} finally {
@ -167,8 +173,7 @@ export default {
return state.locked_bases;
},
isInfobaseLocked: (state) => (name) => state.locked_bases.includes(name),
getInfobaseByName: (state) => (name) =>
state.infobases.find((infobase) => name === infobase.name),
getInfobaseByName: infobaseByName,
isLoading(state) {
return state.is_loading;
},