addBillingDetails.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import {Fragment, useCallback, useEffect, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import * as Sentry from '@sentry/react';
  4. import {Button} from 'sentry/components/button';
  5. import {Alert} from 'sentry/components/core/alert';
  6. import LoadingError from 'sentry/components/loadingError';
  7. import LoadingIndicator from 'sentry/components/loadingIndicator';
  8. import Panel from 'sentry/components/panels/panel';
  9. import PanelBody from 'sentry/components/panels/panelBody';
  10. import PanelFooter from 'sentry/components/panels/panelFooter';
  11. import PanelItem from 'sentry/components/panels/panelItem';
  12. import {IconEdit} from 'sentry/icons';
  13. import {t} from 'sentry/locale';
  14. import {space} from 'sentry/styles/space';
  15. import useApi from 'sentry/utils/useApi';
  16. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  17. import BillingDetailsForm from 'getsentry/components/billingDetailsForm';
  18. import type {BillingDetails} from 'getsentry/types';
  19. import {AddressType} from 'getsentry/types';
  20. import {getCountryByCode} from 'getsentry/utils/ISO3166codes';
  21. import {getRegionChoiceName, getTaxFieldInfo} from 'getsentry/utils/salesTax';
  22. import StepHeader from 'getsentry/views/amCheckout/steps/stepHeader';
  23. import type {StepProps} from 'getsentry/views/amCheckout/types';
  24. type State = {
  25. isLoading: boolean;
  26. loadError: Error | null;
  27. submitError: any;
  28. useExisting: boolean;
  29. billingDetails?: BillingDetails;
  30. };
  31. function FormFieldWrapper({children}: {children: React.ReactNode}) {
  32. return <FormFieldBody>{children}</FormFieldBody>;
  33. }
  34. function AddBillingDetails({
  35. isActive,
  36. isCompleted,
  37. onEdit,
  38. organization,
  39. prevStepCompleted,
  40. stepNumber,
  41. onCompleteStep,
  42. }: StepProps) {
  43. const title = t('Billing Details');
  44. const [state, setState] = useState<State>({
  45. submitError: null,
  46. isLoading: false,
  47. loadError: null,
  48. useExisting: false,
  49. });
  50. const api = useApi();
  51. const fetchBillingDetails = useCallback(async () => {
  52. if (isActive) {
  53. setState(prevState => ({...prevState, isLoading: true, loadError: null}));
  54. try {
  55. const response: BillingDetails = await api.requestPromise(
  56. `/customers/${organization.slug}/billing-details/`
  57. );
  58. setState(prevState => ({
  59. ...prevState,
  60. isLoading: false,
  61. billingDetails: response,
  62. useExisting: response.addressType === AddressType.STRUCTURED,
  63. }));
  64. } catch (error) {
  65. setState(prevState => ({...prevState, loadError: error, isLoading: false}));
  66. if (error.status !== 401 && error.status !== 403) {
  67. Sentry.captureException(error);
  68. }
  69. }
  70. }
  71. }, [isActive, api, organization.slug]);
  72. useEffect(() => {
  73. fetchBillingDetails();
  74. }, [fetchBillingDetails]);
  75. function renderDetailsForm() {
  76. if (state.isLoading || state.loadError || !state.billingDetails) {
  77. return (
  78. <Fragment>
  79. {state.loadError ? (
  80. <LoadingError onRetry={fetchBillingDetails} />
  81. ) : (
  82. <LoadingIndicator />
  83. )}
  84. <StepFooter>
  85. <Button disabled priority="primary">
  86. {t('Continue')}
  87. </Button>
  88. </StepFooter>
  89. </Fragment>
  90. );
  91. }
  92. const hasBillingAddress = state.billingDetails.addressType === AddressType.STRUCTURED;
  93. const footerStyle = {paddingRight: `${space(2)}`};
  94. const fieldProps = {
  95. stacked: true,
  96. inline: false,
  97. flexibleControlStateSize: true,
  98. showHelpInTooltip: true,
  99. };
  100. if (state.useExisting) {
  101. return (
  102. <FormWrapper
  103. icon={<IconEdit size="xs" />}
  104. hasBillingAddress={hasBillingAddress}
  105. onClick={() => setState({...state, useExisting: false})}
  106. editButtonLabel={t('Edit Details')}
  107. >
  108. <AddressItems billingDetails={state.billingDetails} />
  109. <StepFooter>
  110. <Button priority="primary" onClick={() => onCompleteStep(stepNumber)}>
  111. {t('Continue')}
  112. </Button>
  113. </StepFooter>
  114. </FormWrapper>
  115. );
  116. }
  117. return (
  118. <FormWrapper
  119. hasBillingAddress={hasBillingAddress}
  120. onClick={() => setState({...state, useExisting: true})}
  121. editButtonLabel={t('Cancel')}
  122. >
  123. {state.submitError && (
  124. <ErrorAlert type="error">
  125. {t('There was an error submitting billing details. Please try again.')}
  126. </ErrorAlert>
  127. )}
  128. <BillingDetailsForm
  129. isDetailed={false}
  130. initialData={state.billingDetails}
  131. requireChanges={false}
  132. wrapper={FormFieldWrapper}
  133. organization={organization}
  134. onPreSubmit={() => setState({...state, submitError: null})}
  135. onSubmitSuccess={() => onCompleteStep(stepNumber)}
  136. onSubmitError={err => setState({...state, submitError: err})}
  137. submitLabel={t('Continue')}
  138. fieldProps={fieldProps}
  139. footerStyle={footerStyle}
  140. />
  141. </FormWrapper>
  142. );
  143. }
  144. return (
  145. <Panel>
  146. <StepHeader
  147. canSkip={prevStepCompleted}
  148. title={title}
  149. isActive={isActive}
  150. stepNumber={stepNumber}
  151. isCompleted={isCompleted}
  152. onEdit={onEdit}
  153. />
  154. {isActive && <PanelBody data-test-id={title}>{renderDetailsForm()}</PanelBody>}
  155. </Panel>
  156. );
  157. }
  158. type FormWrapperProps = {
  159. children: React.ReactNode;
  160. editButtonLabel: React.ReactNode;
  161. hasBillingAddress?: boolean;
  162. icon?: React.ReactNode;
  163. onClick?: () => void;
  164. };
  165. function FormWrapper({
  166. children,
  167. editButtonLabel,
  168. hasBillingAddress,
  169. icon,
  170. onClick,
  171. }: FormWrapperProps) {
  172. return (
  173. <Fragment>
  174. <Heading>
  175. <DetailsText>
  176. {t('Current billing details on file')}
  177. <SubText>
  178. {t("Your company's address of record will appear on all receipts.")}
  179. </SubText>
  180. </DetailsText>
  181. {hasBillingAddress && (
  182. <Button size="xs" icon={icon} onClick={onClick}>
  183. {editButtonLabel}
  184. </Button>
  185. )}
  186. </Heading>
  187. {children}
  188. </Fragment>
  189. );
  190. }
  191. type AddressItemProps = {title: React.ReactNode; value: string | null};
  192. function AddressItem({title, value}: AddressItemProps) {
  193. if (!value) {
  194. return null;
  195. }
  196. return (
  197. <StyledPanelItem>
  198. <SubText>{title}</SubText>
  199. <div>{value}</div>
  200. </StyledPanelItem>
  201. );
  202. }
  203. function AddressItems({billingDetails}: {billingDetails: BillingDetails}) {
  204. const taxFieldInfo = getTaxFieldInfo(billingDetails.countryCode);
  205. const countryName =
  206. getCountryByCode(billingDetails.countryCode)?.name || billingDetails.countryCode;
  207. const regionName = getRegionChoiceName(
  208. billingDetails.countryCode,
  209. billingDetails.region
  210. );
  211. return (
  212. <StyledAddressItems>
  213. <AddressItem title={t('Company Name')} value={billingDetails.companyName} />
  214. <AddressItem title={t('Country')} value={countryName} />
  215. <AddressItem title={t('Street Address 1')} value={billingDetails.addressLine1} />
  216. <AddressItem title={t('Street Address 2')} value={billingDetails.addressLine2} />
  217. <AddressItem title={t('City')} value={billingDetails.city} />
  218. <AddressItem title={t('State / Region')} value={regionName} />
  219. <AddressItem title={t('Postal Code')} value={billingDetails.postalCode} />
  220. <AddressItem title={taxFieldInfo.label} value={billingDetails.taxNumber} />
  221. </StyledAddressItems>
  222. );
  223. }
  224. export default AddBillingDetails;
  225. const FormFieldBody = styled('div')`
  226. padding: ${space(2)} 0 ${space(2)} ${space(2)};
  227. `;
  228. const ErrorAlert = styled(Alert)`
  229. margin: ${space(2)} ${space(2)} 0px ${space(2)};
  230. `;
  231. const StepFooter = styled(PanelFooter)`
  232. padding: ${space(2)};
  233. display: grid;
  234. align-items: center;
  235. justify-content: end;
  236. `;
  237. const Heading = styled('div')`
  238. display: grid;
  239. grid-auto-flow: column;
  240. justify-content: space-between;
  241. align-items: center;
  242. padding: ${space(2)} ${space(2)} ${space(1)} ${space(2)};
  243. gap: ${space(4)};
  244. `;
  245. const DetailsText = styled(TextBlock)`
  246. font-size: ${p => p.theme.fontSizeExtraLarge};
  247. margin-bottom: 0px;
  248. font-weight: 600;
  249. `;
  250. const SubText = styled('div')`
  251. font-size: ${p => p.theme.fontSizeMedium};
  252. color: ${p => p.theme.subText};
  253. line-height: 1.2;
  254. font-weight: normal;
  255. `;
  256. const StyledPanelItem = styled(PanelItem)`
  257. display: grid;
  258. grid-auto-flow: row;
  259. padding: ${space(1.5)} 0;
  260. gap: ${space(1)};
  261. &:first-child {
  262. padding-top: 0px;
  263. }
  264. &:last-child {
  265. padding-bottom: 0px;
  266. }
  267. `;
  268. const StyledAddressItems = styled('div')`
  269. padding: ${space(2)};
  270. `;