summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/boards/components/sidebar/board_sidebar_due_date.vue
blob: 4a664d5beef67de7c3fa6076f9767bf42aefc897 (plain)
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
<script>
import { mapGetters, mapActions } from 'vuex';
import { GlButton, GlDatepicker } from '@gitlab/ui';
import BoardEditableItem from '~/boards/components/sidebar/board_editable_item.vue';
import { dateInWords, formatDate, parsePikadayDate } from '~/lib/utils/datetime_utility';
import createFlash from '~/flash';
import { __ } from '~/locale';

export default {
  components: {
    BoardEditableItem,
    GlButton,
    GlDatepicker,
  },
  data() {
    return {
      loading: false,
    };
  },
  computed: {
    ...mapGetters(['activeIssue', 'projectPathForActiveIssue']),
    hasDueDate() {
      return this.activeIssue.dueDate != null;
    },
    parsedDueDate() {
      if (!this.hasDueDate) {
        return null;
      }

      return parsePikadayDate(this.activeIssue.dueDate);
    },
    formattedDueDate() {
      if (!this.hasDueDate) {
        return '';
      }

      return dateInWords(this.parsedDueDate, true);
    },
  },
  methods: {
    ...mapActions(['setActiveIssueDueDate']),
    async openDatePicker() {
      await this.$nextTick();
      this.$refs.datePicker.calendar.show();
    },
    async setDueDate(date) {
      this.loading = true;
      this.$refs.sidebarItem.collapse();

      try {
        const dueDate = date ? formatDate(date, 'yyyy-mm-dd') : null;
        await this.setActiveIssueDueDate({ dueDate, projectPath: this.projectPathForActiveIssue });
      } catch (e) {
        createFlash({ message: this.$options.i18n.updateDueDateError });
      } finally {
        this.loading = false;
      }
    },
  },
  i18n: {
    dueDate: __('Due date'),
    removeDueDate: __('remove due date'),
    updateDueDateError: __('An error occurred when updating the issue due date'),
  },
};
</script>

<template>
  <board-editable-item
    ref="sidebarItem"
    class="board-sidebar-due-date"
    :title="$options.i18n.dueDate"
    :loading="loading"
    @open="openDatePicker"
  >
    <template v-if="hasDueDate" #collapsed>
      <div class="gl-display-flex gl-align-items-center">
        <strong class="gl-text-gray-900">{{ formattedDueDate }}</strong>
        <span class="gl-mx-2">-</span>
        <gl-button
          variant="link"
          class="gl-text-gray-500!"
          data-testid="reset-button"
          :disabled="loading"
          @click="setDueDate(null)"
        >
          {{ $options.i18n.removeDueDate }}
        </gl-button>
      </div>
    </template>
    <template>
      <gl-datepicker
        ref="datePicker"
        :value="parsedDueDate"
        show-clear-button
        @input="setDueDate"
        @clear="setDueDate(null)"
      />
    </template>
  </board-editable-item>
</template>
<style>
/*
 * This can be removed after closing:
 * https://gitlab.com/gitlab-org/gitlab-ui/-/issues/1048
 */
.board-sidebar-due-date .gl-datepicker,
.board-sidebar-due-date .gl-datepicker-input {
  width: 100%;
}
</style>