Skip to Content
BBRICKSv0.7.4
Recipes

Challenge builder

A four-step builder with Stepper, BuilderStepCard and the step fields, beside a sticky ChallengeSummary with warnings that block launch.

A challenge is built in four short steps beside a summary that says what it will cost and what is wrong with it. The library has the pieces, the form state and its checks; your app holds the draft and launches it.

The whole builder

A Stepper and one BuilderStepCard per step around that step's fields (ChallengeBasicsFields, ChallengeRewardFields for the goal and the payout, ChallengeAudienceFields), with the flat ChallengeSummary beside it in a sticky side column (PageGrid split="8-4" stickyAside). One ChallengeDraft feeds every step; challengeIssues(draft) finds what is missing, and each issue becomes a bad summary warning that disables Launch and links back to its step. Try Continue with an empty name.

Pinned to TRNDR presetPR theme
Code
import { useState } from 'react';
import { BuilderStepCard, ChallengeAudienceFields, ChallengeBasicsFields, ChallengeRewardFields, ChallengeSummary, PageGrid, Stepper, ToastRegion, challengeIssues, defaultChallengeDraft, type BuilderBrand, type ChallengeDraft, type ChallengeField, type SummaryWarning, type ToastMessage } from '@ergasti/bricks';

const BRANDS: Array<BuilderBrand & { color: string }> = [{ id: 'glow', name: 'Glow Lab', color: '#C7325C' }, { id: 'nile', name: 'Nile Threads', color: '#2C45A6' }, { id: 'kemet', name: 'Kemet Home', color: '#3D5A10', paused: true }];

const GIFTS = [{ id: 'serum', name: 'Glow serum set', cost: '300.00', left: 4 }];

const STEPS = ['Basics', 'Type & goal', 'Who can join', 'Reward'];

const STEP_IDS = ['basics', 'goal', 'join', 'reward'] as const;

const AVAILABLE = 43390;

const stepOf = (field: ChallengeField): (typeof STEP_IDS)[number] => {
  if (field === 'brand' || field === 'name' || field === 'starts' || field === 'ends' || field === 'description') return 'basics';
  if (field === 'audience') return 'join';
  if (field === 'perWinner' || field === 'giftId' || field === 'reward') return 'reward';
  return 'goal';
};

const WORDS: Record<string, string> = {
  brand: 'Pick who the challenge is for.', name: 'Give the challenge a name.', starts: 'Pick a start date.', ends: 'The end date must come after the start date.',
  winners: 'The winners must be a whole number.', goal: 'Set a goal.', cap: 'Set a cap on the extra commission.', perWinner: 'Set the cash per winner.', giftId: 'Pick a gift.',
};

