diff --git a/frontend/common/dispatcher/app-actions.js b/frontend/common/dispatcher/app-actions.js index 9e0c4bf6a0ca..898209b40465 100644 --- a/frontend/common/dispatcher/app-actions.js +++ b/frontend/common/dispatcher/app-actions.js @@ -179,9 +179,10 @@ const AppActions = Object.assign({}, BaseAppActions, { id, }) }, - getOrganisation(organisationId) { + getOrganisation(organisationId, force) { Dispatcher.handleViewAction({ actionType: Actions.GET_ORGANISATION, + force, id: organisationId, }) }, diff --git a/frontend/common/stores/organisation-store.js b/frontend/common/stores/organisation-store.js index 9a1b7499ac92..b285b852de12 100644 --- a/frontend/common/stores/organisation-store.js +++ b/frontend/common/stores/organisation-store.js @@ -11,6 +11,9 @@ import filter from 'lodash/filter' import findIndex from 'lodash/findIndex' import keyBy from 'lodash/keyBy' +// Placeholder id until an organisation loads; never a real API id. +const NO_ORGANISATION = 'account' + const controller = { createProject: (name) => { store.saving() @@ -132,73 +135,80 @@ const controller = { ] : [], ), - ).then((res) => { - if (`${id}` === `${store.id}`) { - // eslint-disable-next-line prefer-const - let [_projects, users, invites, subscriptionMeta] = res - let projects = sortBy(_projects, (project) => - project.name.toLowerCase(), - ) + ) + .then((res) => { + if (`${id}` === `${store.id}`) { + // eslint-disable-next-line prefer-const + let [_projects, users, invites, subscriptionMeta] = res + let projects = sortBy(_projects, (project) => + project.name.toLowerCase(), + ) - store.model = { - ...store.model, - invites: invites && invites.results, - subscriptionMeta, - users: sortBy(users, (user) => { - const isYou = user.id === AccountStore.getUser().id - if (isYou) { - return `` - } - return `${user.first_name || ''} ${ - user.last_name || '' - }`.toLowerCase() - }), - } + store.model = { + ...store.model, + invites: invites && invites.results, + subscriptionMeta, + users: sortBy(users, (user) => { + const isYou = user.id === AccountStore.getUser().id + if (isYou) { + return `` + } + return `${user.first_name || ''} ${ + user.last_name || '' + }`.toLowerCase() + }), + } - if (Project.hideInviteLinks) { - store.loaded() - } else { - data - .get(`${Project.api}organisations/${id}/invite-links/`) - .then((links) => { - store.model.inviteLinks = links - if (!links || !links.length) { - Promise.all([ - data.post( - `${Project.api}organisations/${id}/invite-links/`, - { - role: 'ADMIN', - }, - ), - data.post( - `${Project.api}organisations/${id}/invite-links/`, - { - role: 'USER', - }, - ), - ]).then(() => { - data - .get(`${Project.api}organisations/${id}/invite-links/`) - .then((links) => { - store.model.inviteLinks = links + if (Project.hideInviteLinks) { + store.loaded() + } else { + data + .get(`${Project.api}organisations/${id}/invite-links/`) + .then((links) => { + store.model.inviteLinks = links + if (!links || !links.length) { + // Returned so a failure reaches the catch below. + return Promise.all([ + data.post( + `${Project.api}organisations/${id}/invite-links/`, + { + role: 'ADMIN', + }, + ), + data.post( + `${Project.api}organisations/${id}/invite-links/`, + { + role: 'USER', + }, + ), + ]).then(() => + data + .get(`${Project.api}organisations/${id}/invite-links/`) + .then((links) => { + store.model.inviteLinks = links - store.loaded() - }) - }) - } else { + store.loaded() + }), + ) + } store.loaded() - } - }) - .catch((e) => { - API.ajaxHandler(store, e) - }) - } + }) + .catch((e) => { + API.ajaxHandler(store, e) + }) + } - store.model.projects = projects - store.model.keyedProjects = keyBy(store.model.projects, 'id') - store.loaded() - } - }) + store.model.projects = projects + store.model.keyedProjects = keyBy(store.model.projects, 'id') + store.loaded() + } + }) + .catch((e) => { + // A newer request may have replaced this one. + if (`${id}` === `${store.id}`) { + API.ajaxHandler(store, e) + } + }) } }, invalidateInviteLink: (link) => { @@ -263,16 +273,21 @@ const store = Object.assign({}, BaseStore, { return store.model && store.model.subscriptionMeta }, getUsers: () => store.model && store.model.users, - id: 'account', + id: NO_ORGANISATION, }) store.dispatcherIndex = Dispatcher.register(store, (payload) => { const action = payload.action // this is our action from handleViewAction switch (action.actionType) { - case Actions.GET_ORGANISATION: - controller.getOrganisation(action.id || store.id, action.force) + case Actions.GET_ORGANISATION: { + // A refresh before any organisation has loaded has nothing to fetch. + const id = action.id || store.id + if (id && id !== NO_ORGANISATION) { + controller.getOrganisation(id, action.force) + } break + } case Actions.CREATE_PROJECT: controller.createProject(action.name) break diff --git a/frontend/web/components/pages/onboarding/hooks/bootstrapOnboarding.ts b/frontend/web/components/pages/onboarding/hooks/bootstrapOnboarding.ts index b527ab4ceb2b..499413673ff2 100644 --- a/frontend/web/components/pages/onboarding/hooks/bootstrapOnboarding.ts +++ b/frontend/web/components/pages/onboarding/hooks/bootstrapOnboarding.ts @@ -222,7 +222,8 @@ export async function bootstrapOnboarding( if (flag) { await ensureOnboardingTag(store, project, flag) } - AppActions.refreshOrganisation() + // The store has no organisation loaded on this page, so name it. + AppActions.getOrganisation(organisation.id, true) return { environment, featureName: flag?.name ?? FLAG_NAME,