File size: 5,462 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
import config from '@automattic/calypso-config';
import { getLanguage } from '@automattic/i18n-utils';
import { addPlanToCart, getNewSiteParams, processItemCart } from '@automattic/onboarding';
import { useMutation } from '@tanstack/react-query';
import { getLocaleSlug } from 'i18n-calypso';
import wpcomRequest from 'wpcom-proxy-request';
import { useSelector } from 'calypso/state';
import { getCurrentUserName, isUserLoggedIn } from 'calypso/state/current-user/selectors';
import { useFlowState } from '../declarative-flow/internals/state-manager/store';
import { getFlowFromURL } from '../utils/get-flow-from-url';
import type { DomainSuggestion, NewSiteSuccessResponse, Site } from '@automattic/data-stores';
import type { SiteGoal } from '@automattic/data-stores/src/onboard';
import type { MinimalRequestCartProduct } from '@automattic/shopping-cart';
type Params = {
flowName: string;
userIsLoggedIn: boolean;
isPurchasingDomainItem: boolean;
themeSlugWithRepo: string;
siteVisibility: Site.Visibility;
siteTitle: string;
siteAccentColor: string;
useThemeHeadstart: boolean;
username: string;
domainCartItems: MinimalRequestCartProduct[];
partnerBundle?: string | null;
domainItem?: DomainSuggestion;
sourceSlug?: string;
siteIntent?: string;
siteGoals?: SiteGoal[];
planCartItems?: MinimalRequestCartProduct[] | null;
};
async function fillCart(
siteSlug: string,
flowName: string,
userIsLoggedIn: boolean,
themeSlugWithRepo: string,
domainCartItems: MinimalRequestCartProduct[] | null | undefined,
planCartItems: MinimalRequestCartProduct[] | null | undefined
) {
if ( siteSlug && planCartItems?.length ) {
for ( const planCartItem of planCartItems ) {
await addPlanToCart( siteSlug, flowName, true, themeSlugWithRepo, planCartItem );
}
}
const isFreeThemePreselected = themeSlugWithRepo.startsWith( 'pub' );
if ( domainCartItems?.length ) {
for ( const domainCartItem of domainCartItems ) {
await processItemCart(
siteSlug,
isFreeThemePreselected,
themeSlugWithRepo,
flowName,
userIsLoggedIn,
domainCartItem
);
}
}
}
export const createSite = async ( {
flowName,
userIsLoggedIn,
isPurchasingDomainItem,
themeSlugWithRepo,
siteVisibility,
siteTitle,
siteAccentColor,
useThemeHeadstart,
username,
domainCartItems,
partnerBundle = null,
domainItem,
sourceSlug,
siteIntent,
planCartItems,
}: Params ) => {
const newSiteParams = getNewSiteParams( {
flowToCheck: flowName,
isPurchasingDomainItem,
themeSlugWithRepo,
siteTitle,
siteAccentColor,
useThemeHeadstart,
siteVisibility,
username,
sourceSlug,
siteIntent,
partnerBundle,
} );
const locale = getLocaleSlug();
const siteCreationResponse: NewSiteSuccessResponse = await wpcomRequest( {
path: '/sites/new',
apiVersion: '1.1',
method: 'POST',
body: {
...newSiteParams,
locale,
lang_id: getLanguage( locale as string )?.value,
client_id: config( 'wpcom_signup_id' ),
client_secret: config( 'wpcom_signup_key' ),
options: newSiteParams.options,
},
} );
const parsedBlogURL = new URL( siteCreationResponse?.blog_details.url );
const siteSlug = parsedBlogURL.hostname;
const siteId = siteCreationResponse?.blog_details.blogid;
const siteDetails = {
siteId,
siteSlug,
domainItem,
siteCreated: true,
goToCheckout: Boolean( planCartItems?.length ),
};
await fillCart(
siteSlug,
flowName,
userIsLoggedIn,
themeSlugWithRepo,
domainCartItems,
planCartItems
);
return siteDetails;
};
export const useCreateSite = () => {
const flowName = getFlowFromURL();
const userIsLoggedIn = useSelector( isUserLoggedIn );
const { get, set } = useFlowState();
const domains = get( 'domains' );
const username = useSelector( getCurrentUserName );
const planCartItems = get( 'plans' )?.cartItems;
const createdSite = get( 'site' );
const siteTitle = get( 'newsletterSetup' )?.siteTitle as string;
/**
* Support singular and multiple domain cart items.
*/
const mergedDomainCartItems = Array.isArray( domains?.domainCart )
? domains?.domainCart.slice( 0 )
: [];
if ( domains?.domainItem ) {
mergedDomainCartItems.push( domains?.domainItem );
}
return useMutation( {
mutationFn: async ( {
theme,
siteIntent,
}: {
theme: string;
siteIntent: string;
siteGoals?: SiteGoal[];
} ) => {
if ( createdSite ) {
// If the site already exists, we need to fill the cart with the domain and plan items.
// Because the user may have changed their mind about the domain or plan.
await fillCart(
createdSite.siteSlug,
flowName,
userIsLoggedIn,
theme,
mergedDomainCartItems,
planCartItems
);
return createdSite;
}
return createSite( {
flowName,
userIsLoggedIn,
isPurchasingDomainItem: false,
themeSlugWithRepo: theme,
siteVisibility: 1,
siteTitle,
// We removed the color option during newsletter onboarding.
// But backend still expects/needs a value, so supplying the default.
// Ideally should remove this and update code downstream to handle this.
siteAccentColor: '#113AF5',
useThemeHeadstart: true,
username,
domainCartItems: mergedDomainCartItems,
partnerBundle: null,
domainItem: domains?.domainItem as DomainSuggestion | undefined,
siteIntent,
planCartItems,
} );
},
onSuccess: ( data ) => {
set( 'site', data );
return data;
},
throwOnError: true,
} ).mutateAsync;
};
|