Skip to content

Commit

Permalink
feat: add fetchData parameter (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed May 13, 2020
1 parent 5057468 commit 8dcdea4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
language: node_js
node_js:
- "node"
- 'lts/*'
6 changes: 3 additions & 3 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"previsualization"
],
"dependencies": {
"@microlink/mql": "~0.6.12",
"is-localhost-url": "~1.0.0",
"@microlink/mql": "~0.6.13",
"is-localhost-url": "~1.0.3",
"nanoclamp": "~1.3.3"
},
"devDependencies": {
Expand All @@ -45,7 +45,7 @@
"enzyme-adapter-react-16": "latest",
"eslint-plugin-jsx-a11y": "latest",
"husky": "latest",
"jest": "latest",
"jest": "25",
"jest-environment-enzyme": "latest",
"jest-enzyme": "latest",
"jest-styled-components": "latest",
Expand Down
18 changes: 11 additions & 7 deletions packages/react/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import { useIntersectionObserver } from './utils/hooks'

const Card = props => {
const {
className = '',
className,
fetchData,
lazy,
loading,
media: mediaProp,
Expand Down Expand Up @@ -63,12 +64,13 @@ const Card = props => {
[isLazyEnabled, hasIntersected]
)

const fetchData = useCallback(() => {
const toFetchData = useCallback(() => {
if (canFetchData) {
setLoading(true)
const fetch = isFunction(setData)
? Promise.resolve({})
: fetchFromApi(apiUrl, apiUrlProps)

const fetch = fetchData
? fetchFromApi(apiUrl, apiUrlProps)
: Promise.resolve({})

fetch
.then(({ data }) => mergeData(data))
Expand Down Expand Up @@ -97,7 +99,7 @@ microlink.io/${error.code.toLowerCase()}
const mergeData = useCallback(
fetchedData => {
const payload = isFunction(setData)
? setData()
? setData(fetchedData)
: { ...fetchedData, ...setData }

const {
Expand Down Expand Up @@ -158,7 +160,7 @@ microlink.io/${error.code.toLowerCase()}
[mediaProps, setData]
)

useEffect(fetchData, [url, setData, hasIntersected])
useEffect(toFetchData, [url, setData, hasIntersected])

const isLoading = isLoadingUndefined ? loadingState : loading

Expand Down Expand Up @@ -216,13 +218,15 @@ const Microlink = props => (
)

Microlink.defaultProps = {
className: '',
apiKey: undefined,
autoPlay: true,
controls: true,
direction: 'ltr',
lazy: true,
loop: true,
media: ['image', 'logo'],
fetchData: true,
muted: true,
playsInline: true,
size: 'normal'
Expand Down

0 comments on commit 8dcdea4

Please sign in to comment.