diff --git a/src/components/PhotoSwipe/PhotoSwipe.tsx b/src/components/PhotoSwipe/PhotoSwipe.tsx index e77a01663..d3769ad04 100644 --- a/src/components/PhotoSwipe/PhotoSwipe.tsx +++ b/src/components/PhotoSwipe/PhotoSwipe.tsx @@ -1,5 +1,4 @@ -import React, { useState } from 'react'; -import PropTypes from 'prop-types'; +import React, { useEffect, useState } from 'react'; import Photoswipe from 'photoswipe'; import PhotoswipeUIDefault from 'photoswipe/dist/photoswipe-ui-default'; import classnames from 'classnames'; @@ -16,191 +15,181 @@ interface Iprops { gettingData?: (instance: any, index: number, item: file) => void; id?: string; className?: string; - favItemIds: Set - setFavItemIds: React.Dispatch>> + favItemIds: Set; + setFavItemIds: (favItemIds: Set) => void; + refetchData: () => void; }; -interface Istates { - isOpen: boolean; - favItemIds: Set -} -class PhotoSwipe extends React.Component { - private pswpElement: any = null; - private photoSwipe: any = null; - state = { - isOpen: this.props.isOpen, - favItemIds: this.props.favItemIds, - }; - componentDidMount = () => { - const { isOpen } = this.state; +function PhotoSwipe(props: Iprops) { + + let pswpElement; + const [photoSwipe, setPhotoSwipe] = useState>(); + + const { isOpen } = props; + const [isFav, setIsFav] = useState(false) + + + useEffect(() => { + if (!pswpElement) + return; + if (isOpen) + openPhotoSwipe(); + + }, [pswpElement]); + + useEffect(() => { + if (!pswpElement) + return; if (isOpen) { - this.openPhotoSwipe(this.props); + openPhotoSwipe(); } - }; - - componentWillReceiveProps = (nextProps) => { - const { isOpen } = this.state; - if (nextProps.isOpen) { - if (!isOpen) { - this.openPhotoSwipe(nextProps); - } else { - this.updateItems(nextProps.items); - } - } else if (isOpen) { - this.closePhotoSwipe(); + if (!isOpen) { + closePhotoSwipe(); } - }; + return () => { + closePhotoSwipe(); + } + }, [isOpen]); - componentWillUnmount = () => { - this.closePhotoSwipe(); - }; + function updateFavButton() { + console.log(this.currItem.id, props.favItemIds) + setIsFav(isInFav(this?.currItem)); + } - openPhotoSwipe = (props) => { + + const openPhotoSwipe = () => { const { items, options } = props; - const pswpElement = this.pswpElement; - this.photoSwipe = new Photoswipe(pswpElement, PhotoswipeUIDefault, items, options); + let photoSwipe = new Photoswipe(pswpElement, PhotoswipeUIDefault, items, options); events.forEach((event) => { const callback = props[event]; if (callback || event === 'destroy') { - const self = this; - this.photoSwipe.listen(event, function (...args) { + photoSwipe.listen(event, function (...args) { if (callback) { args.unshift(this); callback(...args); } if (event === 'destroy') { - self.handleClose(); + handleClose(); } }); } }); - this.setState({ - isOpen: true, - }, () => { - this.photoSwipe.init(); - }); + photoSwipe.listen('beforeChange', updateFavButton); + photoSwipe.init(); + setPhotoSwipe(photoSwipe); + }; - updateItems = (items = []) => { - this.photoSwipe.items.length = 0; + const updateItems = (items = []) => { + photoSwipe.items = []; items.forEach((item) => { - this.photoSwipe.items.push(item); + photoSwipe.items.push(item); }); - this.photoSwipe.invalidateCurrItems(); - this.photoSwipe.updateSize(true); + photoSwipe.invalidateCurrItems(); + photoSwipe.updateSize(true); }; - closePhotoSwipe = () => { - if (!this.photoSwipe) { - return; + const closePhotoSwipe = () => { + if (photoSwipe) + photoSwipe.close(); + }; + + const handleClose = () => { + const { onClose } = props; + if (onClose) { + onClose(); } - this.photoSwipe.close(); }; - - handleClose = () => { - const { onClose } = this.props; - this.setState({ - isOpen: false - }, () => { - if (onClose) { - onClose(); - } - }); - }; - isInFav = (file) => { - const { favItemIds } = this.state; + const isInFav = (file) => { + const { favItemIds } = props; if (favItemIds && file) { return favItemIds.has(file.id); } + else + return false; } - onFavClick = (file) => { - const { setFavItemIds } = this.props; - const { favItemIds } = this.state; - if (!favItemIds || !file) - return; - if (!this.isInFav(file)) { + const onFavClick = (file) => { + const { favItemIds, refetchData, setFavItemIds } = props; + if (!isInFav(file)) { favItemIds.add(file.id); - setFavItemIds(favItemIds); console.log("added to Favorites"); - this.setState(() => ({ favItemIds })); - + setIsFav(true); + setFavItemIds(favItemIds); } else { favItemIds.delete(file.id); - setFavItemIds(favItemIds); - this.setState(() => ({ favItemIds })); console.log("removed from Favorites"); + setIsFav(false); + setFavItemIds(favItemIds); + } } + const { id } = props; + let { className } = props; + className = classnames(['pswp', className]).trim(); + return ( +