From 83996d63e10076f04d97fca765c453e64ed61e0b Mon Sep 17 00:00:00 2001 From: David Date: Tue, 29 Mar 2022 02:28:09 +0200 Subject: [PATCH] #32 star added --- src/components/TranslationHistory.js | 61 +++++++++++++------ src/components/TranslationHistory.module.scss | 10 +++ src/components/form.js | 16 +++-- src/history.js | 40 +++++++++--- 4 files changed, 94 insertions(+), 33 deletions(-) create mode 100644 src/components/TranslationHistory.module.scss diff --git a/src/components/TranslationHistory.js b/src/components/TranslationHistory.js index 763af78..154bd3f 100644 --- a/src/components/TranslationHistory.js +++ b/src/components/TranslationHistory.js @@ -2,13 +2,25 @@ import React, { useState } from "react"; import { Drawer, IconButton, + Button, List, ListItemButton, ListItemText, ListSubheader, Tooltip, } from "@mui/material"; -import { History as HistoryIcon } from "@mui/icons-material"; +import { + History as HistoryIcon, + Star as StarIcon, + StarBorder as StarBorderIcon, +} from "@mui/icons-material"; + +import { + changeStarInHistory, + removeItemFromHistory, +} from "../history"; + +import styles from "./TranslationHistory.module.scss" export function TranslationHistory({ getHistory, onSelect }) { @@ -25,25 +37,23 @@ export function TranslationHistory({ getHistory, onSelect }) { setHistory(getHistory()); } - function selectItem(text) { - onSelect(text); + function selectItem(item) { + onSelect(item.text, item.fromLanguageId, item.toLanguageId); setHistoryOpen(false); } return (
-
- - - - - -
+ + + + + + History } > - {history.map((text, i) => ( - selectItem(text)}> - - + {history.sort((a,b) => a.star ? -1 : b.star ? 1 : 0).map((value, index) => ( +
+ { value.star ? + + : + + } + selectItem(value)}> + ${value.toLanguageId} : ${value.text}`} /> + +
))}
diff --git a/src/components/TranslationHistory.module.scss b/src/components/TranslationHistory.module.scss new file mode 100644 index 0000000..17e75f6 --- /dev/null +++ b/src/components/TranslationHistory.module.scss @@ -0,0 +1,10 @@ +.header{ + font-weight: bold; +} + +.historyItem{ + display: grid; + grid-template-columns: auto 1fr auto; + align-items: center; + justify-items: start; +} \ No newline at end of file diff --git a/src/components/form.js b/src/components/form.js index 705e86e..ae408be 100644 --- a/src/components/form.js +++ b/src/components/form.js @@ -15,7 +15,10 @@ import { SwapVert, } from "@mui/icons-material"; -import { getHistory, saveHistory } from "../history"; +import { + getHistory, + saveHistory, +} from "../history"; import { translate } from "../api"; import { TranslationHistory } from "./TranslationHistory"; import { transliterateCyrilToLatin, transliterateLatinToCyril } from "../transliterate"; @@ -64,7 +67,13 @@ const Form = () => { setSource(text); setLoading(true); - debouncedSave(languages.source, text); + if(fromLanguage === languageCs.id) + setLanguages((state) => ({ source: languageCs, target: languageUk })); + else + setLanguages((state) => ({ source: languageUk, target: languageCs })); + + + debouncedSave(fromLanguage, toLanguage, text); debouncedTranslate({ text, fromLanguage, @@ -96,7 +105,6 @@ const Form = () => { const oldSource = languages.source; const oldTarget = languages.target; setTranslation(""); - setLanguages((state) => ({ source: state.target, target: state.source })); /**/// switch - keep source text as source handleChangeSource(source, oldTarget.id, oldSource.id); @@ -168,7 +176,7 @@ const Form = () => { } getHistory(languages.source)} + getHistory={() => getHistory()} onSelect={handleChangeSource} />
diff --git a/src/history.js b/src/history.js index 29bb304..ca8bfeb 100644 --- a/src/history.js +++ b/src/history.js @@ -1,10 +1,10 @@ -const maxHistory = 100; +const MAX_HISTORY_COUNT = 100; -export function getHistory(language) { +export function getHistory() { if (typeof window === "undefined") return []; - - const historyJson = localStorage.getItem(`history-${language}`) || ""; + + const historyJson = localStorage.getItem("translationHistory"); if (historyJson == null) return []; @@ -16,13 +16,35 @@ export function getHistory(language) { } } -export function saveHistory(language, text) { +export function saveHistory(fromLanguageId, toLanguageId, text) { if (text === "") return; - const originalHistory = getHistory(language); - const history = [text, ...originalHistory.filter((item) => item !== text)]; - const limitedHistory = history.slice(0, maxHistory); + const historyItem = { + fromLanguageId, + toLanguageId, + text, + star: false, + } + + let originalHistory = getHistory(); + let foundDuplicate = originalHistory.findIndex((item) => item.text === text) !== -1; + + if(foundDuplicate){ + originalHistory.sort((a,b) => a.text === text ? -1 : b.text === text ? 1 : 0); + } else { + originalHistory.unshift(historyItem); + } + + localStorage.setItem("translationHistory", JSON.stringify(originalHistory.slice(0, MAX_HISTORY_COUNT))); +} + +export function removeItemFromHistory(index){ + localStorage.removeItem(index); +} - localStorage.setItem(`history-${language}`, JSON.stringify(limitedHistory)); +export function changeStarInHistory(value, star){ + let originalHistory = getHistory(); + originalHistory.find((item) => item.text === value.text && item.fromLanguageId === value.fromLanguageId && item.toLanguageId === value.toLanguageId).star = star; + localStorage.setItem("translationHistory", JSON.stringify(originalHistory)); }