webapp/src/stores/zine/reactions.ts

64 lines
1.9 KiB
TypeScript
Raw Normal View History

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
2022-11-15 14:24:50 +00:00
export const REACTIONS_AMOUNT_PER_PAGE = 100
2022-09-13 09:59:04 +00:00
let reactionsOrdered: WritableAtom<Reaction[]>
2022-10-05 08:55:26 +00:00
export 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-11-15 14:24:50 +00:00
limit = REACTIONS_AMOUNT_PER_PAGE,
2022-09-14 11:20:31 +00:00
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-11-15 14:24:50 +00:00
const data = await apiClient.loadReactionsBy({ by: { shout: articleSlug }, amount: limit, offset })
2022-10-04 10:45:10 +00:00
// TODO: const [data, provider] = roomConnect(articleSlug, username, "reactions")
2022-10-01 08:57:34 +00:00
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-11-15 14:24:50 +00:00
const reactionsForShouts = await apiClient.loadReactionsBy({
by: { shouts: shoutSlugs },
amount: limit,
offset
})
2022-09-29 11:50:48 +00:00
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 }))