-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from netceteragroup/hooks
Rewrite with Hooks
- Loading branch information
Showing
16 changed files
with
449 additions
and
294 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import React from 'react'; | ||
import { useMessageSource } from 'react-message-source'; | ||
|
||
export default function Hooks() { | ||
const { getMessage } = useMessageSource(); | ||
return <span>Translation with a hook: {getMessage('hello.world')}</span>; | ||
} |
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,5 +1,8 @@ | ||
/** | ||
* The Public API. | ||
*/ | ||
export { Provider, withMessages, propTypes } from './lib/messageSource'; | ||
export { Provider, Consumer } from './lib/MessageSourceContext'; | ||
export { FetchingProvider } from './lib/FetchingProvider'; | ||
export { useMessageSource } from './lib/useMessageSource'; | ||
export { withMessages } from './lib/withMessages'; | ||
export { propTypes } from './lib/propTypes'; |
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,112 +1,101 @@ | ||
import React, { Component } from 'react'; | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import { Provider } from './messageSource'; | ||
import { Provider } from './MessageSourceContext'; | ||
|
||
const identity = x => x; | ||
|
||
export class FetchingProvider extends Component { | ||
state = { | ||
translations: {}, | ||
fetching: false, | ||
}; | ||
|
||
mounted = false; | ||
|
||
static propTypes = { | ||
/** | ||
* The URL which serves the text messages. | ||
* Required. | ||
*/ | ||
url: PropTypes.string.isRequired, | ||
|
||
/** | ||
* Makes the rendering of the sub-tree synchronous. | ||
* The components will not render until fetching of the text messages finish. | ||
* | ||
* Defaults to true. | ||
*/ | ||
blocking: PropTypes.bool, | ||
|
||
/** | ||
* A function which can transform the response received from GET /props.url | ||
* to a suitable format: | ||
* | ||
* Example: | ||
* function transform(response) { | ||
* return response.textMessages; | ||
* } | ||
*/ | ||
transform: PropTypes.func, | ||
|
||
/** | ||
* Invoked when fetching of text messages starts. | ||
*/ | ||
onFetchingStart: PropTypes.func, | ||
|
||
/** | ||
* Invoked when fetching of text messages finishes. | ||
*/ | ||
onFetchingEnd: PropTypes.func, | ||
|
||
/** | ||
* Invoked when fetching fails. | ||
*/ | ||
onFetchingError: PropTypes.func, | ||
|
||
/** | ||
* Children. | ||
*/ | ||
children: PropTypes.node, | ||
}; | ||
|
||
static defaultProps = { | ||
blocking: true, | ||
transform: identity, | ||
onFetchingStart: identity, | ||
onFetchingEnd: identity, | ||
onFetchingError: identity, | ||
}; | ||
|
||
componentDidMount() { | ||
this.mounted = true; | ||
this.fetchResources(); | ||
} | ||
|
||
componentDidUpdate(prevProps) { | ||
if (this.props.url !== prevProps.url) { | ||
this.fetchResources(); | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
this.mounted = false; | ||
} | ||
|
||
fetchResources = () => { | ||
const { url, transform, onFetchingStart, onFetchingEnd, onFetchingError } = this.props; | ||
|
||
this.setState({ fetching: true }, onFetchingStart); | ||
const initialState = { | ||
translations: {}, | ||
isFetching: false, | ||
}; | ||
|
||
/** | ||
* A special <Provider /> which can load translations from remote URL | ||
* via a `GET` request and pass them down the component tree. | ||
*/ | ||
export function FetchingProvider(props) { | ||
const { url, blocking, children, transform, onFetchingStart, onFetchingEnd, onFetchingError } = props; | ||
const [{ translations, isFetching }, setState] = React.useState(initialState); | ||
|
||
React.useEffect(() => { | ||
let isStillMounted = true; | ||
|
||
setState(state => ({ ...state, isFetching: true })); | ||
onFetchingStart(); | ||
|
||
fetch(url) | ||
.then(r => r.json()) | ||
.then(response => { | ||
if (this.mounted) { | ||
this.setState( | ||
{ | ||
translations: transform(response), | ||
fetching: false, | ||
}, | ||
onFetchingEnd, | ||
); | ||
if (isStillMounted) { | ||
setState({ | ||
translations: transform(response), | ||
isFetching: false, | ||
}); | ||
onFetchingEnd(); | ||
} | ||
}) | ||
.catch(onFetchingError); | ||
}; | ||
|
||
render() { | ||
const { blocking, children } = this.props; | ||
const { translations, fetching } = this.state; | ||
const shouldRenderSubtree = !blocking || (blocking && !fetching); | ||
return <Provider value={translations}>{shouldRenderSubtree ? children : null}</Provider>; | ||
} | ||
|
||
return () => { | ||
isStillMounted = false; | ||
}; | ||
}, [url]); // re-fetch only when url changes | ||
|
||
const shouldRenderSubtree = !blocking || (blocking && !isFetching); | ||
return <Provider value={translations}>{shouldRenderSubtree ? children : null}</Provider>; | ||
} | ||
|
||
FetchingProvider.propTypes = { | ||
/** | ||
* The URL which serves the text messages. | ||
* Required. | ||
*/ | ||
url: PropTypes.string.isRequired, | ||
|
||
/** | ||
* Makes the rendering of the sub-tree synchronous. | ||
* The components will not render until fetching of the text messages finish. | ||
* | ||
* Defaults to true. | ||
*/ | ||
blocking: PropTypes.bool, | ||
|
||
/** | ||
* A function which can transform the response received from GET /props.url | ||
* to a suitable format: | ||
* | ||
* Example: | ||
* function transform(response) { | ||
* return response.textMessages; | ||
* } | ||
*/ | ||
transform: PropTypes.func, | ||
|
||
/** | ||
* Invoked when fetching of text messages starts. | ||
*/ | ||
onFetchingStart: PropTypes.func, | ||
|
||
/** | ||
* Invoked when fetching of text messages finishes. | ||
*/ | ||
onFetchingEnd: PropTypes.func, | ||
|
||
/** | ||
* Invoked when fetching fails. | ||
*/ | ||
onFetchingError: PropTypes.func, | ||
|
||
/** | ||
* Children. | ||
*/ | ||
children: PropTypes.node, | ||
}; | ||
|
||
FetchingProvider.defaultProps = { | ||
blocking: true, | ||
transform: identity, | ||
onFetchingStart: identity, | ||
onFetchingEnd: identity, | ||
onFetchingError: identity, | ||
}; |
Oops, something went wrong.