From 9ba698059a14c26179d7e17453004b34af53a19b Mon Sep 17 00:00:00 2001 From: Andrew Kostka Date: Mon, 19 May 2025 10:57:48 +0000 Subject: [PATCH] Use state for wiki profile card --- src/backend/api.js | 2 +- .../Pages/ManageWiki/Cards/Profile.vue | 39 ++++++++----------- src/store/wikis.js | 13 +++++-- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/backend/api.js b/src/backend/api.js index 16e3e8fb..0e9c2005 100644 --- a/src/backend/api.js +++ b/src/backend/api.js @@ -77,7 +77,7 @@ export const updateLogo = async ({ file, fileName, wikiId }) => { export const updateSetting = async (setting, payload) => axios.post(`/wiki/setting/${setting}/update`, { ...payload, setting }) export const updateSkin = async payload => updateSetting('wgDefaultSkin', payload) export const wikiDetails = async payload => (await axios.post('/wiki/details', payload)).data.data -export const updateProfile = async payload => axios.post('/wiki/profile', payload) +export const updateProfile = async payload => (await axios.post('/wiki/profile', payload)).data.data export const wikiDiscovery = async ({ sort, direction, active, currentPage, resultsPerPage }) => { return (await axios.get('/wiki', { params: { diff --git a/src/components/Pages/ManageWiki/Cards/Profile.vue b/src/components/Pages/ManageWiki/Cards/Profile.vue index 918f16c2..5e4f5cd1 100644 --- a/src/components/Pages/ManageWiki/Cards/Profile.vue +++ b/src/components/Pages/ManageWiki/Cards/Profile.vue @@ -137,16 +137,16 @@ export default { }, computed: { hasProfile: function () { - return !!this.profile.updated_at + return !!this.profile?.updated_at }, hasAudience: function () { - return !!this.profile.audience + return !!this.profile?.audience }, isWikiTemporary: function () { - return this.profile.temporality === 'temporary' + return this.profile?.temporality === 'temporary' }, updatedAt: function () { - if (this.profile.updated_at) { + if (this.profile?.updated_at) { return `Last updated on ${new Date(this.profile.updated_at).toLocaleString()}` } return false @@ -154,13 +154,15 @@ export default { }, methods: { getQuestionResponse (question) { - const customResponse = this.profile[question + '_other'] - if (customResponse) { - return `Other: ${customResponse}` - } - const providedResponse = this.profile[question] - if (providedResponse) { - return providedResponses[question][providedResponse] + if (this.profile) { + const customResponse = this.profile[question + '_other'] + if (customResponse) { + return `Other: ${customResponse}` + } + const providedResponse = this.profile[question] + if (providedResponse) { + return providedResponses[question][providedResponse] + } } return 'No answer selected.' }, @@ -184,11 +186,8 @@ export default { temporality: this.dialog.data.stepTwo.temporality, ...(this.dialog.data.stepTwo.otherTemporality && { temporality_other: this.dialog.data.stepTwo.otherTemporality }) } - - this.profile = (await this.$store.dispatch('updateProfile', { - wiki: this.wikiId, profile: JSON.stringify(profile) - })).data.data ?? {} - + await this.$store.dispatch('updateProfile', { wiki: this.wikiId, profile: JSON.stringify(profile) }) + this.profile = this.$store.state.wikis.currentWikiProfile this.$refs.message.show('success', 'Intended use has been updated.') this.dialog.show = false } catch (error) { @@ -200,13 +199,7 @@ export default { } }, async created () { - try { - const details = await this.$api.wikiDetails({ wiki: this.wikiId }) - this.profile = details.wiki_latest_profile ?? {} - } catch (error) { - console.log(error) - this.$refs.message.show('error', 'Something went wrong with fetching your intended use.') - } + this.profile = this.$store.state.wikis.currentWikiProfile } } diff --git a/src/store/wikis.js b/src/store/wikis.js index 61e1de42..a2028c62 100644 --- a/src/store/wikis.js +++ b/src/store/wikis.js @@ -153,6 +153,9 @@ const mutations = { }, set_current_wiki_entityImportError (state, error) { state.currentWikiEntityImportError = error + }, + set_current_wiki_profile (state, value) { + state.currentWikiProfile = value } } @@ -166,7 +169,10 @@ const actions = { }) .catch(err => commit('set_current_wiki_entityImportError', err)) api.wikiDetails({ wiki: wikiId }) - .then(details => commit('set_current_wiki_settings', details)) + .then(details => { + commit('set_current_wiki_settings', details) + commit('set_current_wiki_profile', details.wiki_latest_profile) + }) }, resetWikisState ({ commit }) { commit('wikis_resetState') @@ -197,8 +203,9 @@ const actions = { updateSetting ({ commit }, payload) { return api.updateSetting(payload.setting, payload) }, - updateProfile ({ commit }, payload) { - return api.updateProfile(payload) + async updateProfile ({ commit }, payload) { + const response = await api.updateProfile(payload) + commit('set_current_wiki_profile', response) }, triggerEntityImport ({ commit }, wikiId) { return api.importEntities({ wikiId })