New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
[v4][ContextualSaveBar] Remove withContext #1498
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,45 @@ | ||
import 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 * as React from 'react'; | ||
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 React.memo(function ContextualSaveBar({ | ||
message, | ||
saveAction, | ||
discardAction, | ||
alignContentFlush, | ||
}: Props) { | ||
const frame = React.useContext(FrameContext); | ||
|
||
componentWillUnmount() { | ||
this.props.context.removeContextualSaveBar(); | ||
} | ||
React.useEffect( | ||
() => { | ||
if (!frame) return; | ||
|
||
componentDidUpdate(oldProps: ComposedProps) { | ||
const { | ||
props: {polaris, context, ...rest}, | ||
} = this; | ||
if (contextualSaveBarHasChanged(rest, oldProps)) { | ||
context.setContextualSaveBar(rest); | ||
} | ||
} | ||
|
||
render() { | ||
return null; | ||
} | ||
} | ||
frame.setContextualSaveBar({ | ||
message, | ||
saveAction, | ||
discardAction, | ||
alignContentFlush, | ||
}); | ||
}, | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[message, saveAction, discardAction, alignContentFlush], | ||
); | ||
|
||
function contextualSaveBarHasChanged( | ||
{message, saveAction, discardAction}: Props, | ||
{ | ||
message: oldMessage, | ||
saveAction: oldsaveAction, | ||
discardAction: oldDiscardAction, | ||
}: Props, | ||
) { | ||
return Boolean( | ||
message !== oldMessage || | ||
!isEqual(saveAction, oldsaveAction) || | ||
!isEqual(discardAction, oldDiscardAction), | ||
React.useEffect( | ||
() => { | ||
return () => { | ||
if (!frame) return; | ||
frame.removeContextualSaveBar(); | ||
}; | ||
}, | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[], | ||
); | ||
} | ||
|
||
export default compose<Props>( | ||
withContext<Props, WithAppProviderProps, FrameContextType>( | ||
FrameContext.Consumer, | ||
), | ||
withAppProvider(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was never being used |
||
)(ContextualSaveBar); | ||
return null; | ||
}); |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Frame context next changes so we don't need it as a dep