[refactor] FollowRequests store

This commit is contained in:
AkiraFukushima 2023-01-03 00:49:49 +09:00
parent f2393b33cb
commit bb527a1840
No known key found for this signature in database
GPG Key ID: B6E51BAC4DE1A957
2 changed files with 44 additions and 33 deletions

View File

@ -7,13 +7,17 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { defineComponent, computed, onMounted } from 'vue' import { defineComponent, computed, onMounted, reactive } from 'vue'
import { ElMessage } from 'element-plus' import { ElMessage } from 'element-plus'
import { useI18next } from 'vue3-i18next' import { useI18next } from 'vue3-i18next'
import { Entity } from 'megalodon' import { Entity } from 'megalodon'
import { useStore } from '@/store' import { useStore } from '@/store'
import User from '@/components/molecules/User.vue' import User from '@/components/molecules/User.vue'
import { ACTION_TYPES } from '@/store/TimelineSpace/Contents/FollowRequests' import { ACTION_TYPES } from '@/store/TimelineSpace/Contents/FollowRequests'
import { LocalAccount } from '~/src/types/localAccount'
import { LocalServer } from '~/src/types/localServer'
import { MyWindow } from '~/src/types/global'
import { useRoute } from 'vue-router'
export default defineComponent({ export default defineComponent({
name: 'follow-requests', name: 'follow-requests',
@ -21,8 +25,17 @@ export default defineComponent({
setup() { setup() {
const space = 'TimelineSpace/Contents/FollowRequests' const space = 'TimelineSpace/Contents/FollowRequests'
const store = useStore() const store = useStore()
const route = useRoute()
const i18n = useI18next() const i18n = useI18next()
const win = (window as any) as MyWindow
const id = computed(() => parseInt(route.params.id as string))
const account = reactive<{ account: LocalAccount | null; server: LocalServer | null }>({
account: null,
server: null
})
const requests = computed(() => store.state.TimelineSpace.Contents.FollowRequests.requests) const requests = computed(() => store.state.TimelineSpace.Contents.FollowRequests.requests)
onMounted(async () => { onMounted(async () => {
@ -30,23 +43,27 @@ export default defineComponent({
}) })
const initialize = async () => { const initialize = async () => {
await store.dispatch(`${space}/${ACTION_TYPES.FETCH_REQUESTS}`).catch(_ => { const [a, s]: [LocalAccount, LocalServer] = await win.ipcRenderer.invoke('get-local-account', id.value)
account.account = a
account.server = s
await store.dispatch(`${space}/${ACTION_TYPES.FETCH_REQUESTS}`, account).catch(_ => {
ElMessage({ ElMessage({
message: i18n.t('message.timeline_fetch_error'), message: i18n.t('message.timeline_fetch_error'),
type: 'error' type: 'error'
}) })
}) })
} }
const accept = (account: Entity.Account) => { const accept = (user: Entity.Account) => {
store.dispatch(`${space}/${ACTION_TYPES.ACCEPT_REQUEST}`, account).catch(_ => { store.dispatch(`${space}/${ACTION_TYPES.ACCEPT_REQUEST}`, { user, account: account.account, server: account.server }).catch(_ => {
ElMessage({ ElMessage({
message: i18n.t('message.follow_request_accept_error'), message: i18n.t('message.follow_request_accept_error'),
type: 'error' type: 'error'
}) })
}) })
} }
const reject = (account: Entity.Account) => { const reject = (user: Entity.Account) => {
store.dispatch(`${space}/${ACTION_TYPES.REJECT_REQUEST}`, account).catch(_ => { store.dispatch(`${space}/${ACTION_TYPES.REJECT_REQUEST}`, { user, account: account.account, server: account.server }).catch(_ => {
ElMessage({ ElMessage({
message: i18n.t('message.follow_request_reject_error'), message: i18n.t('message.follow_request_reject_error'),
type: 'error' type: 'error'

View File

@ -1,6 +1,8 @@
import generator, { Entity } from 'megalodon' import generator, { Entity } from 'megalodon'
import { Module, MutationTree, ActionTree } from 'vuex' import { Module, MutationTree, ActionTree } from 'vuex'
import { RootState } from '@/store' import { RootState } from '@/store'
import { LocalAccount } from '~/src/types/localAccount'
import { LocalServer } from '~/src/types/localServer'
export type FollowRequestsState = { export type FollowRequestsState = {
requests: Array<Entity.Account> requests: Array<Entity.Account>
@ -27,39 +29,31 @@ export const ACTION_TYPES = {
} }
const actions: ActionTree<FollowRequestsState, RootState> = { const actions: ActionTree<FollowRequestsState, RootState> = {
[ACTION_TYPES.FETCH_REQUESTS]: async ({ commit, rootState }): Promise<Array<Entity.Account>> => { [ACTION_TYPES.FETCH_REQUESTS]: async (
const client = generator( { commit, rootState },
rootState.TimelineSpace.server!.sns, req: { account: LocalAccount; server: LocalServer }
rootState.TimelineSpace.server!.baseURL, ): Promise<Array<Entity.Account>> => {
rootState.TimelineSpace.account!.accessToken, const client = generator(req.server.sns, req.server.baseURL, req.account.accessToken, rootState.App.userAgent)
rootState.App.userAgent
)
const res = await client.getFollowRequests() const res = await client.getFollowRequests()
commit(MUTATION_TYPES.UPDATE_REQUESTS, res.data) commit(MUTATION_TYPES.UPDATE_REQUESTS, res.data)
return res.data return res.data
}, },
[ACTION_TYPES.ACCEPT_REQUEST]: async ({ dispatch, rootState }, user: Entity.Account) => { [ACTION_TYPES.ACCEPT_REQUEST]: async (
const client = generator( { dispatch, rootState },
rootState.TimelineSpace.server!.sns, req: { user: Entity.Account; account: LocalAccount; server: LocalServer }
rootState.TimelineSpace.server!.baseURL, ) => {
rootState.TimelineSpace.account!.accessToken, const client = generator(req.server.sns, req.server.baseURL, req.account.accessToken, rootState.App.userAgent)
rootState.App.userAgent const res = await client.acceptFollowRequest(req.user.id)
) await dispatch(ACTION_TYPES.FETCH_REQUESTS, { account: req.account, server: req.server })
const res = await client.acceptFollowRequest(user.id)
await dispatch('fetchRequests')
dispatch('TimelineSpace/SideMenu/fetchFollowRequests', rootState.TimelineSpace.account, { root: true })
return res.data return res.data
}, },
[ACTION_TYPES.REJECT_REQUEST]: async ({ dispatch, rootState }, user: Entity.Account) => { [ACTION_TYPES.REJECT_REQUEST]: async (
const client = generator( { dispatch, rootState },
rootState.TimelineSpace.server!.sns, req: { user: Entity.Account; account: LocalAccount; server: LocalServer }
rootState.TimelineSpace.server!.baseURL, ) => {
rootState.TimelineSpace.account!.accessToken, const client = generator(req.server.sns, req.server.baseURL, req.account.accessToken, rootState.App.userAgent)
rootState.App.userAgent const res = await client.rejectFollowRequest(req.user.id)
) await dispatch(ACTION_TYPES.FETCH_REQUESTS, { action: req.account, server: req.server })
const res = await client.rejectFollowRequest(user.id)
await dispatch('fetchRequests')
dispatch('TimelineSpace/SideMenu/fetchFollowRequests', rootState.TimelineSpace.account, { root: true })
return res.data return res.data
} }
} }