From 00564e51603c7c0bc25167b072364392706413d6 Mon Sep 17 00:00:00 2001 From: Thomas Eidens Date: Wed, 14 Feb 2024 15:00:04 +0100 Subject: [PATCH] Fixed multiple fetches of docmap data --- .../src/components/highlights/list-item.vue | 43 ++++++++------ frontend/src/store/by-article-id.js | 58 +++++++++++++++++++ frontend/src/store/index.js | 2 + 3 files changed, 84 insertions(+), 19 deletions(-) create mode 100644 frontend/src/store/by-article-id.js diff --git a/frontend/src/components/highlights/list-item.vue b/frontend/src/components/highlights/list-item.vue index 5d34ffc9..a1f5a700 100644 --- a/frontend/src/components/highlights/list-item.vue +++ b/frontend/src/components/highlights/list-item.vue @@ -136,10 +136,9 @@ v-card(v-if="article" color="tertiary") diff --git a/frontend/src/store/by-article-id.js b/frontend/src/store/by-article-id.js new file mode 100644 index 00000000..0d227bc7 --- /dev/null +++ b/frontend/src/store/by-article-id.js @@ -0,0 +1,58 @@ +import httpClient from '../lib/http' +import { BASE_URL } from '../lib/http' +import { parseDocmaps } from '@source-data/render-rev' + +const docmapsApiPath = (doi) => `${BASE_URL}/api/v2/docmap/${doi}` + +export const byArticleId = { + namespaced: true, + state: { + reviewProcesses: {}, + }, + getters: { + getReviewProcessForDoi: (state) => (doi) => { + const data = state.reviewProcesses[doi] + if (!data) { + return undefined + } + const { reviewProcess } = data + return reviewProcess + } + }, + mutations: { + setReviewProcessForDoi(state, {doi, data}) { + state.reviewProcesses[doi] = data + }, + }, + actions: { + async fetchReviewProcessForDoi({ commit, state }, doi) { + if (state.reviewProcesses[doi]) { + const { promise } = state.reviewProcesses[doi] + return promise + } + // First we get the docmaps + const url = docmapsApiPath(doi) + const promise = httpClient.get(url) + .then((response) => parseDocmaps(response.data)) + .then((reviewProcess) => { + const data = { promise: Promise.resolve(), reviewProcess } + commit('setReviewProcessForDoi', {doi, data}) + }) + .catch(function (error) { + if (error.response.status === 400) { + commit("setSnack", + { message: "snack.message.error.request", + color: "red" }, + { root: true }); + } else { + commit("setSnack", + { message: "snack.message.error.server", + color: "red" }, + { root: true }); + } + }) + const data = { promise, reviewProcess: undefined } + commit('setReviewProcessForDoi', {doi, data}) + }, + }, +} diff --git a/frontend/src/store/index.js b/frontend/src/store/index.js index a0116b73..c0a03746 100644 --- a/frontend/src/store/index.js +++ b/frontend/src/store/index.js @@ -1,11 +1,13 @@ import Vue from 'vue' import Vuex from 'vuex' +import { byArticleId } from './by-article-id' import { byFilters } from './by-filters' Vue.use(Vuex) export default new Vuex.Store({ modules: { + byArticleId, byFilters }, state: {