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",
"youAreUsing": "You are using",
"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) => {
switch (viewState) {
default:
case ViewStateEnum.card:
case ViewStateEnum.CARD:
return <HistoryCardList entries={entries} onPinClick={onPinClick} pageIndex={pageIndex}
onLastPageIndexChange={setLastPageIndex}/>
case ViewStateEnum.table:
case ViewStateEnum.TABLE:
return <HistoryTable entries={entries} onPinClick={onPinClick} pageIndex={pageIndex}
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 { Typeahead } from 'react-bootstrap-typeahead'
import './typeahead-hacks.scss'
import { ClearHistoryButton } from './clear-history-button'
export type HistoryToolbarChange = (settings: HistoryToolbarState) => void;
@ -17,24 +18,25 @@ export interface HistoryToolbarState {
}
export enum ViewStateEnum {
card,
table
CARD,
TABLE
}
export interface HistoryToolbarProps {
onSettingsChange: HistoryToolbarChange
tags: string[]
onSettingsChange: HistoryToolbarChange
tags: string[]
onClearHistory: () => void
}
export const initState: HistoryToolbarState = {
viewState: ViewStateEnum.card,
viewState: ViewStateEnum.CARD,
titleSortDirection: SortModeEnum.no,
lastVisitedSortDirection: SortModeEnum.no,
keywordSearch: '',
selectedTags: []
}
export const HistoryToolbar: React.FC<HistoryToolbarProps> = ({ onSettingsChange, tags }) => {
export const HistoryToolbar: React.FC<HistoryToolbarProps> = ({ onSettingsChange, tags, onClearHistory }) => {
const [t] = useTranslation()
const [state, setState] = useState<HistoryToolbarState>(initState)
@ -102,9 +104,7 @@ export const HistoryToolbar: React.FC<HistoryToolbarProps> = ({ onSettingsChange
</Button>
</InputGroup>
<InputGroup className={'mr-1'}>
<Button variant={'light'} title={t('clearHistory')}>
<FontAwesomeIcon icon={'trash'}/>
</Button>
<ClearHistoryButton onClearHistory={onClearHistory}/>
</InputGroup>
<InputGroup className={'mr-1'}>
<Button variant={'light'} title={t('refreshHistory')}>
@ -116,9 +116,9 @@ export const HistoryToolbar: React.FC<HistoryToolbarProps> = ({ onSettingsChange
onChange={(newViewState: ViewStateEnum) => {
toggleViewChanged(newViewState)
}}>
<ToggleButton className={'btn-light'} value={ViewStateEnum.card}><Trans
<ToggleButton className={'btn-light'} value={ViewStateEnum.CARD}><Trans
i18nKey={'cards'}/></ToggleButton>
<ToggleButton className={'btn-light'} value={ViewStateEnum.table}><Trans
<ToggleButton className={'btn-light'} value={ViewStateEnum.TABLE}><Trans
i18nKey={'table'}/></ToggleButton>
</ToggleButtonGroup>
</InputGroup>

View file

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

View file

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