<template>
  <note-form
    :note="note"
    @save="addNote"
    @hide="hide"
  />
</template>

<script>
  import NoteForm from '@/components/notes/NoteForm';

  import {mapGetters} from 'vuex';
  import {constructNoteMutation} from '@/helpers/new-note-mutation.js';

  export default {
    components: {
      NoteForm
    },

    data() {
      return {
        note: {}
      };
    },

    computed: {
      ...mapGetters(['currentContent', 'currentNoteBlock', 'currentNoteParent', 'noteType'])
    },

    methods: {
      addNote(n) {
        const content = this.currentContent;
        const block = this.currentNoteBlock;
        const parent = this.currentNoteParent;
        const type = this.noteType;
        const text = n.text;
        let note = {};
        if (content > '') {
          note = {
            content,
            block,
            text,
          };
        } else {
          note = {
            parent,
            text
          };
        }
        if(type){
          note.type = type;
        }

        this.$apollo
          .mutate(constructNoteMutation(note))
          .then(this.hide);
      },
      hide() {
        this.$store.dispatch('hideModal');
      }
    }
  };
</script>
