import React, { useEffect, useRef, useState } from 'react' import { useI18n } from '@wordpress/react-i18n' import { Button, ButtonLink, Card, Link, Notice, Stack, Text, } from '@wordpress/ui' import { cancelActivation, claimActivation, connectActivation, UnclaimedActivation, unclaimedActivation, } from './model/activation' import { landingPageLink, pluginPageLink } from './landing/pluginPage' import { leaveFor } from './navigation' import './landing/landing.scss' import './activate.scss' type PendingAction = 'connect' | 'cancel' | undefined type ClaimState = | { kind: 'finishing' } | { kind: 'confirming' shop: string webshopName: string pending: PendingAction cancelFailed: boolean } | { kind: 'cancelled' } | { kind: UnclaimedActivation } type Confirming = Extract const linkParameters = (): { activation: string | null code: string | null } => { const search = new URLSearchParams(window.location.search) return { activation: search.get('activation'), code: search.get('code') } } const Activate = () => { const { __ } = useI18n() const [{ activation, code }] = useState(linkParameters) const [state, setState] = useState({ kind: activation && code ? 'finishing' : 'notStartedHere', }) const [attempt, setAttempt] = useState(1) const claimedAttempt = useRef(0) useEffect(() => { if (!activation || !code || claimedAttempt.current === attempt) { return } claimedAttempt.current = attempt claimActivation(activation, code) .then(({ shop, webshopName }): ClaimState => ({ kind: 'confirming', shop, webshopName, pending: undefined, cancelFailed: false, })) .catch((error): ClaimState => ({ kind: unclaimedActivation(error) })) .then(setState) }, [activation, code, attempt]) const tryAgain = () => { setState({ kind: 'finishing' }) setAttempt(current => current + 1) } const connect = (confirming: Confirming) => { setState({ ...confirming, pending: 'connect', cancelFailed: false }) connectActivation(activation!, code!) .then(({ firstConnection }) => leaveFor(landingPageLink({ justConnected: firstConnection })) ) .catch(error => setState({ kind: unclaimedActivation(error) })) } const cancel = (confirming: Confirming) => { setState({ ...confirming, pending: 'cancel', cancelFailed: false }) cancelActivation(activation!) .then((): ClaimState => ({ kind: 'cancelled' })) .catch((): ClaimState => ({ ...confirming, pending: undefined, cancelFailed: true, })) .then(setState) } const failures: Record = { notStartedHere: __( 'This activation was not started here. Start it again with Update connection in the settings.', 'posten-bring-checkout' ), expired: __( 'This activation has expired. Start it again with Update connection in the settings.', 'posten-bring-checkout' ), failed: __( 'The activation could not be finished. Try again, or start again with Update connection in the settings.', 'posten-bring-checkout' ), } const content = () => { switch (state.kind) { case 'finishing': return ( {__('Finishing the activation…', 'posten-bring-checkout')} ) case 'confirming': return ( {__( 'Check that these details are correct. When you confirm, Posten Bring Checkout uses this Mybring webshop for shipping options at checkout and for booking shipments.', 'posten-bring-checkout' )}
{__('WooCommerce shop', 'posten-bring-checkout')}
{state.shop}
{__('Mybring webshop', 'posten-bring-checkout')}
{state.webshopName}
{__( "Don't recognise this webshop? Cancel and contact Posten Bring customer service.", 'posten-bring-checkout' )} {state.cancelFailed && ( {__( 'Could not cancel the activation. Try again.', 'posten-bring-checkout' )} )}
) case 'cancelled': return ( {__( 'Nothing was changed. You can start again with Set up in Mybring or Update connection.', 'posten-bring-checkout' )}
{__('Go to Posten Bring Checkout', 'posten-bring-checkout')}
) default: return ( {failures[state.kind]} {state.kind === 'failed' && ( )} {__('Go to settings', 'posten-bring-checkout')} ) } } return (

{__('Activate Posten Bring Checkout', 'posten-bring-checkout')}

{state.kind === 'confirming' && ( }> {__('Confirm your Mybring webshop', 'posten-bring-checkout')} )} {content()}
) } export default Activate