-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
JanAckermann
committed
Mar 3, 2022
1 parent
2976911
commit 046ed01
Showing
8 changed files
with
471 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
215 changes: 215 additions & 0 deletions
215
packages/web-app-files/src/components/Spaces/QuotaModal.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
<template> | ||
<portal to="app.runtime.modal"> | ||
<oc-modal | ||
id="edit-quota-modal" | ||
:title="modalTitle" | ||
:button-cancel-text="$gettext('Cancel')" | ||
:button-confirm-text="$gettext('Confirm')" | ||
:button-confirm-disabled="confirmButtonDisabled" | ||
@confirm="editQuota" | ||
@cancel="cancel" | ||
> | ||
<template #content> | ||
<oc-select | ||
id="quota-input-select" | ||
v-model="selectedOption" | ||
:selectable="optionSelectable" | ||
taggable | ||
push-tags | ||
:clearable="false" | ||
:options="options" | ||
:create-option="createOption" | ||
option-label="displayValue" | ||
:label="$gettext('Space quota')" | ||
> | ||
<template #selected-option="{ displayValue, displayUnit }"> | ||
<span>{{ displayValue }}</span> | ||
<span v-if="displayUnit" class="oc-text-muted oc-ml-s">{{ displayUnit }}</span> | ||
</template> | ||
<template #search="{ attributes, events }"> | ||
<input class="vs__search" v-bind="attributes" v-on="events" /> | ||
</template> | ||
<template #option="{ displayValue, displayUnit, error }"> | ||
<div class="oc-flex oc-flex-between"> | ||
<span>{{ displayValue }}</span> | ||
<span v-if="displayUnit" class="oc-text-muted">{{ displayUnit }}</span> | ||
</div> | ||
<div v-if="error" class="oc-text-input-danger">{{ error }}</div> | ||
</template> | ||
</oc-select> | ||
<p | ||
class="oc-mt-xs oc-text-meta" | ||
v-text="$gettext('Select an item or enter your own value')" | ||
/> | ||
</template> | ||
</oc-modal> | ||
</portal> | ||
</template> | ||
|
||
<script> | ||
import { mapActions, mapGetters, mapMutations } from 'vuex' | ||
import { clientService } from 'web-pkg/src/services' | ||
export default { | ||
name: 'SpaceQuotaModal', | ||
props: { | ||
space: { | ||
type: Object, | ||
required: true | ||
}, | ||
cancel: { | ||
type: Function, | ||
required: true | ||
} | ||
}, | ||
data: function () { | ||
return { | ||
selectedOption: {}, | ||
options: [], | ||
DEFAULT_OPTIONS: [ | ||
{ | ||
displayValue: '1', | ||
displayUnit: 'GB', | ||
value: Math.pow(10, 9) | ||
}, | ||
{ | ||
displayValue: '5', | ||
displayUnit: 'GB', | ||
value: 5 * Math.pow(10, 9) | ||
}, | ||
{ | ||
displayValue: '10', | ||
displayUnit: 'GB', | ||
value: 10 * Math.pow(10, 9) | ||
}, | ||
{ | ||
displayValue: '50', | ||
displayUnit: 'GB', | ||
value: 50 * Math.pow(10, 9) | ||
}, | ||
{ | ||
displayValue: '100', | ||
displayUnit: 'GB', | ||
value: 100 * Math.pow(10, 9) | ||
}, | ||
{ | ||
displayValue: '500', | ||
displayUnit: 'GB', | ||
value: 500 * Math.pow(10, 9) | ||
}, | ||
{ | ||
displayValue: '1000', | ||
displayUnit: 'GB', | ||
value: 10000 * Math.pow(10, 9) | ||
} | ||
] | ||
} | ||
}, | ||
computed: { | ||
...mapGetters(['getToken', 'configuration']), | ||
confirmButtonDisabled() { | ||
return this.space.spaceQuota.total === this.selectedOption.value | ||
}, | ||
modalTitle() { | ||
return this.$gettextInterpolate(this.$gettext('Edit quota for space %{name}'), { | ||
name: this.space.name | ||
}) | ||
} | ||
}, | ||
mounted() { | ||
this.setOptions() | ||
}, | ||
methods: { | ||
...mapActions(['showMessage']), | ||
...mapMutations('Files', ['UPDATE_RESOURCE_FIELD']), | ||
editQuota() { | ||
const newTotalQuota = this.selectedOption.value | ||
if (isNaN(newTotalQuota)) { | ||
return this.showMessage({ | ||
title: this.$gettext('Editing space quota failed…'), | ||
status: 'danger' | ||
}) | ||
} | ||
const graphClient = clientService.graphAuthenticated(this.configuration.server, this.getToken) | ||
return graphClient.drives | ||
.updateDrive(this.space.id, { quota: { total: this.selectedOption.value } }, {}) | ||
.then(({ data }) => { | ||
this.cancel() | ||
this.UPDATE_RESOURCE_FIELD({ | ||
id: this.space.id, | ||
field: 'spaceQuota', | ||
value: data.quota | ||
}) | ||
this.showMessage({ | ||
title: this.$gettext('Space quota was edited successfully') | ||
}) | ||
}) | ||
.catch((error) => { | ||
console.error(error) | ||
this.showMessage({ | ||
title: this.$gettext('Failed to change space quota'), | ||
desc: error, | ||
status: 'danger' | ||
}) | ||
}) | ||
}, | ||
optionSelectable(option) { | ||
if (!option.value) { | ||
return false | ||
} | ||
return !isNaN(option.value) | ||
}, | ||
createOption(option) { | ||
if (option.endsWith('.') || option.endsWith(',')) { | ||
option = option.slice(0, -1) | ||
} | ||
const optionIsNumberRegex = /^[1-9]\d*(([.,])\d+)?$/g | ||
if (!optionIsNumberRegex.test(option)) { | ||
return { | ||
displayValue: option, | ||
error: this.$gettext('Please enter only numbers') | ||
} | ||
} | ||
option = option.replace(',', '.') | ||
return { | ||
displayValue: parseFloat(option).toFixed(2).toString().replace('.00', ''), | ||
displayUnit: 'GB', | ||
value: parseFloat(option).toFixed(2) * Math.pow(10, 9) | ||
} | ||
}, | ||
setOptions() { | ||
this.options = [...this.DEFAULT_OPTIONS] | ||
const selectedQuotaInOptions = this.options.find( | ||
(option) => option.value === this.space.spaceQuota.total | ||
) | ||
if (selectedQuotaInOptions) { | ||
this.selectedOption = selectedQuotaInOptions | ||
} else { | ||
const newOption = { | ||
displayValue: (this.space.spaceQuota.total * Math.pow(10, -9)) | ||
.toFixed(2) | ||
.toString() | ||
.replace('.00', ''), | ||
displayUnit: 'GB', | ||
value: this.space.spaceQuota.total | ||
} | ||
this.options.push(newOption) | ||
this.options = [ | ||
...this.options.filter((o) => o.value).sort((a, b) => a.value - b.value), | ||
...this.options.filter((o) => !o.value) | ||
] | ||
this.selectedOption = newOption | ||
} | ||
} | ||
} | ||
} | ||
</script> |
41 changes: 41 additions & 0 deletions
41
packages/web-app-files/src/mixins/spaces/actions/editQuota.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { mapState } from 'vuex' | ||
|
||
export default { | ||
data: () => { | ||
return { | ||
$_editQuota_modalOpen: false | ||
} | ||
}, | ||
computed: { | ||
...mapState('Files', ['currentFolder']), | ||
$_editQuota_items() { | ||
return [ | ||
{ | ||
name: 'editQuota', | ||
icon: 'hard-drive', | ||
label: () => { | ||
return this.$gettext('Edit quota') | ||
}, | ||
handler: this.$_editQuota_trigger, | ||
isEnabled: ({ resources }) => { | ||
if (resources.length !== 1) { | ||
return false | ||
} | ||
|
||
return resources[0].spaceQuota | ||
}, | ||
componentType: 'oc-button', | ||
class: 'oc-files-actions-edit-quota-content-trigger' | ||
} | ||
] | ||
} | ||
}, | ||
methods: { | ||
$_editQuota_trigger() { | ||
this.$data.$_editQuota_modalOpen = true | ||
}, | ||
$_editQuota_closeModal() { | ||
this.$data.$_editQuota_modalOpen = false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.