-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- abstract HeaderBar component Signed-off-by: Christopher Ng <chrng8@gmail.com>
- Loading branch information
Showing
5 changed files
with
290 additions
and
39 deletions.
There are no files selected for viewing
149 changes: 149 additions & 0 deletions
149
apps/settings/src/components/PersonalInfo/LanguageSection/Language.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,149 @@ | ||
<!-- | ||
- @copyright 2021, Christopher Ng <chrng8@gmail.com> | ||
- | ||
- @author Christopher Ng <chrng8@gmail.com> | ||
- | ||
- @license GNU AGPL version 3 or any later version | ||
- | ||
- 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 <http://www.gnu.org/licenses/>. | ||
--> | ||
|
||
<template> | ||
<div class="language"> | ||
<select | ||
id="language" | ||
ref="language" | ||
name="language" | ||
:placeholder="t('settings', 'Language')" | ||
required | ||
@input="onLanguageChange"> | ||
<option | ||
:value="language.code"> | ||
{{ language.name }} | ||
</option> | ||
<option v-for="commonLanguage in commonLanguages" | ||
:key="commonLanguage.code" | ||
:value="commonLanguage.code"> | ||
{{ commonLanguage.name }} | ||
</option> | ||
<optgroup label="––––––––––" /> | ||
<option v-for="otherLanguage in otherLanguages" | ||
:key="otherLanguage.code" | ||
:value="otherLanguage.code"> | ||
{{ otherLanguage.name }} | ||
</option> | ||
</select> | ||
|
||
<a | ||
href="https://www.transifex.com/nextcloud/nextcloud/" | ||
target="_blank" | ||
rel="noreferrer noopener"> | ||
<em>{{ t('settings', 'Help translate') }}</em> | ||
</a> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { showError } from '@nextcloud/dialogs' | ||
|
||
import { saveLanguage } from '../../../service/PersonalInfo/LanguageService' | ||
|
||
export default { | ||
name: 'Language', | ||
|
||
props: { | ||
commonLanguages: { | ||
type: Array, | ||
required: true, | ||
}, | ||
otherLanguages: { | ||
type: Array, | ||
required: true, | ||
}, | ||
language: { | ||
type: Object, | ||
required: true, | ||
}, | ||
}, | ||
|
||
data() { | ||
return { | ||
initialLanguage: this.language, | ||
} | ||
}, | ||
|
||
computed: { | ||
allLanguages() { | ||
return Object.freeze( | ||
[...this.commonLanguages, ...this.otherLanguages] | ||
.reduce((acc, { code, name }) => ({ ...acc, [code]: name }), {}) | ||
) | ||
}, | ||
}, | ||
|
||
methods: { | ||
async onLanguageChange(e) { | ||
const language = this.constructLanguage(e.target.value) | ||
this.$emit('update:language', language) | ||
|
||
if (this.$refs.language?.checkValidity()) { | ||
await this.updateLanguage(language) | ||
} | ||
}, | ||
|
||
async updateLanguage(language) { | ||
try { | ||
const responseData = await saveLanguage(language.code) | ||
this.handleResponse({ | ||
language, | ||
status: responseData.ocs?.meta?.status, | ||
}) | ||
this.reloadPage() | ||
} catch (e) { | ||
this.handleResponse({ | ||
errorMessage: 'Unable to update language', | ||
error: e, | ||
}) | ||
} | ||
}, | ||
|
||
constructLanguage(languageCode) { | ||
return { | ||
code: languageCode, | ||
name: this.allLanguages[languageCode], | ||
} | ||
}, | ||
|
||
handleResponse({ language, status, errorMessage, error }) { | ||
if (status === 'ok') { | ||
// Ensure that local state reflects server state | ||
this.initialLanguage = language | ||
} else { | ||
showError(t('settings', errorMessage)) | ||
this.logger.error(errorMessage, error) | ||
} | ||
}, | ||
|
||
reloadPage() { | ||
location.reload() | ||
}, | ||
}, | ||
} | ||
</script> | ||
|
||
<style lang="scss" scoped> | ||
.language { | ||
display: grid; | ||
} | ||
</style> |
102 changes: 102 additions & 0 deletions
102
apps/settings/src/components/PersonalInfo/LanguageSection/LanguageSection.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,102 @@ | ||
<!-- | ||
- @copyright 2021, Christopher Ng <chrng8@gmail.com> | ||
- | ||
- @author Christopher Ng <chrng8@gmail.com> | ||
- | ||
- @license GNU AGPL version 3 or any later version | ||
- | ||
- 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 <http://www.gnu.org/licenses/>. | ||
--> | ||
|
||
<template> | ||
<form | ||
ref="form" | ||
class="section" | ||
@submit.stop.prevent="() => {}"> | ||
<HeaderBar | ||
:account-property="accountProperty" | ||
label-for="language" | ||
:is-valid-form="isValidForm" /> | ||
|
||
<template v-if="isEditable"> | ||
<Language | ||
:common-languages="commonLanguages" | ||
:other-languages="otherLanguages" | ||
:language.sync="language" | ||
@update:language="onUpdateLanguage" /> | ||
</template> | ||
|
||
<span v-else> | ||
{{ t('settings', 'No language set') }} | ||
</span> | ||
</form> | ||
</template> | ||
|
||
<script> | ||
import { loadState } from '@nextcloud/initial-state' | ||
|
||
import Language from './Language' | ||
import HeaderBar from '../shared/HeaderBar' | ||
|
||
import { SETTING_PROPERTY_READABLE_ENUM } from '../../../constants/AccountPropertyConstants' | ||
|
||
const { languages: { activeLanguage, commonLanguages, otherLanguages } } = loadState('settings', 'personalInfoParameters', {}) | ||
|
||
export default { | ||
name: 'LanguageSection', | ||
|
||
components: { | ||
Language, | ||
HeaderBar, | ||
}, | ||
|
||
data() { | ||
return { | ||
accountProperty: SETTING_PROPERTY_READABLE_ENUM.LANGUAGE, | ||
isValidForm: true, | ||
commonLanguages, | ||
otherLanguages, | ||
language: activeLanguage, | ||
} | ||
}, | ||
|
||
computed: { | ||
isEditable() { | ||
return Boolean(this.language) | ||
}, | ||
}, | ||
|
||
mounted() { | ||
this.$nextTick(() => this.updateFormValidity()) | ||
}, | ||
|
||
methods: { | ||
onUpdateLanguage() { | ||
this.$nextTick(() => this.updateFormValidity()) | ||
}, | ||
|
||
updateFormValidity() { | ||
this.isValidForm = this.$refs.form?.checkValidity() | ||
}, | ||
}, | ||
} | ||
</script> | ||
|
||
<style lang="scss" scoped> | ||
form::v-deep button { | ||
&:disabled { | ||
cursor: default; | ||
} | ||
} | ||
</style> |
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
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.