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