Skip to content

Commit

Permalink
🐛 Fix events being added erroneously and add to v2 #1824
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandolucchesi committed Aug 11, 2023
1 parent 2c87c79 commit e599a07
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
22 changes: 16 additions & 6 deletions sanityv3/plugins/asset-source-fotoware/src/FotowareAssetSource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ const FotowareAssetSource = forwardRef<HTMLDivElement>((props: any, ref) => {
return handleRequestError('Redirect state did not match request state', setError, 'auth', newWindow)
}

window.removeEventListener('message', handleAuthEvent)

return true
}

Expand Down Expand Up @@ -166,16 +164,28 @@ const FotowareAssetSource = forwardRef<HTMLDivElement>((props: any, ref) => {
}, [accessToken, asset])

useEffect(() => {
window.addEventListener('message', handleWidgetEvent)
window.addEventListener('message', handleAuthEvent)
if (accessToken) {
window.removeEventListener('message', handleAuthEvent)
}
}, [accessToken, handleAuthEvent])

useEffect(() => {
window.addEventListener('message', handleWidgetEvent)
setContainer(document.createElement('div'))

return () => {
window.removeEventListener('message', handleWidgetEvent)
}
}, [handleWidgetEvent])

useEffect(() => {
window.addEventListener('message', handleAuthEvent)
setContainer(document.createElement('div'))

return () => {
window.removeEventListener('message', handleAuthEvent)
}
}, [handleWidgetEvent, handleAuthEvent])
}, [handleAuthEvent])

useEffect(() => {
const authURL = getAuthURL(requestState)
Expand All @@ -197,7 +207,7 @@ const FotowareAssetSource = forwardRef<HTMLDivElement>((props: any, ref) => {
if (accessToken && newWindow.current) {
newWindow.current.close()
}
}, [container, requestState, accessToken])
}, [container, requestState, handleAuthEvent, accessToken])

if (!HAS_ENV_VARS) {
return (
Expand Down
24 changes: 17 additions & 7 deletions studio/plugins/asset-source-fotoware/src/FotowareAssetSource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ const FotowareAssetSource = forwardRef<HTMLDivElement>((props: any, ref) => {
return handleRequestError('Redirect state did not match request state', setError, 'auth', newWindow)
}

window.removeEventListener('message', handleAuthEvent)

return true
}

Expand Down Expand Up @@ -124,6 +122,7 @@ const FotowareAssetSource = forwardRef<HTMLDivElement>((props: any, ref) => {
})

if (!response || response.status !== 200) return

const data = await response.json()

const assetTitle = asset && asset?.builtinFields.find((item: FWAttributeField) => item.field === 'title')
Expand Down Expand Up @@ -165,16 +164,28 @@ const FotowareAssetSource = forwardRef<HTMLDivElement>((props: any, ref) => {
}, [accessToken, asset])

useEffect(() => {
window.addEventListener('message', handleWidgetEvent)
window.addEventListener('message', handleAuthEvent)
if (accessToken) {
window.removeEventListener('message', handleAuthEvent)
}
}, [accessToken, handleAuthEvent])

useEffect(() => {
window.addEventListener('message', handleWidgetEvent)
setContainer(document.createElement('div'))

return () => {
window.removeEventListener('message', handleWidgetEvent)
}
}, [handleWidgetEvent])

useEffect(() => {
window.addEventListener('message', handleAuthEvent)
setContainer(document.createElement('div'))

return () => {
window.removeEventListener('message', handleAuthEvent)
}
}, [handleWidgetEvent, handleAuthEvent])
}, [handleAuthEvent])

useEffect(() => {
const authURL = getAuthURL(requestState)
Expand All @@ -196,7 +207,7 @@ const FotowareAssetSource = forwardRef<HTMLDivElement>((props: any, ref) => {
if (accessToken && newWindow.current) {
newWindow.current.close()
}
}, [container, requestState, accessToken])
}, [container, requestState, handleAuthEvent, accessToken])

if (!HAS_ENV_VARS) {
return (
Expand All @@ -220,7 +231,6 @@ const FotowareAssetSource = forwardRef<HTMLDivElement>((props: any, ref) => {
if (accessToken && iframeURL && !loading) {
return <FotowareWidget onClose={onClose} url={iframeURL} iframeRef={iframeRef} />
}

return (
<Dialog width={0} id="fotowareAssetSource" header="Select image from Fotoware" onClose={onClose} ref={ref}>
{container && !accessToken && createPortal(props.children, container)}
Expand Down

0 comments on commit e599a07

Please sign in to comment.