Add state saving to Container

This commit is contained in:
2018-04-09 11:05:33 +05:30
parent 420fc21fea
commit 84bc1ebef7
2 changed files with 27 additions and 1 deletions
@@ -17,7 +17,7 @@ interface CreateEditorAppState {
} }
export class CreateEditorApp extends React.Component<{}, CreateEditorAppState> { export class CreateEditorApp extends React.Component<{}, CreateEditorAppState> {
localStorageKey: 'appData'; localStorageKey: string = 'appData';
constructor(props: {}) { constructor(props: {}) {
super(props); super(props);
@@ -22,6 +22,8 @@ interface DragContainerState {
} }
export class DragContainer extends React.Component<DragContainerProps, DragContainerState> { export class DragContainer extends React.Component<DragContainerProps, DragContainerState> {
localStorageKey: string = 'containerData';
constructor(props: DragContainerProps) { constructor(props: DragContainerProps) {
super(props); super(props);
@@ -42,6 +44,30 @@ export class DragContainer extends React.Component<DragContainerProps, DragConta
this.handleDragStop = this.handleDragStop.bind(this); this.handleDragStop = this.handleDragStop.bind(this);
} }
componentDidMount() {
this.loadFromLocalStorage();
}
componentDidUpdate() {
this.saveToLocalStorage();
}
saveToLocalStorage() {
const pastOffsetJSON = JSON.stringify(this.state.pastCumulativeOffsetMap);
localStorage.setItem(this.localStorageKey, pastOffsetJSON);
}
loadFromLocalStorage() {
const pastOffsetJSON = localStorage.getItem(this.localStorageKey);
if (pastOffsetJSON) {
const pastOffsetObject = JSON.parse(pastOffsetJSON);
this.setState({
pastCumulativeOffsetMap: pastOffsetObject
});
}
}
handleDragStartOnDraggableNode(draggableId: string, ev: React.MouseEvent<HTMLDivElement>): void { handleDragStartOnDraggableNode(draggableId: string, ev: React.MouseEvent<HTMLDivElement>): void {
this.setState({ this.setState({
ongoing: { ongoing: {