diff --git a/web/src/stores/dandiset.ts b/web/src/stores/dandiset.ts index 22d3524cf..63aad9639 100644 --- a/web/src/stores/dandiset.ts +++ b/web/src/stores/dandiset.ts @@ -8,7 +8,15 @@ import { dandiRest, user } from '@/rest'; import type { User, Version } from '@/types'; import { draftVersion } from '@/utils/constants'; + +function isUnauthenticatedOrForbidden(err: unknown) { + return axios.isAxiosError(err) && err.response && [401, 403].includes(err.response.status); +} + interface State { + meta: { + dandisetExistsAndEmbargoed: boolean, + }; dandiset: Version | null; versions: Version[] | null, owners: User[] | null, @@ -17,6 +25,7 @@ interface State { export const useDandisetStore = defineStore('dandiset', { state: (): State => ({ + meta: { dandisetExistsAndEmbargoed: false }, dandiset: null, versions: null, owners: null, @@ -40,6 +49,7 @@ export const useDandisetStore = defineStore('dandiset', { }, actions: { async uninitializeDandisets() { + this.meta.dandisetExistsAndEmbargoed = false; this.dandiset = null; this.versions = null; this.owners = null; @@ -60,10 +70,11 @@ export const useDandisetStore = defineStore('dandiset', { try { res = await dandiRest.versions(identifier); } catch (err) { - // 401 errors are normal, and indicate that this dandiset is embargoed + // 401/403 errors are normal, and indicate that this dandiset is embargoed // and the user doesn't have permission to view it. - if (axios.isAxiosError(err) && err.response?.status === 401) { + if (isUnauthenticatedOrForbidden(err)) { res = null; + this.meta.dandisetExistsAndEmbargoed = true; } else { throw err; } @@ -74,19 +85,23 @@ export const useDandisetStore = defineStore('dandiset', { } }, async fetchDandiset({ identifier, version }: Record) { - const sanitizedVersion = version || (await dandiRest.mostRecentVersion(identifier))?.version; - - if (!sanitizedVersion) { - this.dandiset = null; - return; - } - try { + const sanitizedVersion = version || (await dandiRest.mostRecentVersion(identifier))?.version; + if (!sanitizedVersion) { + this.dandiset = null; + return; + } + const data = await dandiRest.specificVersion(identifier, sanitizedVersion); this.dandiset = data; } catch (err) { if (axios.isAxiosError(err) && err.response && err.response.status < 500) { this.dandiset = null; + + if (isUnauthenticatedOrForbidden(err)) { + this.meta.dandisetExistsAndEmbargoed = true; + } + } else { throw err; } diff --git a/web/src/views/DandisetLandingView/DandisetLandingView.vue b/web/src/views/DandisetLandingView/DandisetLandingView.vue index 3f86b3b85..0d72237fd 100644 --- a/web/src/views/DandisetLandingView/DandisetLandingView.vue +++ b/web/src/views/DandisetLandingView/DandisetLandingView.vue @@ -193,20 +193,11 @@ watch(() => props.identifier, async () => { } // Set default values - embargoedOrUnauthenticated.value = false; loading.value = true; - // Catch error to check if response is 401, for embargoed dandisets - try { - await store.initializeDandisets({ identifier, version }); - - } catch (e) { - if (axios.isAxiosError(e) && (e.response?.status === 401 || e.response?.status === 403)) { - embargoedOrUnauthenticated.value = true - } else { - throw e - } - } + // Check if response is 401 or 403, for embargoed dandisets + await store.initializeDandisets({ identifier, version }); + embargoedOrUnauthenticated.value = store.meta.dandisetExistsAndEmbargoed; loading.value = false; }, { immediate: true });