Skip to content

Commit

Permalink
update document title and change filename of downloaded data
Browse files Browse the repository at this point in the history
  • Loading branch information
ddecrulle committed Mar 21, 2024
1 parent 33dcd06 commit d6fb527
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/components/Orchestrator/CustomPages/EndPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export function EndPage(props: { date: number }) {
const { date } = props
export function EndPage(props: { date: number | undefined }) {
const { date = Date.now() } = props
const formatedData = new Date(date).toLocaleString()
return (
<>
Expand Down
12 changes: 8 additions & 4 deletions src/components/Orchestrator/Orchestrator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
type LunaticSource,
type LunaticError,
} from '@inseefr/lunatic'
import { slotComponents } from '@inseefr/lunatic-dsfr'
import { fr } from '@codegouvfr/react-dsfr'
import { downloadAsJson } from 'utils/downloadAsJson'
import { useNavigate } from '@tanstack/react-router'
Expand All @@ -19,6 +18,7 @@ import { assert } from 'tsafe/assert'
import type { SurveyUnitData } from 'model/SurveyUnitData'
import type { StateData } from 'model/StateData'
import { isBlockingError, isSameErrors } from './utils/controls'
import { slotComponents } from './slotComponents'

export function Orchestrator(props: {
source: LunaticSource
Expand Down Expand Up @@ -95,7 +95,7 @@ export function Orchestrator(props: {
},
})

const getStateData = (): StateData => {
const getCurrentStateData = (): StateData => {
switch (currentPage) {
case 'endPage':
return { date: Date.now(), currentPage, state: 'VALIDATED' }
Expand All @@ -115,9 +115,11 @@ export function Orchestrator(props: {
downloadAsJson<SurveyUnitData>({
dataToDownload: {
data: getData(false),
stateData: getStateData(),
stateData: getCurrentStateData(),
personalization: surveyUnitData?.personalization,
},
//The label of source is not dynamic
filename: `${source.label.value}-${new Date().toLocaleDateString()}`,
})
}

Expand Down Expand Up @@ -158,7 +160,9 @@ export function Orchestrator(props: {
/>
)}
{currentPage === 'validationPage' && <Validation />}
{currentPage === 'endPage' && <EndPage date={Date.now()} />}
{currentPage === 'endPage' && (
<EndPage date={surveyUnitData?.stateData?.date} />
)}
<ValidationModal actions={validationModalActions} />
</div>
</Navigation>
Expand Down
16 changes: 16 additions & 0 deletions src/components/Orchestrator/slotComponents.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { LunaticSlotComponents } from '@inseefr/lunatic'
import { slotComponents as dsfrSlotsComponents } from '@inseefr/lunatic-dsfr'
import { useSequenceTitle } from 'hooks/useDocumentTitle'
import { assert } from 'tsafe/assert'

const Sequence: LunaticSlotComponents['Sequence'] = (props) => {
const DsfrSequence = dsfrSlotsComponents['Sequence']

assert(DsfrSequence !== undefined)

useSequenceTitle(props.label)

return <DsfrSequence {...props} />
}

export const slotComponents = { ...dsfrSlotsComponents, Sequence }
17 changes: 16 additions & 1 deletion src/hooks/useDocumentTitle.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import { useEffect } from 'react'
import { useEffect, type ReactNode } from 'react'
import { renderToString } from 'react-dom/server'

export function useDocumentTitle(title: string) {
useEffect(() => {

const prevTitle = document.title

document.title = title

return () => {
document.title = prevTitle
}
})
}

export function useSequenceTitle(sequenceLabel: ReactNode) {
//TODO get the name of the site (in metadata.json I hope)
/**
* This is a dirty, sequenceLabel is today a LabelType VTL|MD,
* it will be replaced by VTL only so we will be able to just sequenceLabel.toString()
*/
return useDocumentTitle(
`${renderToString(sequenceLabel)} | Filière d'Enquête`
)
}
3 changes: 2 additions & 1 deletion src/pages/Accessibility/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const accessibilityRoute = createRoute({
path: 'accessibilite',
component: AccessibilityPage,
loader: () => {
document.title = 'Accessibilité'
//TODO get name (Filière d'Enquête) in metadata
document.title = "Accessibilité | Filière d'Enquête"
},
})
3 changes: 2 additions & 1 deletion src/pages/Legals/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const legalsRoute = createRoute({
path: 'mentions-legales',
component: LegalPage,
loader: () => {
document.title = 'Mentions Légales'
//TODO get name (Filière d'Enquête) in metadata
document.title = "Mentions Légales | Filière d'Enquête"
},
})
3 changes: 2 additions & 1 deletion src/pages/NavigationAssistance/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const navigationAssistanceRoute = createRoute({
path: 'aide-a-la-navigation',
component: NavigationAssistancePage,
loader: () => {
document.title = 'Aide à la navigation'
//TODO get name (Filière d'Enquête) in metadata
document.title = "Aide à la navigation | Filière d'Enquête"
},
})
3 changes: 2 additions & 1 deletion src/pages/Security/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const securityRoute = createRoute({
path: 'securite',
component: SecurityPage,
loader: () => {
document.title = 'Sécurité'
//TODO get name (Filière d'Enquête) in metadata
document.title = "Sécurité | Filière d'Enquête"
},
})
3 changes: 2 additions & 1 deletion src/pages/SiteMap/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const siteMapRoute = createRoute({
path: 'plan-du-site',
component: SiteMapPage,
loader: () => {
document.title = 'Plan du site'
//TODO get name (Filière d'Enquête) in metadata
document.title = "Plan du site | Filière d'Enquête"
},
})
3 changes: 2 additions & 1 deletion src/pages/Visualize/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export const visualizeRoute = createRoute({
context: { queryClient },
deps: { sourceUrl, surveyUnitDataUrl, metadataUrl, nomenclature },
}) => {
document.title = 'Visualisation'
//TODO get name (Filière d'Enquête) in metadata
document.title = "Visualisation | Filière d'Enquête"

if (!sourceUrl) {
return
Expand Down

0 comments on commit d6fb527

Please sign in to comment.