import { Button, Notice, TextControl } from '@wordpress/components'; import { __, sprintf } from '@wordpress/i18n'; import { useState } from 'react'; import { api, bootstrap, toApiError } from '../api'; import { planName } from '../labels'; import { useResource } from '../useResource'; import { Section } from './Ui'; import type { StoreGroup as StoreGroupDto, StoreGroupResult } from '../types'; /** Motif de refus, dit au marchand avec l'action qui debloque. */ function refusal(reason: string | undefined): string { switch (reason) { case 'expired': return __('This code has expired. Generate a new one in the store on Scale.', 'convrail'); case 'self': return __( 'This code comes from this store. Enter it in the store you want to add.', 'convrail', ); case 'full': return __( 'The plan already covers the most stores it allows. Remove one to add another.', 'convrail', ); case 'plan': return __('Sharing a plan between stores is included in Scale.', 'convrail'); case 'platform': return __('Stores in one group must be on the same platform.', 'convrail'); case 'paid': return __( 'This store already pays for its own plan. Switch it back to Free first, then enter the code.', 'convrail', ); case 'has_members': return __( 'This store already shares its plan with other stores, so it cannot join another group.', 'convrail', ); case 'already_covered': return __( 'This store already shares the plan of another store. Leave that group first.', 'convrail', ); case 'covered': return __( 'This store gets its plan from another store, so it cannot add stores itself.', 'convrail', ); default: return __( 'This code is not valid, or it was already used. Generate a new one in the store on Scale.', 'convrail', ); } } /** * « Vos boutiques »: le groupe d'une offre Scale. Meme contenu que l'ecran Shopify: la boutique * qui paie voit ses places et genere un code, la boutique rattachee voit d'ou vient son offre, et * une boutique gratuite saisit un code. */ export function StoreGroup({ ownPlan, onChanged }: { ownPlan: string; onChanged: () => void }) { const group = useResource(() => api.storeGroup()); const [code, setCode] = useState(''); const [entering, setEntering] = useState(false); const [busy, setBusy] = useState(false); const [result, setResult] = useState(null); const [error, setError] = useState(null); const run = async ( action: 'code' | 'join' | 'remove' | 'leave', body: Record = {}, ) => { setBusy(true); setError(null); try { const outcome = await api.storeGroupAction(action, body); setResult(outcome); if (outcome.ok) { group.reload(); if (action !== 'code') onChanged(); } } catch (failure) { setError(toApiError(failure).message); } finally { setBusy(false); } }; const data = group.data; if (!data || (data.role === 'none' && ownPlan !== 'free')) return null; /* * Boutique seule sur l'offre gratuite: un bloc « Vos boutiques » avec un champ de code parlait * d'un groupe qui n'existe pas. Une ligne suffit, avec d'ou vient le code, et le champ ne * s'ouvre qu'a la demande. */ if (data.role === 'none') { const refused = result && !result.ok; return (
{error ? ( {error} ) : null} {refused ? ( {refusal(result.reason)} ) : null}

{__('Already on Scale with another store? Enter its code.', 'convrail')}{' '} {entering || refused ? null : ( )}

{entering || refused ? ( <>

{__( 'In the store on Scale, open Convrail, go to Plans and billing and click Add a store: it shows a code valid for 24 hours. Enter it here and this store gets the same plan.', 'convrail', )}

) : null}
); } return (
{error ? ( {error} ) : null} {result && !result.ok ? ( {refusal(result.reason)} ) : null} {result?.ok && result.billingDomain ? ( {sprintf( /* translators: %s: domain of the store that pays. */ __('Done. This store now shares the plan of %s.', 'convrail'), result.billingDomain, )} ) : null} {data.role === 'billing' && data.seats ? ( <>

{sprintf( /* translators: 1: stores used, 2: stores allowed, 3: plan name. */ __( '%1$s of %2$s stores on your %3$s plan. Every store you add gets the same plan, at no extra cost.', 'convrail', ), String(data.seats.used), String(data.seats.limit), planName(data.plan ?? 'scale'), )}

    {/* Cette boutique compte dans les places: elle doit figurer dans la liste. */}
  • {bootstrap().domain || __('This store', 'convrail')}{' '} {__('This store', 'convrail')}
  • {data.members.map((member) => (
  • {member.domain}{' '}
  • ))}
{data.members.length === 0 ? (

{__('Add up to two more stores from the same platform.', 'convrail')}

) : null} {result?.ok && result.code ? ( {result.code}{' '} {__( 'Open Convrail in the other store, go to Plans and billing, and enter this code. It works once, for 24 hours.', 'convrail', )} ) : null} ) : null} {data.role === 'covered' && data.coveredBy ? ( <>

{data.active ? sprintf( /* translators: 1: plan name, 2: domain of the store that pays. */ __( 'Your %1$s plan is provided by %2$s. Leave the group to choose your own plan, including Free.', 'convrail', ), planName(data.coveredBy.plan), data.coveredBy.domain, ) : sprintf( /* translators: %s: domain of the store that pays. */ __( '%s is no longer on a plan that covers several stores, so this store is back on its own plan. It gets the shared plan again as soon as that store is back on Scale.', 'convrail', ), data.coveredBy.domain, )}

) : null}
); }