api update
This commit is contained in:
parent
49f7d4efc1
commit
9470175535
|
@ -71,13 +71,7 @@ export const AuthorView = (props: Props) => {
|
|||
const fetchData = async (slug) => {
|
||||
try {
|
||||
const [subscriptionsResult, followersResult] = await Promise.all([
|
||||
(async () => {
|
||||
const [getAuthors, getTopics] = await Promise.all([
|
||||
apiClient.getAuthorFollowingAuthors({ slug }),
|
||||
apiClient.getAuthorFollowingTopics({ slug }),
|
||||
])
|
||||
return { authors: getAuthors, topics: getTopics }
|
||||
})(),
|
||||
apiClient.getAuthorFollows({ slug }),
|
||||
apiClient.getAuthorFollowers({ slug }),
|
||||
])
|
||||
|
||||
|
|
|
@ -69,10 +69,12 @@ export const HomeView = (props: Props) => {
|
|||
}
|
||||
|
||||
const result = await apiClient.getRandomTopicShouts(RANDOM_TOPIC_SHOUTS_COUNT)
|
||||
if (!result) console.warn('[apiClient.getRandomTopicShouts] failed')
|
||||
if (!result || result.error) console.warn('[apiClient.getRandomTopicShouts] failed')
|
||||
batch(() => {
|
||||
if (result?.topic) setRandomTopic(result.topic)
|
||||
if (result?.shouts) setRandomTopicArticles(result.shouts)
|
||||
if (!result?.error) {
|
||||
if (result?.topic) setRandomTopic(result.topic)
|
||||
if (result?.shouts) setRandomTopicArticles(result.shouts)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
|
|
|
@ -2,20 +2,14 @@ import { Accessor, JSX, createContext, createEffect, createSignal, useContext }
|
|||
import { createStore } from 'solid-js/store'
|
||||
|
||||
import { apiClient } from '../graphql/client/core'
|
||||
import { Author, Community, FollowingEntity, Topic } from '../graphql/schema/core.gen'
|
||||
import { AuthorFollows, FollowingEntity } from '../graphql/schema/core.gen'
|
||||
|
||||
import { useSession } from './session'
|
||||
|
||||
type SubscriptionsData = {
|
||||
topics?: Topic[]
|
||||
authors?: Author[]
|
||||
communities?: Community[]
|
||||
}
|
||||
|
||||
interface FollowingContextType {
|
||||
loading: Accessor<boolean>
|
||||
subscriptions: SubscriptionsData
|
||||
setSubscriptions: (subscriptions: SubscriptionsData) => void
|
||||
subscriptions: AuthorFollows
|
||||
setSubscriptions: (subscriptions: AuthorFollows) => void
|
||||
setFollowing: (what: FollowingEntity, slug: string, value: boolean) => void
|
||||
loadSubscriptions: () => void
|
||||
follow: (what: FollowingEntity, slug: string) => Promise<void>
|
||||
|
@ -29,23 +23,24 @@ export function useFollowing() {
|
|||
return useContext(FollowingContext)
|
||||
}
|
||||
|
||||
const EMPTY_SUBSCRIPTIONS = {
|
||||
const EMPTY_SUBSCRIPTIONS: AuthorFollows = {
|
||||
topics: [],
|
||||
authors: [],
|
||||
shouts: [],
|
||||
communities: [],
|
||||
}
|
||||
|
||||
export const FollowingProvider = (props: { children: JSX.Element }) => {
|
||||
const [loading, setLoading] = createSignal<boolean>(false)
|
||||
const [subscriptions, setSubscriptions] = createStore<SubscriptionsData>(EMPTY_SUBSCRIPTIONS)
|
||||
const { author } = useSession()
|
||||
const [subscriptions, setSubscriptions] = createStore<AuthorFollows>(EMPTY_SUBSCRIPTIONS)
|
||||
const { author, session } = useSession()
|
||||
|
||||
const fetchData = async () => {
|
||||
setLoading(true)
|
||||
try {
|
||||
if (apiClient.private) {
|
||||
console.debug('[context.following] fetching subs data...')
|
||||
const result = await apiClient.getMySubscriptions()
|
||||
const result = await apiClient.getAuthorFollows({ user: session()?.user.id })
|
||||
setSubscriptions(result || EMPTY_SUBSCRIPTIONS)
|
||||
console.info('[context.following] subs:', subscriptions)
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import type {
|
||||
Author,
|
||||
AuthorFollows,
|
||||
CommonResult,
|
||||
Community,
|
||||
FollowingEntity,
|
||||
LoadShoutsOptions,
|
||||
MutationDelete_ShoutArgs,
|
||||
|
@ -37,16 +37,14 @@ import shoutsLoadSearch from '../query/core/articles-load-search'
|
|||
import loadShoutsUnrated from '../query/core/articles-load-unrated'
|
||||
import authorBy from '../query/core/author-by'
|
||||
import authorFollowers from '../query/core/author-followers'
|
||||
import authorFollows from '../query/core/author-follows'
|
||||
import authorId from '../query/core/author-id'
|
||||
import authorsAll from '../query/core/authors-all'
|
||||
import authorFollowedAuthors from '../query/core/authors-followed-by'
|
||||
import authorsLoadBy from '../query/core/authors-load-by'
|
||||
import authorFollowedCommunities from '../query/core/communities-followed-by'
|
||||
import mySubscriptions from '../query/core/my-followed'
|
||||
import reactionsLoadBy from '../query/core/reactions-load-by'
|
||||
import topicBySlug from '../query/core/topic-by-slug'
|
||||
import topicsAll from '../query/core/topics-all'
|
||||
import authorFollowedTopics from '../query/core/topics-followed-by'
|
||||
import topicsRandomQuery from '../query/core/topics-random'
|
||||
|
||||
const publicGraphQLClient = createGraphQLClient('core')
|
||||
|
@ -86,7 +84,7 @@ export const apiClient = {
|
|||
return response.data.get_topics_random
|
||||
},
|
||||
|
||||
getRandomTopicShouts: async (limit: number): Promise<{ topic: Topic; shouts: Shout[] }> => {
|
||||
getRandomTopicShouts: async (limit: number): Promise<CommonResult> => {
|
||||
const resp = await publicGraphQLClient.query(articlesLoadRandomTopic, { limit }).toPromise()
|
||||
if (!resp.data) console.error('[graphql.client.core] load_shouts_random_topic', resp)
|
||||
return resp.data.load_shouts_random_topic
|
||||
|
@ -96,6 +94,7 @@ export const apiClient = {
|
|||
const response = await apiClient.private.mutation(followMutation, { what, slug }).toPromise()
|
||||
return response.data.follow
|
||||
},
|
||||
|
||||
unfollow: async ({ what, slug }: { what: FollowingEntity; slug: string }) => {
|
||||
const response = await apiClient.private.mutation(unfollowMutation, { what, slug }).toPromise()
|
||||
return response.data.unfollow
|
||||
|
@ -107,48 +106,53 @@ export const apiClient = {
|
|||
|
||||
return response.data.get_topics_all
|
||||
},
|
||||
|
||||
getAllAuthors: async () => {
|
||||
const response = await publicGraphQLClient.query(authorsAll, {}).toPromise()
|
||||
if (!response.data) console.error('[graphql.client.core] getAllAuthors', response)
|
||||
|
||||
return response.data.get_authors_all
|
||||
},
|
||||
|
||||
getAuthor: async (params: { slug?: string; author_id?: number }): Promise<Author> => {
|
||||
const response = await publicGraphQLClient.query(authorBy, params).toPromise()
|
||||
return response.data.get_author
|
||||
},
|
||||
|
||||
getAuthorId: async (params: { user: string }): Promise<Author> => {
|
||||
const response = await publicGraphQLClient.query(authorId, params).toPromise()
|
||||
return response.data.get_author_id
|
||||
},
|
||||
|
||||
getAuthorFollowers: async ({ slug }: { slug: string }): Promise<Author[]> => {
|
||||
const response = await publicGraphQLClient.query(authorFollowers, { slug }).toPromise()
|
||||
return response.data.get_author_followers
|
||||
},
|
||||
getAuthorFollowingAuthors: async ({ slug }: { slug: string }): Promise<Author[]> => {
|
||||
const response = await publicGraphQLClient.query(authorFollowedAuthors, { slug }).toPromise()
|
||||
return response.data.get_author_followed
|
||||
},
|
||||
getAuthorFollowingTopics: async ({ slug }: { slug: string }): Promise<Topic[]> => {
|
||||
const response = await publicGraphQLClient.query(authorFollowedTopics, { slug }).toPromise()
|
||||
return response.data.get_topics_by_author
|
||||
},
|
||||
getAuthorFollowingCommunities: async ({ slug }: { slug: string }): Promise<Community[]> => {
|
||||
const response = await publicGraphQLClient.query(authorFollowedCommunities, { slug }).toPromise()
|
||||
return response.data.get_communities_by_author
|
||||
|
||||
getAuthorFollows: async (params: {
|
||||
slug?: string
|
||||
author_id?: number
|
||||
user?: string
|
||||
}): Promise<AuthorFollows> => {
|
||||
const response = await publicGraphQLClient.query(authorFollows, params).toPromise()
|
||||
return response.data.get_author_follows
|
||||
},
|
||||
|
||||
updateAuthor: async (input: ProfileInput) => {
|
||||
const response = await apiClient.private.mutation(updateAuthor, { profile: input }).toPromise()
|
||||
return response.data.update_author
|
||||
},
|
||||
|
||||
getTopic: async ({ slug }: { slug: string }): Promise<Topic> => {
|
||||
const response = await publicGraphQLClient.query(topicBySlug, { slug }).toPromise()
|
||||
return response.data.get_topic
|
||||
},
|
||||
|
||||
createArticle: async ({ article }: { article: ShoutInput }): Promise<Shout> => {
|
||||
const response = await apiClient.private.mutation(createArticle, { shout: article }).toPromise()
|
||||
return response.data.create_shout.shout
|
||||
},
|
||||
|
||||
updateArticle: async ({
|
||||
shout_id,
|
||||
shout_input,
|
||||
|
@ -164,10 +168,12 @@ export const apiClient = {
|
|||
console.debug('[graphql.client.core] updateArticle:', response.data)
|
||||
return response.data.update_shout.shout
|
||||
},
|
||||
|
||||
deleteShout: async (params: MutationDelete_ShoutArgs): Promise<void> => {
|
||||
const response = await apiClient.private.mutation(deleteShout, params).toPromise()
|
||||
console.debug('[graphql.client.core] deleteShout:', response)
|
||||
},
|
||||
|
||||
getDrafts: async (): Promise<Shout[]> => {
|
||||
const response = await apiClient.private.query(draftsLoad, {}).toPromise()
|
||||
console.debug('[graphql.client.core] getDrafts:', response)
|
||||
|
@ -233,9 +239,4 @@ export const apiClient = {
|
|||
.toPromise()
|
||||
return resp.data.load_reactions_by
|
||||
},
|
||||
getMySubscriptions: async (): Promise<CommonResult> => {
|
||||
const resp = await apiClient.private.query(mySubscriptions, {}).toPromise()
|
||||
|
||||
return resp.data.get_my_followed
|
||||
},
|
||||
}
|
||||
|
|
54
src/graphql/query/core/author-follows.ts
Normal file
54
src/graphql/query/core/author-follows.ts
Normal file
|
@ -0,0 +1,54 @@
|
|||
import { gql } from '@urql/core'
|
||||
|
||||
export default gql`
|
||||
query GetAuthorFollows($slug: String, $user: String, $author_id: Int) {
|
||||
get_author_follows(slug: $slug, user: $user, author_id: $author_id) {
|
||||
authors {
|
||||
id
|
||||
slug
|
||||
name
|
||||
pic
|
||||
bio
|
||||
stat {
|
||||
shouts
|
||||
followers
|
||||
}
|
||||
}
|
||||
topics {
|
||||
id
|
||||
slug
|
||||
title
|
||||
stat {
|
||||
shouts
|
||||
followers
|
||||
}
|
||||
}
|
||||
shouts {
|
||||
id
|
||||
slug
|
||||
title
|
||||
subtitle
|
||||
main_topic
|
||||
authors {
|
||||
id
|
||||
name
|
||||
slug
|
||||
pic
|
||||
}
|
||||
stat {
|
||||
viewed
|
||||
rating
|
||||
commented
|
||||
}
|
||||
created_at
|
||||
updated_at
|
||||
}
|
||||
communities {
|
||||
id
|
||||
slug
|
||||
name
|
||||
pic
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
|
@ -1,17 +0,0 @@
|
|||
import { gql } from '@urql/core'
|
||||
|
||||
export default gql`
|
||||
query AuthorsFollowedByQuery($slug: String, $user: String, $author_id: Int) {
|
||||
get_author_followed(slug: $slug, user: $user, author_id: $author_id) {
|
||||
id
|
||||
slug
|
||||
name
|
||||
pic
|
||||
bio
|
||||
created_at
|
||||
stat {
|
||||
shouts
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
Loading…
Reference in New Issue
Block a user