Guest User

Untitled

a guest
Jun 24th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. import { Component, OnInit, Input } from '@angular/core';
  2. import { ModalController, Events } from '@ionic/angular';
  3. import { AmplifyService } from 'aws-amplify-angular'
  4. import { ListItemModal } from './list.item.modal';
  5. import { ToDoItem, ToDoList } from '../../classes/item.class';
  6.  
  7. @Component({
  8. selector: 'app-list-page',
  9. templateUrl: 'list.page.html'
  10. })
  11. export class ListPage implements OnInit {
  12.  
  13. amplifyService: AmplifyService;
  14. modal: any;
  15. data: any;
  16. user: any;
  17. itemList: ToDoList|any;
  18. signedIn: boolean;
  19.  
  20. constructor(
  21. public modalController: ModalController,
  22. amplify: AmplifyService,
  23. events: Events
  24.  
  25. ) {
  26.  
  27. this.amplifyService = amplify;
  28. // Listen for changes to the AuthState in order to change item list appropriately
  29. events.subscribe('data:AuthState', async (data) => {
  30. if (data.user){
  31. this.user = await this.amplifyService.auth().currentUserInfo();
  32. this.getItems();
  33. } else {
  34. this.itemList = [];
  35. this.user = null;
  36. }
  37. })
  38.  
  39.  
  40. }
  41.  
  42. async ngOnInit(){
  43. // Use AWS Amplify to get user data when creating items
  44. this.user = await this.amplifyService.auth().currentUserInfo();
  45. this.getItems();
  46. }
  47.  
  48. async modify(item, i) {
  49. let props = {
  50. itemList: this.itemList,
  51. /*
  52. We pass in an item paramenter only when the user clicks on an existing item
  53. and therefore populate an editItem value so that our modal knows this is an edit operation.
  54. */
  55. editItem: item || undefined
  56. };
  57.  
  58. // Create the modal
  59. this.modal = await this.modalController.create({
  60. component: ListItemModal,
  61. componentProps: props
  62. });
  63.  
  64. // Listen for the modal to be closed...
  65. this.modal.onDidDismiss((result) => {
  66. if (result.data.newItem){
  67. // ...and add a new item if modal passes back newItem
  68. result.data.itemList.items.push(result.data.newItem)
  69. } else if (result.data.editItem){
  70. // ...or splice the items array if the modal passes back editItem
  71. result.data.itemList.items[i] = result.data.editItem
  72. }
  73. this.save(result.data.itemList);
  74. })
  75. return this.modal.present()
  76. }
  77.  
  78. delete(i){
  79. this.itemList.items.splice(i, 1);
  80. this.save(this.itemList);
  81.  
  82. }
  83.  
  84. complete(i){
  85. this.itemList.items[i].status = "complete";
  86. this.save(this.itemList);
  87. }
  88.  
  89. save(list){
  90. // Use AWS Amplify to save the list...
  91. this.amplifyService.api().post('ToDoItemsCRUD', '/ToDoItems', {body: list}).then((i) => {
  92. // ... and to get the list after we save it.
  93. this.getItems()
  94. })
  95. .catch((err) => {
  96. console.log(`Error saving list: ${err}`)
  97. })
  98. }
  99.  
  100. getItems(){
  101. if (this.user){
  102. // Use AWS Amplify to get the list
  103. this.amplifyService.api().get('ToDoItemsCRUD', `/ToDoItems/${this.user.id}`, {}).then((res) => {
  104. if (res && res.length > 0){
  105. this.itemList = res[0];
  106. } else {
  107. this.itemList = new ToDoList({userId: this.user.id, items: []});
  108. }
  109. })
  110. .catch((err) => {
  111. console.log(`Error getting list: ${err}`)
  112. })
  113. } else {
  114. console.log('Cannot get items: no active user')
  115. }
  116. }
  117.  
  118. }
Add Comment
Please, Sign In to add comment