Guest User

Untitled

a guest
Feb 16th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. import { Component, Input } from '@angular/core';
  2.  
  3. import { Apollo } from 'apollo-angular';
  4.  
  5. import gql from 'graphql-tag';
  6.  
  7. import { FormBuilder, FormGroup, Validators } from '@angular/forms';
  8.  
  9. import { Note } from '../types';
  10. import { Router } from '@angular/router';
  11.  
  12. @Component({
  13. selector: 'add-note',
  14. templateUrl: './add-note.component.html',
  15. styleUrls: ['./add-note.component.scss']
  16. })
  17. export class AddNoteComponent {
  18.  
  19. addForm: FormGroup;
  20.  
  21. constructor(private apollo: Apollo, private formBuilder: FormBuilder, public router: Router) {
  22. // menggunakan form builder
  23. this.addForm = this.formBuilder.group({
  24. 'id': ['', Validators.required],
  25. 'judul': ['', Validators.required],
  26. 'deskripsi': [''],
  27. 'tgl': ['']
  28. });
  29. }
  30.  
  31. // fungsi untuk menambah note dari value form builder dan diteruskan dengan query mutation
  32. addNote(formData: Note) {
  33. this.apollo.mutate({
  34. mutation: gql`
  35. mutation addNote($id: String!, $judul: String!, $deskripsi: String, $tgl: String!) {
  36. addNote(id: $id, judul: $judul, deskripsi: $deskripsi, tgl: $tgl) {
  37. id
  38. judul
  39. }
  40. }
  41. `,
  42. variables: {
  43. id: formData.id,
  44. judul: formData.judul,
  45. deskripsi: formData.deskripsi,
  46. tgl: formData.tgl
  47. },
  48. refetchQueries: [
  49. {
  50. query: gql`
  51. {
  52. notes {
  53. id
  54. judul
  55. tgl
  56. }
  57. }
  58. `
  59. }
  60. ]
  61. }).subscribe(() => this.router.navigate(['/list-note']));
  62. }
  63.  
  64. }
Add Comment
Please, Sign In to add comment