You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Email.vue 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. <template>
  22. <div>
  23. <div class="email">
  24. <input
  25. id="email"
  26. ref="email"
  27. type="email"
  28. :name="inputName"
  29. :placeholder="inputPlaceholder"
  30. :value="email"
  31. autocapitalize="none"
  32. autocomplete="on"
  33. autocorrect="off"
  34. required
  35. @input="onEmailChange">
  36. <div class="email__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. <template v-if="!primary">
  42. <FederationControl
  43. :account-property="accountProperty"
  44. :additional="true"
  45. :additional-value="email"
  46. :disabled="federationDisabled"
  47. :handle-scope-change="saveAdditionalEmailScope"
  48. :scope.sync="localScope"
  49. @update:scope="onScopeChange" />
  50. </template>
  51. <Actions
  52. class="email__actions"
  53. :aria-label="t('settings', 'Email options')"
  54. :disabled="deleteDisabled"
  55. :force-menu="true">
  56. <ActionButton
  57. :aria-label="deleteEmailLabel"
  58. :close-after-click="true"
  59. :disabled="deleteDisabled"
  60. icon="icon-delete"
  61. @click.stop.prevent="deleteEmail">
  62. {{ deleteEmailLabel }}
  63. </ActionButton>
  64. <ActionButton v-if="!primary || !isNotificationEmail"
  65. :aria-label="setNotificationMailLabel"
  66. :close-after-click="true"
  67. :disabled="setNotificationMailDisabled"
  68. icon="icon-favorite"
  69. @click.stop.prevent="setNotificationMail">
  70. {{ setNotificationMailLabel }}
  71. </ActionButton>
  72. </Actions>
  73. </div>
  74. </div>
  75. <em v-if="isNotificationEmail">
  76. {{ t('settings', 'Primary email for password reset and notifications') }}
  77. </em>
  78. </div>
  79. </template>
  80. <script>
  81. import Actions from '@nextcloud/vue/dist/Components/Actions'
  82. import ActionButton from '@nextcloud/vue/dist/Components/ActionButton'
  83. import {showError} from '@nextcloud/dialogs'
  84. import debounce from 'debounce'
  85. import FederationControl from '../shared/FederationControl'
  86. import {ACCOUNT_PROPERTY_READABLE_ENUM, VERIFICATION_ENUM} from '../../../constants/AccountPropertyConstants'
  87. import {
  88. removeAdditionalEmail,
  89. saveAdditionalEmail,
  90. saveAdditionalEmailScope,
  91. saveNotificationEmail,
  92. savePrimaryEmail,
  93. updateAdditionalEmail
  94. } from '../../../service/PersonalInfo/EmailService'
  95. import {validateEmail} from '../../../utils/validate'
  96. export default {
  97. name: 'Email',
  98. components: {
  99. Actions,
  100. ActionButton,
  101. FederationControl,
  102. },
  103. props: {
  104. email: {
  105. type: String,
  106. required: true,
  107. },
  108. index: {
  109. type: Number,
  110. default: 0,
  111. },
  112. primary: {
  113. type: Boolean,
  114. default: false,
  115. },
  116. scope: {
  117. type: String,
  118. required: true,
  119. },
  120. activeNotificationEmail: {
  121. type: String,
  122. default: '',
  123. },
  124. localVerificationState: {
  125. type: Number,
  126. default: VERIFICATION_ENUM.NOT_VERIFIED,
  127. },
  128. },
  129. data() {
  130. return {
  131. accountProperty: ACCOUNT_PROPERTY_READABLE_ENUM.EMAIL,
  132. initialEmail: this.email,
  133. localScope: this.scope,
  134. saveAdditionalEmailScope,
  135. showCheckmarkIcon: false,
  136. showErrorIcon: false,
  137. isNotificationEmail: (this.email === this.activeNotificationEmail)
  138. || (this.primary && this.activeNotificationEmail === ''),
  139. }
  140. },
  141. computed: {
  142. deleteDisabled() {
  143. if (this.primary) {
  144. // Disable for empty primary email as there is nothing to delete
  145. // OR when initialEmail (reflects server state) and email (current input) are not the same
  146. return this.email === '' || this.initialEmail !== this.email
  147. } else if (this.initialEmail !== '') {
  148. return this.initialEmail !== this.email
  149. }
  150. return false
  151. },
  152. deleteEmailLabel() {
  153. if (this.primary) {
  154. return t('settings', 'Remove primary email')
  155. }
  156. return t('settings', 'Delete email')
  157. },
  158. setNotificationMailDisabled() {
  159. return !this.primary && this.localVerificationState !== VERIFICATION_ENUM.VERIFIED
  160. },
  161. setNotificationMailLabel() {
  162. if (this.isNotificationEmail) {
  163. return t('settings', 'Unset as primary email')
  164. } else if (!this.primary && this.localVerificationState !== VERIFICATION_ENUM.VERIFIED) {
  165. return t('settings', 'This address is not confirmed')
  166. }
  167. return t('settings', 'Set as primary mail')
  168. },
  169. federationDisabled() {
  170. return !this.initialEmail
  171. },
  172. inputName() {
  173. if (this.primary) {
  174. return 'email'
  175. }
  176. return 'additionalEmail[]'
  177. },
  178. inputPlaceholder() {
  179. if (this.primary) {
  180. return t('settings', 'Your email address')
  181. }
  182. return t('settings', 'Additional email address {index}', { index: this.index + 1 })
  183. },
  184. },
  185. mounted() {
  186. if (!this.primary && this.initialEmail === '') {
  187. // $nextTick is needed here, otherwise it may not always work https://stackoverflow.com/questions/51922767/autofocus-input-on-mount-vue-ios/63485725#63485725
  188. this.$nextTick(() => this.$refs.email?.focus())
  189. }
  190. },
  191. methods: {
  192. onEmailChange(e) {
  193. this.$emit('update:email', e.target.value)
  194. this.debounceEmailChange(e.target.value.trim())
  195. },
  196. debounceEmailChange: debounce(async function(email) {
  197. if (validateEmail(email) || email === '') {
  198. if (this.primary) {
  199. await this.updatePrimaryEmail(email)
  200. } else {
  201. if (email) {
  202. if (this.initialEmail === '') {
  203. await this.addAdditionalEmail(email)
  204. } else {
  205. await this.updateAdditionalEmail(email)
  206. }
  207. }
  208. }
  209. }
  210. }, 500),
  211. async deleteEmail() {
  212. if (this.primary) {
  213. this.$emit('update:email', '')
  214. await this.updatePrimaryEmail('')
  215. } else {
  216. await this.deleteAdditionalEmail()
  217. }
  218. },
  219. async updatePrimaryEmail(email) {
  220. try {
  221. const responseData = await savePrimaryEmail(email)
  222. this.handleResponse({
  223. email,
  224. status: responseData.ocs?.meta?.status,
  225. })
  226. } catch (e) {
  227. if (email === '') {
  228. this.handleResponse({
  229. errorMessage: 'Unable to delete primary email address',
  230. error: e,
  231. })
  232. } else {
  233. this.handleResponse({
  234. errorMessage: 'Unable to update primary email address',
  235. error: e,
  236. })
  237. }
  238. }
  239. },
  240. async addAdditionalEmail(email) {
  241. try {
  242. const responseData = await saveAdditionalEmail(email)
  243. this.handleResponse({
  244. email,
  245. status: responseData.ocs?.meta?.status,
  246. })
  247. } catch (e) {
  248. this.handleResponse({
  249. errorMessage: 'Unable to add additional email address',
  250. error: e,
  251. })
  252. }
  253. },
  254. async setNotificationMail() {
  255. try {
  256. const newNotificationMailValue = (this.primary || this.isNotificationEmail) ? '' : this.initialEmail
  257. const responseData = await saveNotificationEmail(newNotificationMailValue)
  258. this.handleResponse({
  259. notificationEmail: newNotificationMailValue,
  260. status: responseData.ocs?.meta?.status,
  261. })
  262. } catch (e) {
  263. this.handleResponse({
  264. errorMessage: 'Unable to choose this email for notifications',
  265. error: e,
  266. })
  267. }
  268. },
  269. async updateAdditionalEmail(email) {
  270. try {
  271. const responseData = await updateAdditionalEmail(this.initialEmail, email)
  272. this.handleResponse({
  273. email,
  274. status: responseData.ocs?.meta?.status,
  275. })
  276. } catch (e) {
  277. this.handleResponse({
  278. errorMessage: 'Unable to update additional email address',
  279. error: e,
  280. })
  281. }
  282. },
  283. async deleteAdditionalEmail() {
  284. try {
  285. const responseData = await removeAdditionalEmail(this.initialEmail)
  286. this.handleDeleteAdditionalEmail(responseData.ocs?.meta?.status)
  287. } catch (e) {
  288. this.handleResponse({
  289. errorMessage: 'Unable to delete additional email address',
  290. error: e,
  291. })
  292. }
  293. },
  294. handleDeleteAdditionalEmail(status) {
  295. if (status === 'ok') {
  296. this.$emit('delete-additional-email')
  297. } else {
  298. this.handleResponse({
  299. errorMessage: 'Unable to delete additional email address',
  300. })
  301. }
  302. },
  303. handleResponse({ email, notificationEmail, status, errorMessage, error }) {
  304. if (status === 'ok') {
  305. // Ensure that local state reflects server state
  306. if (email) {
  307. this.initialEmail = email
  308. } else if (notificationEmail) {
  309. this.activeNotificationEmail = notificationEmail
  310. }
  311. this.showCheckmarkIcon = true
  312. setTimeout(() => { this.showCheckmarkIcon = false }, 2000)
  313. } else {
  314. showError(t('settings', errorMessage))
  315. this.logger.error(errorMessage, error)
  316. this.showErrorIcon = true
  317. setTimeout(() => { this.showErrorIcon = false }, 2000)
  318. }
  319. },
  320. onScopeChange(scope) {
  321. this.$emit('update:scope', scope)
  322. },
  323. },
  324. }
  325. </script>
  326. <style lang="scss" scoped>
  327. .email {
  328. display: grid;
  329. align-items: center;
  330. input {
  331. grid-area: 1 / 1;
  332. width: 100%;
  333. height: 34px;
  334. margin: 3px 3px 3px 0;
  335. padding: 7px 6px;
  336. color: var(--color-main-text);
  337. border: 1px solid var(--color-border-dark);
  338. border-radius: var(--border-radius);
  339. background-color: var(--color-main-background);
  340. font-family: var(--font-face);
  341. cursor: text;
  342. }
  343. .email__actions-container {
  344. grid-area: 1 / 1;
  345. justify-self: flex-end;
  346. height: 30px;
  347. display: flex;
  348. gap: 0 2px;
  349. margin-right: 5px;
  350. .email__actions {
  351. opacity: 0.4 !important;
  352. &:hover {
  353. opacity: 0.8 !important;
  354. }
  355. &::v-deep button {
  356. height: 30px !important;
  357. min-height: 30px !important;
  358. width: 30px !important;
  359. min-width: 30px !important;
  360. }
  361. }
  362. .icon-checkmark,
  363. .icon-error {
  364. height: 30px !important;
  365. min-height: 30px !important;
  366. width: 30px !important;
  367. min-width: 30px !important;
  368. top: 0;
  369. right: 0;
  370. float: none;
  371. }
  372. }
  373. }
  374. .fade-enter,
  375. .fade-leave-to {
  376. opacity: 0;
  377. }
  378. .fade-enter-active {
  379. transition: opacity 200ms ease-out;
  380. }
  381. .fade-leave-active {
  382. transition: opacity 300ms ease-out;
  383. }
  384. </style>