Skip to content

Commit

Permalink
v1.0.3; Fix fetch image multi times
Browse files Browse the repository at this point in the history
  • Loading branch information
x3388638 committed Jan 11, 2020
1 parent 213e48e commit b39fc78
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
27 changes: 22 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useCallback } from 'react'
import React, { useState, useCallback, useEffect } from 'react'
import ReactDOM from 'react-dom'
import BrowerBtn from './components/BrowseBtn'
import Gallery from './components/Gallery'
Expand Down Expand Up @@ -33,28 +33,45 @@ const renderReactApp = canonicalUrl => {
const App = () => {
const [isOpen, setIsOpen] = useState(false)
const [isFetching, setIsFetching] = useState(false)
const [isFetchError, setIsFetchError] = useState(false)
const { fetchImagesByPostID, images } = useFetchImage()

const handleOpen = useCallback(() => {
useEffect(() => {
if (isOpen && !images.length && !isFetching) {
fetchImages()
}
}, [isOpen, images, fetchImages])

useEffect(() => {
if (isOpen && isFetchError) {
handleClose()
}
}, [isOpen, isFetchError, handleClose])

const fetchImages = useCallback(() => {
const postID = document
.querySelector('link[rel=canonical]')
.href.match(/(\d*$)/)[0]

setIsFetching(true)
setIsFetchError(false)
fetchImagesByPostID(postID)
.then(() => {
setIsFetching(false)
})
.catch(() => {
setIsFetching(false)
setIsOpen(false)
looseBody()
setIsFetchError(true)
})

setIsOpen(true)
fixBody()
}, [fetchImagesByPostID])

const handleOpen = useCallback(() => {
setIsOpen(true)
fixBody()
}, [])

const handleClose = useCallback(() => {
looseBody()
setIsOpen(false)
Expand Down
Loading

0 comments on commit b39fc78

Please sign in to comment.