2022-04-21 03:10:18 +00:00
|
|
|
import { gql } from 'graphql-request'
|
|
|
|
|
import useSWR from 'swr'
|
2023-09-01 04:39:30 +00:00
|
|
|
import { makeGqlFetcher } from '../networkHelpers'
|
2022-04-21 19:03:52 +00:00
|
|
|
|
|
|
|
|
export type SubscriptionStatus = 'ACTIVE' | 'DELETED' | 'UNSUBSCRIBED'
|
|
|
|
|
|
2023-07-12 07:25:07 +00:00
|
|
|
export enum SubscriptionType {
|
|
|
|
|
RSS = 'RSS',
|
|
|
|
|
NEWSLETTER = 'NEWSLETTER',
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 10:03:24 +00:00
|
|
|
export enum FetchContentType {
|
|
|
|
|
ALWAYS = 'ALWAYS',
|
|
|
|
|
NEVER = 'NEVER',
|
|
|
|
|
WHEN_EMPTY = 'WHEN_EMPTY',
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 19:03:52 +00:00
|
|
|
export type Subscription = {
|
|
|
|
|
id: string
|
|
|
|
|
name: string
|
2023-08-28 07:10:39 +00:00
|
|
|
type: SubscriptionType
|
2023-07-12 07:25:07 +00:00
|
|
|
newsletterEmail?: string
|
2022-04-21 19:03:52 +00:00
|
|
|
|
|
|
|
|
url?: string
|
2024-02-19 02:57:37 +00:00
|
|
|
icon?: string
|
2022-04-21 19:03:52 +00:00
|
|
|
description?: string
|
|
|
|
|
|
|
|
|
|
status: SubscriptionStatus
|
2023-01-17 07:37:09 +00:00
|
|
|
createdAt: string
|
|
|
|
|
updatedAt: string
|
2023-07-12 07:25:07 +00:00
|
|
|
lastFetchedAt?: string
|
2024-02-07 05:02:19 +00:00
|
|
|
mostRecentItemDate?: string
|
2024-04-05 09:26:38 +00:00
|
|
|
failedAt?: string
|
2024-02-07 05:02:19 +00:00
|
|
|
|
2024-03-05 10:03:24 +00:00
|
|
|
fetchContentType?: FetchContentType
|
2022-04-22 08:05:07 +00:00
|
|
|
}
|
2022-04-21 03:10:18 +00:00
|
|
|
|
|
|
|
|
type SubscriptionsQueryResponse = {
|
2023-10-17 05:36:47 +00:00
|
|
|
error: any
|
2023-09-07 11:10:42 +00:00
|
|
|
isLoading: boolean
|
2022-04-21 03:10:18 +00:00
|
|
|
isValidating: boolean
|
|
|
|
|
subscriptions: Subscription[]
|
|
|
|
|
revalidate: () => void
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SubscriptionsResponseData = {
|
|
|
|
|
subscriptions: SubscriptionsData
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SubscriptionsData = {
|
|
|
|
|
subscriptions: unknown
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 07:25:07 +00:00
|
|
|
export function useGetSubscriptionsQuery(
|
2023-09-01 04:27:06 +00:00
|
|
|
type: SubscriptionType | undefined = undefined,
|
2023-07-12 07:25:07 +00:00
|
|
|
sortBy = 'UPDATED_TIME'
|
|
|
|
|
): SubscriptionsQueryResponse {
|
2022-04-21 03:10:18 +00:00
|
|
|
const query = gql`
|
2023-09-01 04:19:36 +00:00
|
|
|
query GetSubscriptions($type: SubscriptionType, $sort: SortParams) {
|
|
|
|
|
subscriptions(type: $type, sort: $sort) {
|
2022-04-21 03:10:18 +00:00
|
|
|
... on SubscriptionsSuccess {
|
|
|
|
|
subscriptions {
|
|
|
|
|
id
|
|
|
|
|
name
|
2023-08-28 07:10:39 +00:00
|
|
|
type
|
2022-04-22 08:05:07 +00:00
|
|
|
newsletterEmail
|
2022-04-21 03:10:18 +00:00
|
|
|
url
|
2024-02-19 02:57:37 +00:00
|
|
|
icon
|
2022-04-21 03:10:18 +00:00
|
|
|
description
|
|
|
|
|
status
|
|
|
|
|
unsubscribeMailTo
|
|
|
|
|
unsubscribeHttpUrl
|
|
|
|
|
createdAt
|
|
|
|
|
updatedAt
|
2023-07-12 07:25:07 +00:00
|
|
|
lastFetchedAt
|
2024-03-05 10:03:24 +00:00
|
|
|
fetchContentType
|
2024-02-07 05:02:19 +00:00
|
|
|
mostRecentItemDate
|
2024-04-05 09:26:38 +00:00
|
|
|
failedAt
|
2022-04-21 03:10:18 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
... on SubscriptionsError {
|
|
|
|
|
errorCodes
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`
|
|
|
|
|
|
2023-09-01 04:54:55 +00:00
|
|
|
const variables = {
|
|
|
|
|
type,
|
|
|
|
|
sort: {
|
|
|
|
|
by: sortBy,
|
|
|
|
|
},
|
|
|
|
|
}
|
2023-09-07 11:16:02 +00:00
|
|
|
const { data, error, mutate, isValidating } = useSWR(
|
2023-09-01 04:54:55 +00:00
|
|
|
[query, variables],
|
|
|
|
|
makeGqlFetcher(variables)
|
2023-09-01 04:39:30 +00:00
|
|
|
)
|
2023-09-01 04:19:36 +00:00
|
|
|
|
2023-09-01 04:39:30 +00:00
|
|
|
try {
|
2022-04-21 03:10:18 +00:00
|
|
|
if (data) {
|
|
|
|
|
const result = data as SubscriptionsResponseData
|
|
|
|
|
const subscriptions = result.subscriptions.subscriptions as Subscription[]
|
|
|
|
|
return {
|
2023-10-17 05:36:47 +00:00
|
|
|
error,
|
2023-09-07 11:10:42 +00:00
|
|
|
isLoading: !error && !data,
|
2022-04-21 03:10:18 +00:00
|
|
|
isValidating,
|
|
|
|
|
subscriptions,
|
|
|
|
|
revalidate: () => {
|
|
|
|
|
mutate()
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log('error', error)
|
|
|
|
|
}
|
|
|
|
|
return {
|
2023-10-17 05:36:47 +00:00
|
|
|
error,
|
2023-09-07 11:10:42 +00:00
|
|
|
isLoading: !error && !data,
|
2023-01-17 10:46:39 +00:00
|
|
|
isValidating: true,
|
2022-04-21 03:10:18 +00:00
|
|
|
subscriptions: [],
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
|
|
|
revalidate: () => {},
|
|
|
|
|
}
|
|
|
|
|
}
|