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 }> => {
|
2022-11-18 02:23:04 +00:00
|
|
|
const data = await apiClient.getReactionsBy({ by, limit: limit + 1, offset })
|
2022-11-16 06:33:58 +00:00
|
|
|
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 })
|
2022-11-16 06:41:26 +00:00
|
|
|
console.debug(resp)
|
2022-11-16 06:33:58 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|