2022-09-09 11:53:35 +00:00
|
|
|
import { apiClient } from '../../utils/apiClient'
|
|
|
|
import type { Author } from '../../graphql/types.gen'
|
2022-09-22 09:37:49 +00:00
|
|
|
import { byCreated } from '../../utils/sortby'
|
|
|
|
|
|
|
|
import { getLogger } from '../../utils/logger'
|
2022-09-29 17:37:21 +00:00
|
|
|
import { createSignal } from 'solid-js'
|
|
|
|
import { createLazyMemo } from '@solid-primitives/memo'
|
2022-09-22 09:37:49 +00:00
|
|
|
|
|
|
|
const log = getLogger('authors store')
|
2022-09-09 11:53:35 +00:00
|
|
|
|
|
|
|
export type AuthorsSortBy = 'created' | 'name'
|
|
|
|
|
2022-09-28 20:16:44 +00:00
|
|
|
const [sortAllBy, setSortAllBy] = createSignal<AuthorsSortBy>('created')
|
2022-09-09 11:53:35 +00:00
|
|
|
|
2022-09-28 20:16:44 +00:00
|
|
|
const [authorEntities, setAuthorEntities] = createSignal<{ [authorSlug: string]: Author }>({})
|
|
|
|
const [authorsByTopic, setAuthorsByTopic] = createSignal<{ [topicSlug: string]: Author[] }>({})
|
2022-09-13 09:59:04 +00:00
|
|
|
|
2022-09-29 17:37:21 +00:00
|
|
|
const sortedAuthors = createLazyMemo(() => {
|
2022-09-28 20:16:44 +00:00
|
|
|
const authors = Object.values(authorEntities())
|
|
|
|
switch (sortAllBy()) {
|
|
|
|
case 'created': {
|
2022-10-05 11:13:15 +00:00
|
|
|
log.debug('sorted by created')
|
2022-09-28 20:16:44 +00:00
|
|
|
authors.sort(byCreated)
|
|
|
|
break
|
2022-09-09 11:53:35 +00:00
|
|
|
}
|
2022-09-28 20:16:44 +00:00
|
|
|
case 'name': {
|
2022-10-05 11:13:15 +00:00
|
|
|
log.debug('sorted by name')
|
2022-09-28 20:16:44 +00:00
|
|
|
authors.sort((a, b) => a.name.localeCompare(b.name))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return authors
|
|
|
|
})
|
2022-09-13 13:38:26 +00:00
|
|
|
|
2022-09-09 11:53:35 +00:00
|
|
|
const addAuthors = (authors: Author[]) => {
|
|
|
|
const newAuthorEntities = authors.reduce((acc, author) => {
|
|
|
|
acc[author.slug] = author
|
|
|
|
return acc
|
|
|
|
}, {} as Record<string, Author>)
|
|
|
|
|
2022-09-28 20:16:44 +00:00
|
|
|
setAuthorEntities((prevAuthorEntities) => {
|
|
|
|
return {
|
|
|
|
...prevAuthorEntities,
|
2022-09-09 11:53:35 +00:00
|
|
|
...newAuthorEntities
|
2022-09-28 20:16:44 +00:00
|
|
|
}
|
|
|
|
})
|
2022-09-09 11:53:35 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 15:11:14 +00:00
|
|
|
export const loadAuthor = async ({ slug }: { slug: string }): Promise<void> => {
|
|
|
|
// TODO:
|
|
|
|
const articles = await apiClient.getArticlesForAuthors({ authorSlugs: [slug], limit: 1 })
|
|
|
|
const author = articles[0].authors.find((a) => a.slug === slug)
|
|
|
|
addAuthors([author])
|
|
|
|
}
|
|
|
|
|
2022-09-28 20:16:44 +00:00
|
|
|
export const addAuthorsByTopic = (newAuthorsByTopic: { [topicSlug: string]: Author[] }) => {
|
|
|
|
const allAuthors = Object.values(newAuthorsByTopic).flat()
|
2022-09-13 09:59:04 +00:00
|
|
|
addAuthors(allAuthors)
|
|
|
|
|
2022-09-28 20:16:44 +00:00
|
|
|
setAuthorsByTopic((prevAuthorsByTopic) => {
|
|
|
|
return Object.entries(newAuthorsByTopic).reduce((acc, [topicSlug, authors]) => {
|
2022-09-13 09:59:04 +00:00
|
|
|
if (!acc[topicSlug]) {
|
|
|
|
acc[topicSlug] = []
|
|
|
|
}
|
|
|
|
|
|
|
|
authors.forEach((author) => {
|
|
|
|
if (!acc[topicSlug].some((a) => a.slug === author.slug)) {
|
|
|
|
acc[topicSlug].push(author)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return acc
|
2022-09-28 20:16:44 +00:00
|
|
|
}, prevAuthorsByTopic)
|
|
|
|
})
|
2022-09-13 09:59:04 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 11:53:35 +00:00
|
|
|
export const loadAllAuthors = async (): Promise<void> => {
|
|
|
|
const authors = await apiClient.getAllAuthors()
|
|
|
|
addAuthors(authors)
|
|
|
|
}
|
|
|
|
|
2022-09-13 09:59:04 +00:00
|
|
|
type InitialState = {
|
|
|
|
authors?: Author[]
|
|
|
|
}
|
|
|
|
|
2022-09-23 07:38:48 +00:00
|
|
|
export const useAuthorsStore = (initialState: InitialState = {}) => {
|
2022-09-28 20:16:44 +00:00
|
|
|
addAuthors([...(initialState.authors || [])])
|
2022-09-13 09:59:04 +00:00
|
|
|
|
2022-09-28 20:16:44 +00:00
|
|
|
return { authorEntities, sortedAuthors, authorsByTopic }
|
2022-09-09 11:53:35 +00:00
|
|
|
}
|