<template>
  <project-entry-form
    :project-entry="projectEntry"
    @save="saveEntry"
    @hide="hideModal"/>
</template>

<script>
  import ProjectEntryForm from './ProjectEntryForm';

  import {mapGetters} from 'vuex';

  import PROJECT_ENTRY_QUERY from '@/graphql/gql/queries/projectEntryQuery.gql';
  import UPDATE_PROJECT_ENTRY_MUTATION from '@/graphql/gql/mutations/updateProjectEntry.gql';

  export default {
    components: {
      ProjectEntryForm
    },

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

    apollo: {
      projectEntry() {
        return {
          query: PROJECT_ENTRY_QUERY,
          variables: {
            id: this.currentProjectEntry
          }
        };
      }
    },

    computed: {
      ...mapGetters(['currentProjectEntry'])
    },

    methods: {
      saveEntry(entry) {
        this.$apollo.mutate({
          mutation: UPDATE_PROJECT_ENTRY_MUTATION,
          variables: {
            input: {
              projectEntry: entry
            }
          }
        }).then(() => {
          this.hideModal();
        });
      },
      hideModal() {
        this.$store.dispatch('hideModal');
      }
    },
  };
</script>
