Skip to content

Commit

Permalink
refactor: use the new refactored migrateSituation from `@publicodes/t…
Browse files Browse the repository at this point in the history
…ools` (#577)
  • Loading branch information
Clemog authored Jun 18, 2024
2 parents 4d8a06d + 038881b commit 8cf5ced
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 234 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"@next/bundle-analyzer": "^14.1.0",
"@next/mdx": "^14.0.2",
"@publicodes/react-ui": "^1.2.0",
"@publicodes/tools": "^1.1.2",
"@publicodes/tools": "^1.2.0",
"@sentry/nextjs": "^7.80.0",
"@sentry/react": "^7.80.0",
"@tanstack/react-query": "^5.28.4",
Expand Down
5 changes: 3 additions & 2 deletions src/app/_components/MainLayoutProviders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import { IframeOptionsProvider } from '@/app/_components/mainLayoutProviders/IframeOptionsContext'
import { UserProvider } from '@/publicodes-state'
import { MigrationType, RegionFromGeolocation } from '@/publicodes-state/types'
import { RegionFromGeolocation } from '@/publicodes-state/types'
import { Migration } from '@publicodes/tools/migration'
import { PropsWithChildren } from 'react'
import MainHooks from './mainLayoutProviders/MainHooks'
import { PreventNavigationProvider } from './mainLayoutProviders/PreventNavigationProvider'
Expand All @@ -11,7 +12,7 @@ import SimulationSyncProvider from './mainLayoutProviders/SimulationSyncProvider

type Props = {
region: RegionFromGeolocation
migrationInstructions: MigrationType
migrationInstructions: Migration
}
export default function MainLayoutProviders({
children,
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/modelFetching/getMigrationInstructions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MigrationType } from '@/publicodes-state/types'
import migration from '@incubateur-ademe/nosgestesclimat/public/migration.json'
import { Migration } from '@publicodes/tools/migration'
import { importPreviewFile } from './importPreviewFile'

type Props = {
Expand All @@ -10,7 +10,7 @@ type Props = {
*/
export async function getMigrationInstructions({
PRNumber,
}: Props = {}): Promise<MigrationType> {
}: Props = {}): Promise<Migration> {
if (PRNumber) {
const fileName = `migration.json`
return importPreviewFile({ fileName, PRNumber })
Expand Down
20 changes: 8 additions & 12 deletions src/helpers/simulation/generateSimulation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { migrateSimulation } from '@/publicodes-state/helpers/migrateSimulation'
import { MigrationType, Simulation } from '@/publicodes-state/types'
import { Simulation } from '@/publicodes-state/types'
import { Migration } from '@publicodes/tools/migration'
import { captureException } from '@sentry/react'
import { v4 as uuidv4 } from 'uuid'

Expand All @@ -18,7 +19,7 @@ export function generateSimulation({
savedViaEmail,
migrationInstructions,
}: Partial<Simulation> & {
migrationInstructions?: MigrationType
migrationInstructions?: Migration
} = {}): Simulation {
let simulation = {
id,
Expand All @@ -35,16 +36,11 @@ export function generateSimulation({
savedViaEmail,
} as Simulation

if (migrationInstructions) {
try {
simulation = migrateSimulation({
simulation,
migrationInstructions,
})
} catch (error) {
console.warn('Error trying to migrate LocalStorage:', error)
captureException(error)
}
try {
simulation = migrateSimulation(simulation, migrationInstructions)
} catch (error) {
console.warn('Error trying to migrate LocalStorage:', error)
captureException(error)
}

return simulation
Expand Down
53 changes: 31 additions & 22 deletions src/publicodes-state/helpers/migrateSimulation.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { migrateSituation } from '@publicodes/tools/migration'
import { MigrationType, Simulation } from '../types'
import { Migration, migrateSituation } from '@publicodes/tools/migration'
import { Simulation } from '../types'

type Props = {
simulation: Simulation
migrationInstructions: MigrationType
}
type Return = Simulation

export function migrateSimulation({
simulation: oldSimulation,
migrationInstructions,
}: Props): Return {
const simulation = JSON.parse(JSON.stringify(oldSimulation)) as Simulation
export function migrateSimulation(
simulation: Simulation & { group?: string; poll?: string },
migrationInstructions: Migration | undefined
): Simulation {
if (migrationInstructions) {
simulation.situation = migrateSituation(
simulation.situation,
migrationInstructions
)

const { situationMigrated, foldedStepsMigrated } = migrateSituation({
situation: simulation.situation,
foldedSteps: simulation.foldedSteps,
migrationInstructions,
})
// NOTE: folded steps (i.e. answered rules) are can be map to a situation,
// where the keys are the rule names and the value is undefined.
simulation.foldedSteps = Object.keys(
migrateSituation(
Object.fromEntries(
simulation.foldedSteps.map((step) => [step, undefined])
),
migrationInstructions
)
)
}
// If group or poll is defined, we convert it to groups or polls and delete it
if (simulation.group) {
simulation.groups = [simulation.group]
delete simulation.group
}

simulation.situation = situationMigrated
simulation.foldedSteps = foldedStepsMigrated
if (simulation.poll) {
simulation.polls = [simulation.poll]
delete simulation.poll
}

return simulation
}
27 changes: 0 additions & 27 deletions src/publicodes-state/helpers/migration/convertSimulation.ts

This file was deleted.

137 changes: 0 additions & 137 deletions src/publicodes-state/helpers/migration/filterSimulation.ts

This file was deleted.

10 changes: 4 additions & 6 deletions src/publicodes-state/hooks/useUser/useSimulations.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
'use client'

import { generateSimulation } from '@/helpers/simulation/generateSimulation'
import { Migration } from '@publicodes/tools/migration'
import { Dispatch, SetStateAction, useCallback, useMemo } from 'react'
import {
MigrationType,
Simulation,
UpdateCurrentSimulationProps,
} from '../../types'
import { Simulation, UpdateCurrentSimulationProps } from '../../types'

type Props = {
simulations: Simulation[]
setSimulations: Dispatch<SetStateAction<Simulation[]>>
currentSimulationId: string
setCurrentSimulationId: Dispatch<SetStateAction<string>>
migrationInstructions: MigrationType
migrationInstructions: Migration
}

export default function useSimulations({
simulations,
setSimulations,
Expand Down
5 changes: 3 additions & 2 deletions src/publicodes-state/providers/userProvider/context.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use client'

import { Migration } from '@publicodes/tools/migration'
import { Dispatch, SetStateAction, createContext } from 'react'
import { v4 as uuid } from 'uuid'
import { MigrationType, Simulation, Tutorials, User } from '../../types'
import { Simulation, Tutorials, User } from '../../types'

type UserContextType = {
user: User
Expand All @@ -13,7 +14,7 @@ type UserContextType = {
setSimulations: Dispatch<SetStateAction<Simulation[]>>
currentSimulationId: string
setCurrentSimulationId: Dispatch<SetStateAction<string>>
migrationInstructions: MigrationType
migrationInstructions: Migration
}

export default createContext<UserContextType>({
Expand Down
5 changes: 3 additions & 2 deletions src/publicodes-state/providers/userProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import { PropsWithChildren } from 'react'

import { MigrationType, RegionFromGeolocation } from '@/publicodes-state/types'
import { RegionFromGeolocation } from '@/publicodes-state/types'
import { Migration } from '@publicodes/tools/migration'
import UserContext from './context'
import useUpdateOldLocalStorage from './useOldLocalStorage'
import usePersistentSimulations from './usePersistentSimulations'
Expand All @@ -21,7 +22,7 @@ type Props = {
/**
* The migration instructions for old localstorage
*/
migrationInstructions: MigrationType
migrationInstructions: Migration
}
export default function UserProvider({
children,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { generateSimulation } from '@/helpers/simulation/generateSimulation'
import { getIsLocalStorageAvailable } from '@/utils/getIsLocalStorageAvailable'
import { Migration } from '@publicodes/tools/migration'
import { useEffect, useState } from 'react'
import { MigrationType, Simulation } from '../../types'
import { Simulation } from '../../types'

const isLocalStorageAvailable = getIsLocalStorageAvailable()

type Props = {
storageKey: string
migrationInstructions: MigrationType
migrationInstructions: Migration
}
export default function usePersistentSimulations({
storageKey,
Expand Down
Loading

0 comments on commit 8cf5ced

Please sign in to comment.