@@ -6,6 +6,7 @@ import uuid from 'react-uuid';
66import _ from 'underscore' ;
77import { Icon , Input , Message } from 'semantic-ui-react' ;
88import i18n from '../i18n/i18n' ;
9+ import ListSessionUtils from '../utils/ListSession' ;
910import Toaster from './Toaster' ;
1011
1112type Props = {
@@ -131,9 +132,6 @@ type State = {
131132 sortDirection : ?string
132133} ;
133134
134- const SESSION_KEY = 'DataList' ;
135- const SESSION_DEFAULT = '{}' ;
136-
137135const SORT_ASCENDING = 'ascending' ;
138136const SORT_DESCENDING = 'descending' ;
139137
@@ -285,19 +283,6 @@ const useDataList = (WrappedComponent: ComponentType<any>) => (
285283 return { filters } ;
286284 }
287285
288- /**
289- * Returns the session storage key for the current list.
290- *
291- * @returns {string|null }
292- */
293- getSessionKey ( ) {
294- if ( ! this . props . session ) {
295- return null ;
296- }
297-
298- return `${ SESSION_KEY } .${ this . props . session . key } ` ;
299- }
300-
301286 /**
302287 * Initializes the state based on the passed props.
303288 *
@@ -307,7 +292,8 @@ const useDataList = (WrappedComponent: ComponentType<any>) => (
307292 * saved: boolean, count: number, filters: (*|{}), page: number, error: null, loading: boolean, items: *[] }}
308293 */
309294 initializeState ( props : Props ) {
310- const session = this . restoreSession ( ) ;
295+ const { key, storage } = props . session || { } ;
296+ const session = ListSessionUtils . restoreSession ( key , storage ) || { } ;
311297
312298 const filters = session . filters || this . getDefaultFilters ( props ) ;
313299 const page = session . page || 1 ;
@@ -445,6 +431,14 @@ const useDataList = (WrappedComponent: ComponentType<any>) => (
445431 } ) ;
446432 }
447433
434+ /**
435+ * When no columns are sortable, load data as is
436+ *
437+ */
438+ onInit ( page ? : number = 1 ) {
439+ this . setState ( { sortColumn : '' , sortDirection : '' , page } , this . fetchData . bind ( this ) ) ;
440+ }
441+
448442 /**
449443 * Sets the new active page and reloads the data.
450444 *
@@ -516,14 +510,6 @@ const useDataList = (WrappedComponent: ComponentType<any>) => (
516510 this . setState ( { sortColumn, sortDirection, page } , this . fetchData . bind ( this ) ) ;
517511 }
518512
519- /**
520- * When no columns are sortable, load data as is
521- *
522- */
523- onInit(page?: number = 1) {
524- this . setState ( { sortColumn : '' , sortDirection : '' , page } , this . fetchData . bind ( this ) ) ;
525- }
526-
527513 /**
528514 * Renders the DataList component.
529515 *
@@ -565,7 +551,7 @@ const useDataList = (WrappedComponent: ComponentType<any>) => (
565551 sortColumn = { this . state . sortColumn }
566552 sortDirection = { this . state . sortDirection }
567553 / >
568- { this . state . saved && (
554+ { this . state . saved && (
569555 < Toaster
570556 onDismiss = { ( ) => this . setState ( { saved : false } ) }
571557 type = { Toaster . MessageTypes . positive }
@@ -578,7 +564,7 @@ const useDataList = (WrappedComponent: ComponentType<any>) => (
578564 />
579565 </ Toaster >
580566 ) }
581- { this . state . error && (
567+ { this . state . error && (
582568 < Toaster
583569 onDismiss = { ( ) => this . setState ( { error : false } ) }
584570 timeout = { 0 }
@@ -633,25 +619,11 @@ const useDataList = (WrappedComponent: ComponentType<any>) => (
633619 ) ;
634620 }
635621
636- /**
637- * Restores the DataList session object.
638- */
639- restoreSession ( ) {
640- const key = this . getSessionKey ( ) ;
641-
642- if ( ! key ) {
643- return { } ;
644- }
645-
646- const session = sessionStorage . getItem ( key ) || SESSION_DEFAULT ;
647- return JSON . parse ( session ) ;
648- }
649-
650622 /**
651623 * Sets the DataList session object.
652624 */
653625 setSession ( ) {
654- const key = this . getSessionKey ( ) ;
626+ const { key, storage } = this . props . session || { } ;
655627
656628 if ( ! key ) {
657629 return ;
@@ -666,14 +638,14 @@ const useDataList = (WrappedComponent: ComponentType<any>) => (
666638 sortDirection
667639 } = this . state ;
668640
669- sessionStorage.setItem (key, JSON.stringify( {
641+ ListSessionUtils . setSession ( key , storage , {
670642 filters,
671643 page,
672644 perPage,
673645 search,
674646 sortColumn,
675647 sortDirection
676- } )) ;
648+ } ) ;
677649 }
678650 }
679651) ;
0 commit comments