|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { |
| 4 | + ConsentManagerDialog as C15TConsentManagerDialog, |
| 5 | + ConsentManagerWidget as C15TConsentManagerWidget, |
| 6 | + ConsentManagerDialogProps, |
| 7 | + ConsentManagerWidgetProps, |
| 8 | + useConsentManager, |
| 9 | +} from '@c15t/nextjs'; |
| 10 | +import { useTranslations } from 'next-intl'; |
| 11 | +import { useCallback } from 'react'; |
| 12 | + |
| 13 | +import { Checkbox } from '@/vibes/soul/form/checkbox'; |
| 14 | +import { Button } from '@/vibes/soul/primitives/button'; |
| 15 | + |
| 16 | +function ConsentManagerDialogHeaderTitle() { |
| 17 | + const t = useTranslations('Components.ConsentManager.Dialog'); |
| 18 | + |
| 19 | + return ( |
| 20 | + <C15TConsentManagerDialog.HeaderTitle asChild> |
| 21 | + <div className="font-heading !text-2xl !tracking-normal">{t('title')}</div> |
| 22 | + </C15TConsentManagerDialog.HeaderTitle> |
| 23 | + ); |
| 24 | +} |
| 25 | + |
| 26 | +function ConsentManagerDialogHeaderDescription() { |
| 27 | + const t = useTranslations('Components.ConsentManager.Dialog'); |
| 28 | + |
| 29 | + return ( |
| 30 | + <C15TConsentManagerDialog.HeaderDescription asChild> |
| 31 | + <div className="font-body">{t('description')}</div> |
| 32 | + </C15TConsentManagerDialog.HeaderDescription> |
| 33 | + ); |
| 34 | +} |
| 35 | + |
| 36 | +export function ConsentManagerDialog(props: ConsentManagerDialogProps) { |
| 37 | + return ( |
| 38 | + <C15TConsentManagerDialog.Root {...props}> |
| 39 | + <C15TConsentManagerDialog.Card> |
| 40 | + <C15TConsentManagerDialog.Header> |
| 41 | + <ConsentManagerDialogHeaderTitle /> |
| 42 | + <ConsentManagerDialogHeaderDescription /> |
| 43 | + </C15TConsentManagerDialog.Header> |
| 44 | + <C15TConsentManagerDialog.Content> |
| 45 | + <ConsentManagerWidget /> |
| 46 | + </C15TConsentManagerDialog.Content> |
| 47 | + </C15TConsentManagerDialog.Card> |
| 48 | + </C15TConsentManagerDialog.Root> |
| 49 | + ); |
| 50 | +} |
| 51 | + |
| 52 | +function ConsentManagerWidgetRejectButton() { |
| 53 | + const t = useTranslations('Components.ConsentManager.Common'); |
| 54 | + |
| 55 | + return ( |
| 56 | + <C15TConsentManagerWidget.RejectButton asChild noStyle themeKey="widget.footer.reject-button"> |
| 57 | + <Button size="small" variant="tertiary"> |
| 58 | + {t('rejectAll')} |
| 59 | + </Button> |
| 60 | + </C15TConsentManagerWidget.RejectButton> |
| 61 | + ); |
| 62 | +} |
| 63 | + |
| 64 | +function ConsentManagerWidgetAcceptAllButton() { |
| 65 | + const t = useTranslations('Components.ConsentManager.Common'); |
| 66 | + |
| 67 | + return ( |
| 68 | + <C15TConsentManagerWidget.AcceptAllButton |
| 69 | + asChild |
| 70 | + noStyle |
| 71 | + themeKey="widget.footer.accept-button" |
| 72 | + > |
| 73 | + <Button size="small" variant="primary"> |
| 74 | + {t('acceptAll')} |
| 75 | + </Button> |
| 76 | + </C15TConsentManagerWidget.AcceptAllButton> |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +function ConsentManagerWidgetSaveButton() { |
| 81 | + const t = useTranslations('Components.ConsentManager.Common'); |
| 82 | + |
| 83 | + return ( |
| 84 | + <C15TConsentManagerWidget.SaveButton asChild noStyle themeKey="widget.footer.save-button"> |
| 85 | + <Button size="small" variant="secondary"> |
| 86 | + {t('save')} |
| 87 | + </Button> |
| 88 | + </C15TConsentManagerWidget.SaveButton> |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +function ConsentManagerAccordionItems() { |
| 93 | + const { selectedConsents, setSelectedConsent, getDisplayedConsents } = useConsentManager(); |
| 94 | + const t = useTranslations('Components.ConsentManager.ConsentTypes'); |
| 95 | + const handleConsentChange = useCallback( |
| 96 | + ( |
| 97 | + name: 'necessary' | 'functionality' | 'marketing' | 'measurement' | 'experience', |
| 98 | + checked: boolean, |
| 99 | + ) => { |
| 100 | + setSelectedConsent(name, checked); |
| 101 | + }, |
| 102 | + [setSelectedConsent], |
| 103 | + ); |
| 104 | + |
| 105 | + return getDisplayedConsents().map((consent) => ( |
| 106 | + <C15TConsentManagerWidget.AccordionItem |
| 107 | + key={consent.name} |
| 108 | + themeKey="widget.accordion.item" |
| 109 | + value={consent.name} |
| 110 | + > |
| 111 | + <C15TConsentManagerWidget.AccordionTrigger themeKey="widget.accordion.trigger"> |
| 112 | + <C15TConsentManagerWidget.AccordionTriggerInner themeKey="widget.accordion.trigger-inner"> |
| 113 | + <C15TConsentManagerWidget.AccordionArrow /> |
| 114 | + {t(`${consent.name}.title`)} |
| 115 | + </C15TConsentManagerWidget.AccordionTriggerInner> |
| 116 | + |
| 117 | + <Checkbox |
| 118 | + checked={selectedConsents[consent.name]} |
| 119 | + disabled={consent.disabled} |
| 120 | + onCheckedChange={(checked: boolean) => handleConsentChange(consent.name, checked)} |
| 121 | + onClick={(e: React.MouseEvent<HTMLButtonElement>) => e.stopPropagation()} |
| 122 | + onKeyDown={(e: React.KeyboardEvent<HTMLButtonElement>) => e.stopPropagation()} |
| 123 | + onKeyUp={(e: React.KeyboardEvent<HTMLButtonElement>) => e.stopPropagation()} |
| 124 | + /> |
| 125 | + </C15TConsentManagerWidget.AccordionTrigger> |
| 126 | + <C15TConsentManagerWidget.AccordionContent |
| 127 | + theme={{ |
| 128 | + content: { themeKey: 'widget.accordion.content' }, |
| 129 | + contentInner: { themeKey: 'widget.accordion.content-inner' }, |
| 130 | + }} |
| 131 | + > |
| 132 | + {t(`${consent.name}.description`)} |
| 133 | + </C15TConsentManagerWidget.AccordionContent> |
| 134 | + </C15TConsentManagerWidget.AccordionItem> |
| 135 | + )); |
| 136 | +} |
| 137 | + |
| 138 | +function ConsentManagerWidget(props: ConsentManagerWidgetProps) { |
| 139 | + return ( |
| 140 | + <C15TConsentManagerWidget.Root {...props}> |
| 141 | + <C15TConsentManagerWidget.Accordion type="multiple"> |
| 142 | + <ConsentManagerAccordionItems /> |
| 143 | + </C15TConsentManagerWidget.Accordion> |
| 144 | + <C15TConsentManagerWidget.Footer> |
| 145 | + <C15TConsentManagerWidget.FooterSubGroup themeKey="widget.footer.sub-group"> |
| 146 | + <ConsentManagerWidgetRejectButton /> |
| 147 | + <ConsentManagerWidgetAcceptAllButton /> |
| 148 | + </C15TConsentManagerWidget.FooterSubGroup> |
| 149 | + <ConsentManagerWidgetSaveButton /> |
| 150 | + </C15TConsentManagerWidget.Footer> |
| 151 | + </C15TConsentManagerWidget.Root> |
| 152 | + ); |
| 153 | +} |
0 commit comments