-
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.
Newsletter Categories: Add Newsletter Categories to Subscription Mana…
…gement page (#81521) * Rename to useSubscribedNewsletterCategories * Export getCategoriesKey * Create useNewsletterCategorySubscriptionMutation * Fix Subscriber Details displayed categories * Create SubscribeToNewsletterCategories component * Fix tests * Add loading state and feature flag * Optmize mutation to send only updaded categories * Extract NewsletterCategoriesResponse * Fix repeated cache key * Await query invalidation * Optmize rendering newsletter categories
- Loading branch information
Showing
15 changed files
with
210 additions
and
34 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 |
---|---|---|
|
@@ -136,6 +136,10 @@ | |
} | ||
} | ||
} | ||
|
||
&__loading { | ||
margin-bottom: 20px; | ||
} | ||
} | ||
|
||
&__unsubscribe-button.components-button { | ||
|
86 changes: 86 additions & 0 deletions
86
client/blocks/reader-site-subscription/subscribe-to-newsletter-categories.tsx
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,86 @@ | ||
import config from '@automattic/calypso-config'; | ||
import { Spinner } from '@automattic/components'; | ||
import { ToggleControl } from '@wordpress/components'; | ||
import { useTranslate } from 'i18n-calypso'; | ||
import { useMemo } from 'react'; | ||
import { | ||
useNewsletterCategorySubscriptionMutation, | ||
useSubscribedNewsletterCategories, | ||
} from 'calypso/data/newsletter-categories'; | ||
|
||
type SubscribeToNewsletterCategoriesProps = { | ||
siteId: number; | ||
}; | ||
|
||
const SubscribeToNewsletterCategories = ( { siteId }: SubscribeToNewsletterCategoriesProps ) => { | ||
const translate = useTranslate(); | ||
const { data: subscribedNewsletterCategoriesData, isLoading } = useSubscribedNewsletterCategories( | ||
{ siteId } | ||
); | ||
const { mutate, isLoading: isSaving } = useNewsletterCategorySubscriptionMutation( siteId ); | ||
|
||
const subscribedCategoryIds = useMemo( | ||
() => | ||
subscribedNewsletterCategoriesData?.newsletterCategories | ||
.filter( ( category ) => !! category.subscribed ) | ||
.map( ( category ) => category.id ) || [], | ||
[ subscribedNewsletterCategoriesData ] | ||
); | ||
|
||
const handleToggle = ( categoryId: number ) => { | ||
mutate( [ | ||
{ | ||
term_id: categoryId, | ||
subscribe: ! subscribedCategoryIds.includes( categoryId ), | ||
}, | ||
] ); | ||
}; | ||
|
||
if ( | ||
! config.isEnabled( 'settings/newsletter-categories' ) || | ||
! subscribedNewsletterCategoriesData?.enabled | ||
) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<> | ||
<hr className="subscriptions__separator" /> | ||
<div className="site-subscription-info"> | ||
<h2 className="site-subscription-info__heading">{ translate( 'Subscription' ) }</h2> | ||
|
||
{ isLoading ? ( | ||
<div className="site-subscription-info__loading"> | ||
<Spinner /> | ||
</div> | ||
) : ( | ||
<dl className="site-subscription-info__list"> | ||
<dt>{ translate( 'Subscribed to' ) }</dt> | ||
<dd> | ||
{ subscribedNewsletterCategoriesData?.newsletterCategories.map( | ||
( newletterCategory ) => ( | ||
<div className="setting-item" key={ newletterCategory.id }> | ||
<ToggleControl | ||
checked={ newletterCategory.subscribed } | ||
onChange={ () => handleToggle( newletterCategory.id ) } | ||
disabled={ isSaving } | ||
label={ newletterCategory.name } | ||
/> | ||
<p className="setting-item__hint"> | ||
{ translate( 'Receive emails for new posts in %s', { | ||
args: [ newletterCategory.name ], | ||
comment: 'Name of the site that the user tried to resubscribe to.', | ||
} ) } | ||
</p> | ||
</div> | ||
) | ||
) } | ||
</dd> | ||
</dl> | ||
) } | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export default SubscribeToNewsletterCategories; |
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
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
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
72 changes: 72 additions & 0 deletions
72
client/data/newsletter-categories/use-newsletter-category-subscription-mutation.tsx
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,72 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query'; | ||
import request from 'wpcom-proxy-request'; | ||
import { NewsletterCategories, NewsletterCategoriesResponse } from './types'; | ||
import { getSubscribedNewsletterCategoriesKey } from './use-subscribed-newsletter-categories-query'; | ||
|
||
type NewsletterCategorySubscription = { | ||
term_id: number; | ||
subscribe: boolean; | ||
}; | ||
|
||
const useNewsletterCategorySubscriptionMutation = ( siteId: string | number ) => { | ||
const queryClient = useQueryClient(); | ||
const subscribedCategoriesCacheKey = getSubscribedNewsletterCategoriesKey( siteId ); | ||
|
||
return useMutation( { | ||
mutationFn: async ( categorySubscriptions: NewsletterCategorySubscription[] ) => { | ||
return await request< NewsletterCategoriesResponse >( { | ||
path: `/sites/${ siteId }/newsletter-categories/subscriptions`, | ||
method: 'POST', | ||
apiVersion: '2', | ||
apiNamespace: 'wpcom/v2', | ||
body: { categories: categorySubscriptions }, | ||
} ); | ||
}, | ||
onMutate: async ( categorySubscriptions: NewsletterCategorySubscription[] ) => { | ||
await queryClient.cancelQueries( subscribedCategoriesCacheKey ); | ||
|
||
const previousData = queryClient.getQueryData< NewsletterCategories >( | ||
subscribedCategoriesCacheKey | ||
); | ||
|
||
queryClient.setQueryData( | ||
subscribedCategoriesCacheKey, | ||
( oldData?: NewsletterCategories ) => { | ||
const newSubscribedCategories = | ||
previousData?.newsletterCategories.map( ( category ) => { | ||
const categorySubscription = categorySubscriptions.find( | ||
( subscription ) => subscription.term_id === category.id | ||
); | ||
|
||
if ( ! categorySubscription ) { | ||
return category; | ||
} | ||
|
||
return { | ||
...category, | ||
subscribed: categorySubscription.subscribe, | ||
}; | ||
} ) || []; | ||
|
||
const updatedData = { | ||
...oldData, | ||
enabled: oldData?.enabled || false, | ||
newsletterCategories: newSubscribedCategories, | ||
}; | ||
|
||
return updatedData; | ||
} | ||
); | ||
|
||
return { previousData }; | ||
}, | ||
onError: ( error, variables, context ) => { | ||
queryClient.setQueryData( subscribedCategoriesCacheKey, context?.previousData ); | ||
}, | ||
onSettled: async () => { | ||
await queryClient.invalidateQueries( subscribedCategoriesCacheKey ); | ||
}, | ||
} ); | ||
}; | ||
|
||
export default useNewsletterCategorySubscriptionMutation; |
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
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
Oops, something went wrong.