[refactor] FollowRequests store
This commit is contained in:
parent
f2393b33cb
commit
bb527a1840
|
@ -7,13 +7,17 @@
|
|||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
import { defineComponent, computed, onMounted } from 'vue'
|
||||
import { defineComponent, computed, onMounted, reactive } from 'vue'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { useI18next } from 'vue3-i18next'
|
||||
import { Entity } from 'megalodon'
|
||||
import { useStore } from '@/store'
|
||||
import User from '@/components/molecules/User.vue'
|
||||
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({
|
||||
name: 'follow-requests',
|
||||
|
@ -21,8 +25,17 @@ export default defineComponent({
|
|||
setup() {
|
||||
const space = 'TimelineSpace/Contents/FollowRequests'
|
||||
const store = useStore()
|
||||
const route = useRoute()
|
||||
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)
|
||||
|
||||
onMounted(async () => {
|
||||
|
@ -30,23 +43,27 @@ export default defineComponent({
|
|||
})
|
||||
|
||||
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({
|
||||
message: i18n.t('message.timeline_fetch_error'),
|
||||
type: 'error'
|
||||
})
|
||||
})
|
||||
}
|
||||
const accept = (account: Entity.Account) => {
|
||||
store.dispatch(`${space}/${ACTION_TYPES.ACCEPT_REQUEST}`, account).catch(_ => {
|
||||
const accept = (user: Entity.Account) => {
|
||||
store.dispatch(`${space}/${ACTION_TYPES.ACCEPT_REQUEST}`, { user, account: account.account, server: account.server }).catch(_ => {
|
||||
ElMessage({
|
||||
message: i18n.t('message.follow_request_accept_error'),
|
||||
type: 'error'
|
||||
})
|
||||
})
|
||||
}
|
||||
const reject = (account: Entity.Account) => {
|
||||
store.dispatch(`${space}/${ACTION_TYPES.REJECT_REQUEST}`, account).catch(_ => {
|
||||
const reject = (user: Entity.Account) => {
|
||||
store.dispatch(`${space}/${ACTION_TYPES.REJECT_REQUEST}`, { user, account: account.account, server: account.server }).catch(_ => {
|
||||
ElMessage({
|
||||
message: i18n.t('message.follow_request_reject_error'),
|
||||
type: 'error'
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
import generator, { Entity } from 'megalodon'
|
||||
import { Module, MutationTree, ActionTree } from 'vuex'
|
||||
import { RootState } from '@/store'
|
||||
import { LocalAccount } from '~/src/types/localAccount'
|
||||
import { LocalServer } from '~/src/types/localServer'
|
||||
|
||||
export type FollowRequestsState = {
|
||||
requests: Array<Entity.Account>
|
||||
|
@ -27,39 +29,31 @@ export const ACTION_TYPES = {
|
|||
}
|
||||
|
||||
const actions: ActionTree<FollowRequestsState, RootState> = {
|
||||
[ACTION_TYPES.FETCH_REQUESTS]: async ({ commit, rootState }): Promise<Array<Entity.Account>> => {
|
||||
const client = generator(
|
||||
rootState.TimelineSpace.server!.sns,
|
||||
rootState.TimelineSpace.server!.baseURL,
|
||||
rootState.TimelineSpace.account!.accessToken,
|
||||
rootState.App.userAgent
|
||||
)
|
||||
[ACTION_TYPES.FETCH_REQUESTS]: async (
|
||||
{ commit, rootState },
|
||||
req: { account: LocalAccount; server: LocalServer }
|
||||
): Promise<Array<Entity.Account>> => {
|
||||
const client = generator(req.server.sns, req.server.baseURL, req.account.accessToken, rootState.App.userAgent)
|
||||
const res = await client.getFollowRequests()
|
||||
commit(MUTATION_TYPES.UPDATE_REQUESTS, res.data)
|
||||
return res.data
|
||||
},
|
||||
[ACTION_TYPES.ACCEPT_REQUEST]: async ({ dispatch, rootState }, user: Entity.Account) => {
|
||||
const client = generator(
|
||||
rootState.TimelineSpace.server!.sns,
|
||||
rootState.TimelineSpace.server!.baseURL,
|
||||
rootState.TimelineSpace.account!.accessToken,
|
||||
rootState.App.userAgent
|
||||
)
|
||||
const res = await client.acceptFollowRequest(user.id)
|
||||
await dispatch('fetchRequests')
|
||||
dispatch('TimelineSpace/SideMenu/fetchFollowRequests', rootState.TimelineSpace.account, { root: true })
|
||||
[ACTION_TYPES.ACCEPT_REQUEST]: async (
|
||||
{ dispatch, rootState },
|
||||
req: { user: Entity.Account; account: LocalAccount; server: LocalServer }
|
||||
) => {
|
||||
const client = generator(req.server.sns, req.server.baseURL, req.account.accessToken, rootState.App.userAgent)
|
||||
const res = await client.acceptFollowRequest(req.user.id)
|
||||
await dispatch(ACTION_TYPES.FETCH_REQUESTS, { account: req.account, server: req.server })
|
||||
return res.data
|
||||
},
|
||||
[ACTION_TYPES.REJECT_REQUEST]: async ({ dispatch, rootState }, user: Entity.Account) => {
|
||||
const client = generator(
|
||||
rootState.TimelineSpace.server!.sns,
|
||||
rootState.TimelineSpace.server!.baseURL,
|
||||
rootState.TimelineSpace.account!.accessToken,
|
||||
rootState.App.userAgent
|
||||
)
|
||||
const res = await client.rejectFollowRequest(user.id)
|
||||
await dispatch('fetchRequests')
|
||||
dispatch('TimelineSpace/SideMenu/fetchFollowRequests', rootState.TimelineSpace.account, { root: true })
|
||||
[ACTION_TYPES.REJECT_REQUEST]: async (
|
||||
{ dispatch, rootState },
|
||||
req: { user: Entity.Account; account: LocalAccount; server: LocalServer }
|
||||
) => {
|
||||
const client = generator(req.server.sns, req.server.baseURL, req.account.accessToken, rootState.App.userAgent)
|
||||
const res = await client.rejectFollowRequest(req.user.id)
|
||||
await dispatch(ACTION_TYPES.FETCH_REQUESTS, { action: req.account, server: req.server })
|
||||
return res.data
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue