All files / app/assets/javascripts/static_site_editor/pages home.vue

83.78% Statements 31/37
100% Branches 2/2
70.59% Functions 12/17
83.78% Lines 31/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170  1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                               17x                   16x     16x     16x       17x               1x     4x 4x 4x   4x 4x   7x   7x                   7x     7x                                 4x     7x           87x                                                                        
<script>
import createFlash from '~/flash';
import Tracking from '~/tracking';
 
import EditArea from '../components/edit_area.vue';
import EditMetaModal from '../components/edit_meta_modal.vue';
import InvalidContentMessage from '../components/invalid_content_message.vue';
import SkeletonLoader from '../components/skeleton_loader.vue';
import SubmitChangesError from '../components/submit_changes_error.vue';
import { LOAD_CONTENT_ERROR, TRACKING_ACTION_INITIALIZE_EDITOR } from '../constants';
import hasSubmittedChangesMutation from '../graphql/mutations/has_submitted_changes.mutation.graphql';
import submitContentChangesMutation from '../graphql/mutations/submit_content_changes.mutation.graphql';
import appDataQuery from '../graphql/queries/app_data.query.graphql';
import sourceContentQuery from '../graphql/queries/source_content.query.graphql';
import { SUCCESS_ROUTE } from '../router/constants';
 
export default {
  components: {
    SkeletonLoader,
    EditArea,
    EditMetaModal,
    InvalidContentMessage,
    SubmitChangesError,
  },
  apollo: {
    appData: {
      query: appDataQuery,
    },
    sourceContent: {
      query: sourceContentQuery,
      update: ({
        project: {
          file: { title, content },
        },
      }) => {
        return { title, content };
      },
      variables() {
        return {
          project: this.appData.project,
          sourcePath: this.appData.sourcePath,
        };
      },
      skip() {
        return !this.appData.isSupportedContent;
      },
      error() {
        createFlash({
          message: LOAD_CONTENT_ERROR,
        });
      },
    },
  },
  data() {
    return {
      content: null,
      images: null,
      formattedMarkdown: null,
      submitChangesError: null,
      isSavingChanges: false,
    };
  },
  computed: {
    isLoadingContent() {
      return this.$apollo.queries.sourceContent.loading;
    },
    isContentLoaded() {
      return Boolean(this.sourceContent);
    },
    projectSplit() {
      return this.appData.project.split('/'); // TODO: refactor so `namespace` and `project` remain distinct
    },
  },
  mounted() {
    Tracking.event(document.body.dataset.page, TRACKING_ACTION_INITIALIZE_EDITOR);
  },
  methods: {
    onHideModal() {
      this.isSavingChanges = false;
      this.$refs.editMetaModal.hide();
    },
    onDismissError() {
      this.submitChangesError = null;
    },
    onPrepareSubmit({ formattedMarkdown, content, images }) {
      this.content = content;
      this.images = images;
      this.formattedMarkdown = formattedMarkdown;
 
      this.isSavingChanges = true;
      this.$refs.editMetaModal.show();
    },
    onSubmit(mergeRequestMeta) {
      // eslint-disable-next-line promise/catch-or-return
      this.$apollo
        .mutate({
          mutation: hasSubmittedChangesMutation,
          variables: {
            input: {
              hasSubmittedChanges: true,
            },
          },
        })
        .finally(() => {
          this.$router.push(SUCCESS_ROUTE);
        });
 
      this.$apollo
        .mutate({
          mutation: submitContentChangesMutation,
          variables: {
            input: {
              project: this.appData.project,
              username: this.appData.username,
              sourcePath: this.appData.sourcePath,
              targetBranch: this.appData.branch,
              content: this.content,
              formattedMarkdown: this.formattedMarkdown,
              images: this.images,
              mergeRequestMeta,
            },
          },
        })
        .catch((e) => {
          this.submitChangesError = e.message;
        })
        .finally(() => {
          this.isSavingChanges = false;
        });
    },
  },
};
</script>
<template>
  <div class="container d-flex gl-flex-direction-column pt-2 h-100">
    <template v-if="appData.isSupportedContent">
      <skeleton-loader v-if="isLoadingContent" class="w-75 gl-align-self-center gl-mt-5" />
      <submit-changes-error
        v-if="submitChangesError"
        :error="submitChangesError"
        @retry="onSubmit"
        @dismiss="onDismissError"
      />
      <edit-area
        v-if="isContentLoaded"
        :title="sourceContent.title"
        :content="sourceContent.content"
        :saving-changes="isSavingChanges"
        :return-url="appData.returnUrl"
        :mounts="appData.mounts"
        :branch="appData.branch"
        :base-url="appData.baseUrl"
        :project="appData.project"
        :image-root="appData.imageUploadPath"
        @submit="onPrepareSubmit"
      />
      <edit-meta-modal
        ref="editMetaModal"
        :source-path="appData.sourcePath"
        :namespace="projectSplit[0]"
        :project="projectSplit[1]"
        @primary="onSubmit"
        @hide="onHideModal"
      />
    </template>
 
    <invalid-content-message v-else class="w-75" />
  </div>
</template>