Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed withContext from ContextualSaveBar
- Loading branch information
1 parent
623cd1f
commit f0b3338
Showing
1 changed file
with
25 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,37 @@ | ||
import * as React from 'react'; | ||
import compose from '@shopify/react-compose'; | ||
import isEqual from 'lodash/isEqual'; | ||
import {ContextualSaveBarProps, FrameContextType, FrameContext} from '../Frame'; | ||
import withContext from '../WithContext'; | ||
import {WithContextTypes} from '../../types'; | ||
import {withAppProvider, WithAppProviderProps} from '../AppProvider'; | ||
import {ContextualSaveBarProps, FrameContext} from '../Frame'; | ||
|
||
// The script in the styleguide that generates the Props Explorer data expects | ||
// a component's props to be found in the Props interface. This silly workaround | ||
// ensures that the Props Explorer table is generated correctly, instead of | ||
// crashing if we write `ContextualSaveBar extends React.Component<ContextualSaveBarProps>` | ||
interface Props extends ContextualSaveBarProps {} | ||
export type ComposedProps = Props & | ||
WithAppProviderProps & | ||
WithContextTypes<FrameContextType>; | ||
|
||
class ContextualSaveBar extends React.PureComponent<ComposedProps, never> { | ||
componentDidMount() { | ||
const { | ||
props: {polaris, context, ...rest}, | ||
} = this; | ||
context.setContextualSaveBar(rest); | ||
} | ||
export default function ContextualSaveBar({ | ||
message, | ||
saveAction, | ||
discardAction, | ||
alignContentFlush, | ||
}: Props) { | ||
const frame = React.useContext(FrameContext); | ||
|
||
componentWillUnmount() { | ||
this.props.context.removeContextualSaveBar(); | ||
} | ||
React.useEffect( | ||
() => { | ||
frame.setContextualSaveBar({ | ||
message, | ||
saveAction, | ||
discardAction, | ||
alignContentFlush, | ||
}); | ||
}, | ||
[message, saveAction, discardAction, alignContentFlush], | ||
); | ||
|
||
componentDidUpdate(oldProps: ComposedProps) { | ||
const { | ||
props: {polaris, context, ...rest}, | ||
} = this; | ||
if (contextualSaveBarHasChanged(rest, oldProps)) { | ||
context.setContextualSaveBar(rest); | ||
} | ||
} | ||
React.useEffect(() => { | ||
return () => { | ||
frame.removeContextualSaveBar(); | ||
}; | ||
}, []); | ||
|
||
render() { | ||
return null; | ||
} | ||
return null; | ||
} | ||
|
||
function contextualSaveBarHasChanged( | ||
{message, saveAction, discardAction}: Props, | ||
{ | ||
message: oldMessage, | ||
saveAction: oldsaveAction, | ||
discardAction: oldDiscardAction, | ||
}: Props, | ||
) { | ||
return Boolean( | ||
message !== oldMessage || | ||
!isEqual(saveAction, oldsaveAction) || | ||
!isEqual(discardAction, oldDiscardAction), | ||
); | ||
} | ||
|
||
export default compose<Props>( | ||
withContext<Props, WithAppProviderProps, FrameContextType>( | ||
FrameContext.Consumer, | ||
), | ||
withAppProvider(), | ||
)(ContextualSaveBar); |