refs #3301 Rewrite Preferences/General with composition API

This commit is contained in:
AkiraFukushima 2022-05-05 18:56:48 +09:00
parent f18ca59c06
commit 5232a0bb00
No known key found for this signature in database
GPG Key ID: B6E51BAC4DE1A957
2 changed files with 86 additions and 79 deletions

View File

@ -40,104 +40,103 @@
</div> </div>
</template> </template>
<script> <script lang="ts">
import { mapState, mapGetters } from 'vuex' import { defineComponent, computed, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useI18next } from 'vue3-i18next'
import { useStore } from '@/store'
import { ACTION_TYPES } from '@/store/Preferences/General'
export default { export default defineComponent({
name: 'general', name: 'general',
computed: { setup() {
...mapState('Preferences/General', { const space = 'Preferences/General'
loading: state => state.loading const store = useStore()
}), const i18n = useI18next()
...mapState({
backgroundColor: state => state.App.theme.background_color const loading = computed(() => store.state.Preferences.General.loading)
}), const backgroundColor = computed(() => store.state.App.theme.background_color)
...mapGetters('Preferences/General', ['notDarwin']), const notDarwin = computed(() => store.getters[`${space}/notDarwin`])
sound_fav_rb: { const sound_fav_rb = computed({
get() { get: () => store.state.Preferences.General.general.sound.fav_rb,
return this.$store.state.Preferences.General.general.sound.fav_rb set: (value: boolean) =>
}, store.dispatch(`${space}/${ACTION_TYPES.UPDATE_SOUND}`, {
set(value) {
this.$store.dispatch('Preferences/General/updateSound', {
fav_rb: value fav_rb: value
}) })
} })
}, const sound_toot = computed({
sound_toot: { get: () => store.state.Preferences.General.general.sound.toot,
get() { set: (value: boolean) =>
return this.$store.state.Preferences.General.general.sound.toot store.dispatch(`${space}/${ACTION_TYPES.UPDATE_SOUND}`, {
},
set(value) {
this.$store.dispatch('Preferences/General/updateSound', {
toot: value toot: value
}) })
} })
}, const timeline_cw = computed({
timeline_cw: { get: () => store.state.Preferences.General.general.timeline.cw,
get() { set: (value: boolean) =>
return this.$store.state.Preferences.General.general.timeline.cw store.dispatch('Preferences/General/updateTimeline', {
},
set(value) {
this.$store.dispatch('Preferences/General/updateTimeline', {
cw: value cw: value
}) })
} })
}, const timeline_nsfw = computed({
timeline_nsfw: { get: () => store.state.Preferences.General.general.timeline.nsfw,
get() { set: (value: boolean) =>
return this.$store.state.Preferences.General.general.timeline.nsfw store.dispatch(`${space}/${ACTION_TYPES.UPDATE_TIMELINE}`, {
},
set(value) {
this.$store.dispatch('Preferences/General/updateTimeline', {
nsfw: value nsfw: value
}) })
} })
}, const timeline_hide_attachments = computed({
timeline_hide_attachments: { get: () => store.state.Preferences.General.general.timeline.hideAllAttachments,
get() { set: (value: boolean) =>
return this.$store.state.Preferences.General.general.timeline.hideAllAttachments store.dispatch(`${space}/${ACTION_TYPES.UPDATE_TIMELINE}`, {
},
set(value) {
this.$store.dispatch('Preferences/General/updateTimeline', {
hideAllAttachments: value hideAllAttachments: value
}) })
} })
}, const other_launch = computed({
other_launch: { get: () => store.state.Preferences.General.general.other.launch,
get() { set: (value: boolean) =>
return this.$store.state.Preferences.General.general.other.launch store.dispatch(`${space}/${ACTION_TYPES.UPDATE_OTHER}`, {
},
set(value) {
this.$store.dispatch('Preferences/General/updateOther', {
launch: value launch: value
}) })
} })
}
}, onMounted(() => {
created() { store.dispatch(`${space}/${ACTION_TYPES.LOAD_GENERAL}`).catch(() => {
this.$store.dispatch('Preferences/General/loadGeneral').catch(() => { ElMessage({
this.$message({ message: i18n.t('message.preferences_load_error'),
message: this.$t('message.preferences_load_error'), type: 'error'
type: 'error' })
}) })
}) })
},
methods: { const reset = () => {
reset() { store
this.$store .dispatch(`${space}/${ACTION_TYPES.RESET}`)
.dispatch('Preferences/General/reset')
.then(language => { .then(language => {
this.$i18n.locale = language i18n.changeLanguage(language)
}) })
.catch(() => { .catch(() => {
this.$message({ ElMessage({
message: this.$t('message.preferences_load_error'), message: i18n.t('message.preferences_load_error'),
type: 'error' type: 'error'
}) })
}) })
} }
return {
loading,
backgroundColor,
notDarwin,
sound_fav_rb,
sound_toot,
timeline_cw,
timeline_nsfw,
timeline_hide_attachments,
other_launch,
reset
}
} }
} })
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>

