From b9dc6231702aeaf273db67e49ed38d86fc1b2b58 Mon Sep 17 00:00:00 2001 From: lamacchinadesiderante Date: Wed, 11 Jan 2023 00:35:50 +0100 Subject: [PATCH] add copy to clipboard / read from querystring functionalities --- .../src/components/CopyToClipboardButton.tsx | 58 ++++++ frontend/src/components/Search.tsx | 171 ++++++++++++------ frontend/src/i18n.json | 8 +- 3 files changed, 175 insertions(+), 62 deletions(-) create mode 100644 frontend/src/components/CopyToClipboardButton.tsx diff --git a/frontend/src/components/CopyToClipboardButton.tsx b/frontend/src/components/CopyToClipboardButton.tsx new file mode 100644 index 0000000..294058a --- /dev/null +++ b/frontend/src/components/CopyToClipboardButton.tsx @@ -0,0 +1,58 @@ +import { Box, Button, Card, Flex, Icon, Spacer } from '@chakra-ui/react'; +import React, { useState } from 'react'; +import { useTranslation } from 'react-i18next'; +import { IoShareSocialOutline } from 'react-icons/io5'; +import MediaQuery from 'react-responsive'; +import { MEDIA_QUERY_DESKTOP_STARTS, MEDIA_QUERY_MOBILE_ENDS } from '../constants/mediaquery'; + +export interface IProps { + show: boolean + onClick(): void +} + +const CopyToClipboardButton: React.FC = (props) => { + + const { show, onClick } = props + + const [showSuccess, setShowSuccess] = useState(false) + + const { t } = useTranslation(); + + const handleClick = () => { + onClick() + setShowSuccess(true) + + setTimeout(() => { + setShowSuccess(false) + }, 3000) + } + + return ( + <> + + {show && + + + + + + + } + + + {show && + + + + + } + + + ); +}; + +export default CopyToClipboardButton; diff --git a/frontend/src/components/Search.tsx b/frontend/src/components/Search.tsx index 0e6f627..498d324 100644 --- a/frontend/src/components/Search.tsx +++ b/frontend/src/components/Search.tsx @@ -1,5 +1,6 @@ -import { GridItem, Icon, SimpleGrid } from '@chakra-ui/react'; -import React, { useState } from 'react'; +import { Button, GridItem, Icon, SimpleGrid, Flex, Box, Card } from '@chakra-ui/react'; +import React, { useEffect, useState } from 'react'; +import MediaQuery from 'react-responsive' import { TbBook2, TbBuilding, @@ -11,10 +12,13 @@ import { import search, { Book } from '../scripts/searcher'; import { IoLanguage } from 'react-icons/io5'; +import { IoShareSocialOutline } from 'react-icons/io5'; import SearchInput from './SearchInput'; import { useDebounceEffect } from 'ahooks'; import { useTranslation } from 'react-i18next'; import SearchLanguage from './SearchLanguage'; +import { MEDIA_QUERY_DESKTOP_STARTS, MEDIA_QUERY_MOBILE_ENDS } from '../constants/mediaquery'; +import CopyToClipboardButton from './CopyToClipboardButton'; function constructQuery(parts: Record): string { return Object.keys(parts) @@ -44,6 +48,27 @@ const Search: React.FC = ({ setBooks }) => { const [complexQuery, setComplexQuery] = useState(''); const [showLanguageDropdown, setShowLanguageDropdown] = useState(true) + useEffect(() => { + const params = new Proxy(new URLSearchParams(window.location.search), { + //@ts-ignore + get: (searchParams, prop) => searchParams.get(prop), + }); + + //@ts-ignore + params.title && setTitle(String(params.title)) + + //@ts-ignore + params.author && setAuthor(String(params.author)) + + //@ts-ignore + if (params.language) { + //@ts-ignore + setLanguage(String(params.language)) + setShowLanguageDropdown(false) + } + + }, []) + const handleLanguageChange = (language: string) => { if (language == 'input') { setShowLanguageDropdown(false) @@ -57,6 +82,27 @@ const Search: React.FC = ({ setBooks }) => { setLanguage('') } + const hasAnySearchBeenMade = () => { + return ( + title !== '' + || author !== '' + || language !== '' + || publisher !== '' + || extension !== '' + || isbn !== '' + ) + } + + const copyToClipboard = () => { + const searchQuery = `${window.location.host}?title=${title}&author=${author}&language=${language}&publisher=${publisher}&isbn=${isbn}&extension=${extension}`; + + navigator.clipboard.writeText(searchQuery).then(() => { + //console.log('Async: Copying to clipboard was successful!'); + }, (err) => { + //console.error('Async: Could not copy text: ', err); + }); + } + useDebounceEffect( () => { const query = complexQuery @@ -72,66 +118,71 @@ const Search: React.FC = ({ setBooks }) => { ); return ( - - } - placeholder={t('book.title')} - value={title} - onChange={setTitle} - /> - } - placeholder={t('book.author')} - value={author} - onChange={setAuthor} - /> - } - placeholder={t('book.publisher')} - value={publisher} - onChange={setPublisher} - /> - } - placeholder={t('book.extension')} - value={extension} - onChange={setExtension} - /> - - {!showLanguageDropdown && (} - placeholder={t('input.select_language')} - value={language} - onChange={handleLanguageChange} - onClear={handleLanguageReset} - />)} - - {showLanguageDropdown && (} - placeholder={t('input.select_language')} - value={language} - onChange={handleLanguageChange} - />)} - - } - placeholder={t('book.isbn')} - value={isbn} - onChange={setISBN} - /> - + <> + } - placeholder={t('search.complex')} - value={complexQuery} - onChange={setComplexQuery} + icon={} + placeholder={t('book.title')} + value={title} + onChange={setTitle} /> - - + } + placeholder={t('book.author')} + value={author} + onChange={setAuthor} + /> + } + placeholder={t('book.publisher')} + value={publisher} + onChange={setPublisher} + /> + } + placeholder={t('book.extension')} + value={extension} + onChange={setExtension} + /> + + {!showLanguageDropdown && (} + placeholder={t('input.select_language')} + value={language} + onChange={handleLanguageChange} + onClear={handleLanguageReset} + />)} + + {showLanguageDropdown && (} + placeholder={t('input.select_language')} + value={language} + onChange={handleLanguageChange} + />)} + + } + placeholder={t('book.isbn')} + value={isbn} + onChange={setISBN} + /> + + } + placeholder={t('search.complex')} + value={complexQuery} + onChange={setComplexQuery} + /> + + + + ); }; diff --git a/frontend/src/i18n.json b/frontend/src/i18n.json index 9ea1ccd..e76b92a 100644 --- a/frontend/src/i18n.json +++ b/frontend/src/i18n.json @@ -39,7 +39,9 @@ "collapse": "Collapse" }, "search": { - "complex": "Complex search" + "complex": "Complex search", + "copy_search_link": "Copy to clipboard", + "copy_success": "Copied!" }, "settings": { "title": "Settings", @@ -205,7 +207,9 @@ "collapse": "Richiudi" }, "search": { - "complex": "Ricerca dettagliata" + "complex": "Ricerca dettagliata", + "copy_search_link": "Copia link ricerca", + "copy_success": "Copiato negli appunti!" }, "settings": { "title": "Impostazioni",