Skip to content

Commit

Permalink
refactor: define params query in context
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulFarault committed Jan 31, 2023
1 parent 583dcf8 commit 5681f20
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 21 deletions.
12 changes: 0 additions & 12 deletions src/app/dashboard/services/layout.tsx

This file was deleted.

11 changes: 3 additions & 8 deletions src/components/Services/ComponentsNav/ComponentsNav.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { useRouter } from 'next/router'
import { useSelectService } from 'src/features/variables'
import { getFirstElementIfArray } from 'src/utils'
import { useParamsContext } from '../useParamsContext'
import { ComponentsDropdown } from './ComponentsDropdown'
import { ComponentsTabs } from './ComponentsTabs'

export function ComponentsNav() {
const {
query: { serviceId: tempServiceId, componentId: tempComponentId },
isReady,
} = useRouter()
const currentComponentId = isReady && getFirstElementIfArray(tempComponentId)
const currentServiceId = isReady && getFirstElementIfArray(tempServiceId)
const { serviceId: currentServiceId, componentId: currentComponentId } =
useParamsContext()

const [usedComponents, unusedComponents] = useSelectService(
currentServiceId
Expand Down
25 changes: 25 additions & 0 deletions src/components/Services/Layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useRouter } from 'next/router'
import { PageTitle } from 'src/components/Layout/primitives/PageTitle'
import { ComponentsNav } from 'src/components/Services/ComponentsNav'
import { getFirstElementIfArray } from 'src/utils'
import { ParamsContextProvider } from './ParamsContext'

export function Layout({ children }) {
const {
query: { serviceId: tempServiceId, componentId: tempComponentId },
isReady,
} = useRouter()
const currentServiceId = isReady && getFirstElementIfArray(tempServiceId)
const currentComponentId = isReady && getFirstElementIfArray(tempComponentId)

return (
<ParamsContextProvider
serviceId={currentServiceId}
componentId={currentComponentId}
>
<PageTitle>Variables configuration</PageTitle>
<ComponentsNav />
{children}
</ParamsContextProvider>
)
}
14 changes: 14 additions & 0 deletions src/components/Services/ParamsContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { createContext } from 'react'

export const ParamsContext = createContext<{
serviceId: string
componentId?: string
}>(null)

export function ParamsContextProvider({ children, serviceId, componentId }) {
return (
<ParamsContext.Provider value={{ serviceId, componentId }}>
{children}
</ParamsContext.Provider>
)
}
1 change: 1 addition & 0 deletions src/components/Services/index.tsx
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './ComponentsNav'
export * from './VariablesDisplay'
export * from './Layout'
12 changes: 12 additions & 0 deletions src/components/Services/useParamsContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useContext } from 'react'
import { ParamsContext } from './ParamsContext'

export const useParamsContext = () => {
const paramContext = useContext(ParamsContext)
if (!paramContext) {
throw new Error(
'useParamsContext must be used within a ParamsContextProvider'
)
}
return paramContext
}
3 changes: 2 additions & 1 deletion src/pages/services/[serviceId].tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useRouter } from 'next/router'
import { ReactElement } from 'react'
import DashboardLayout from 'src/app/dashboard/layout'
import ServiceLayout from 'src/app/dashboard/services/layout'

import {
Layout as ServiceLayout,
ValidateBar,
VariablesContextProvider,
VariablesDisplay,
Expand Down

0 comments on commit 5681f20

Please sign in to comment.