From fb4eda2ad70f35627f6650c1d99fae1ada0d33d0 Mon Sep 17 00:00:00 2001 From: Sai Ganesh Date: Wed, 23 May 2018 11:45:04 -0400 Subject: [PATCH] added ability to add and remove recipes and clearing local storage --- .../settings-recipe/settings-recipe.html | 12 ++++- .../pages/settings-recipe/settings-recipe.ts | 8 ++++ .../settings-recipes/settings-recipes.html | 6 +++ .../settings-recipes/settings-recipes.ts | 13 ++++++ client/src/pages/settings/settings.html | 10 +++++ client/src/pages/settings/settings.ts | 44 ++++++++++++++++++- client/src/providers/recipes-data.ts | 7 ++- 7 files changed, 96 insertions(+), 4 deletions(-) diff --git a/client/src/pages/settings-recipe/settings-recipe.html b/client/src/pages/settings-recipe/settings-recipe.html index c980d87..0b46bf5 100644 --- a/client/src/pages/settings-recipe/settings-recipe.html +++ b/client/src/pages/settings-recipe/settings-recipe.html @@ -3,11 +3,21 @@ {{pageTitle}} + - + + Edit Recipe Name + + + Recipe Ingredients list + diff --git a/client/src/pages/settings-recipe/settings-recipe.ts b/client/src/pages/settings-recipe/settings-recipe.ts index d59f643..a7693b4 100644 --- a/client/src/pages/settings-recipe/settings-recipe.ts +++ b/client/src/pages/settings-recipe/settings-recipe.ts @@ -56,4 +56,12 @@ export class SettingsRecipePage { this.recipe.ingredients.splice(index,1); } + deleteCurrent(recipe){ + var index = this.recipesData.recipes.findIndex(item => item.id == this.recipe.id); + this.recipesData.recipes.splice(index,1); + this.recipesData.saveDataToLS(); + this.nav.pop(); + } + + } diff --git a/client/src/pages/settings-recipes/settings-recipes.html b/client/src/pages/settings-recipes/settings-recipes.html index d7c4783..1938317 100644 --- a/client/src/pages/settings-recipes/settings-recipes.html +++ b/client/src/pages/settings-recipes/settings-recipes.html @@ -11,4 +11,10 @@

{{recipesData.getRecipeDescription(recipe)}}

+ + + + + +
diff --git a/client/src/pages/settings-recipes/settings-recipes.ts b/client/src/pages/settings-recipes/settings-recipes.ts index 11ceaeb..2182460 100644 --- a/client/src/pages/settings-recipes/settings-recipes.ts +++ b/client/src/pages/settings-recipes/settings-recipes.ts @@ -22,5 +22,18 @@ export class SettingsRecipesPage { presentRecipe(recipe) { this.nav.push(SettingsRecipePage, {recipe}); } + + createNewRecipe() { + console.log("Total number of existing recipes = ", this.recipesData.recipes.length ); + let new_id = this.recipesData.recipes + .map(recipe => recipe.id) + .sort( (a, b) => b - a )[ 0 ] || 0; + new_id = new_id + 1; + console.log("Creating New Recipe with id = ", new_id ); + this.recipes.push({id: new_id , name: "NEW ITEM !!!", image:"cape_codder_small.png", description: "", ingredients:[]}) + var indexOfTheOneJustCreated = this.recipesData.recipes.findIndex(item => item.id == new_id); + var recipe = this.recipesData.recipes[indexOfTheOneJustCreated]; + this.nav.push(SettingsRecipePage, {recipe}); + } } diff --git a/client/src/pages/settings/settings.html b/client/src/pages/settings/settings.html index 9656de7..c945651 100644 --- a/client/src/pages/settings/settings.html +++ b/client/src/pages/settings/settings.html @@ -35,4 +35,14 @@ + + Reset data +
+ +
+ +
+
diff --git a/client/src/pages/settings/settings.ts b/client/src/pages/settings/settings.ts index 88a2e2b..4e059df 100644 --- a/client/src/pages/settings/settings.ts +++ b/client/src/pages/settings/settings.ts @@ -1,9 +1,11 @@ import { Component, NgZone } from '@angular/core'; import { Events, NavController /*, NavParams */} from 'ionic-angular'; +import { AlertController } from 'ionic-angular'; import { Barbot } from '../../providers/barbot'; import { SettingsIngredientsPage } from '../settings-ingredients/settings-ingredients'; import { SettingsRecipesPage } from '../settings-recipes/settings-recipes'; +import { RecipesData } from '../../providers/recipes-data'; @Component({ selector: 'page-settings', @@ -14,10 +16,14 @@ export class SettingsPage { status: any; buttonDisabled: boolean = false; + + constructor(public nav: NavController, private events: Events, private barbot: Barbot, - private ngZone: NgZone + public atrCtrl: AlertController, + private ngZone: NgZone, + public recipesData: RecipesData ) { this.listenToEvents(); } @@ -52,6 +58,42 @@ export class SettingsPage { }); } + + showPromptAlert() { + let alert = this.atrCtrl.create({ + title: 'Clear local data', + inputs: [ + { + name: 'pin', + placeholder: 'Enter pin', + type: 'password' + } + ], + buttons: [ + { + text: 'Cancel', + role: 'cancel', + handler: data => { + console.log('You Clicked on Cancel'); + } + }, + { + text: 'Clear Data', + handler: data => { + if (data.pin == '2345') { + this.recipesData.clearData(); + this.recipesData.init(); + } else { + // invalid login + return false; + } + + } + } + ] + }); + alert.present(); +} presentIngredients() { this.nav.push(SettingsIngredientsPage); } diff --git a/client/src/providers/recipes-data.ts b/client/src/providers/recipes-data.ts index 08875ff..bf6005c 100644 --- a/client/src/providers/recipes-data.ts +++ b/client/src/providers/recipes-data.ts @@ -20,6 +20,7 @@ export class RecipesData { } init() { + //this.ls.clear(); // this is to clear everything on start Just for Debugging let shouldCopyPredefinedData; shouldCopyPredefinedData = this.lsDataKeys.some( datasetKey => @@ -29,6 +30,10 @@ export class RecipesData { return !shouldCopyPredefinedData ? this.loadFromLS() : this.loadFromJSON(); } + clearData(){ + this.ls.clear(); + } + loadFromLS() { this.lsDataKeys.forEach( lsDataKey => { this[ lsDataKey ] = JSON.parse( this.ls.getItem(lsDataKey) ); @@ -68,7 +73,6 @@ export class RecipesData { } saveIngrediant(modifiedIngredient) { - let ingredientToModify = this.ingredients.find( ingredient => ingredient.id == modifiedIngredient.id ); @@ -80,7 +84,6 @@ export class RecipesData { } else { this.ingredients.unshift(modifiedIngredient); } - this.saveDataToLS(); }