@@ -18,6 +18,7 @@ import {
1818 siteLatestAtomicTransferQuery ,
1919 siteFeaturesQuery ,
2020 removePurchaseMutation ,
21+ userPreferenceQuery ,
2122} from '@automattic/api-queries' ;
2223import config from '@automattic/calypso-config' ;
2324import { localizeUrl } from '@automattic/i18n-utils' ;
@@ -77,6 +78,7 @@ import {
7778 FEEDBACK_STEP ,
7879 NEXT_ADVENTURE_STEP ,
7980 OFFER_ACCEPTED_STEP ,
81+ REMOVE_PLAN_STEP ,
8082 UPSELL_STEP ,
8183} from './cancel-purchase-form/steps' ;
8284import CancelPurchaseDomainOptions from './domain-options' ;
@@ -88,7 +90,12 @@ import MarketPlaceSubscriptionsDialog from './marketplace-subscriptions-dialog';
8890import nextStep from './next-step' ;
8991import CancelPurchaseRefundInformation from './refund-information' ;
9092import type { CancelPurchaseState } from './types' ;
91- import type { Purchase , MarketingSurveyDetails , PlanProduct } from '@automattic/api-core' ;
93+ import type {
94+ Purchase ,
95+ MarketingSurveyDetails ,
96+ PlanProduct ,
97+ UserPreferences ,
98+ } from '@automattic/api-core' ;
9299import type { ChangeEvent } from 'react' ;
93100import './style.scss' ;
94101
@@ -113,6 +120,11 @@ export default function CancelPurchase() {
113120 initialized : false ,
114121 } ) ;
115122 const { purchaseId } = cancelPurchaseRoute . useParams ( ) ;
123+ const getCancelPurchaseSurveyCompletedPreferenceKey = (
124+ purchaseId : string | number
125+ ) : keyof UserPreferences => {
126+ return `cancel-purchase-survey-completed-${ purchaseId } ` ;
127+ } ;
116128
117129 // Queries
118130 const { data : purchase , isPending : purchaseQueryIsPending } = useSuspenseQuery (
@@ -150,14 +162,20 @@ export default function CancelPurchase() {
150162 const { data : cancellationOffers } = useQuery (
151163 cancellationOffersQuery ( purchase . blog_id , purchase . ID )
152164 ) ;
165+ const {
166+ data : userHasCompletedCancelSurveyForPurchase ,
167+ isPending : userPreferencesQueryIsPending ,
168+ } = useQuery (
169+ userPreferenceQuery ( getCancelPurchaseSurveyCompletedPreferenceKey ( purchase . ID ) )
170+ ) ;
153171
154172 // Mutations
155173 const cancelAndRefundPurchaseMutate = useMutation ( cancelAndRefundPurchaseMutation ( ) ) ;
156174 const setPurchaseAutoRenewMutation = useMutation ( userPurchaseSetAutoRenewQuery ( ) ) ;
157175 const cancelAndRefundMutation = useMutation ( cancelAndRefundPurchaseMutation ( ) ) ;
158176 const removePurchaseMutator = useMutation ( removePurchaseMutation ( ) ) ;
159177 const extendWithFreeMonthMutation = useMutation ( extendPurchaseWithFreeMonthMutation ( ) ) ;
160- const userPreferences = useMutation ( userPreferencesMutation ( ) ) ;
178+ const userPreferencesMutator = useMutation ( userPreferencesMutation ( ) ) ;
161179 const {
162180 mutate : applyCancellationOffer ,
163181 isPending : isApplyingOffer ,
@@ -199,7 +217,7 @@ export default function CancelPurchase() {
199217 [ key ] : value ,
200218 } ,
201219 } ;
202- userPreferences . mutate ( payload ) ;
220+ userPreferencesMutator . mutate ( payload ) ;
203221 } ;
204222 const flowType = getPurchaseCancellationFlowType ( purchase ) ;
205223
@@ -266,6 +284,7 @@ export default function CancelPurchase() {
266284 const getAllSurveySteps = useCallback ( ( ) => {
267285 let steps = [ FEEDBACK_STEP ] ;
268286 const isJetpack = purchase . is_jetpack_plan_or_product ;
287+ const skipRemovePlanSurvey = purchase . is_plan && userHasCompletedCancelSurveyForPurchase ;
269288
270289 if (
271290 isPartnerPurchase ( purchase ) &&
@@ -292,6 +311,16 @@ export default function CancelPurchase() {
292311 steps . push ( ATOMIC_REVERT_STEP ) ;
293312 }
294313
314+ if ( skipRemovePlanSurvey ) {
315+ if ( steps . includes ( FEEDBACK_STEP ) ) {
316+ steps = steps . filter ( ( step ) => step !== FEEDBACK_STEP ) ;
317+ }
318+ if ( steps . includes ( NEXT_ADVENTURE_STEP ) ) {
319+ steps = steps . filter ( ( step ) => step !== NEXT_ADVENTURE_STEP ) ;
320+ }
321+ steps = [ REMOVE_PLAN_STEP , ...steps ] ;
322+ }
323+
295324 return steps ;
296325 } , [
297326 purchase ,
@@ -346,7 +375,7 @@ export default function CancelPurchase() {
346375 customerConfirmedUnderstanding : false ,
347376 domainConfirmationConfirmed : false ,
348377 initialized : true ,
349- isLoading : true ,
378+ isLoading : REMOVE_PLAN_STEP !== firstStep ,
350379 isNextAdventureValid : false ,
351380 isSubmitting : false ,
352381 questionOneOrder,
@@ -360,7 +389,7 @@ export default function CancelPurchase() {
360389 showDomainOptionsStep : false ,
361390 siteId : undefined ,
362391 solution : '' ,
363- surveyShown : false ,
392+ surveyShown : REMOVE_PLAN_STEP === firstStep ,
364393 surveyStep : firstStep ,
365394 upsell : '' ,
366395 willAtomicSiteRevert : willAtomicSiteRevertAfterPurchaseDeactivation (
@@ -393,10 +422,6 @@ export default function CancelPurchase() {
393422 setState ( ( state ) => ( { ...state , isShowingMarketplaceSubscriptionsDialog : true } ) ) ;
394423 } ;
395424
396- const getCancelPurchaseSurveyCompletedPreferenceKey = ( purchaseId : string | number ) : string => {
397- return `cancel-purchase-survey-completed-${ purchaseId } ` ;
398- } ;
399-
400425 const cancelPurchaseSurveyCompleted = ( purchaseId : number ) => ( ) => {
401426 savePreference ( getCancelPurchaseSurveyCompletedPreferenceKey ( purchaseId ) , true ) ( ) ;
402427 } ;
@@ -1016,7 +1041,8 @@ export default function CancelPurchase() {
10161041 purchaseQueryIsPending ||
10171042 ( Boolean ( purchase . meta ) && domainQueryIsPending ) ||
10181043 siteLatestAtomicTransferQueryIsPending ||
1019- productsQueryIsPending ;
1044+ productsQueryIsPending ||
1045+ userPreferencesQueryIsPending ;
10201046
10211047 const isDataValid = useCallback ( ( ) => {
10221048 if ( isDataLoading ) {
0 commit comments