forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
attempt 1 at dynamic state management
Signed-off-by: Ashwin P Chandran <ashwinpc@amazon.com>
- Loading branch information
Showing
13 changed files
with
221 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/plugins/discover/public/application/utils/state_management/discover_slice.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { createSlice, PayloadAction } from '@reduxjs/toolkit'; | ||
import { TypedUseSelectorHook, createDispatchHook, createSelectorHook } from 'react-redux'; | ||
import { createContext } from 'react'; | ||
import { Filter, Query } from '../../../../../data/public'; | ||
import { DiscoverServices } from '../../../build_services'; | ||
import { DataExplorerRootState } from '../../../../../data_explorer/public'; | ||
|
||
export interface DiscoverState { | ||
/** | ||
* Columns displayed in the table | ||
*/ | ||
columns?: string[]; | ||
/** | ||
* Array of applied filters | ||
*/ | ||
filters?: Filter[]; | ||
/** | ||
* id of the used index pattern | ||
*/ | ||
index?: string; | ||
/** | ||
* Used interval of the histogram | ||
*/ | ||
interval?: string; | ||
/** | ||
* Lucence or DQL query | ||
*/ | ||
query?: Query; | ||
/** | ||
* Array of the used sorting [[field,direction],...] | ||
*/ | ||
sort?: string[][]; | ||
/** | ||
* id of the used saved query | ||
*/ | ||
savedQuery?: string; | ||
} | ||
|
||
export interface RootState extends DataExplorerRootState { | ||
discover: DiscoverState; | ||
} | ||
|
||
const initialState = {} as DiscoverState; | ||
|
||
export const getPreloadedState = async ({ data }: DiscoverServices): Promise<DiscoverState> => { | ||
// console.log(data.query.timefilter.timefilter.getRefreshInterval().value.toString()); | ||
return { | ||
...initialState, | ||
interval: data.query.timefilter.timefilter.getRefreshInterval().value.toString(), | ||
}; | ||
}; | ||
|
||
export const discoverSlice = createSlice({ | ||
name: 'discover', | ||
initialState, | ||
reducers: { | ||
setState<T>(state: T, action: PayloadAction<DiscoverState>) { | ||
return action.payload; | ||
}, | ||
updateState<T>(state: T, action: PayloadAction<Partial<DiscoverState>>) { | ||
state = { | ||
...state, | ||
...action.payload, | ||
}; | ||
}, | ||
}, | ||
}); | ||
|
||
// Exposing the state functions as generics | ||
export const setState = discoverSlice.actions.setState as <T>(payload: T) => PayloadAction<T>; | ||
export const updateState = discoverSlice.actions.updateState as <T>( | ||
payload: Partial<T> | ||
) => PayloadAction<Partial<T>>; | ||
|
||
export const { reducer } = discoverSlice; | ||
export const contextDiscover = createContext<any>({}); | ||
|
||
export const useTypedSelector: TypedUseSelectorHook<RootState> = createSelectorHook( | ||
contextDiscover | ||
); | ||
|
||
export const useDispatch = createDispatchHook(contextDiscover); |
4 changes: 4 additions & 0 deletions
4
src/plugins/discover/public/application/utils/state_management/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.