import React, { PureComponent } from 'react' import styled from '@emotion/styled' import { css } from '@emotion/react' import Dialog from './dialog' import { DefaultButton, GreenButton } from './buttons' import { Destination, CustomCategories, CategoryPreferences, PreferenceDialogTemplate } from '../types' const hideOnMobile = css` @media (max-width: 600px) { display: none; } ` const TableScroll = styled('div')` overflow-x: auto; margin-top: 16px; ` const Table = styled('table')` border-collapse: collapse; font-size: 12px; ` const ColumnHeading = styled('th')` background: #f7f8fa; color: #1f4160; font-weight: 600; text-align: left; border-width: 2px; ` const RowHeading = styled('th')` font-weight: normal; text-align: left; ` const Row = styled('tr')` th, td { vertical-align: top; padding: 8px 12px; border: 1px solid rgba(67, 90, 111, 0.114); } td { border-top: none; } ` const InputCell = styled('td')` input { vertical-align: middle; } label { display: block; margin-bottom: 4px; white-space: nowrap; } ` interface PreferenceDialogProps { innerRef: (element: HTMLElement | null) => void onCancel: () => void onSave: () => void onChange: (name: string, value: boolean) => void marketingDestinations: Destination[] advertisingDestinations: Destination[] functionalDestinations: Destination[] marketingAndAnalytics?: boolean | null advertising?: boolean | null functional?: boolean | null customCategories?: CustomCategories destinations: Destination[] preferences: CategoryPreferences title: React.ReactNode content: React.ReactNode preferencesDialogTemplate?: PreferenceDialogTemplate } export default class PreferenceDialog extends PureComponent { static displayName = 'PreferenceDialog' static defaultProps = { marketingAndAnalytics: null, advertising: null, functional: null } render() { const { innerRef, onCancel, marketingDestinations, advertisingDestinations, functionalDestinations, marketingAndAnalytics, advertising, functional, customCategories, destinations, title, content, preferences, preferencesDialogTemplate } = this.props const { headings, checkboxes, actionButtons } = preferencesDialogTemplate! const functionalInfo = preferencesDialogTemplate?.categories!.find(c => c.key === 'functional') const marketingInfo = preferencesDialogTemplate?.categories!.find(c => c.key === 'marketing') const advertisingInfo = preferencesDialogTemplate?.categories!.find( c => c.key === 'advertising' ) const essentialInfo = preferencesDialogTemplate?.categories!.find(c => c.key === 'essential') const buttons = (
{actionButtons!.cancelValue} {actionButtons!.saveValue}
) return ( {content} {headings!.allowValue} {headings!.categoryValue} {headings!.purposeValue} {headings!.toolsValue} {!customCategories && ( <> {functionalInfo?.name} {marketingInfo?.name} {advertisingInfo?.name} )} {customCategories && Object.entries(customCategories).map( ([categoryName, { integrations, purpose }]) => ( {categoryName} ) )} {essentialInfo?.name}

{functionalInfo?.description}

{functionalInfo?.example}

{functionalDestinations.map(d => d.name).join(', ')}

{marketingInfo?.description}

{marketingInfo?.example}

{marketingDestinations.map(d => d.name).join(', ')}

{advertisingInfo?.description}

{advertisingInfo?.example}

{advertisingDestinations.map(d => d.name).join(', ')}

{purpose}

{destinations .filter(d => integrations.includes(d.id)) .map(d => d.name) .join(', ')} N/A

{essentialInfo?.description}

{essentialInfo?.example}

) } handleChange = e => { const { onChange } = this.props onChange(e.target.name, e.target.value === 'true') } handleSubmit = (e: React.FormEvent) => { const { onSave, preferences, marketingAndAnalytics, advertising, functional, customCategories } = this.props e.preventDefault() // Safe guard against browsers that don't prevent the // submission of invalid forms (Safari < 10.1) if ( !customCategories && (marketingAndAnalytics === null || advertising === null || functional === null) ) { return } // Safe guard against custom categories being null if ( customCategories && Object.keys(customCategories).some(category => preferences[category] === null) ) { return } onSave() } }