-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring to common hooks and MDSEnabledComponent
Signed-off-by: Ramakrishna Chilaka <[email protected]>
- Loading branch information
1 parent
66bc3d3
commit 9ef5c90
Showing
27 changed files
with
166 additions
and
328 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
public/components/MDSEnabledComponent/MDSEnabledComponent.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,52 @@ | ||
import React, { useContext, useEffect } from "react"; | ||
import { DataSourceMenuContext, DataSourceMenuProperties } from "../../services/DataSourceMenuContext"; | ||
import { useHistory } from "react-router"; | ||
import queryString from "query-string"; | ||
|
||
export default class MDSEnabledComponent< | ||
Props extends DataSourceMenuProperties, | ||
State extends DataSourceMenuProperties | ||
> extends React.Component<Props, State> { | ||
constructor(props: Props) { | ||
super(props); | ||
this.state = { | ||
dataSourceId: props.dataSourceId, | ||
dataSourceLabel: props.dataSourceLabel, | ||
multiDataSourceEnabled: props.multiDataSourceEnabled, | ||
} as State; | ||
} | ||
|
||
static getDerivedStateFromProps<Props extends DataSourceMenuProperties, State extends DataSourceMenuProperties>( | ||
nextProps: Props, | ||
prevState: State | ||
) { | ||
// static members cannot reference class type parameters | ||
if ( | ||
nextProps.multiDataSourceEnabled && | ||
(nextProps.dataSourceId != prevState.dataSourceId || nextProps.dataSourceLabel != prevState.dataSourceLabel) | ||
) { | ||
return { | ||
dataSourceId: nextProps.dataSourceId, | ||
dataSourceLabel: nextProps.dataSourceLabel, | ||
}; | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
export function useUpdateUrlWithDataSourceProperties() { | ||
const dataSourceMenuProps = useContext(DataSourceMenuContext); | ||
const { dataSourceId, dataSourceLabel, multiDataSourceEnabled } = dataSourceMenuProps; | ||
if (multiDataSourceEnabled) { | ||
// mds flag can't change while the app is loaded | ||
const history = useHistory(); | ||
useEffect(() => { | ||
history.replace({ | ||
search: queryString.stringify({ | ||
dataSourceId, | ||
dataSourceLabel, | ||
}), | ||
}); | ||
}, [dataSourceId, dataSourceLabel]); | ||
} | ||
} |
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 @@ | ||
import MDSEnabledComponent from "./MDSEnabledComponent"; | ||
|
||
export default MDSEnabledComponent; | ||
export * from "./MDSEnabledComponent"; |
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
Oops, something went wrong.