webapp/src/stores/zine/reactions.ts

53 lines
1.6 KiB
TypeScript
Raw Normal View History

2022-11-16 06:33:58 +00:00
import type { Reaction, Shout } from '../../graphql/types.gen'
2022-09-09 11:53:35 +00:00
import { apiClient } from '../../utils/apiClient'
2022-11-16 06:33:58 +00:00
import { createSignal } from 'solid-js'
// TODO: import { roomConnect } from '../../utils/p2p'
2022-09-13 09:59:04 +00:00
2022-11-15 14:24:50 +00:00
export const REACTIONS_AMOUNT_PER_PAGE = 100
2022-11-16 06:33:58 +00:00
const [sortedReactions, setSortedReactions] = createSignal<Reaction[]>([])
const [reactionsByShout, setReactionsByShout] = createSignal<{ [articleSlug: string]: Reaction[] }>({})
2022-11-15 14:24:50 +00:00
2022-11-16 06:33:58 +00:00
export const loadReactionsBy = async ({
by,
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
}: {
2022-11-16 06:33:58 +00:00
by
2022-09-14 11:20:31 +00:00
limit?: number
offset?: number
2022-11-16 06:33:58 +00:00
}): Promise<{ hasMore: boolean }> => {
const data = await apiClient.loadReactionsBy({ by, limit: limit + 1, offset })
const hasMore = data.length === limit + 1
if (hasMore) data.splice(-1)
2022-10-04 10:45:10 +00:00
// TODO: const [data, provider] = roomConnect(articleSlug, username, "reactions")
2022-11-16 06:33:58 +00:00
setSortedReactions(data)
return { hasMore }
2022-09-09 11:53:35 +00:00
}
2022-11-16 06:33:58 +00:00
export const createReaction = async (reaction: Reaction) => {
const { reaction: r } = await apiClient.createReaction({ reaction })
return r
}
export const updateReaction = async (reaction: Reaction) => {
const { reaction: r } = await apiClient.updateReaction({ reaction })
return r
2022-09-09 11:53:35 +00:00
}
2022-11-16 06:33:58 +00:00
export const deleteReaction = async (reactionId: number) => {
const resp = await apiClient.destroyReaction({ id: reactionId })
return resp
}
export const useReactionsStore = (initialState: { reactions?: Reaction[] }) => {
if (initialState.reactions) {
setSortedReactions([...initialState.reactions])
}
2022-09-09 11:53:35 +00:00
2022-11-16 06:33:58 +00:00
return {
reactionsByShout,
sortedReactions,
createReaction,
updateReaction,
deleteReaction,
loadReactionsBy
}
}