function Builder() {
  const [step, setStep] = useState(1);
  const [seen, setSeen] = useState(1);
  const [pending, setPending] = useState(false);
  const [toast, setToast] = useState<ToastMessage | null>(null);
  const [draft, setDraft] = useState<ChallengeDraft>({ ...defaultChallengeDraft('glow'), name: 'Winter top 10', starts: '2026-12-01', ends: '2026-12-31' });
  const brand = BRANDS.find((b) => b.id === draft.brand);
  const brandName = brand?.name ?? 'All brands';
  const trndrWide = draft.brand === 'all';
  const go = (next: number) => { setStep(next); setSeen((s) => Math.max(s, next)); };
  const hold = draft.type === 'boost' ? Number(draft.cap || 0) : Number(draft.perWinner || 0) * Number(draft.winners || 0);
  const issues = challengeIssues(draft);
  const warnings: SummaryWarning[] = [
    ...Object.keys(issues).map((field) => ({ id: field, tone: 'bad' as const, text: WORDS[field] ?? 'Check this field.', step: stepOf(field as ChallengeField) })),
    ...(hold > AVAILABLE ? [{ id: 'budget', tone: 'bad' as const, text: `The wallet has EGP ${AVAILABLE.toLocaleString('en')}: lower the reward or the winners.`, step: 'reward' }] : []),
    ...(!trndrWide && draft.audience === 'public' ? [{ id: 'public', text: `Every creator on TRNDR can join, not only ${brandName}’s.`, step: 'join' }] : []),
  ];
  const fields = [
    <ChallengeBasicsFields key="basics" value={draft} onChange={setDraft} brands={BRANDS} validate={seen > 1} />,
    <ChallengeRewardFields key="goal" value={draft} onChange={setDraft} validate={seen > 2} />,
    <ChallengeAudienceFields key="join" value={draft} onChange={setDraft} brandName={brandName} trndrWide={trndrWide} />,
    <ChallengeRewardFields key="reward" part="payout" value={draft} onChange={setDraft} gifts={GIFTS} brandName={brandName} validate />,
  ][step - 1];

  return (
    <PageGrid split="8-4" align="start" stickyAside>
      <div>
        <Stepper steps={STEPS} current={step - 1} onStep={(i) => go(i + 1)} />
        <BuilderStepCard title={STEPS[step - 1]} step={step} onBack={() => go(step - 1)} onContinue={() => go(step + 1)}>{fields}</BuilderStepCard>
      </div>
      <div>
        <ChallengeSummary
          layout="flat" name={draft.name || 'Untitled challenge'} brand={brand ?? { id: 'all', name: brandName }} payer={trndrWide ? 'trndr' : 'brand'} status="upcoming"
          dates={draft.starts && draft.ends ? `${draft.starts} → ${draft.ends}` : '—'}
          goal={draft.type === 'leader' ? `Top ${draft.winners} by ${draft.rankBy}` : draft.type === 'target' ? `${draft.goal} ${draft.goalKind}` : `×${draft.boost} commission`}
          eligibility={{ audience: draft.audience }}
          rewards={draft.type === 'boost' ? [{ key: 'cap', label: 'Cap on the extra', amount: Number(draft.cap || 0).toFixed(2), figure: 'challenge_commission' }]
            : draft.reward === 'cash' ? [{ key: 'per', label: 'Per winner', amount: Number(draft.perWinner || 0).toFixed(2), figure: 'challenge_commission' }]
              : [{ key: 'gift', label: 'Gift', value: GIFTS[0].name }]}
          gifts={draft.type !== 'boost' && draft.reward === 'gift' ? [{ id: 1, name: GIFTS[0].name, left: GIFTS[0].left, needed: Number(draft.winners || 0) }] : []}
          budget={{ hold: hold.toFixed(2), available: AVAILABLE.toFixed(2) }}
          warnings={warnings}
          steps={{ basics: STEPS[0], goal: STEPS[1], join: STEPS[2], reward: STEPS[3] }}
          onEditStep={(id) => go(STEP_IDS.indexOf(id as (typeof STEP_IDS)[number]) + 1)}
          pending={pending}
          onPrimary={() => { setPending(true); window.setTimeout(() => { setPending(false); setToast({ message: `${draft.name} is scheduled.` }); }, 900); }}
          onSaveDraft={() => setToast({ message: 'Draft saved.' })}
          money={{ decimals: 'auto' }}
        />
        <ToastRegion toast={toast} onDismiss={() => setToast(null)} />
      </div>
    </PageGrid>
  );
}

Steps

  1. Hold one draft. Start from defaultChallengeDraft(brandId) and keep the whole ChallengeDraft in state. Every step component is controlled (value, onChange) and hands back the whole value, so one state feeds all four steps and the summary.
  2. Lay out the page. A PageGrid with split="8-4" and stickyAside puts the step on the left and the summary on the right, where it stays in view while the page scrolls.
  3. Show where you are. Stepper takes the step names and current; with onStep the finished steps become buttons that go back. BuilderStepCard is the step itself: its title, "Step n of 4", the fields, and a footer with Back (disabled on step 1) and "Continue →", or "Check the summary, then launch →" on the last step.
  4. Put the fields in the card. ChallengeBasicsFields (who it is for, the name, the dates, the description), ChallengeRewardFields for the type and goal (part="goal", the default) and for the payout (part="payout", cash per winner or a gift from confirmed stock), and ChallengeAudienceFields (public or the brand's affiliates). Pass validate once PR has been past a step, so the built-in checks show under the fields; errors from your server win over them.
  5. Review on the summary. ChallengeSummary with layout="flat" takes the name, brand, dates, goal, eligibility, reward lines, gifts and the budget it holds against what the wallet has. Pass steps and onEditStep and each section links back to its step.
  6. Work out the warnings. challengeIssues(draft) returns what is missing or wrong, by field, only for the fields the draft's type and reward use. Turn each into a SummaryWarning with tone: 'bad' and a step: it disables Launch and links to the step that fixes it. Add your own (a hold the wallet cannot cover); a warn one only informs. canSubmit overrides the check when your app knows better.
  7. Launch. onPrimary sends it; set pending while it saves. onSaveDraft keeps it for later. A brand's request under review uses mode="approve" with onReject instead.

Related: ChallengeSummary, SummarySection, Leaderboard, ReasonDialog, PR builder screen.

On this page