Skip to content

Commit 89b6f67

Browse files
feat(notebooks): add actions, thunks, reducer, selector for checking if org has notebooks (#6410)
* feat(notebooks): add flows actions, reducer, selector, and thunk * feat(notebooks): add api call for Notebooks to GetOrganizations
1 parent 4b786bf commit 89b6f67

File tree

7 files changed

+75
-1
lines changed

7 files changed

+75
-1
lines changed

src/flows/actions/flowsActions.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {Notebook} from 'src/client/notebooksRoutes'
2+
3+
export const SET_NOTEBOOKS = 'SET_NOTEBOOKS'
4+
5+
export type Actions = ReturnType<typeof setNotebooks>
6+
7+
export const setNotebooks = (notebooks: Notebook[]) => ({
8+
type: SET_NOTEBOOKS,
9+
notebooks,
10+
})

src/flows/actions/flowsThunks.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {Dispatch} from 'redux'
2+
3+
import {Notebook} from 'src/client/notebooksRoutes'
4+
5+
import {getNotebooks as fetchNotebooks} from 'src/client/notebooksRoutes'
6+
import {setNotebooks} from 'src/flows/actions/flowsActions'
7+
8+
export const getNotebooks =
9+
(orgID: string) => async (dispatch: Dispatch<any>) => {
10+
try {
11+
const response = await fetchNotebooks({query: {orgID}})
12+
13+
if (response.status !== 200) {
14+
throw new Error()
15+
}
16+
17+
dispatch(setNotebooks(response.data.flows))
18+
} catch {
19+
const emptyNotebooks = [] as Notebook[]
20+
dispatch(setNotebooks(emptyNotebooks))
21+
}
22+
}

src/flows/reducers/flowsReducer.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {Notebook} from 'src/client/notebooksRoutes'
2+
import {Actions, SET_NOTEBOOKS} from 'src/flows/actions/flowsActions'
3+
4+
export interface NotebooksState {
5+
notebooks: Notebook[]
6+
}
7+
8+
const INITIAL_STATE = {
9+
notebooks: [],
10+
}
11+
12+
export const flowsReducer = (
13+
state = INITIAL_STATE,
14+
action: Actions
15+
): NotebooksState => {
16+
switch (action.type) {
17+
case SET_NOTEBOOKS: {
18+
return {...state, notebooks: action.notebooks}
19+
}
20+
default: {
21+
return state
22+
}
23+
}
24+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {Notebook} from 'src/client/notebooksRoutes'
2+
import {AppState} from 'src/types'
3+
4+
export const selectNotebooks = (state: AppState): Notebook[] => {
5+
return state.resources.notebooks.notebooks
6+
}

src/shared/containers/GetOrganizations.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {RemoteDataState} from 'src/types'
3939

4040
// Thunks
4141
import {getQuartzIdentityThunk} from 'src/identity/actions/thunks'
42+
import {getNotebooks} from 'src/flows/actions/flowsThunks'
4243

4344
const GetOrganizations: FunctionComponent = () => {
4445
const {status: orgLoadingStatus} = useSelector(getAllOrgs)
@@ -49,7 +50,6 @@ const GetOrganizations: FunctionComponent = () => {
4950

5051
const dispatch = useDispatch()
5152

52-
// This doesn't require another API call.
5353
useEffect(() => {
5454
if (orgLoadingStatus === RemoteDataState.NotStarted) {
5555
dispatch(getOrganizations())
@@ -92,6 +92,12 @@ const GetOrganizations: FunctionComponent = () => {
9292
}
9393
}, [account.type, account.isUpgradeable])
9494

95+
useEffect(() => {
96+
if (CLOUD && org?.id) {
97+
dispatch(getNotebooks(org.id))
98+
}
99+
}, [org.id])
100+
95101
return (
96102
<PageSpinner loading={orgLoadingStatus}>
97103
<Suspense fallback={<PageSpinner />}>

src/store/configureStore.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import alertBuilderReducer from 'src/alerting/reducers/alertBuilder'
6161
import perfReducer from 'src/perf/reducers'
6262
import quartzOrganizationReducer from 'src/identity/quartzOrganizations/reducers'
6363
import orgCreationAllowancesReducer from 'src/identity/allowances/reducers'
64+
import {flowsReducer} from 'src/flows/reducers/flowsReducer'
6465

6566
// Types
6667
import {AppState, LocalStorage} from 'src/types'
@@ -110,6 +111,7 @@ export const rootReducer = (history: History) => (state, action) => {
110111
endpoints: endpointsReducer,
111112
labels: labelsReducer,
112113
members: membersReducer,
114+
notebooks: flowsReducer,
113115
orgs: orgsReducer,
114116
rules: rulesReducer,
115117
scrapers: scrapersReducer,

src/types/resources.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import {
2121
Subscription,
2222
} from 'src/types'
2323

24+
import {NotebooksState} from 'src/flows/reducers/flowsReducer'
25+
2426
export enum ResourceType {
2527
Authorizations = 'tokens',
2628
Buckets = 'buckets',
@@ -31,6 +33,7 @@ export enum ResourceType {
3133
Labels = 'labels',
3234
Orgs = 'orgs',
3335
Members = 'members',
36+
Notebooks = 'notebooks',
3437
NotificationRules = 'rules',
3538
NotificationEndpoints = 'endpoints',
3639
Plugins = 'plugins',
@@ -90,6 +93,7 @@ export interface ResourceState {
9093
[ResourceType.Dashboards]: DashboardsState
9194
[ResourceType.Labels]: NormalizedState<Label>
9295
[ResourceType.Members]: NormalizedState<Member>
96+
[ResourceType.Notebooks]: NotebooksState
9397
[ResourceType.Orgs]: OrgsState
9498
[ResourceType.Scrapers]: NormalizedState<Scraper>
9599
[ResourceType.Secrets]: NormalizedState<Secret>

0 commit comments

Comments
 (0)