View File

@ -45,15 +45,23 @@ const mutations: MutationTree<GeneralState> = {
} }
} }
export const ACTION_TYPES = {
LOAD_GENERAL: 'loadGeneral',
UPDATE_SOUND: 'updateSound',
UPDATE_TIMELINE: 'updateTimeline',
UPDATE_OTHER: 'updateOther',
RESET: 'reset'
}
const actions: ActionTree<GeneralState, RootState> = { const actions: ActionTree<GeneralState, RootState> = {
loadGeneral: async ({ commit }) => { [ACTION_TYPES.LOAD_GENERAL]: async ({ commit }) => {
const conf: BaseConfig = await win.ipcRenderer.invoke('get-preferences').finally(() => { const conf: BaseConfig = await win.ipcRenderer.invoke('get-preferences').finally(() => {
commit(MUTATION_TYPES.CHANGE_LOADING, false) commit(MUTATION_TYPES.CHANGE_LOADING, false)
}) })
commit(MUTATION_TYPES.UPDATE_GENERAL, conf.general as General) commit(MUTATION_TYPES.UPDATE_GENERAL, conf.general as General)
return conf return conf
}, },
updateSound: async ({ commit, state }, sound: object) => { [ACTION_TYPES.UPDATE_SOUND]: async ({ commit, state }, sound: object) => {
commit(MUTATION_TYPES.CHANGE_LOADING, true) commit(MUTATION_TYPES.CHANGE_LOADING, true)
const newSound: Sound = Object.assign({}, state.general.sound, sound) const newSound: Sound = Object.assign({}, state.general.sound, sound)
const newGeneral: General = Object.assign({}, toRaw(state.general), { const newGeneral: General = Object.assign({}, toRaw(state.general), {
@ -67,7 +75,7 @@ const actions: ActionTree<GeneralState, RootState> = {
}) })
commit(MUTATION_TYPES.UPDATE_GENERAL, conf.general as General) commit(MUTATION_TYPES.UPDATE_GENERAL, conf.general as General)
}, },
updateTimeline: async ({ commit, state, dispatch }, timeline: object) => { [ACTION_TYPES.UPDATE_TIMELINE]: async ({ commit, state, dispatch }, timeline: object) => {
commit(MUTATION_TYPES.CHANGE_LOADING, true) commit(MUTATION_TYPES.CHANGE_LOADING, true)
const newTimeline: Timeline = Object.assign({}, state.general.timeline, timeline) const newTimeline: Timeline = Object.assign({}, state.general.timeline, timeline)
const newGeneral: General = Object.assign({}, toRaw(state.general), { const newGeneral: General = Object.assign({}, toRaw(state.general), {
@ -82,7 +90,7 @@ const actions: ActionTree<GeneralState, RootState> = {
commit(MUTATION_TYPES.UPDATE_GENERAL, conf.general as General) commit(MUTATION_TYPES.UPDATE_GENERAL, conf.general as General)
dispatch('App/loadPreferences', null, { root: true }) dispatch('App/loadPreferences', null, { root: true })
}, },
updateOther: async ({ commit, state, dispatch }, other: {}) => { [ACTION_TYPES.UPDATE_OTHER]: async ({ commit, state, dispatch }, other: {}) => {
commit(MUTATION_TYPES.CHANGE_LOADING, true) commit(MUTATION_TYPES.CHANGE_LOADING, true)
const newOther: Other = Object.assign({}, state.general.other, other) const newOther: Other = Object.assign({}, state.general.other, other)
const newGeneral: General = Object.assign({}, toRaw(state.general), { const newGeneral: General = Object.assign({}, toRaw(state.general), {
@ -98,7 +106,7 @@ const actions: ActionTree<GeneralState, RootState> = {
dispatch('App/loadPreferences', null, { root: true }) dispatch('App/loadPreferences', null, { root: true })
await win.ipcRenderer.invoke('change-auto-launch', newOther.launch) await win.ipcRenderer.invoke('change-auto-launch', newOther.launch)
}, },
reset: async ({ commit, dispatch }): Promise<string> => { [ACTION_TYPES.RESET]: async ({ commit, dispatch }): Promise<string> => {
commit(MUTATION_TYPES.CHANGE_LOADING, true) commit(MUTATION_TYPES.CHANGE_LOADING, true)
try { try {
const conf: BaseConfig = await win.ipcRenderer.invoke('reset-preferences') const conf: BaseConfig = await win.ipcRenderer.invoke('reset-preferences')