follow requests refactor
This commit is contained in:
parent
e3bc6dbb65
commit
10e3cb588a
12 changed files with 507 additions and 210 deletions
|
|
@ -24,8 +24,6 @@ const followRequestFetcher = ({ credentials }) => {
|
|||
const startFetching = () => {
|
||||
if (interval.value) throw new Error('Interval already exists!')
|
||||
|
||||
fetchAndUpdate()
|
||||
|
||||
interval.value = promiseInterval(fetchAndUpdate, 10000)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,12 +1,19 @@
|
|||
import { defineStore } from 'pinia'
|
||||
|
||||
import followRequestFetcher from 'src/stores/fetchers/follow_requests.js'
|
||||
import { useMergedConfigStore } from 'src/stores/merged_config.js'
|
||||
import { useNotificationsStore } from 'src/stores/notifications.js'
|
||||
import { useOAuthStore } from 'src/stores/oauth.js'
|
||||
|
||||
import { approveUser, denyUser } from 'src/api/user.js'
|
||||
|
||||
export const useFollowRequestsStore = defineStore('followRequests', {
|
||||
state: () => ({
|
||||
fetcher: null,
|
||||
requests: new Map(),
|
||||
showingApproveConfirmDialog: false,
|
||||
showingDenyConfirmDialog: false,
|
||||
tempId: null,
|
||||
}),
|
||||
getters: {
|
||||
followRequestsCount(state) {
|
||||
|
|
@ -14,6 +21,7 @@ export const useFollowRequestsStore = defineStore('followRequests', {
|
|||
},
|
||||
},
|
||||
actions: {
|
||||
// Fetcher stuff
|
||||
startFetching() {
|
||||
if (this.fetcher) throw new Error('Fetcher already exists!')
|
||||
|
||||
|
|
@ -31,8 +39,76 @@ export const useFollowRequestsStore = defineStore('followRequests', {
|
|||
setFollowRequests(requests) {
|
||||
this.requests = new Map(requests.map((user) => [user.id, user]))
|
||||
},
|
||||
remove(id) {
|
||||
|
||||
// Confirm dialogs
|
||||
showApproveConfirmDialog(id) {
|
||||
this.showingApproveConfirmDialog = true
|
||||
this.tempId = id
|
||||
},
|
||||
showDenyConfirmDialog(id) {
|
||||
this.showingDenyConfirmDialog = true
|
||||
this.tempId = id
|
||||
},
|
||||
hideApproveConfirmDialog() {
|
||||
this.showingApproveConfirmDialog = false
|
||||
this.tempId = null
|
||||
},
|
||||
hideDenyConfirmDialog() {
|
||||
this.showingDenyConfirmDialog = false
|
||||
this.tempId = null
|
||||
},
|
||||
|
||||
// Dialog/Instant fork
|
||||
approve(id) {
|
||||
if (useMergedConfigStore().mergedConfig.modalOnApproveFollow) {
|
||||
this.showApproveConfirmDialog(id)
|
||||
} else {
|
||||
this.doApprove(id)
|
||||
}
|
||||
},
|
||||
deny(id) {
|
||||
if (useMergedConfigStore().mergedConfig.modalOnDenyFollow) {
|
||||
this.showDenyConfirmDialog(id)
|
||||
} else {
|
||||
this.doDeny(id)
|
||||
}
|
||||
},
|
||||
|
||||
// Actual calls
|
||||
async doApprove(userId) {
|
||||
const id = userId ?? this.tempId
|
||||
this.hideApproveConfirmDialog()
|
||||
|
||||
await approveUser({
|
||||
id,
|
||||
credentials: useOAuthStore().token,
|
||||
})
|
||||
|
||||
const notifId = this.findFollowRequestNotificationId(id)
|
||||
notifId && useNotificationsStore().markSingleNotificationAsSeen(notifId)
|
||||
this.requests.delete(id)
|
||||
},
|
||||
async doDeny(userId) {
|
||||
const id = userId ?? this.tempId
|
||||
this.hideDenyConfirmDialog()
|
||||
|
||||
await denyUser({
|
||||
id,
|
||||
credentials: useOAuthStore().token,
|
||||
})
|
||||
|
||||
const notifId = this.findFollowRequestNotificationId(id)
|
||||
notifId && useNotificationsStore().markSingleNotificationAsSeen(notifId)
|
||||
this.requests.delete(id)
|
||||
},
|
||||
|
||||
// Utility
|
||||
findFollowRequestNotificationId(userId) {
|
||||
const notif = useNotificationsStore().data.find(
|
||||
(notif) =>
|
||||
notif.from_profile.id === userId && notif.type === 'follow_request',
|
||||
)
|
||||
return notif?.id
|
||||
},
|
||||
},
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue