Skip to content

Instantly share code, notes, and snippets.

@haverchuck
Created June 23, 2018 02:03
Show Gist options
  • Save haverchuck/1a86a2388859f4b8bf530be5750a6edb to your computer and use it in GitHub Desktop.
Save haverchuck/1a86a2388859f4b8bf530be5750a6edb to your computer and use it in GitHub Desktop.
list.page.ts
import { Component, OnInit, Input } from '@angular/core';
import { ModalController, Events } from '@ionic/angular';
import { AmplifyService } from 'aws-amplify-angular'
//import { ListItemModal } from './list.item.modal';
import { ToDoItem, ToDoList } from '../../classes/item.class';
@Component({
selector: 'app-list-page',
templateUrl: 'list.page.html'
})
export class ListPage implements OnInit {
amplifyService: AmplifyService;
modal: any;
data: any;
user: any;
itemList: ToDoList|any;
signedIn: boolean;
constructor(
public modalController: ModalController,
amplify: AmplifyService,
events: Events
) {
this.amplifyService = amplify;
// Listen for changes to the AuthState in order to change item list appropriately
events.subscribe('data:AuthState', async (data) => {
if (data.user){
this.user = await this.amplifyService.auth().currentUserInfo();
this.getItems();
} else {
this.itemList = [];
this.user = null;
}
})
}
async ngOnInit(){
// Use AWS Amplify to get user data when creating items
this.user = await this.amplifyService.auth().currentUserInfo();
this.getItems();
}
async modify(item, i) {
let props = {
itemList: this.itemList,
/*
We pass in an item paramenter only when the user clicks on an existing item and therefore populate an editItem value so that our modal knows this is an edit operation.
*/
editItem: item || undefined
};
// Create the modal
this.modal = await this.modalController.create({
//component: ListItemModal,
componentProps: props
});
// Listen for the modal to be closed...
this.modal.onDidDismiss((result) => {
if (result.data.newItem){
// ...and add a new item if modal passes back newItem
result.data.itemList.items.push(result.data.newItem)
} else if (result.data.editItem){
// ...or splice the items array if the modal passes back editItem
result.data.itemList.items[i] = result.data.editItem
}
this.save(result.data.itemList);
})
return this.modal.present()
}
delete(i){
this.itemList.items.splice(i, 1);
this.save(this.itemList);
}
complete(i){
this.itemList.items[i].status = "complete";
this.save(this.itemList);
}
save(list){
// Use AWS Amplify to save the list...
this.amplifyService.api().post('ToDoCRUD', '/ToDo', {body:
list}).then((i) => {
// ... and to get the list after we save it.
this.getItems()
})
.catch((err) => {
alert('Error saving list:')
})
}
getItems(){
if (this.user){
// Use AWS Amplify to get the list
this.amplifyService.api().get('ToDoCRUD', `/ToDo/${this.user.id}`, {}).then((res) => {
if (res && res.length > 0){
this.itemList = res[0];
} else {
this.itemList = new ToDoList({userId: this.user.id, items: []});
}
})
.catch((err) => {
console.log(`Error getting list: ${err}`)
})
} else {
console.log('Cannot get items: no active user')
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment