From 603ebbb4a555429136241f0035cdf0812ed9a369 Mon Sep 17 00:00:00 2001 From: Untone Date: Wed, 5 Jun 2024 19:11:48 +0300 Subject: [PATCH] session-patch --- .../ProfileSettings/ProfileSettings.tsx | 4 +-- src/context/session.tsx | 29 +++---------------- src/graphql/client/core.ts | 6 ---- src/graphql/query/core/author-id.ts | 26 ----------------- 4 files changed, 6 insertions(+), 59 deletions(-) delete mode 100644 src/graphql/query/core/author-id.ts diff --git a/src/components/ProfileSettings/ProfileSettings.tsx b/src/components/ProfileSettings/ProfileSettings.tsx index a74597a5..6ced2cd6 100644 --- a/src/components/ProfileSettings/ProfileSettings.tsx +++ b/src/components/ProfileSettings/ProfileSettings.tsx @@ -57,7 +57,7 @@ export const ProfileSettings = () => { const [nameError, setNameError] = createSignal() const { form, submit, updateFormField, setForm } = useProfileForm() const { showSnackbar } = useSnackbar() - const { loadAuthor, session } = useSession() + const { loadSession, session } = useSession() const { showConfirm } = useConfirm() const [clearAbout, setClearAbout] = createSignal(false) @@ -112,7 +112,7 @@ export const ProfileSettings = () => { setIsSaving(false) } - await loadAuthor() // renews author's profile + setTimeout(loadSession, 5000) // renews author's profile } const handleCancel = async () => { diff --git a/src/context/session.tsx b/src/context/session.tsx index e4dc15d3..aa2259aa 100644 --- a/src/context/session.tsx +++ b/src/context/session.tsx @@ -47,13 +47,11 @@ const defaultConfig: ConfigType = { export type SessionContextType = { config: Accessor session: Resource - author: Resource + author: Accessor authError: Accessor isSessionLoaded: Accessor loadSession: () => AuthToken | Promise setSession: (token: AuthToken | null) => void // setSession - loadAuthor: (info?: unknown) => Author | Promise - setAuthor: (a: Author) => void requireAuthentication: ( callback: (() => Promise) | (() => void), modalSource: AuthModalSource, @@ -205,6 +203,7 @@ export const SessionProvider = (props: { ssrLoadFrom: 'initial', initialValue: null, }) + const author = createMemo(() => session().user?.app_data?.profile) const checkSessionIsExpired = () => { const expires_at_data = localStorage.getItem('expires_at') @@ -226,15 +225,6 @@ export const SessionProvider = (props: { onCleanup(() => clearTimeout(minuteLater)) - const authorData = async () => { - const u = session()?.user - return u ? (await apiClient.getAuthorId({ user: u.id.trim() })) || null : null - } - const [author, { refetch: loadAuthor, mutate: setAuthor }] = createResource(authorData, { - ssrLoadFrom: 'initial', - initialValue: null, - }) - // when session is loaded createEffect( on( @@ -249,16 +239,8 @@ export const SessionProvider = (props: { } try { - const appdata = session()?.user.app_data - if (appdata) { - const { profile } = appdata - if (profile?.id) { - setAuthor(profile) - addAuthors([profile]) - } else { - setTimeout(loadAuthor, 15) - } - } + const profile = session()?.user?.app_data?.profile + if (profile?.id) addAuthors([profile]) } catch (e) { console.error(e) } @@ -274,7 +256,6 @@ export const SessionProvider = (props: { const reset = () => { setIsSessionLoaded(true) setSession(null) - setAuthor(null) } // initial effect @@ -406,9 +387,7 @@ export const SessionProvider = (props: { updateProfile, setIsSessionLoaded, setSession, - setAuthor, authorizer, - loadAuthor, forgotPassword, changePassword, oauth, diff --git a/src/graphql/client/core.ts b/src/graphql/client/core.ts index cb9f4719..2512b771 100644 --- a/src/graphql/client/core.ts +++ b/src/graphql/client/core.ts @@ -40,7 +40,6 @@ 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 authorsLoadBy from '../query/core/authors-load-by' import reactionsLoadBy from '../query/core/reactions-load-by' @@ -121,11 +120,6 @@ export const apiClient = { return response.data.get_author }, - getAuthorId: async (params: { user: string }): Promise => { - const response = await publicGraphQLClient.query(authorId, params).toPromise() - return response.data.get_author_id - }, - getAuthorFollowers: async ({ slug }: { slug: string }): Promise => { const response = await publicGraphQLClient.query(authorFollowers, { slug }).toPromise() return response.data.get_author_followers diff --git a/src/graphql/query/core/author-id.ts b/src/graphql/query/core/author-id.ts deleted file mode 100644 index 8c621479..00000000 --- a/src/graphql/query/core/author-id.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { gql } from '@urql/core' - -export default gql` - query GetAuthorId($user: String!) { - get_author_id(user: $user) { - id - slug - name - bio - about - pic - links - created_at - last_seen - stat { - shouts - authors - followers - rating - comments - rating_shouts - rating_comments - } - } - } -`