Skip to content

Commit

Permalink
feat: get config from parent window
Browse files Browse the repository at this point in the history
  • Loading branch information
MaGOs92 committed Feb 8, 2024
1 parent 20ba8ad commit c8ddab3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const PublishedFromMoissonneur = ({ currentRevision }: PublishedFromMoiss
const dataset = await getDataset(id[1])

setOrganization(dataset.organization)
setIsOutdated(config?.commune?.outdatedHarvestSources?.includes(sourceId) || false)
setIsOutdated(config?.communes?.outdatedHarvestSources?.includes(sourceId) || false)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const PublishedFromOtherClient = ({ currentRevision }: PublishedFromOther

const isOutdated = useMemo(() => {
return (
config?.commune?.outdatedApiDepotClients?.includes(currentRevision?.client?._id || '') ||
config?.communes?.outdatedApiDepotClients?.includes(currentRevision?.client?._id || '') ||
false
)
}, [config, client])
Expand Down
27 changes: 22 additions & 5 deletions src/contexts/configContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export interface BALWidgetConfig {
welcomeBlockTitle: string
topArticles: BALWidgetLink[]
}
commune: {
communes: {
welcomeBlockTitle: string
outdatedApiDepotClients: string[]
outdatedHarvestSources: string[]
}
Expand All @@ -35,18 +36,34 @@ interface ConfigProviderProps {
export function ConfigProvider({ children }: ConfigProviderProps) {
const [config, setConfig] = useState<BALWidgetConfig | null>(null)
const { getConfig } = useBALAdmin()
const [isLoading, setIsLoading] = useState(false)
const [isLoading, setIsLoading] = useState(true)
const isEmbeddedInIframe = window !== window.parent

useEffect(() => {
function getConfigFromParent(event: { data: { type: string; content: BALWidgetConfig } }) {
if (event.data.type === 'BAL_WIDGET_CONFIG') {
setConfig(event.data.content)
setIsLoading(false)
}
}

async function fetchConfig() {
setIsLoading(true)
const config = await getConfig()
setConfig(config as BALWidgetConfig)
setIsLoading(false)
}

fetchConfig()
}, [getConfig])
if (isEmbeddedInIframe) {
window.parent.postMessage({ type: 'BAL_WIDGET_READY' }, '*')
window.addEventListener('message', getConfigFromParent)
} else {
fetchConfig()
}

return () => {
isEmbeddedInIframe && window.removeEventListener('message', getConfigFromParent)
}
}, [isEmbeddedInIframe, getConfig])

return <ConfigContext.Provider value={config}>{!isLoading && children}</ConfigContext.Provider>
}
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Welcome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function WecomePage() {

return (
<AnimatedPage animation='prev'>
<HelpBlock label='Vous êtes une commune ?'>
<HelpBlock label={config?.communes?.welcomeBlockTitle || ''}>
<CommuneAutocomplete onChange={onSelectCommune} />
</HelpBlock>
<HelpBlock label={config?.gitbook?.welcomeBlockTitle || ''}>
Expand Down

0 comments on commit c8ddab3

Please sign in to comment.