Welcome to mirror list, hosted at ThFree Co, Russian Federation.

timelogs_app.vue « components « time_tracking « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2069e4a672261401e2957f9beafb9106a4e2c7bc (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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<script>
import * as Sentry from '@sentry/browser';
import {
  GlButton,
  GlFormGroup,
  GlFormInput,
  GlLoadingIcon,
  GlKeysetPagination,
  GlDatepicker,
} from '@gitlab/ui';
import { createAlert } from '~/alert';
import { formatTimeSpent } from '~/lib/utils/datetime_utility';
import { s__ } from '~/locale';
import getTimelogsQuery from './queries/get_timelogs.query.graphql';
import TimelogsTable from './timelogs_table.vue';

const ENTRIES_PER_PAGE = 20;

// Define initial dates to current date and time
const INITIAL_TO_DATE = new Date();
const INITIAL_FROM_DATE = new Date();

// Set the initial 'from' date to 30 days before the current date
INITIAL_FROM_DATE.setDate(INITIAL_TO_DATE.getDate() - 30);

export default {
  components: {
    GlButton,
    GlFormGroup,
    GlFormInput,
    GlLoadingIcon,
    GlKeysetPagination,
    GlDatepicker,
    TimelogsTable,
  },
  props: {
    limitToHours: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  data() {
    return {
      projectId: null,
      groupId: null,
      username: null,
      timeSpentFrom: INITIAL_FROM_DATE,
      timeSpentTo: INITIAL_TO_DATE,
      cursor: {
        first: ENTRIES_PER_PAGE,
        after: null,
        last: null,
        before: null,
      },
      queryVariables: {
        startDate: INITIAL_FROM_DATE,
        endDate: INITIAL_TO_DATE,
        projectId: null,
        groupId: null,
        username: null,
      },
      pageInfo: {},
      report: [],
      totalSpentTime: 0,
    };
  },
  apollo: {
    report: {
      query: getTimelogsQuery,
      variables() {
        return {
          ...this.queryVariables,
          ...this.cursor,
        };
      },
      update({ timelogs: { nodes = [], pageInfo = {}, totalSpentTime = 0 } = {} }) {
        this.pageInfo = pageInfo;
        this.totalSpentTime = totalSpentTime;
        return nodes;
      },
      error(error) {
        createAlert({ message: s__('TimeTrackingReport|Something went wrong. Please try again.') });
        Sentry.captureException(error);
      },
    },
  },
  computed: {
    isLoading() {
      return this.$apollo.queries.report.loading;
    },
    showPagination() {
      return this.pageInfo?.hasPreviousPage || this.pageInfo?.hasNextPage;
    },
    formattedTotalSpentTime() {
      return formatTimeSpent(this.totalSpentTime, this.limitToHours);
    },
  },
  methods: {
    nullIfBlank(value) {
      return value === '' ? null : value;
    },
    runReport() {
      this.cursor = {
        first: ENTRIES_PER_PAGE,
        after: null,
        last: null,
        before: null,
      };

      this.queryVariables = {
        startDate: this.nullIfBlank(this.timeSpentFrom),
        endDate: this.nullIfBlank(this.timeSpentTo),
        projectId: this.nullIfBlank(this.projectId),
        groupId: this.nullIfBlank(this.groupId),
        username: this.nullIfBlank(this.username),
      };
    },
    nextPage(item) {
      this.cursor = {
        first: ENTRIES_PER_PAGE,
        after: item,
        last: null,
        before: null,
      };
    },
    prevPage(item) {
      this.cursor = {
        first: null,
        after: null,
        last: ENTRIES_PER_PAGE,
        before: item,
      };
    },
    clearTimeSpentFromDate() {
      this.timeSpentFrom = null;
    },
    clearTimeSpentToDate() {
      this.timeSpentTo = null;
    },
  },
  i18n: {
    username: s__('TimeTrackingReport|Username'),
    from: s__('TimeTrackingReport|From'),
    to: s__('TimeTrackingReport|To'),
    runReport: s__('TimeTrackingReport|Run report'),
    totalTimeSpentText: s__('TimeTrackingReport|Total time spent: '),
  },
};
</script>

<template>
  <div class="gl-display-flex gl-flex-direction-column gl-gap-5 gl-mt-5">
    <form
      class="gl-display-flex gl-flex-direction-column gl-md-flex-direction-row gl-gap-3"
      @submit.prevent="runReport"
    >
      <gl-form-group
        :label="$options.i18n.username"
        label-for="timelog-form-username"
        class="gl-mb-0 gl-md-form-input-md gl-w-full"
      >
        <gl-form-input
          id="timelog-form-username"
          v-model="username"
          data-testid="form-username"
          class="gl-w-full"
        />
      </gl-form-group>
      <gl-form-group
        key="time-spent-from"
        :label="$options.i18n.from"
        class="gl-mb-0 gl-md-form-input-md gl-w-full"
      >
        <gl-datepicker
          v-model="timeSpentFrom"
          :target="null"
          show-clear-button
          autocomplete="off"
          data-testid="form-from-date"
          class="gl-max-w-full!"
          @clear="clearTimeSpentFromDate"
        />
      </gl-form-group>
      <gl-form-group
        key="time-spent-to"
        :label="$options.i18n.to"
        class="gl-mb-0 gl-md-form-input-md gl-w-full"
      >
        <gl-datepicker
          v-model="timeSpentTo"
          :target="null"
          show-clear-button
          autocomplete="off"
          data-testid="form-to-date"
          class="gl-max-w-full!"
          @clear="clearTimeSpentToDate"
        />
      </gl-form-group>
      <gl-button
        class="gl-align-self-end gl-w-full gl-md-w-auto"
        variant="confirm"
        @click="runReport"
        >{{ $options.i18n.runReport }}</gl-button
      >
    </form>
    <div
      v-if="!isLoading"
      data-testid="table-container"
      class="gl-display-flex gl-flex-direction-column"
    >
      <div v-if="report.length" class="gl-display-flex gl-gap-2 gl-border-t gl-py-4">
        <span class="gl-font-weight-bold">{{ $options.i18n.totalTimeSpentText }}</span>
        <span data-testid="total-time-spent-container">{{ formattedTotalSpentTime }}</span>
      </div>

      <timelogs-table :limit-to-hours="limitToHours" :entries="report" />

      <gl-keyset-pagination
        v-if="showPagination"
        v-bind="pageInfo"
        class="gl-mt-3 gl-align-self-center"
        @prev="prevPage"
        @next="nextPage"
      />
    </div>
    <gl-loading-icon v-else size="lg" class="gl-mt-5" />
  </div>
</template>