Skip to content

Instantly share code, notes, and snippets.

@YonathanMeguira
Last active July 26, 2018 14:06
Show Gist options
  • Save YonathanMeguira/1543fc61df74d60562a178cbee95a64d to your computer and use it in GitHub Desktop.
Save YonathanMeguira/1543fc61df74d60562a178cbee95a64d to your computer and use it in GitHub Desktop.
dataService.ts
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/index';
import { List, Item } from './shopika.model';
import { ID } from '@datorama/akita';
import { of } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class ShopikaDataService {
/**
* Here we use local storage as our database since we only want to focus on Akita
*/
getLists(): Observable<List> {
const lists = JSON.parse(localStorage.getItem('lists')) || [];
return of(lists);
}
getItems(): Observable<Item[]> {
const items = JSON.parse(localStorage.getItem('items')) || [];
return of(items);
}
saveList(list: List | List[]) {
const existingData = JSON.parse(localStorage.getItem('lists')) || [];
const newData = [...existingData, list];
localStorage.setItem('lists', JSON.stringify(newData));
}
saveItems(items: Item[]) {
const existingData = JSON.parse(localStorage.getItem('items')) || [];
const newData = [...existingData, items];
localStorage.setItem('items', JSON.stringify(newData));
}
updateItemStatus(item: Item) {
const existingItems = JSON.parse(localStorage.getItem('items'));
const index = existingItems.findIndex((data: Item) => data.id === item.id);
existingItems[index] = item;
localStorage.setItem('items', JSON.stringify(existingItems));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment