Rewrite Modals/AddListMember with composition API

This commit is contained in:
AkiraFukushima 2022-04-28 23:25:21 +09:00
parent 9ddb0f92f1
commit 4b2ffd687d
No known key found for this signature in database
GPG Key ID: B6E51BAC4DE1A957
4 changed files with 68 additions and 45 deletions

View File

@ -4,8 +4,8 @@
<jump v-if="jumpModal"></jump> <jump v-if="jumpModal"></jump>
<image-viewer></image-viewer> <image-viewer></image-viewer>
<list-membership></list-membership> <list-membership></list-membership>
<add-list-member></add-list-member> <add-list-member v-if="addListMemberModal"></add-list-member>
<mute-confirm></mute-confirm> <mute-confirm v-if="muteConfirmModal"></mute-confirm>
<shortcut></shortcut> <shortcut></shortcut>
<report v-if="reportModal"></report> <report v-if="reportModal"></report>
</div> </div>
@ -37,7 +37,9 @@ export default {
computed: { computed: {
...mapState({ ...mapState({
jumpModal: state => state.TimelineSpace.Modals.Jump.modalOpen, jumpModal: state => state.TimelineSpace.Modals.Jump.modalOpen,
reportModal: state => state.TimelineSpace.Modals.Report.modalOpen reportModal: state => state.TimelineSpace.Modals.Report.modalOpen,
muteConfirmModal: state => state.TimelineSpace.Modals.MuteConfirm.modalOpen,
addListMemberModal: state => state.TimelineSpace.Modals.AddListMember.modalOpen
}) })
} }
} }

View File

@ -33,58 +33,68 @@
</div> </div>
</template> </template>
<script> <script lang="ts">
import { mapState } from 'vuex' import { defineComponent, computed, ref } from 'vue'
import { Entity } from 'megalodon'
import { ElMessage } from 'element-plus'
import { useI18next } from 'vue3-i18next'
import { useStore } from '@/store'
import { ACTION_TYPES } from '@/store/TimelineSpace/Modals/AddListMember'
import { ACTION_TYPES as LIST_ACTION_TYPES } from '@/store/TimelineSpace/Contents/Lists/Edit'
export default { export default defineComponent({
name: 'add-list-member', name: 'add-list-member',
data() { setup() {
return { const space = 'TimelineSpace/Modals/AddListMember'
name: '' const store = useStore()
} const i18n = useI18next()
},
computed: { const name = ref<string>('')
...mapState({
loadingBackground: state => state.App.theme.wrapper_mask_color, const loadingBackground = computed(() => store.state.App.theme.wrapper_mask_color)
accounts: state => state.TimelineSpace.Modals.AddListMember.accounts, const accounts = computed(() => store.state.TimelineSpace.Modals.AddListMember.accounts)
listId: state => state.TimelineSpace.Modals.AddListMember.targetListId const listId = computed(() => store.state.TimelineSpace.Modals.AddListMember.targetListId)
}), const addListMemberModal = computed({
addListMemberModal: { get: () => store.state.TimelineSpace.Modals.AddListMember.modalOpen,
get() { set: (value: boolean) => store.dispatch(`${space}/${ACTION_TYPES.CHANGE_MODAL}`, value)
return this.$store.state.TimelineSpace.Modals.AddListMember.modalOpen })
},
set(value) { const username = (account: Entity.Account): string => {
this.$store.dispatch('TimelineSpace/Modals/AddListMember/changeModal', value)
}
}
},
methods: {
username(account) {
if (account.display_name !== '') { if (account.display_name !== '') {
return account.display_name return account.display_name
} else { } else {
return account.username return account.username
} }
}, }
search() { const search = () => {
this.$store.dispatch('TimelineSpace/Modals/AddListMember/search', this.name) store.dispatch(`${space}/${ACTION_TYPES.SEARCH}`, name.value)
}, }
add(user) { const add = (account: Entity.Account) => {
this.$store store
.dispatch('TimelineSpace/Modals/AddListMember/add', user) .dispatch(`${space}/${ACTION_TYPES.ADD}`, account)
.then(() => { .then(() => {
this.addListMemberModal = false store.dispatch(`${space}/${ACTION_TYPES.CHANGE_MODAL}`, false)
this.$store.dispatch('TimelineSpace/Contents/Lists/Edit/fetchMembers', this.listId) store.dispatch(`TimelineSpace/Contents/Lists/Edit/${LIST_ACTION_TYPES.FETCH_MEMBERS}`, listId.value)
}) })
.catch(() => { .catch(() => {
this.$message({ ElMessage({
message: this.$t('message.add_user_error'), message: i18n.t('message.add_user_error'),
type: 'error' type: 'error'
}) })
}) })
} }
return {
name,
loadingBackground,
accounts,
addListMemberModal,
username,
search,
add
}
} }
} })
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>

View File

@ -21,8 +21,13 @@ const mutations: MutationTree<EditState> = {
} }
} }
export const ACTION_TYPES = {
FETCH_MEMBERS: 'fetchMembers',
REMOVE_ACCOUNT: 'removeAccount'
}
const actions: ActionTree<EditState, RootState> = { const actions: ActionTree<EditState, RootState> = {
fetchMembers: async ({ commit, rootState }, listId: string): Promise<Array<Entity.Account>> => { [ACTION_TYPES.FETCH_MEMBERS]: async ({ commit, rootState }, listId: string): Promise<Array<Entity.Account>> => {
const client = generator( const client = generator(
rootState.TimelineSpace.sns, rootState.TimelineSpace.sns,
rootState.TimelineSpace.account.baseURL, rootState.TimelineSpace.account.baseURL,
@ -33,7 +38,7 @@ const actions: ActionTree<EditState, RootState> = {
commit(MUTATION_TYPES.CHANGE_MEMBERS, res.data) commit(MUTATION_TYPES.CHANGE_MEMBERS, res.data)
return res.data return res.data
}, },
removeAccount: async ({ rootState }, remove: RemoveAccountFromList): Promise<{}> => { [ACTION_TYPES.REMOVE_ACCOUNT]: async ({ rootState }, remove: RemoveAccountFromList): Promise<{}> => {
const client = generator( const client = generator(
rootState.TimelineSpace.sns, rootState.TimelineSpace.sns,
rootState.TimelineSpace.account.baseURL, rootState.TimelineSpace.account.baseURL,

View File

@ -32,11 +32,17 @@ const mutations: MutationTree<AddListMemberState> = {
} }
} }
export const ACTION_TYPES = {
CHANGE_MODAL: 'changeModal',
SEARCH: 'search',
ADD: 'add'
}
const actions: ActionTree<AddListMemberState, RootState> = { const actions: ActionTree<AddListMemberState, RootState> = {
changeModal: ({ commit }, value: boolean) => { [ACTION_TYPES.CHANGE_MODAL]: ({ commit }, value: boolean) => {
commit(MUTATION_TYPES.CHANGE_MODAL, value) commit(MUTATION_TYPES.CHANGE_MODAL, value)
}, },
search: async ({ commit, rootState }, name: string): Promise<Array<Entity.Account>> => { [ACTION_TYPES.SEARCH]: async ({ commit, rootState }, name: string): Promise<Array<Entity.Account>> => {
const client = generator( const client = generator(
rootState.TimelineSpace.sns, rootState.TimelineSpace.sns,
rootState.TimelineSpace.account.baseURL, rootState.TimelineSpace.account.baseURL,
@ -47,7 +53,7 @@ const actions: ActionTree<AddListMemberState, RootState> = {
commit(MUTATION_TYPES.UPDATE_ACCOUNTS, res.data) commit(MUTATION_TYPES.UPDATE_ACCOUNTS, res.data)
return res.data return res.data
}, },
add: async ({ state, rootState }, account: Entity.Account): Promise<{}> => { [ACTION_TYPES.ADD]: async ({ state, rootState }, account: Entity.Account): Promise<{}> => {
const client = generator( const client = generator(
rootState.TimelineSpace.sns, rootState.TimelineSpace.sns,
rootState.TimelineSpace.account.baseURL, rootState.TimelineSpace.account.baseURL,