From d6199ec281e0ca7d7295472201c8198973b4d427 Mon Sep 17 00:00:00 2001 From: Tatjana Kaschperko Lindt Date: Wed, 17 Jul 2024 15:21:06 +0200 Subject: [PATCH] add language component --- src/components/LanguageSection/Language.vue | 122 ++++++++++++++++++ .../LanguageSection/LanguageSection.vue | 75 +++++++++++ src/constants/AccountPropertyConstants.js | 41 ++++++ .../PersonalInfo/PersonalInfoService.js | 54 ++++++++ src/utils/handlers.js | 48 +++++++ 5 files changed, 340 insertions(+) create mode 100644 src/components/LanguageSection/Language.vue create mode 100644 src/components/LanguageSection/LanguageSection.vue create mode 100644 src/constants/AccountPropertyConstants.js create mode 100644 src/service/PersonalInfo/PersonalInfoService.js create mode 100644 src/utils/handlers.js diff --git a/src/components/LanguageSection/Language.vue b/src/components/LanguageSection/Language.vue new file mode 100644 index 0000000..4714ca1 --- /dev/null +++ b/src/components/LanguageSection/Language.vue @@ -0,0 +1,122 @@ + + + + + + + diff --git a/src/components/LanguageSection/LanguageSection.vue b/src/components/LanguageSection/LanguageSection.vue new file mode 100644 index 0000000..57441ea --- /dev/null +++ b/src/components/LanguageSection/LanguageSection.vue @@ -0,0 +1,75 @@ + + + + + diff --git a/src/constants/AccountPropertyConstants.js b/src/constants/AccountPropertyConstants.js new file mode 100644 index 0000000..18e2f58 --- /dev/null +++ b/src/constants/AccountPropertyConstants.js @@ -0,0 +1,41 @@ +/** + * @copyright 2021, Christopher Ng + * + * @author Christopher Ng + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +/* + * SYNC to be kept in sync with `lib/public/Accounts/IAccountManager.php` + */ + +import { translate as t } from '@nextcloud/l10n' + +/** + * Enum of account setting properties + * + * Account setting properties unlike account properties do not support scopes* + */ +export const ACCOUNT_SETTING_PROPERTY_ENUM = Object.freeze({ + LANGUAGE: 'language', +}) + +/** Enum of account setting properties to human readable setting properties */ +export const ACCOUNT_SETTING_PROPERTY_READABLE_ENUM = Object.freeze({ + LANGUAGE: t('simplesettings', 'Language'), +}) diff --git a/src/service/PersonalInfo/PersonalInfoService.js b/src/service/PersonalInfo/PersonalInfoService.js new file mode 100644 index 0000000..6c3f4b8 --- /dev/null +++ b/src/service/PersonalInfo/PersonalInfoService.js @@ -0,0 +1,54 @@ +/** + * @copyright 2021, Christopher Ng + * + * @author Christopher Ng + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +import axios from '@nextcloud/axios' +import { getCurrentUser } from '@nextcloud/auth' +import { generateOcsUrl } from '@nextcloud/router' +import { confirmPassword } from '@nextcloud/password-confirmation' +import '@nextcloud/password-confirmation/dist/style.css' + +/** + * Save the primary account property value for the user + * + * @param {string} accountProperty the account property + * @param {string|boolean} value the primary value + * @return {object} + */ +export const savePrimaryAccountProperty = async (accountProperty, value) => { + // TODO allow boolean values on backend route handler + // Convert boolean to string for compatibility + if (typeof value === 'boolean') { + value = value ? '1' : '0' + } + + const userId = getCurrentUser().uid + const url = generateOcsUrl('cloud/users/{userId}', { userId }) + + await confirmPassword() + + const res = await axios.put(url, { + key: accountProperty, + value, + }) + + return res.data +} diff --git a/src/utils/handlers.js b/src/utils/handlers.js new file mode 100644 index 0000000..076aaf0 --- /dev/null +++ b/src/utils/handlers.js @@ -0,0 +1,48 @@ +/** + * @copyright 2023 Christopher Ng + * + * @author Christopher Ng + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +import { showError } from '@nextcloud/dialogs' +import { translate as t } from '@nextcloud/l10n' + +import logger from '../logger.ts' + +/** + * @param {import('axios').AxiosError} error the error + * @param {string?} message the message to display + */ +export const handleError = (error, message) => { + let fullMessage = '' + + if (message) { + fullMessage += message + } + + if (error.response?.status === 429) { + if (fullMessage) { + fullMessage += '\n' + } + fullMessage += t('settings', 'There were too many requests from your network. Retry later or contact your administrator if this is an error.') + } + + showError(fullMessage) + logger.error(fullMessage || t('Error'), error) +}