Skip to content

Commit

Permalink
Store wishlist in localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
abritopach committed Feb 7, 2018
1 parent 47e67cb commit 1f499b9
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/components/wish-list/wish-list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Component, OnInit } from '@angular/core';

import { WishList } from '../../models/wishlist.model';

import { onSnapshot } from 'mobx-state-tree';

@Component({
selector: 'app-wish-list',
templateUrl: './wish-list.component.html',
Expand All @@ -16,7 +18,8 @@ export class WishListComponent implements OnInit {
}

ngOnInit() {
this.wishList = WishList.create({

let initialState = {
items: [
{
name: 'Machine Gun Preacher',
Expand All @@ -29,6 +32,20 @@ export class WishListComponent implements OnInit {
image: 'https://prodimage.images-bn.com/pimages/0673419193054_p0_v1_s550x406.jpg'
}
]
};

if (localStorage.getItem('wishlistapp')) {
const json = JSON.parse(localStorage.getItem('wishlistapp'));
if (WishList.is(json)) {
initialState = json;
}
}

this.wishList = WishList.create(initialState);

onSnapshot(this.wishList, snapshot => {
console.log('onSnapshot', snapshot);
localStorage.setItem('wishlistapp', JSON.stringify(snapshot));
});

/*
Expand Down

0 comments on commit 1f499b9

Please sign in to comment.