debian-mirror-gitlab/app/assets/javascripts/members/components/table/expiration_datepicker.vue

105 lines
2.5 KiB
Vue
Raw Normal View History

2021-01-29 00:20:46 +05:30
<script>
import { GlDatepicker } from '@gitlab/ui';
import { mapActions } from 'vuex';
import { getDateInFuture } from '~/lib/utils/datetime_utility';
import { s__ } from '~/locale';
export default {
name: 'ExpirationDatepicker',
components: { GlDatepicker },
2021-04-29 21:17:54 +05:30
inject: ['namespace'],
2021-01-29 00:20:46 +05:30
props: {
member: {
type: Object,
required: true,
},
permissions: {
type: Object,
required: true,
},
},
data() {
return {
selectedDate: null,
busy: false,
};
},
computed: {
minDate() {
// Members expire at the beginning of the day.
// The first selectable day should be tomorrow.
const today = new Date();
const beginningOfToday = new Date(today.setHours(0, 0, 0, 0));
return getDateInFuture(beginningOfToday, 1);
},
disabled() {
return (
this.busy ||
!this.permissions.canUpdate ||
(this.permissions.canOverride && !this.member.isOverridden)
);
},
},
mounted() {
if (this.member.expiresAt) {
this.selectedDate = new Date(this.member.expiresAt);
}
},
methods: {
2021-04-29 21:17:54 +05:30
...mapActions({
updateMemberExpiration(dispatch, payload) {
return dispatch(`${this.namespace}/updateMemberExpiration`, payload);
},
}),
2021-01-29 00:20:46 +05:30
handleInput(date) {
this.busy = true;
this.updateMemberExpiration({
memberId: this.member.id,
expiresAt: date,
})
.then(() => {
this.$toast.show(s__('Members|Expiration date updated successfully.'));
this.busy = false;
})
.catch(() => {
this.busy = false;
});
},
handleClear() {
this.busy = true;
this.updateMemberExpiration({
memberId: this.member.id,
expiresAt: null,
})
.then(() => {
this.$toast.show(s__('Members|Expiration date removed successfully.'));
this.selectedDate = null;
this.busy = false;
})
.catch(() => {
this.busy = false;
});
},
},
};
</script>
<template>
<!-- `:target="null"` allows the datepicker to be opened on focus -->
<!-- `:container="null"` renders the datepicker in the body to prevent conflicting CSS table styles -->
<gl-datepicker
v-model="selectedDate"
class="gl-max-w-full"
show-clear-button
:target="null"
:container="null"
:min-date="minDate"
:placeholder="__('Expiration date')"
:disabled="disabled"
@input="handleInput"
@clear="handleClear"
/>
</template>