Guest User

Untitled

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