From 04f9faf35f4ffea5fdb3c9b71a9af3d964495c4e Mon Sep 17 00:00:00 2001 From: Stukova Olya Date: Wed, 15 Dec 2021 14:37:59 +0500 Subject: [PATCH] Add possibility to create shortened share links #3 --- package.json | 1 + src/components/ui/Share/index.js | 46 +++++++++++++++++++++++-------- src/components/ui/Share/styles.js | 14 ++++++++++ 3 files changed, 49 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 47e23da..424380d 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "react-helmet": "^6.1.0", "react-html-parser": "^2.0.2", "react-router-dom": "^5.0.1", + "tinyurl": "^1.1.7", "use-debounce": "0.0.7" }, "devDependencies": { diff --git a/src/components/ui/Share/index.js b/src/components/ui/Share/index.js index b9a804d..38c0e75 100644 --- a/src/components/ui/Share/index.js +++ b/src/components/ui/Share/index.js @@ -1,15 +1,16 @@ /* eslint-disable no-undef */ /* eslint-disable jsx-a11y/anchor-is-valid */ /* eslint-disable jsx-a11y/anchor-has-content */ -import React, { useContext, useRef, useState } from 'react'; +import React, { useContext, useRef, useState, useEffect } from 'react'; import { observer } from 'mobx-react-lite'; import { - Button, Dialog, DialogActions, DialogContent, DialogTitle, IconButton, TextField, Tooltip, Typography + Button, Dialog, DialogActions, DialogContent, DialogTitle, IconButton, FormControlLabel, Switch, TextField, Tooltip, Typography } from '@material-ui/core'; import CloseIcon from '@material-ui/icons/Close'; import ShareIcon from '@material-ui/icons/Share'; import QRCode from 'qrcode.react'; import _cloneDeep from 'lodash/cloneDeep'; +import TinyURL from 'tinyurl'; import { ConfigStoreContext, QueryStringStoreContext, UiStoreContext } from 'store/stores'; import { parameterKeys } from 'utils/variables'; @@ -21,8 +22,16 @@ const Share = observer(() => { const queryStringStore = useContext(QueryStringStoreContext); const uiStore = useContext(UiStoreContext); const [isOpen, setIsOpen] = useState(false); + const [isShortLink, setIsShortLink] = useState(false); + const [shareUrl, setShareUrl] = useState(''); + const [embedCode, setEmbedCode] = useState(''); const qrImageEl = useRef(null); + useEffect(async () => { + setShareUrl(isShortLink ? await getShortenedShareURL() : getShareURL()); + setEmbedCode(getEmbedCode(isShortLink ? await getShortenedShareURL(true) : getShareURL(true))); + }); + const showShareDialog = () => { setIsOpen(!isOpen); }; @@ -53,22 +62,21 @@ const Share = observer(() => { } }; - const getShareURL = () => { + const getShareURL = (isEmbedded = false) => { const { origin, pathname } = window.location; const queryObject = getQueryObject(); + if (isEmbedded) queryObject[parameterKeys.SIMPLE_UI] = true; const queryString = Object.keys(queryObject).reduce((acc, key, idx) => `${acc}${idx === 0 ? '?' : '&'}${key}=${queryObject[key]}`, ''); return `${origin}${pathname}${queryString}`; }; - const getEmbedCode = () => { - const { origin, pathname } = window.location; - const queryObject = getQueryObject(); - queryObject[parameterKeys.SIMPLE_UI] = true; - const queryString = Object.keys(queryObject).reduce((acc, key, idx) => `${acc}${idx === 0 ? '?' : '&'}${key}=${queryObject[key]}`, ''); - const embedUrl = `${origin}${pathname}${queryString}`; - return ``; + const getShortenedShareURL = async (isEmbedded = false) => { + const url = await TinyURL.shorten(getShareURL(isEmbedded)); + return url; }; + const getEmbedCode = (url) => ``; + const getQueryObject = () => { const result = _cloneDeep(queryStringStore.parameters); if (result[parameterKeys.JSON]) { @@ -141,6 +149,20 @@ const Share = observer(() => { Download +
+ setIsShortLink(event.target.checked)} + color="primary" + /> + )} + label="Short link" + labelPlacement="start" + /> +
{ label="Link" multiline maxRows={3} - defaultValue={getShareURL()} + value={shareUrl} InputProps={{ readOnly: true }} />