From dfbbb075e89ce5c63ab3ae3d805d08869b8ed44b Mon Sep 17 00:00:00 2001 From: Untone Date: Thu, 14 Dec 2023 02:56:44 +0300 Subject: [PATCH] prefixed-queries --- .../Author/AuthorBadge/AuthorBadge.tsx | 4 +- .../Author/AuthorCard/AuthorCard.tsx | 1 - .../Nav/AuthModal/ResetPasswordForm.tsx | 0 src/components/Nav/HeaderAuth.tsx | 4 +- src/components/Nav/ProfilePopup.tsx | 8 +-- .../ProfileSettings/ProfileSettings.tsx | 12 ++-- src/context/connect.tsx | 55 +++++++++---------- src/context/notifications.tsx | 6 +- src/context/session.tsx | 2 +- src/graphql/client/core.ts | 7 ++- src/graphql/query/core/author-by.ts | 4 +- src/graphql/query/core/author-id.ts | 17 ++++++ src/graphql/query/core/query.json | 9 +++ .../query/notifier/notifications-load.ts | 6 +- 14 files changed, 81 insertions(+), 54 deletions(-) create mode 100644 src/components/Nav/AuthModal/ResetPasswordForm.tsx create mode 100644 src/graphql/query/core/author-id.ts create mode 100644 src/graphql/query/core/query.json diff --git a/src/components/Author/AuthorBadge/AuthorBadge.tsx b/src/components/Author/AuthorBadge/AuthorBadge.tsx index 7b5f6489..6b13c40a 100644 --- a/src/components/Author/AuthorBadge/AuthorBadge.tsx +++ b/src/components/Author/AuthorBadge/AuthorBadge.tsx @@ -25,7 +25,7 @@ type Props = { export const AuthorBadge = (props: Props) => { const [isSubscribing, setIsSubscribing] = createSignal(false) const { - session, + author, subscriptions, actions: { loadSubscriptions, requireAuthentication }, } = useSession() @@ -96,7 +96,7 @@ export const AuthorBadge = (props: Props) => { - +
{ const { t, lang } = useLocalize() const { - session, author, subscriptions, isSessionLoaded, diff --git a/src/components/Nav/AuthModal/ResetPasswordForm.tsx b/src/components/Nav/AuthModal/ResetPasswordForm.tsx new file mode 100644 index 00000000..e69de29b diff --git a/src/components/Nav/HeaderAuth.tsx b/src/components/Nav/HeaderAuth.tsx index 5bf7c96c..5e25993d 100644 --- a/src/components/Nav/HeaderAuth.tsx +++ b/src/components/Nav/HeaderAuth.tsx @@ -32,7 +32,7 @@ const MD_WIDTH_BREAKPOINT = 992 export const HeaderAuth = (props: Props) => { const { t } = useLocalize() const { page } = useRouter() - const { author, session, isSessionLoaded, isAuthenticated } = useSession() + const { author, isAuthenticated, isSessionLoaded } = useSession() const { unreadNotificationsCount, actions: { showNotificationsPanel }, @@ -147,7 +147,7 @@ export const HeaderAuth = (props: Props) => {
- +
diff --git a/src/components/Nav/ProfilePopup.tsx b/src/components/Nav/ProfilePopup.tsx index 3427fd52..56f1a5e3 100644 --- a/src/components/Nav/ProfilePopup.tsx +++ b/src/components/Nav/ProfilePopup.tsx @@ -13,7 +13,7 @@ type ProfilePopupProps = Omit export const ProfilePopup = (props: ProfilePopupProps) => { const { - user, + author, actions: { signOut }, } = useSession() @@ -23,18 +23,18 @@ export const ProfilePopup = (props: ProfilePopupProps) => {
  • - {t('Profile')} + {t('Profile')}
  • {t('Drafts')}
  • - + {t('Subscriptions')}
  • - {t('Comments')} + {t('Comments')}
  • {t('Bookmarks')} diff --git a/src/components/ProfileSettings/ProfileSettings.tsx b/src/components/ProfileSettings/ProfileSettings.tsx index c0910595..210617fa 100644 --- a/src/components/ProfileSettings/ProfileSettings.tsx +++ b/src/components/ProfileSettings/ProfileSettings.tsx @@ -177,18 +177,18 @@ export const ProfileSettings = () => {

    {t('Userpic')}

    - +
    { @@ -219,7 +219,7 @@ export const ProfileSettings = () => {
    - + {t('Here you can upload your photo')} diff --git a/src/context/connect.tsx b/src/context/connect.tsx index 0419adc6..7bac4df7 100644 --- a/src/context/connect.tsx +++ b/src/context/connect.tsx @@ -39,38 +39,33 @@ export const ConnectProvider = (props: { children: JSX.Element }) => { } const [retried, setRetried] = createSignal(0) const listen = () => { - if (isAuthenticated() && !connected() && retried() < 4) { - try { - fetchEventSource('https://connect.discours.io', { - method: 'GET', - headers: { - 'Content-Type': 'application/json', - Authorization: getToken(), - }, - onmessage(event) { - const m: SSEMessage = JSON.parse(event.data) - console.log('[context.connect] Received message:', m) + const token = getToken() + console.log(`[context.connect] token: ${token}`) + if (token && !connected() && retried() < 4) { + fetchEventSource('https://connect.discours.io', { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: token, + }, + onmessage(event) { + const m: SSEMessage = JSON.parse(event.data) + console.log('[context.connect] Received message:', m) - // Iterate over all registered handlers and call them - messageHandlers().forEach((handler) => handler(m)) - }, - onclose() { - console.log('[context.connect] sse connection closed by server') - setConnected(false) - }, - onerror(err) { - console.error('[context.connect] sse connection error', err) - setConnected(false) - throw new Error(err) // NOTE: simple hack to close the connection - }, - }) - } catch (err) { - if (retried() < 4) { + // Iterate over all registered handlers and call them + messageHandlers().forEach((handler) => handler(m)) + }, + onclose() { + console.log('[context.connect] sse connection closed by server') + setConnected(false) + }, + onerror(err) { + console.error('[context.connect] sse connection error', err) setRetried((r) => r + 1) - } else { - console.warn('Not trying to reconnect anymore; listen() should be called again.') - } - } + setConnected(false) + throw new Error(err) // NOTE: simple hack to close the connection + }, + }) } } diff --git a/src/context/notifications.tsx b/src/context/notifications.tsx index 8afa027a..a39e6a07 100644 --- a/src/context/notifications.tsx +++ b/src/context/notifications.tsx @@ -10,6 +10,7 @@ import { notifierClient } from '../graphql/client/notifier' import { Notification } from '../graphql/schema/notifier.gen' import { SSEMessage, useConnect } from './connect' +import { useSession } from './session' type NotificationsContextType = { notificationEntities: Record @@ -38,12 +39,13 @@ export const NotificationsProvider = (props: { children: JSX.Element }) => { const [unreadNotificationsCount, setUnreadNotificationsCount] = createSignal(0) const [totalNotificationsCount, setTotalNotificationsCount] = createSignal(0) const [notificationEntities, setNotificationEntities] = createStore>({}) + const { isAuthenticated } = useSession() const apiClient = createMemo(() => { - if (!notifierClient.private) notifierClient.connect() + if (!notifierClient.private && isAuthenticated()) notifierClient.connect() return notifierClient }) const { addHandler } = useConnect() - const loadNotifications = async (options: { limit: number; offset?: number }) => { + const loadNotifications = async (options: { limit?: number; offset?: number }) => { const { notifications, unread, total } = await apiClient().getNotifications(options) const newNotificationEntities = notifications.reduce((acc, notification) => { acc[notification.id] = notification diff --git a/src/context/session.tsx b/src/context/session.tsx index 5b9ad302..a1893233 100644 --- a/src/context/session.tsx +++ b/src/context/session.tsx @@ -101,7 +101,7 @@ export const SessionProvider = (props: { children: JSX.Element }) => { async () => { const u = session()?.user if (u) { - return (await apiClient.getAuthor({ user: u.id })) ?? null + return (await apiClient.getAuthorId({ user: u.id })) ?? null } return null }, diff --git a/src/graphql/client/core.ts b/src/graphql/client/core.ts index c82160ae..c77bdb30 100644 --- a/src/graphql/client/core.ts +++ b/src/graphql/client/core.ts @@ -29,6 +29,7 @@ import draftsLoad from '../query/core/articles-load-drafts' import myFeed from '../query/core/articles-load-feed' import shoutsLoadSearch from '../query/core/articles-load-search' import authorBy from '../query/core/author-by' +import authorId from '../query/core/author-id' import authorFollowers from '../query/core/author-followers' import authorsAll from '../query/core/authors-all' import authorFollowed from '../query/core/authors-followed-by' @@ -79,10 +80,14 @@ export const apiClient = { } return response.data.load_authors_all }, - getAuthor: async (params: { slug?: string; author_id?: number; user?: string }): Promise => { + getAuthor: async (params: { slug?: string; author_id?: number }): Promise => { const response = await publicGraphQLClient.query(authorBy, params).toPromise() 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-by.ts b/src/graphql/query/core/author-by.ts index 3ad7c86b..d40173c1 100644 --- a/src/graphql/query/core/author-by.ts +++ b/src/graphql/query/core/author-by.ts @@ -1,8 +1,8 @@ import { gql } from '@urql/core' export default gql` - query GetAuthorBy($slug: String, $author_id: Int, $user: String) { - get_author(slug: $slug, author_id: $author_id, user: $user) { + query GetAuthorBy($slug: String, $author_id: Int) { + get_author(slug: $slug, author_id: $author_id) { id slug name diff --git a/src/graphql/query/core/author-id.ts b/src/graphql/query/core/author-id.ts new file mode 100644 index 00000000..277e0d44 --- /dev/null +++ b/src/graphql/query/core/author-id.ts @@ -0,0 +1,17 @@ +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 + } + } +` diff --git a/src/graphql/query/core/query.json b/src/graphql/query/core/query.json new file mode 100644 index 00000000..b8f1878c --- /dev/null +++ b/src/graphql/query/core/query.json @@ -0,0 +1,9 @@ +{ + "query": "query ValidateToken($params: ValidateJWTTokenInput!) { validate_jwt_token(params: $params) { is_valid claims } }", + "variables": { + "params": { + "token_type": "access_token", + "token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhbGxvd2VkX3JvbGVzIjpbInJlYWRlciJdLCJhdWQiOiI5YzExMzM3Ny01ZWVhLTRjODktOThlMS02OTMwMjQ2MmZjMDgiLCJleHAiOjE3MDI1MDM3NDksImlhdCI6MTcwMjUwMTk0OSwiaXNzIjoiaHR0cHM6Ly9hdXRoLmRpc2NvdXJzLmlvIiwibG9naW5fbWV0aG9kIjoiYmFzaWNfYXV0aCIsIm5vbmNlIjoiNTNhZmE1NzMtMWZhMC00NDNhLTk4NzktNWE0ZDk3YTQ1OWEzIiwicm9sZXMiOlsicmVhZGVyIl0sInNjb3BlIjpbIm9wZW5pZCIsImVtYWlsIiwicHJvZmlsZSJdLCJzdWIiOiIyNDU3NTc5Yi0yNTk3LTQzNjMtOGU1MC00YWM5ZjY5YzgyMWQiLCJ0b2tlbl90eXBlIjoiYWNjZXNzX3Rva2VuIn0.ImAmAFXkwReYhLvLR1GG3g0mDllQaj7NVWw5q9D7EFUHtpjuNOanlVfzoaqqB6CXkc_uSRlrfiaLkbfCzhbXAWpi49vOI9P2fvLv-41Y0p8FJZnruZDi1c8psyPU5gpCLp3nqtBTTIR2IP-Uu_1oYo2Hl02xdOFjVHw19f-WEf73L6APBPppNeE21IJJy8aU5uXBHv12Y--kH5tEzn83BNBKPm5yWay-k4DVuBnAJt_ODENqm5NAaiG-q2eG3GnXWpjgpDXf0cxVXdtJbvFcPIv-pH7Dm6ae8m4xM7MmVmpzCE5f-Qc1lwcfX51Cr6IMXUjHx0N3n54sKk91CPa_ug" + } + } +} diff --git a/src/graphql/query/notifier/notifications-load.ts b/src/graphql/query/notifier/notifications-load.ts index 052c7cbe..d119048d 100644 --- a/src/graphql/query/notifier/notifications-load.ts +++ b/src/graphql/query/notifier/notifications-load.ts @@ -1,13 +1,13 @@ import { gql } from '@urql/core' export default gql` - query LoadNotificationsQuery($params: NotificationsQueryParams!) { - load_notifications(params: $params) { + query LoadNotificationsQuery($limit: Int, $offset: Int) { + load_notifications(limit: $limit, offset: $offset) { notifications { id entity action - paylaod + payload created_at seen }