2022-09-13 09:59:04 +00:00
|
|
|
import { atom, WritableAtom } from 'nanostores'
|
2022-09-09 11:53:35 +00:00
|
|
|
import type { Reaction } from '../../graphql/types.gen'
|
|
|
|
import { useStore } from '@nanostores/solid'
|
|
|
|
import { apiClient } from '../../utils/apiClient'
|
|
|
|
import { reduceBy } from '../../utils/reduce'
|
2022-10-01 08:57:34 +00:00
|
|
|
// import { roomConnect } from '../../utils/p2p'
|
2022-09-13 09:59:04 +00:00
|
|
|
// FIXME
|
|
|
|
|
|
|
|
let reactionsOrdered: WritableAtom<Reaction[]>
|
|
|
|
const reactions = atom<{ [slug: string]: Reaction[] }>({}) // by shout
|
2022-09-09 11:53:35 +00:00
|
|
|
|
|
|
|
export const useReactionsStore = (initial?: Reaction[]) => {
|
|
|
|
if (!reactionsOrdered) {
|
|
|
|
reactionsOrdered = atom(initial || [])
|
|
|
|
reactionsOrdered.listen((rrr: Reaction[]) => reactions.set(reduceBy(rrr, 'shout')))
|
|
|
|
}
|
|
|
|
return useStore(reactionsOrdered)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const loadArticleReactions = async ({
|
|
|
|
articleSlug,
|
2022-09-14 11:20:31 +00:00
|
|
|
limit = 100,
|
|
|
|
offset = 0
|
2022-09-09 11:53:35 +00:00
|
|
|
}: {
|
|
|
|
articleSlug: string
|
2022-09-14 11:20:31 +00:00
|
|
|
limit?: number
|
|
|
|
offset?: number
|
2022-09-09 11:53:35 +00:00
|
|
|
}): Promise<void> => {
|
2022-10-01 08:57:34 +00:00
|
|
|
const data = await apiClient.getArticleReactions({ articleSlug, limit, offset })
|
|
|
|
// TODO: const [data, provider] = roomConnect(articleSlug)
|
|
|
|
reactionsOrdered.set(data)
|
2022-09-09 11:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const loadReactions = async ({
|
|
|
|
shoutSlugs,
|
2022-09-14 11:20:31 +00:00
|
|
|
limit = 100,
|
|
|
|
offset = 0
|
2022-09-09 11:53:35 +00:00
|
|
|
}: {
|
|
|
|
shoutSlugs: string[]
|
2022-09-14 11:20:31 +00:00
|
|
|
limit: number
|
|
|
|
offset: number
|
2022-09-09 11:53:35 +00:00
|
|
|
}): Promise<void> => {
|
2022-09-29 11:50:48 +00:00
|
|
|
const reactionsForShouts = await apiClient.getReactionsForShouts({ shoutSlugs, limit, offset })
|
|
|
|
reactionsOrdered.set(reactionsForShouts)
|
2022-09-09 11:53:35 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 09:59:04 +00:00
|
|
|
export const createReaction = async (reaction: Reaction) =>
|
|
|
|
// FIXME
|
|
|
|
reactionsOrdered.set(await apiClient.createReaction({ reaction }))
|
2022-09-09 11:53:35 +00:00
|
|
|
|
2022-09-13 09:59:04 +00:00
|
|
|
export const updateReaction = async (reaction: Reaction) =>
|
|
|
|
// FIXME
|
|
|
|
reactionsOrdered.set(await apiClient.updateReaction({ reaction }))
|
2022-09-09 11:53:35 +00:00
|
|
|
|
2022-09-13 09:59:04 +00:00
|
|
|
export const deleteReaction = async (reactionId: number) =>
|
|
|
|
// FIXME
|
|
|
|
reactionsOrdered.set(await apiClient.destroyReaction({ id: reactionId }))
|