-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add category selection to subscriber modal.
- Loading branch information
1 parent
8a0fabd
commit e0f8653
Showing
3 changed files
with
115 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { useQuery, UseQueryResult } from '@tanstack/react-query'; | ||
import request from 'wpcom-proxy-request'; | ||
|
||
export type NewsletterCategory = { | ||
id: number; | ||
name: string; | ||
slug: string; | ||
description: string; | ||
parent: number; | ||
subscribed?: boolean; | ||
}; | ||
|
||
export type NewsletterCategories = { | ||
enabled: boolean; | ||
newsletterCategories: NewsletterCategory[]; | ||
}; | ||
|
||
type NewsletterCategoryQueryProps = { | ||
siteId: number; | ||
}; | ||
|
||
type NewsletterCategoryResponse = { | ||
enabled: boolean; | ||
newsletter_categories: NewsletterCategory[]; | ||
}; | ||
|
||
export const getNewsletterCategoriesKey = ( siteId?: string | number ) => [ | ||
'newsletter-categories', | ||
siteId, | ||
]; | ||
|
||
const convertNewsletterCategoryResponse = ( | ||
response: NewsletterCategoryResponse | ||
): NewsletterCategories => ( { | ||
enabled: response.enabled, | ||
newsletterCategories: response.newsletter_categories, | ||
} ); | ||
|
||
export const useNewsletterCategories = ( { | ||
siteId, | ||
}: NewsletterCategoryQueryProps ): UseQueryResult< NewsletterCategories > => { | ||
const path = `/sites/${ siteId }/newsletter-categories`; | ||
|
||
return useQuery( { | ||
queryKey: getNewsletterCategoriesKey( siteId ), | ||
queryFn: async () => { | ||
try { | ||
const response = await request< NewsletterCategoryResponse >( { | ||
path, | ||
apiVersion: '2', | ||
apiNamespace: 'wpcom/v2', | ||
} ); | ||
return convertNewsletterCategoryResponse( response ); | ||
} catch ( e ) { | ||
return { | ||
enabled: false, | ||
newsletterCategories: [], | ||
error: e instanceof Error ? e.message : 'Unknown error', | ||
}; | ||
} | ||
}, | ||
enabled: !! siteId, | ||
} ); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters