mapped topics store

This commit is contained in:
tonyrewin 2022-09-16 10:45:56 +03:00
parent 55ccd419e2
commit 20bc122bc4

View File

@ -1,27 +1,25 @@
import { apiClient } from '../../utils/apiClient' import { apiClient } from '../../utils/apiClient'
import type { ReadableAtom, WritableAtom } from 'nanostores' import { map, MapStore, ReadableAtom, WritableAtom, atom, computed } from 'nanostores'
import { atom, computed } from 'nanostores'
import type { Topic } from '../../graphql/types.gen' import type { Topic } from '../../graphql/types.gen'
import { useStore } from '@nanostores/solid' import { useStore } from '@nanostores/solid'
import { byCreated, byStat } from '../../utils/sortby' import { byCreated, byStat } from '../../utils/sortby'
import type { AuthorsSortBy } from './authors'
export type TopicsSortBy = 'created' | 'name' export type TopicsSortBy = 'created' | 'name'
const sortAllByStore = atom<TopicsSortBy>('created') const sortAllByStore = atom<TopicsSortBy>('created')
let topicEntitiesStore: WritableAtom<{ [topicSlug: string]: Topic }> let topicEntitiesStore: MapStore<Record<string, Topic>>
let sortedTopicsStore: ReadableAtom<Topic[]> let sortedTopicsStore: ReadableAtom<Topic[]>
let topTopicsStore: ReadableAtom<Topic[]> let topTopicsStore: ReadableAtom<Topic[]>
let randomTopicsStore: WritableAtom<Topic[]> let randomTopicsStore: WritableAtom<Topic[]>
let topicsByAuthorStore: WritableAtom<{ [authorSlug: string]: Topic[] }> let topicsByAuthorStore: MapStore<Record<string, Topic[]>>
const initStore = (initial?: Record<string, Topic>) => { const initStore = (initial?: Record<string, Topic>) => {
if (topicEntitiesStore) { if (topicEntitiesStore) {
return return
} }
topicEntitiesStore = atom<Record<string, Topic>>(initial) topicEntitiesStore = map<Record<string, Topic>>(initial)
sortedTopicsStore = computed([topicEntitiesStore, sortAllByStore], (topicEntities, sortBy) => { sortedTopicsStore = computed([topicEntitiesStore, sortAllByStore], (topicEntities, sortBy) => {
const topics = Object.values(topicEntities) const topics = Object.values(topicEntities)
@ -76,7 +74,7 @@ export const addTopicsByAuthor = (topicsByAuthors: { [authorSlug: string]: Topic
addTopics(allTopics) addTopics(allTopics)
if (!topicsByAuthorStore) { if (!topicsByAuthorStore) {
topicsByAuthorStore = atom<{ [authorSlug: string]: Topic[] }>(topicsByAuthors) topicsByAuthorStore = map<Record<string, Topic[]>>(topicsByAuthors)
} else { } else {
const newState = Object.entries(topicsByAuthors).reduce((acc, [authorSlug, topics]) => { const newState = Object.entries(topicsByAuthors).reduce((acc, [authorSlug, topics]) => {
if (!acc[authorSlug]) { if (!acc[authorSlug]) {
@ -107,10 +105,18 @@ type InitialState = {
} }
export const useTopicsStore = ({ topics, randomTopics }: InitialState = {}) => { export const useTopicsStore = ({ topics, randomTopics }: InitialState = {}) => {
addTopics(topics, randomTopics) console.log('using topics store')
if (topics) {
addTopics(topics)
console.log('topics added')
}
if (randomTopics) {
addTopics(randomTopics)
console.log('random topics added')
}
if (!randomTopicsStore) { if (!randomTopicsStore) {
randomTopicsStore = atom(randomTopics) randomTopicsStore = atom(randomTopics)
console.log('random topics separate store')
} }
const getTopicEntities = useStore(topicEntitiesStore) const getTopicEntities = useStore(topicEntitiesStore)