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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add Redux Starter Kit #961
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0be020d
update dependencies
TylerShin 2b97777
change create store method in browser side
TylerShin 4830879
change server's createStore method & remove unneeded redux-thunk depe…
TylerShin 2556b2c
change layout state
TylerShin 9aaa609
make deviceDetector use redux-hooks api
TylerShin 366f73f
Merge branch 'master' into dev/RSK
TylerShin 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,52 @@ | ||
import * as React from 'react'; | ||
import { throttle, Cancelable } from 'lodash'; | ||
import { connect, DispatchProp } from 'react-redux'; | ||
import { throttle } from 'lodash'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import EnvChecker from '../../helpers/envChecker'; | ||
import UserAgentHelper from '../../helpers/userAgentHelper'; | ||
import { UserDevice, setDeviceType, LayoutState } from '../layouts/reducer'; | ||
import { AppState } from '../../reducers'; | ||
import { LayoutState, UserDevice } from '../layouts/records'; | ||
import { setDeviceToMobile, setDeviceToDesktop, setDeviceToTablet } from '../layouts/actions'; | ||
|
||
const MOBILE_WIDTH = 768; | ||
const TABLET_WIDTH = 1024; | ||
|
||
function mapStateToProps(state: AppState) { | ||
return { | ||
layout: state.layout, | ||
}; | ||
} | ||
const DeviceDetector: React.FC = () => { | ||
const dispatch = useDispatch(); | ||
const layout = useSelector<AppState, LayoutState>(state => state.layout); | ||
|
||
interface DeviceDetectorProps extends DispatchProp { | ||
layout: LayoutState; | ||
} | ||
React.useEffect(() => { | ||
if (EnvChecker.isOnServer()) return; | ||
|
||
class DeviceDetector extends React.PureComponent<DeviceDetectorProps, {}> { | ||
private throttledHandlingWindowSizeChange: (() => void) & Cancelable; | ||
|
||
public constructor(props: DeviceDetectorProps) { | ||
super(props); | ||
|
||
this.throttledHandlingWindowSizeChange = throttle(this.handleWindowSizeChange, 300); | ||
} | ||
|
||
public componentDidMount() { | ||
if (!EnvChecker.isOnServer()) { | ||
const { dispatch } = this.props; | ||
const device = UserAgentHelper.getDevice(); | ||
|
||
if (device && device.type === 'mobile') { | ||
dispatch!(setDeviceToMobile()); | ||
} | ||
|
||
window.addEventListener('resize', this.throttledHandlingWindowSizeChange); | ||
const device = UserAgentHelper.getDevice(); | ||
if (device && device.type === 'mobile') { | ||
dispatch(setDeviceType({ userDevice: UserDevice.MOBILE })); | ||
} | ||
} | ||
|
||
public componentWillUnmount() { | ||
if (!EnvChecker.isOnServer()) { | ||
window.removeEventListener('resize', this.throttledHandlingWindowSizeChange); | ||
} | ||
} | ||
|
||
public render() { | ||
return <span />; | ||
} | ||
|
||
private handleWindowSizeChange = () => { | ||
const { dispatch, layout } = this.props; | ||
|
||
if (!EnvChecker.isOnServer()) { | ||
const handleWindowSizeChange = () => { | ||
if (document.documentElement) { | ||
const currentWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0); | ||
|
||
if (currentWidth < MOBILE_WIDTH && layout.userDevice !== UserDevice.MOBILE) { | ||
dispatch!(setDeviceToMobile()); | ||
dispatch(setDeviceType({ userDevice: UserDevice.MOBILE })); | ||
} else if ( | ||
currentWidth >= MOBILE_WIDTH && | ||
currentWidth < TABLET_WIDTH && | ||
layout.userDevice !== UserDevice.TABLET | ||
) { | ||
dispatch!(setDeviceToTablet()); | ||
dispatch(setDeviceType({ userDevice: UserDevice.TABLET })); | ||
} else if (currentWidth >= TABLET_WIDTH && layout.userDevice !== UserDevice.DESKTOP) { | ||
dispatch!(setDeviceToDesktop()); | ||
dispatch(setDeviceType({ userDevice: UserDevice.DESKTOP })); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
export default connect(mapStateToProps)(DeviceDetector); | ||
}; | ||
const throttledHandlingWindowSizeChange = throttle(handleWindowSizeChange, 300); | ||
window.addEventListener('resize', throttledHandlingWindowSizeChange); | ||
return () => { | ||
if (EnvChecker.isOnServer()) return; | ||
window.removeEventListener('resize', throttledHandlingWindowSizeChange); | ||
}; | ||
}); | ||
|
||
return null; | ||
}; | ||
|
||
export default DeviceDetector; |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
Hooks 👍