added clearHistory functionality (#82)

added clearHistory functionality

Signed-off-by: Philip Molares <philip.molares@udo.edu>
Co-authored-by: mrdrogdrog <mr.drogdrog@gmail.com>
This commit is contained in:
Philip Molares 2020-05-29 15:09:51 +02:00 committed by GitHub
parent 0e8d2f1639
commit 557386f78f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 70 additions and 17 deletions

View file

@ -135,5 +135,7 @@
"clientVersion": "Client version", "clientVersion": "Client version",
"youAreUsing": "You are using", "youAreUsing": "You are using",
"close": "Close", "close": "Close",
"issueTracker": "Found a bug? Fill an issue!" "issueTracker": "Found a bug? Fill an issue!",
"clearHistoryQuestion": "Do you want to clear the history?",
"clearHistoryDisclaimer": "This won't delete any notes."
} }

View file

@ -42,10 +42,10 @@ export const HistoryContent: React.FC<HistoryContentProps> = ({ viewState, entri
const mapViewStateToComponent = (viewState: ViewStateEnum) => { const mapViewStateToComponent = (viewState: ViewStateEnum) => {
switch (viewState) { switch (viewState) {
default: default:
case ViewStateEnum.card: case ViewStateEnum.CARD:
return <HistoryCardList entries={entries} onPinClick={onPinClick} pageIndex={pageIndex} return <HistoryCardList entries={entries} onPinClick={onPinClick} pageIndex={pageIndex}
onLastPageIndexChange={setLastPageIndex}/> onLastPageIndexChange={setLastPageIndex}/>
case ViewStateEnum.table: case ViewStateEnum.TABLE:
return <HistoryTable entries={entries} onPinClick={onPinClick} pageIndex={pageIndex} return <HistoryTable entries={entries} onPinClick={onPinClick} pageIndex={pageIndex}
onLastPageIndexChange={setLastPageIndex}/> onLastPageIndexChange={setLastPageIndex}/>
} }

View file

@ -0,0 +1,38 @@
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import React, { useState, Fragment } from 'react'
import { Button, Modal } from 'react-bootstrap'
import { Trans, useTranslation } from 'react-i18next'
export interface ClearHistoryButtonProps {
onClearHistory: () => void
}
export const ClearHistoryButton: React.FC<ClearHistoryButtonProps> = ({ onClearHistory }) => {
const { t } = useTranslation()
const [show, setShow] = useState(false)
const handleShow = () => setShow(true)
const handleClose = () => setShow(false)
return (
<Fragment>
<Button variant={'light'} title={t('clearHistory')} onClick={handleShow}>
<FontAwesomeIcon icon={'trash'}/>
</Button>
<Modal show={show} onHide={handleClose} animation={true} size="sm">
<Modal.Body className="text-dark">
<h5><Trans i18nKey={'clearHistoryQuestion'}/></h5>
<h6><Trans i18nKey={'clearHistoryDisclaimer'}/></h6>
</Modal.Body>
<Modal.Footer>
<Button variant="secondary" onClick={handleClose}>
<Trans i18nKey={'close'}/>
</Button>
<Button variant="danger" onClick={onClearHistory}>
<Trans i18nKey={'clearHistory'}/>
</Button>
</Modal.Footer>
</Modal>
</Fragment>
)
}

View file

@ -5,6 +5,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { SortButton, SortModeEnum } from '../../../../sort-button/sort-button' import { SortButton, SortModeEnum } from '../../../../sort-button/sort-button'
import { Typeahead } from 'react-bootstrap-typeahead' import { Typeahead } from 'react-bootstrap-typeahead'
import './typeahead-hacks.scss' import './typeahead-hacks.scss'
import { ClearHistoryButton } from './clear-history-button'
export type HistoryToolbarChange = (settings: HistoryToolbarState) => void; export type HistoryToolbarChange = (settings: HistoryToolbarState) => void;
@ -17,24 +18,25 @@ export interface HistoryToolbarState {
} }
export enum ViewStateEnum { export enum ViewStateEnum {
card, CARD,
table TABLE
} }
export interface HistoryToolbarProps { export interface HistoryToolbarProps {
onSettingsChange: HistoryToolbarChange onSettingsChange: HistoryToolbarChange
tags: string[] tags: string[]
onClearHistory: () => void
} }
export const initState: HistoryToolbarState = { export const initState: HistoryToolbarState = {
viewState: ViewStateEnum.card, viewState: ViewStateEnum.CARD,
titleSortDirection: SortModeEnum.no, titleSortDirection: SortModeEnum.no,
lastVisitedSortDirection: SortModeEnum.no, lastVisitedSortDirection: SortModeEnum.no,
keywordSearch: '', keywordSearch: '',
selectedTags: [] selectedTags: []
} }
export const HistoryToolbar: React.FC<HistoryToolbarProps> = ({ onSettingsChange, tags }) => { export const HistoryToolbar: React.FC<HistoryToolbarProps> = ({ onSettingsChange, tags, onClearHistory }) => {
const [t] = useTranslation() const [t] = useTranslation()
const [state, setState] = useState<HistoryToolbarState>(initState) const [state, setState] = useState<HistoryToolbarState>(initState)
@ -102,9 +104,7 @@ export const HistoryToolbar: React.FC<HistoryToolbarProps> = ({ onSettingsChange
</Button> </Button>
</InputGroup> </InputGroup>
<InputGroup className={'mr-1'}> <InputGroup className={'mr-1'}>
<Button variant={'light'} title={t('clearHistory')}> <ClearHistoryButton onClearHistory={onClearHistory}/>
<FontAwesomeIcon icon={'trash'}/>
</Button>
</InputGroup> </InputGroup>
<InputGroup className={'mr-1'}> <InputGroup className={'mr-1'}>
<Button variant={'light'} title={t('refreshHistory')}> <Button variant={'light'} title={t('refreshHistory')}>
@ -116,9 +116,9 @@ export const HistoryToolbar: React.FC<HistoryToolbarProps> = ({ onSettingsChange
onChange={(newViewState: ViewStateEnum) => { onChange={(newViewState: ViewStateEnum) => {
toggleViewChanged(newViewState) toggleViewChanged(newViewState)
}}> }}>
<ToggleButton className={'btn-light'} value={ViewStateEnum.card}><Trans <ToggleButton className={'btn-light'} value={ViewStateEnum.CARD}><Trans
i18nKey={'cards'}/></ToggleButton> i18nKey={'cards'}/></ToggleButton>
<ToggleButton className={'btn-light'} value={ViewStateEnum.table}><Trans <ToggleButton className={'btn-light'} value={ViewStateEnum.TABLE}><Trans
i18nKey={'table'}/></ToggleButton> i18nKey={'table'}/></ToggleButton>
</ToggleButtonGroup> </ToggleButtonGroup>
</InputGroup> </InputGroup>

View file

@ -1,7 +1,7 @@
import React, { Fragment, useEffect, useState } from 'react' import React, { Fragment, useEffect, useState } from 'react'
import { Row } from 'react-bootstrap' import { Row } from 'react-bootstrap'
import { Trans, useTranslation } from 'react-i18next' import { Trans, useTranslation } from 'react-i18next'
import { loadHistoryFromLocalStore, sortAndFilterEntries } from '../../../../utils/historyUtils' import { loadHistoryFromLocalStore, setHistoryToLocalStore, sortAndFilterEntries } from '../../../../utils/historyUtils'
import { HistoryContent } from './history-content/history-content' import { HistoryContent } from './history-content/history-content'
import { HistoryToolbar, HistoryToolbarState, initState as toolbarInitState } from './history-toolbar/history-toolbar' import { HistoryToolbar, HistoryToolbarState, initState as toolbarInitState } from './history-toolbar/history-toolbar'
@ -29,9 +29,14 @@ export const History: React.FC = () => {
if (historyEntries === []) { if (historyEntries === []) {
return return
} }
window.localStorage.setItem('history', JSON.stringify(historyEntries)) setHistoryToLocalStore(historyEntries)
}, [historyEntries]) }, [historyEntries])
const clearHistory = () => {
setHistoryToLocalStore([])
setHistoryEntries([])
}
const pinClick: pinClick = (entryId: string) => { const pinClick: pinClick = (entryId: string) => {
setHistoryEntries((entries) => { setHistoryEntries((entries) => {
return entries.map((entry) => { return entries.map((entry) => {
@ -57,7 +62,11 @@ export const History: React.FC = () => {
<Fragment> <Fragment>
<h1 className="mb-4"><Trans i18nKey="history"/></h1> <h1 className="mb-4"><Trans i18nKey="history"/></h1>
<Row className={'justify-content-center mb-3'}> <Row className={'justify-content-center mb-3'}>
<HistoryToolbar onSettingsChange={setViewState} tags={tags}/> <HistoryToolbar
onSettingsChange={setViewState}
tags={tags}
onClearHistory={clearHistory}
/>
</Row> </Row>
<HistoryContent viewState={viewState.viewState} <HistoryContent viewState={viewState.viewState}
entries={entriesToShow} entries={entriesToShow}

View file

@ -86,3 +86,7 @@ export function loadHistoryFromLocalStore (): HistoryEntry[] {
return JSON.parse(historyJsonString) as HistoryEntry[] return JSON.parse(historyJsonString) as HistoryEntry[]
} }
} }
export function setHistoryToLocalStore (entries: HistoryEntry[]): void {
window.localStorage.setItem('history', JSON.stringify(entries))
}