migrate follow requests to separate pinia store
This commit is contained in:
parent
d8825bae33
commit
8a743c648c
17 changed files with 121 additions and 99 deletions
44
src/stores/fetchers/follow_requests.js
Normal file
44
src/stores/fetchers/follow_requests.js
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import { ref } from 'vue'
|
||||
|
||||
import { useFollowRequestsStore } from 'src/stores/follow_requests.js'
|
||||
import { useUsersStore } from 'src/stores/users.js'
|
||||
|
||||
import { fetchFollowRequests } from 'src/api/user.js'
|
||||
import { promiseInterval } from 'src/services/promise_interval/promise_interval.js'
|
||||
|
||||
const followRequestFetcher = ({ credentials }) => {
|
||||
const interval = ref(null)
|
||||
|
||||
const fetchAndUpdate = () => {
|
||||
return fetchFollowRequests({ credentials })
|
||||
.then((result) => {
|
||||
const { data: requests } = result
|
||||
useFollowRequestsStore().setFollowRequests(requests)
|
||||
useUsersStore().addNewUsers(result)
|
||||
})
|
||||
.catch((e) => {
|
||||
console.error(e)
|
||||
})
|
||||
}
|
||||
|
||||
const startFetching = () => {
|
||||
if (interval.value) throw new Error('Interval already exists!')
|
||||
|
||||
fetchAndUpdate()
|
||||
|
||||
interval.value = promiseInterval(fetchAndUpdate, 10000)
|
||||
}
|
||||
|
||||
const stopFetching = () => {
|
||||
interval.value.stop()
|
||||
interval.value = null
|
||||
}
|
||||
|
||||
return {
|
||||
fetchAndUpdate,
|
||||
startFetching,
|
||||
stopFetching,
|
||||
}
|
||||
}
|
||||
|
||||
export default followRequestFetcher
|
||||
Loading…
Add table
Add a link
Reference in a new issue