Rev 631 | Rev 2157 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import React, { useEffect, useState } from "react";import { useDispatch } from "react-redux";import {EmailIcon,EmailShareButton,FacebookIcon,FacebookShareButton,RedditIcon,RedditShareButton,TelegramIcon,TelegramShareButton,TwitterIcon,TwitterShareButton,WhatsappIcon,WhatsappShareButton,} from "react-share";import { addNotification } from "store/notification/notification.actions";import { axios } from "../../../utils";import styled from "styled-components";const StyledShareContainer = styled.div`display: flex;position: absolute;align-items: center;padding: 5px 1rem;bottom: calc(100% + 0.5rem);gap: 0.5rem;right: 0;width: 16.5rem;border-radius: 10px;background-color: #fff;box-shadow: 0px 4px 4px -2px rgb(0 0 0 / 12%),0px -4px 4px -2px rgb(0 0 0 / 12%);`;export default function withExternalShare(Component, url) {return function (props) {const [shareOptions, setShareOptions] = useState(false);const [shareUrl, setShareUrl] = useState("");const dispatch = useDispatch();const toggleShareOptions = () => {setShareOptions(!shareOptions);};const getShareUrl = async () => {await axios.get(url).then(({ data }) => {if (!data.success) {dispatch(addNotification({ style: "danger", msg: data.data }));setShareOptions(false);return;}setShareUrl(data.data);}).catch((err) => console.log(err));};const incrementCount = async () => {await axios.post(props.shareUrl).then(({ data }) => {if (!data.success) {dispatch(addNotification({ style: "danger", msg: data.data }));return;}setShareOptions(false);props.setValue(data.data);}).catch((err) => console.log(err));};useEffect(() => {if (shareOptions && !shareUrl) getShareUrl();}, [shareOptions]);return (<Component onClick={toggleShareOptions} {...props}>{shareOptions && (<StyledShareContainer><FacebookShareButtonurl={shareUrl}onShareWindowClose={() => incrementCount()}><FacebookIcon size={32} round /></FacebookShareButton><TwitterShareButtonurl={shareUrl}onShareWindowClose={() => incrementCount()}><TwitterIcon size={32} round /></TwitterShareButton><TelegramShareButtonurl={shareUrl}onShareWindowClose={() => incrementCount()}><TelegramIcon size={32} round /></TelegramShareButton><WhatsappShareButtonurl={shareUrl}onShareWindowClose={() => incrementCount()}><WhatsappIcon size={32} round /></WhatsappShareButton><RedditShareButtonurl={shareUrl}onShareWindowClose={() => incrementCount()}><RedditIcon size={32} round /></RedditShareButton><EmailShareButtonurl={shareUrl}onShareWindowClose={() => incrementCount()}><EmailIcon size={32} round /></EmailShareButton></StyledShareContainer>)}</Component>);};}