From 1bb3bdb24af86bea311287d47f648c37547e66e0 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 3 Jun 2021 14:10:44 +0300 Subject: [PATCH] store refactor --- src/store/modules/infobases.js | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/store/modules/infobases.js b/src/store/modules/infobases.js index 9793629..bf1d1e3 100644 --- a/src/store/modules/infobases.js +++ b/src/store/modules/infobases.js @@ -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; },