From 1adafef4e147c01acc8a830b94ed931bcf8bef29 Mon Sep 17 00:00:00 2001 From: lukas Date: Tue, 1 Mar 2022 13:20:39 +0100 Subject: [PATCH] remember sortby selection with localstorage --- src/pages/HomePage/HomePage.tsx | 63 +++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/src/pages/HomePage/HomePage.tsx b/src/pages/HomePage/HomePage.tsx index bb568f4..68e43de 100644 --- a/src/pages/HomePage/HomePage.tsx +++ b/src/pages/HomePage/HomePage.tsx @@ -29,7 +29,7 @@ interface state { subtitle: string; data: VideoTypes.VideoUnloadedType[]; selectionnr: number; - sortby: string; + sortby: SortBy; } /** @@ -39,9 +39,33 @@ export class HomePage extends React.Component { /** keyword variable needed temporary store search keyword */ keyword = ''; + /** + * get text label from sort type + * @param type SortBy type + */ + getLabelFromSortType(type: SortBy): String { + switch (type) { + case SortBy.date: + return 'Date Added'; + case SortBy.length: + return 'Length'; + case SortBy.likes: + return 'Most likes'; + case SortBy.name: + return 'Name'; + case SortBy.random: + return 'Random'; + default: + return ''; + } + } + constructor(props: Props) { super(props); + // get previously stored location from localstorage + const storedSelection = global.localStorage.getItem('sortby'); + this.state = { sideinfo: { VideoNr: 0, @@ -54,11 +78,10 @@ export class HomePage extends React.Component { subtitle: 'All Videos', data: [], selectionnr: 0, - sortby: 'Date Added' + sortby: storedSelection == null ? SortBy.date : parseInt(storedSelection, 10) }; } - sortState = SortBy.date; tagState = DefaultTags.all; componentDidMount(): void { @@ -87,7 +110,7 @@ export class HomePage extends React.Component { fetchVideoData(): void { callAPI( APINode.Video, - {action: 'getMovies', Tag: this.tagState.TagId, Sort: this.sortState}, + {action: 'getMovies', Tag: this.tagState.TagId, Sort: this.state.sortby}, (result: {Videos: VideoTypes.VideoUnloadedType[]; TagName: string}) => { this.setState({ data: result.Videos, @@ -190,15 +213,25 @@ export class HomePage extends React.Component { Sort By:
- {this.state.sortby} + {this.getLabelFromSortType(this.state.sortby)}
- this.onDropDownItemClick(SortBy.date, 'Date Added')}>Date Added - this.onDropDownItemClick(SortBy.likes, 'Most likes')}>Most likes - this.onDropDownItemClick(SortBy.random, 'Random')}>Random - this.onDropDownItemClick(SortBy.name, 'Name')}>Name - this.onDropDownItemClick(SortBy.length, 'Length')}>Length + this.onDropDownItemClick(SortBy.date)}> + {this.getLabelFromSortType(SortBy.date)} + + this.onDropDownItemClick(SortBy.likes)}> + {this.getLabelFromSortType(SortBy.likes)} + + this.onDropDownItemClick(SortBy.random)}> + {this.getLabelFromSortType(SortBy.random)} + + this.onDropDownItemClick(SortBy.name)}> + {this.getLabelFromSortType(SortBy.name)} + + this.onDropDownItemClick(SortBy.length)}> + {this.getLabelFromSortType(SortBy.length)} +
@@ -214,10 +247,12 @@ export class HomePage extends React.Component { * @param type type of sort action * @param name new header title */ - onDropDownItemClick(type: SortBy, name: string): void { - this.sortState = type; - this.setState({sortby: name}); - this.fetchVideoData(); + onDropDownItemClick(type: SortBy): void { + this.setState({sortby: type}, (): void => { + global.localStorage.setItem('sortby', type.toString()); + + this.fetchVideoData(); + }); } }