@@ -12,7 +12,7 @@ import Button from '../../ui/Button'
1212import { Column , Columns } from '../../ui/Columns'
1313import TextInput from '../../ui/form/TextInput'
1414import Modal , { ModalProps } from '../../ui/Modal'
15- import { ChannelType , TemplateProofParams } from '../../types'
15+ import { ChannelType , Template , TemplateProofParams } from '../../types'
1616import FormWrapper from '../../ui/form/FormWrapper'
1717import SourceEditor from '../../ui/SourceEditor'
1818import { useTranslation } from 'react-i18next'
@@ -43,41 +43,25 @@ const SendProof = ({ open, onClose, onSubmit, type }: SendProofProps) => {
4343 )
4444}
4545
46- export default function CampaignPreview ( ) {
46+ interface TemplatePreviewProps {
47+ template : Template
48+ }
4749
50+ const TemplatePreview = ( { template } : TemplatePreviewProps ) => {
4851 const [ project ] = useContext ( ProjectContext )
4952 const { t } = useTranslation ( )
50- const { currentTemplate } = useContext ( TemplateContext )
5153 const showAddState = useState ( false )
5254 const [ isUserLookupOpen , setIsUserLookupOpen ] = useState ( false )
5355 const [ templatePreviewError , setTemplatePreviewError ] = useState < string | undefined > ( undefined )
5456 const [ isSendProofOpen , setIsSendProofOpen ] = useState ( false )
5557 const [ proofResponse , setProofResponse ] = useState < any > ( undefined )
56-
57- if ( ! currentTemplate ) {
58- return ( < >
59- < Heading title = { t ( 'preview' ) } size = "h3" actions = {
60- < >
61- < VariantSelector />
62- < LocaleSelector showAddState = { showAddState } />
63- </ >
64- } />
65- < Alert
66- variant = "plain"
67- title = { t ( 'add_template' ) }
68- body = { t ( 'no_template_alert_body' ) }
69- actions = { < Button onClick = { ( ) => showAddState [ 1 ] ( true ) } > { t ( 'create_template' ) } </ Button > }
70- />
71- </ > )
72- }
73-
74- const [ data , setData ] = useState ( currentTemplate . data )
58+ const [ data , setData ] = useState ( template . data )
7559 const [ value , setValue ] = useState < string | undefined > ( '{\n "user": {},\n "event": {}\n}' )
76- useEffect ( ( ) => { handleEditorChange ( value ) } , [ value , currentTemplate ] )
60+ useEffect ( ( ) => { handleEditorChange ( value ) } , [ value , template ] )
7761
7862 const handleEditorChange = useMemo ( ( ) => debounce ( async ( value ?: string ) => {
7963 try {
80- const { data } = await api . templates . preview ( project . id , currentTemplate . id , JSON . parse ( value ?? '{}' ) )
64+ const { data } = await api . templates . preview ( project . id , template . id , JSON . parse ( value ?? '{}' ) )
8165 setTemplatePreviewError ( undefined )
8266 setData ( data )
8367 } catch ( error : any ) {
@@ -87,11 +71,11 @@ export default function CampaignPreview() {
8771 }
8872 setTemplatePreviewError ( error . message )
8973 }
90- } ) , [ currentTemplate ] )
74+ } ) , [ template ] )
9175
9276 const handleSendProof = async ( recipient : string ) => {
9377 try {
94- const response = await api . templates . proof ( project . id , currentTemplate . id , {
78+ const response = await api . templates . proof ( project . id , template . id , {
9579 variables : JSON . parse ( value ?? '{}' ) ,
9680 recipient,
9781 } )
@@ -105,7 +89,7 @@ export default function CampaignPreview() {
10589 return
10690 }
10791 setIsSendProofOpen ( false )
108- currentTemplate . type === 'webhook'
92+ template . type === 'webhook'
10993 ? toast . success ( 'Webhook test has been successfully sent!' )
11094 : toast . success ( 'Template proof has been successfully sent!' )
11195 }
@@ -137,7 +121,7 @@ export default function CampaignPreview() {
137121 </ Column >
138122 < Column fullscreen = { true } >
139123 < Heading title = "Preview" size = "h4" actions = {
140- currentTemplate . type === 'webhook'
124+ template . type === 'webhook'
141125 ? < Button
142126 size = "small"
143127 variant = "secondary"
@@ -152,7 +136,7 @@ export default function CampaignPreview() {
152136 title = { t ( 'template_error' ) } >
153137 { t ( 'template_handlebars_error' ) } { templatePreviewError }
154138 </ Alert > }
155- < Preview template = { { type : currentTemplate . type , data } } response = { proofResponse } />
139+ < Preview template = { { type : template . type , data } } response = { proofResponse } />
156140 </ Column >
157141 </ Columns >
158142
@@ -169,7 +153,32 @@ export default function CampaignPreview() {
169153 open = { isSendProofOpen }
170154 onClose = { setIsSendProofOpen }
171155 onSubmit = { handleSendProof }
172- type = { currentTemplate . type } />
156+ type = { template . type } />
173157 </ >
174158 )
175159}
160+
161+ export default function CampaignPreview ( ) {
162+ const { t } = useTranslation ( )
163+ const { currentTemplate } = useContext ( TemplateContext )
164+ const showAddState = useState ( false )
165+
166+ if ( ! currentTemplate ) {
167+ return < >
168+ < Heading title = { t ( 'preview' ) } size = "h3" actions = {
169+ < >
170+ < VariantSelector />
171+ < LocaleSelector showAddState = { showAddState } />
172+ </ >
173+ } />
174+ < Alert
175+ variant = "plain"
176+ title = { t ( 'add_template' ) }
177+ body = { t ( 'no_template_alert_body' ) }
178+ actions = { < Button onClick = { ( ) => showAddState [ 1 ] ( true ) } > { t ( 'create_template' ) } </ Button > }
179+ />
180+ </ >
181+ }
182+
183+ return < TemplatePreview template = { currentTemplate } />
184+ }
0 commit comments