まだ行っていない場合は、PWA Kit プロジェクトにhooks in {overridesDir}/appというフォルダーを作成します。
hooksフォルダーにuse-dnt-notification.jsというファイルを作成します。
このコードをコピーしてuse-dnt-notification.jsに貼り付けます。この例では、selectedDntを使用して、DntNotificationコンポーネントによって制御されるカスタマイズされたバナーをレンダリングします。また、updateDnt関数を使用して、買い物客の追跡設定をブール値として適用します。「Do Not Track値」を参照してください。
1import React, {useEffect}from 'react'2import PropTypes from 'prop-types'3import{FormattedMessage, useIntl}from 'react-intl'4import{5 Button,6 Modal,7 ModalContent,8 ModalBody,9 ModalCloseButton,10 useDisclosure,11 Heading,12 Stack,13 Text,14 Flex15}from '@salesforce/retail-react-app/app/components/shared/ui'16import{useDNT}from '@salesforce/commerce-sdk-react'17import{useLocation}from 'react-router-dom'1819export const DntNotification = ({isOpen, onOpen, onClose})=>{20 const{selectedDnt, updateDNT} = useDNT()21 const{formatMessage} = useIntl()22 const location = useLocation()2324 useEffect(()=>{25 if(selectedDnt === undefined){26 onOpen()27}else{28 onClose()29}30}, [location, selectedDnt])3132 const onCloseNotification = ()=>{33 updateDNT(null)34 onClose()35}3637 return(38<Modal39 size="sm"40 data-testid="sf-dnt-notification"41 blockScrollOnMount={false}42 closeOnOverlayClick={false}43 trapFocus={false}44 isOpen={isOpen}45 onOpen={onOpen}46 onClose={onCloseNotification}47>48<ModalContent49 position="fixed"50 bottom="4"51 right="4"52 maxW="400px"53 pointerEvents="all"54 containerProps={{55 pointerEvents: 'none'56}}57 border="2px solid"58>59<ModalCloseButton60 aria-label={formatMessage({61 id: 'dnt_notification.button.assistive_msg.close',62 defaultMessage: 'Close consent tracking form'63})}64 />65<ModalBody pb={8} bg="white" marginTop={4}>66<Heading as="h3" fontSize={25} width="100%" marginBottom={5}>67<FormattedMessage68 defaultMessage="Your Privacy Matters to Us"69 id="dnt_notification.title"70 />71</Heading>72<Flex direction="column">73<Text>74<FormattedMessage75 defaultMessage="We collect data to improve your experience, deliver personalized content or ads, and better understand how our site is used. By selecting 'Accept,' you agree to our use of tracking technologies. "76 id="dnt_notification.description"77 />78</Text>79<Stack direction="column" spacing={4} mt={4} align="flex-end">80<>81<Button82 onClick={()=>{83 updateDNT(true)84 onClose()85}}86 width="100%"87>88<FormattedMessage defaultMessage="Decline" id="dnt_notification.button.decline" />89</Button>90<Button91 onClick={()=>{92 updateDNT(false)93 onClose()94}}95 width="100%"96>97<FormattedMessage defaultMessage="Accept" id="dnt_notification.button.accept" />98</Button>99</>100</Stack>101</Flex>102</ModalBody>103</ModalContent>104</Modal>105)106}107108DntNotification.propTypes = {109 isOpen: PropTypes.bool.isRequired,110 onOpen: PropTypes.func.isRequired,111 onClose: PropTypes.func.isRequired112}113114/**115 *116 * @returns{Object} - Object props to be passed into the DntNotification component117 */118export const useDntNotification = ()=>{119 const{isOpen, onOpen, onClose} = useDisclosure()120121 return{122 isOpen,123 onOpen,124 onClose125}126}
1import{2 DntNotification,3 useDntNotification4}from '{overridesDir}/app/hooks/use-dnt-notification'56const App = (props)=>{7 ...8 const dntNotification = useDntNotification()9 ...1011 return(12 ...13 // Put the consent tracking banner in your chosen location in your code by adding the dntNotification component.14<DntNotification{...dntNotification} />15 ...16)17}