|
| 1 | +<!-- |
| 2 | + - @copyright 2021, Christopher Ng <chrng8@gmail.com> |
| 3 | + - |
| 4 | + - @author Christopher Ng <chrng8@gmail.com> |
| 5 | + - |
| 6 | + - @license GNU AGPL version 3 or any later version |
| 7 | + - |
| 8 | + - This program is free software: you can redistribute it and/or modify |
| 9 | + - it under the terms of the GNU Affero General Public License as |
| 10 | + - published by the Free Software Foundation, either version 3 of the |
| 11 | + - License, or (at your option) any later version. |
| 12 | + - |
| 13 | + - This program is distributed in the hope that it will be useful, |
| 14 | + - but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + - GNU Affero General Public License for more details. |
| 17 | + - |
| 18 | + - You should have received a copy of the GNU Affero General Public License |
| 19 | + - along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | +--> |
| 21 | + |
| 22 | +<template> |
| 23 | + <div class="company"> |
| 24 | + <input |
| 25 | + id="company" |
| 26 | + type="text" |
| 27 | + name="company" |
| 28 | + :placeholder="t('settings', 'Your company')" |
| 29 | + :value="company" |
| 30 | + autocapitalize="none" |
| 31 | + autocomplete="on" |
| 32 | + autocorrect="off" |
| 33 | + required |
| 34 | + @input="onCompanyChange"> |
| 35 | + |
| 36 | + <div class="company__actions-container"> |
| 37 | + <transition name="fade"> |
| 38 | + <span v-if="showCheckmarkIcon" class="icon-checkmark" /> |
| 39 | + <span v-else-if="showErrorIcon" class="icon-error" /> |
| 40 | + </transition> |
| 41 | + </div> |
| 42 | + </div> |
| 43 | +</template> |
| 44 | + |
| 45 | +<script> |
| 46 | +import { showError } from '@nextcloud/dialogs' |
| 47 | +import { emit } from '@nextcloud/event-bus' |
| 48 | +import debounce from 'debounce' |
| 49 | +
|
| 50 | +// import { savePrimaryCompany } from '../../../service/PersonalInfo/DisplayNameService' |
| 51 | +// import { validateCompany } from '../../../utils/validate' |
| 52 | +
|
| 53 | +export default { |
| 54 | + name: 'Company', |
| 55 | +
|
| 56 | + props: { |
| 57 | + company: { |
| 58 | + type: String, |
| 59 | + required: true, |
| 60 | + }, |
| 61 | + scope: { |
| 62 | + type: String, |
| 63 | + required: true, |
| 64 | + }, |
| 65 | + }, |
| 66 | +
|
| 67 | + data() { |
| 68 | + return { |
| 69 | + initialCompany: this.company, |
| 70 | + localScope: this.scope, |
| 71 | + showCheckmarkIcon: false, |
| 72 | + showErrorIcon: false, |
| 73 | + } |
| 74 | + }, |
| 75 | +
|
| 76 | + methods: { |
| 77 | + onCompanyChange(e) { |
| 78 | + this.$emit('update:company', e.target.value) |
| 79 | + this.debounceCompanyChange(e.target.value.trim()) |
| 80 | + }, |
| 81 | +
|
| 82 | + debounceCompanyChange: debounce(async function(company) { |
| 83 | + if (validateCompany(company)) { |
| 84 | + await this.updatePrimaryCompany(company) |
| 85 | + } |
| 86 | + }, 500), |
| 87 | +
|
| 88 | + async updatePrimaryCompany(company) { |
| 89 | + try { |
| 90 | + const responseData = await savePrimaryCompany(company) |
| 91 | + this.handleResponse({ |
| 92 | + company, |
| 93 | + status: responseData.ocs?.meta?.status, |
| 94 | + }) |
| 95 | + } catch (e) { |
| 96 | + this.handleResponse({ |
| 97 | + errorMessage: 'Unable to update full name', |
| 98 | + error: e, |
| 99 | + }) |
| 100 | + } |
| 101 | + }, |
| 102 | +
|
| 103 | + handleResponse({ company, status, errorMessage, error }) { |
| 104 | + if (status === 'ok') { |
| 105 | + // Ensure that local state reflects server state |
| 106 | + this.initialCompany = company |
| 107 | + emit('settings:display-name:updated', company) |
| 108 | + this.showCheckmarkIcon = true |
| 109 | + setTimeout(() => { this.showCheckmarkIcon = false }, 2000) |
| 110 | + } else { |
| 111 | + showError(t('settings', errorMessage)) |
| 112 | + this.logger.error(errorMessage, error) |
| 113 | + this.showErrorIcon = true |
| 114 | + setTimeout(() => { this.showErrorIcon = false }, 2000) |
| 115 | + } |
| 116 | + }, |
| 117 | +
|
| 118 | + onScopeChange(scope) { |
| 119 | + this.$emit('update:scope', scope) |
| 120 | + }, |
| 121 | + }, |
| 122 | +} |
| 123 | +</script> |
| 124 | +
|
| 125 | +<style lang="scss" scoped> |
| 126 | +.company { |
| 127 | + display: grid; |
| 128 | + align-items: center; |
| 129 | +
|
| 130 | + input { |
| 131 | + grid-area: 1 / 1; |
| 132 | + width: 100%; |
| 133 | + height: 34px; |
| 134 | + margin: 3px 3px 3px 0; |
| 135 | + padding: 7px 6px; |
| 136 | + color: var(--color-main-text); |
| 137 | + border: 1px solid var(--color-border-dark); |
| 138 | + border-radius: var(--border-radius); |
| 139 | + background-color: var(--color-main-background); |
| 140 | + font-family: var(--font-face); |
| 141 | + cursor: text; |
| 142 | + } |
| 143 | +
|
| 144 | + .company__actions-container { |
| 145 | + grid-area: 1 / 1; |
| 146 | + justify-self: flex-end; |
| 147 | + height: 30px; |
| 148 | +
|
| 149 | + display: flex; |
| 150 | + gap: 0 2px; |
| 151 | + margin-right: 5px; |
| 152 | +
|
| 153 | + .icon-checkmark, |
| 154 | + .icon-error { |
| 155 | + height: 30px !important; |
| 156 | + min-height: 30px !important; |
| 157 | + width: 30px !important; |
| 158 | + min-width: 30px !important; |
| 159 | + top: 0; |
| 160 | + right: 0; |
| 161 | + float: none; |
| 162 | + } |
| 163 | + } |
| 164 | +} |
| 165 | +
|
| 166 | +.fade-enter, |
| 167 | +.fade-leave-to { |
| 168 | + opacity: 0; |
| 169 | +} |
| 170 | +
|
| 171 | +.fade-enter-active { |
| 172 | + transition: opacity 200ms ease-out; |
| 173 | +} |
| 174 | +
|
| 175 | +.fade-leave-active { |
| 176 | + transition: opacity 300ms ease-out; |
| 177 | +} |
| 178 | +</style> |
0 commit comments