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