Skip to content

Commit 755dcf1

Browse files
modify datepickers to use datetime
1 parent cb31496 commit 755dcf1

File tree

3 files changed

+44
-18
lines changed

3 files changed

+44
-18
lines changed

ui/src/utils/date.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,52 @@ import store from '@/store'
1818

1919
import dayjs from 'dayjs'
2020
import utc from 'dayjs/plugin/utc'
21-
import isToday from 'dayjs/plugin/isToday'
2221

2322
dayjs.extend(utc)
24-
dayjs.extend(isToday)
2523

26-
export function parseDayJsObject ({ value, format }) {
24+
export function parseDayJsObject ({ value, format = true, keepMoment = true }) {
25+
console.log(value)
26+
2727
if (!value) {
2828
return null
2929
}
3030

31+
if (typeof value === 'string') {
32+
value = dayjs(value)
33+
}
34+
35+
if (!store.getters.usebrowsertimezone) {
36+
value = value.utc(keepMoment)
37+
}
38+
3139
if (!format) {
3240
return value
3341
}
3442

35-
return value.format(format)
36-
}
37-
38-
export function isDayJsObjectToday (dayJsObject) {
39-
return dayJsObject.isToday()
43+
return value.format()
4044
}
4145

46+
/**
47+
* When passing a string/dayjs to the date picker component, it converts the value to the browser timezone; therefore,
48+
* we need to normalize the value to UTC if user is not using browser's timezone.
49+
* @param {*} value The datetime to normalize.
50+
* @returns A dayjs object with the datetime normalized to UTC if user is not using browser's timezone;
51+
* otherwise, a correspondent dayjs object based on the value passed.
52+
*/
4253
export function parseDateToDatePicker (value) {
4354
if (!value) {
4455
return null
4556
}
4657

47-
const format = 'YYYY-MM-DD'
48-
return dayjs(value, format)
58+
if (typeof value === 'string') {
59+
value = dayjs(value)
60+
}
61+
62+
if (store.getters.usebrowsertimezone) {
63+
return value
64+
}
65+
66+
return value.utc(false)
4967
}
5068

5169
export function toLocalDate ({ date, timezoneoffset = store.getters.timezoneoffset, usebrowsertimezone = store.getters.usebrowsertimezone }) {

ui/src/views/plugins/quota/CreateQuotaTariff.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
v-model:value="form.startDate"
7474
:disabled-date="disabledStartDate"
7575
:placeholder="$t('placeholder.quota.tariff.startdate')"
76+
show-time
7677
/>
7778
</a-form-item>
7879
<a-form-item ref="endDate" name="endDate">
@@ -83,6 +84,7 @@
8384
v-model:value="form.endDate"
8485
:disabled-date="disabledEndDate"
8586
:placeholder="$t('placeholder.quota.tariff.enddate')"
87+
show-time
8688
/>
8789
</a-form-item>
8890
<div :span="24" class="action-button">
@@ -98,7 +100,7 @@ import { api } from '@/api'
98100
import { ref, reactive, toRaw } from 'vue'
99101
import TooltipLabel from '@/components/widgets/TooltipLabel'
100102
import { getQuotaTypes } from '@/utils/quota'
101-
import { dayjs, isDayJsObjectToday, parseDayJsObject } from '@/utils/date'
103+
import { dayjs, parseDayJsObject } from '@/utils/date'
102104
import { mixinForm } from '@/utils/mixin'
103105
104106
export default {
@@ -145,11 +147,11 @@ export default {
145147
values.usageType = values.usageType.split('-')[0]
146148
147149
if (values.startDate) {
148-
values.startDate = isDayJsObjectToday(values.startDate) ? null : parseDayJsObject({ value: values.startDate, format: 'YYYY-MM-DD' })
150+
values.startDate = parseDayJsObject({ value: values.startDate })
149151
}
150152
151153
if (values.endDate) {
152-
values.endDate = parseDayJsObject({ value: values.endDate, format: 'YYYY-MM-DD' })
154+
values.endDate = parseDayJsObject({ value: values.endDate })
153155
}
154156
155157
this.loading = true

ui/src/views/plugins/quota/EditQuotaTariff.vue

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
v-model:value="form.endDate"
5151
:disabled-date="disabledEndDate"
5252
:placeholder="$t('placeholder.quota.tariff.enddate')"
53+
show-time
5354
/>
5455
</a-form-item>
5556
<div :span="24" class="action-button">
@@ -66,6 +67,7 @@ import { dayjs, parseDateToDatePicker, parseDayJsObject } from '@/utils/date'
6667
import { mixinForm } from '@/utils/mixin'
6768
import TooltipLabel from '@/components/widgets/TooltipLabel'
6869
import { ref, reactive, toRaw } from 'vue'
70+
import store from '@/store'
6971
7072
export default {
7173
name: 'EditQuotaTariff',
@@ -125,10 +127,8 @@ export default {
125127
params.value = values.value
126128
}
127129
128-
const resourceEndDate = this.resource.endDate?.split('T')[0]
129-
const parsedEndDate = parseDayJsObject({ value: values.endDate, format: 'YYYY-MM-DD' })
130-
if (parsedEndDate && resourceEndDate !== parsedEndDate) {
131-
params.enddate = parsedEndDate
130+
if (values.endDate && !values.endDate.isSame(this.resource.endDate)) {
131+
params.enddate = parseDayJsObject({ value: values.endDate })
132132
}
133133
134134
if (Object.keys(params).length === 1) {
@@ -159,7 +159,13 @@ export default {
159159
})
160160
},
161161
disabledEndDate (current) {
162-
return current < dayjs.utc(this.resource.effectiveDate).startOf('day')
162+
const lowerEndDateLimit = dayjs(this.resource.effectiveDate)
163+
const startOfToday = dayjs().startOf('day')
164+
165+
if (store.getters.usebrowsertimezone) {
166+
return current < startOfToday || current < lowerEndDateLimit.startOf('day')
167+
}
168+
return current < startOfToday || current < lowerEndDateLimit.utc(false).startOf('day')
163169
}
164170
}
165171
}

0 commit comments

Comments
 (0)