Skip to content

Commit

Permalink
fix(BUX-112): improvements to CR
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-4chain committed Jul 24, 2023
1 parent 13937f3 commit ddd68ab
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 29 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ services:
image: postgres:15.2-alpine
volumes:
- pgdata:/var/lib/postgresql/data
# - ./data/sql/db:/var/lib/postgresql/data:Z
environment:
- POSTGRES_NAME=postgres
- POSTGRES_USER=postgres
Expand Down
1 change: 0 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ export const App = () => {
useEffect(() => {
setError('')
apiUrl &&
apiUrl &&
getUser(apiUrl)
.then((response) => {
const currentUserData: LoggedInUser = {
Expand Down
10 changes: 0 additions & 10 deletions src/providers/config/hooks.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/providers/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './hooks'
export * from './provider'
14 changes: 11 additions & 3 deletions src/providers/config/provider.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { loadConfigFromFile } from '@/utils/loadConfig'
import { FC, ReactNode, createContext, useEffect, useState } from 'react'
import { FC, ReactNode, createContext, useContext, useEffect, useState } from 'react'

export type ConfigType = {
apiUrl: string | undefined
paymailDomain: string | undefined
}

export type ConfigContextType = {
type ConfigContextType = {
config: ConfigType
setConfig: React.Dispatch<React.SetStateAction<ConfigType>>
}

export const ConfigContext = createContext<ConfigContextType | undefined>(undefined)
const ConfigContext = createContext<ConfigContextType | undefined>(undefined)
ConfigContext.displayName = 'ConfigContext'

type ConfigProviderProps = {
Expand All @@ -37,3 +37,11 @@ export const ConfigProvider: FC<ConfigProviderProps> = ({ children }) => {

return <ConfigContext.Provider value={value}>{children}</ConfigContext.Provider>
}

export const useConfig = () => {
const ctx = useContext(ConfigContext)
if (!ctx) {
throw new Error('useConfig must be use within ConfigProvider')
}
return ctx
}
31 changes: 17 additions & 14 deletions src/utils/loadConfig.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
import { ConfigType } from '@/providers'

export const loadConfigFromFile = async () => {
let defaultConfig;
let defaultConfig
try {
const defaultPublicConfigFile = await fetch("/config.default.json");
defaultConfig = await defaultPublicConfigFile.json();
const defaultPublicConfigFile = await fetch('/config.default.json')
defaultConfig = await defaultPublicConfigFile.json()
} catch {
throw new Error("Default config does not exist in the public directory");
throw new Error('Default config does not exist in the public directory')
}

let overrideConfig;
let overrideConfig
try {
const overrideConfigFile = await fetch("/env-config.json");
overrideConfig = await overrideConfigFile.json();
const overrideConfigFile = await fetch('/env-config.json')
overrideConfig = await overrideConfigFile.json()
} catch {
console.info("File env-config.json not specified or wrong format (requires json). Using default config...");
return defaultConfig;
console.info('File env-config.json not specified or wrong format (requires json). Using default config...')
return defaultConfig
}

console.info("Using merged (default with override) config...");
return mergeConfig(defaultConfig, overrideConfig);
console.info('Using merged (default with override) config...')
return mergeConfig(defaultConfig, overrideConfig)
}

const mergeConfig = (defaultConfig: ConfigType, overrideConfig: ConfigType) => {
const initialConfigKeys = Object.keys(defaultConfig)
const overrideConfigMatchingKeys = Object.keys(overrideConfig).filter((key) => initialConfigKeys.includes(key))
const overrideConfigMatchingKeys = Object.keys(overrideConfig).filter((key) =>
initialConfigKeys.includes(key)
) as (keyof ConfigType)[]

overrideConfigMatchingKeys.forEach((key) => {
// @ts-ignore: types will always match here, because of using the same key for both objects
defaultConfig[key] = overrideConfig[key]
if (typeof defaultConfig[key] === typeof overrideConfig[key]) {
defaultConfig[key] = overrideConfig[key]
}
})

return defaultConfig
Expand Down

0 comments on commit ddd68ab

Please sign in to comment.