Skip to content

Commit 5c9591f

Browse files
authored
fix: improve quartzOrganizations reducer compatibility with quartz (#5108)
1 parent cec4055 commit 5c9591f

File tree

3 files changed

+63
-26
lines changed

3 files changed

+63
-26
lines changed

src/identity/quartzOrganizations/actions/creators/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,8 @@ export const setQuartzOrganizationsStatus = (status: RemoteDataState) =>
2424
status: status,
2525
} as const)
2626

27-
export const setQuartzDefaultOrg = (
28-
oldDefaultOrgId: string,
29-
newDefaultOrgId: string
30-
) =>
27+
export const setQuartzDefaultOrg = (newDefaultOrgId: string) =>
3128
({
3229
type: SET_QUARTZ_DEFAULT_ORG,
33-
oldDefaultOrgId: oldDefaultOrgId,
3430
newDefaultOrgId: newDefaultOrgId,
3531
} as const)

src/identity/quartzOrganizations/actions/thunks/index.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ import {OrganizationSummaries} from 'src/client/unityRoutes'
1919
type Actions = QuartzOrganizationActions | PublishNotificationAction
2020
type DefaultOrg = OrganizationSummaries[number]
2121

22+
class OrgNotFoundError extends Error {
23+
constructor(message) {
24+
super(message)
25+
this.name = 'DefaultOrgNotFoundError'
26+
}
27+
}
28+
2229
// Error Reporting
2330
import {reportErrorThroughHoneyBadger} from 'src/shared/utils/errors'
2431

@@ -31,7 +38,6 @@ export const getQuartzOrganizationsThunk = () => async (
3138
const quartzOrganizations = await fetchQuartzOrgs()
3239

3340
dispatch(setQuartzOrganizations(quartzOrganizations))
34-
dispatch(setQuartzOrganizationsStatus(RemoteDataState.Done))
3541
} catch (err) {
3642
dispatch(setQuartzOrganizationsStatus(RemoteDataState.Error))
3743

@@ -42,18 +48,32 @@ export const getQuartzOrganizationsThunk = () => async (
4248
}
4349
}
4450

45-
export const updateDefaultOrgThunk = (
46-
oldDefaultOrg: DefaultOrg,
47-
newDefaultOrg: DefaultOrg
48-
) => async (dispatch: Dispatch<Actions>, getState: GetState) => {
51+
export const updateDefaultOrgThunk = (newDefaultOrg: DefaultOrg) => async (
52+
dispatch: Dispatch<Actions>,
53+
getState: GetState
54+
) => {
4955
try {
5056
dispatch(setQuartzOrganizationsStatus(RemoteDataState.Loading))
5157

5258
await updateDefaultQuartzOrg(newDefaultOrg.id)
5359

54-
dispatch(setQuartzDefaultOrg(oldDefaultOrg.id, newDefaultOrg.id))
60+
dispatch(setQuartzDefaultOrg(newDefaultOrg.id))
61+
62+
const state = getState()
63+
const orgStatus = state.identity.currentIdentity.status
64+
65+
if (orgStatus === RemoteDataState.Error) {
66+
const defaultOrgErrMsg =
67+
'quartzOrganizations state does not contain requested default organization'
5568

56-
dispatch(setQuartzOrganizationsStatus(RemoteDataState.Done))
69+
reportErrorThroughHoneyBadger(new OrgNotFoundError(defaultOrgErrMsg), {
70+
name: defaultOrgErrMsg,
71+
context: {
72+
org: newDefaultOrg,
73+
state: getState(),
74+
},
75+
})
76+
}
5777
} catch (err) {
5878
dispatch(setQuartzOrganizationsStatus(RemoteDataState.Error))
5979

src/identity/quartzOrganizations/reducers/index.ts

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import {
88
import {emptyOrg} from 'src/identity/components/GlobalHeader/DefaultEntities'
99
import produce from 'immer'
1010

11-
import {OrganizationSummaries} from 'src/client/unityRoutes'
11+
// Types
1212

13-
const initialState = {
13+
import {OrganizationSummaries} from 'src/client/unityRoutes'
14+
import {RemoteDataState} from 'src/types'
15+
export const initialState = {
1416
orgs: [emptyOrg] as OrganizationSummaries,
1517
} as QuartzOrganizations
1618

@@ -19,27 +21,46 @@ export default (state = initialState, action: Actions): QuartzOrganizations =>
1921
switch (action.type) {
2022
case SET_QUARTZ_ORGANIZATIONS: {
2123
draftState.orgs = action.quartzOrganizations
24+
draftState.status = RemoteDataState.Done
25+
2226
return
2327
}
28+
2429
case SET_QUARTZ_ORGANIZATIONS_STATUS: {
2530
draftState.status = action.status
2631
return
2732
}
2833

2934
case SET_QUARTZ_DEFAULT_ORG: {
30-
const {oldDefaultOrgId, newDefaultOrgId} = action
31-
32-
if (oldDefaultOrgId !== newDefaultOrgId) {
33-
draftState.orgs.forEach(org => {
34-
if (org.id === oldDefaultOrgId) {
35-
org.isDefault = false
36-
}
37-
38-
if (org.id === newDefaultOrgId) {
39-
org.isDefault = true
40-
}
41-
})
35+
// No existing default org is acceptable; oldDefaultOrg may be undefined.
36+
const oldDefaultOrg = draftState.orgs.find(
37+
org => org.isDefault === true
38+
)
39+
const oldDefaultOrgId = oldDefaultOrg?.id
40+
const {newDefaultOrgId} = action
41+
42+
if (oldDefaultOrgId === newDefaultOrgId) {
43+
return
4244
}
45+
46+
let newIdCount = 0
47+
48+
draftState.orgs.forEach(org => {
49+
if (org.id === oldDefaultOrgId) {
50+
org.isDefault = false
51+
}
52+
if (org.id === newDefaultOrgId) {
53+
org.isDefault = true
54+
newIdCount++
55+
}
56+
})
57+
58+
if (newIdCount !== 1) {
59+
draftState.status = RemoteDataState.Error
60+
return
61+
}
62+
63+
draftState.status = RemoteDataState.Done
4364
return
4465
}
4566
}

0 commit comments

Comments
 (0)