diff --git a/client/src/components/sorter.tsx b/client/src/components/sorter.tsx index c12e27d..ba4d0b8 100644 --- a/client/src/components/sorter.tsx +++ b/client/src/components/sorter.tsx @@ -45,12 +45,48 @@ export function sortByDate(x: {metadata: {creationTimestamp: string}}) { return -Date.parse(x.metadata.creationTimestamp); } +function getFieldName (field: TODO) { + if(typeof field === 'function') { + return field.name + } + return field +} + export default class Sorter extends Base { + + onClickHandler() { + const {sort} = this.props; + + if(sort) { + this.sort() + const url = new URL(window.location.href); + const fieldName = getFieldName(sort.field) + + url.searchParams.set('sortKey', fieldName) + url.searchParams.set('sortDir', sort.direction) + + window.history.pushState({}, '', url.search + url.hash); + } + } + + componentDidMount() { + const {sort} = this.props; + + const urlParams = new URLSearchParams(window.location.search); + + const sortKey = urlParams.get('sortKey') + const sortDir = urlParams.get('sortDir') + + if(sort && getFieldName(sort.field) === sortKey) { + sort.direction = sortDir as Direction + sort.onSort(sort) + } + } + render() { const {children, sort} = this.props; - return ( -
sort && this.sort()}> +
this.onClickHandler()}> {children} {sort && (