-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from bitcoin-sv/feat-641-public-config
feat(BUX-641): fetch config from server
- Loading branch information
Showing
13 changed files
with
129 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
{ | ||
"apiUrl": "http://localhost:3002", | ||
"paymailDomain": "example.com", | ||
"wsUrl": "ws://localhost:3002/api/websocket" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Component, ErrorInfo, PropsWithChildren } from 'react' | ||
import { ErrorBar } from './components/ErrorBar' | ||
import styled from '@emotion/styled' | ||
|
||
type ErrorBoundaryState = { | ||
hasError: boolean | ||
} | ||
|
||
export class ErrorBoundary extends Component<PropsWithChildren, ErrorBoundaryState> { | ||
constructor(props: PropsWithChildren) { | ||
super(props) | ||
this.state = { hasError: false } | ||
} | ||
|
||
static getDerivedStateFromError(): ErrorBoundaryState | null { | ||
return { hasError: true } | ||
} | ||
|
||
componentDidCatch(error: Error, info: ErrorInfo) { | ||
console.error(error, info.componentStack) | ||
} | ||
|
||
render() { | ||
if (this.state.hasError) { | ||
return ( | ||
<Container> | ||
<ErrorBar errorMsg="Something went wrong" withReloadButton /> | ||
</Container> | ||
) | ||
} | ||
|
||
return this.props.children | ||
} | ||
} | ||
|
||
const Container = styled.div` | ||
padding: 20px; | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import axios from 'axios' | ||
|
||
export const getServerConfig = async (apiUrl: string) => { | ||
const { data: response } = await axios.get(`${apiUrl}/config`, { | ||
withCredentials: false, | ||
}) | ||
return response | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './GetUser' | ||
export * from './LoginUser' | ||
export * from './RegisterUser' | ||
export * from './GetServerConfig' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './user' | ||
export * from './public_config' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export type ExperimentalConfig = { | ||
pike_enabled: boolean | ||
} | ||
|
||
export type ServerConfig = { | ||
paymail_domain: string | ||
experimental_features: ExperimentalConfig | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,5 @@ | ||
import { useConfig } from '@4chain-ag/react-configuration' | ||
import { useServerConfig } from '@/providers/server_config/hooks' | ||
|
||
export const usePaymailDomain = () => { | ||
const { config } = useConfig() | ||
const domain = config?.paymailDomain | ||
if (typeof domain !== 'string') { | ||
return 'example.com' | ||
} | ||
return domain | ||
return useServerConfig().paymail_domain | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { useContext } from 'react' | ||
|
||
import { ServerConfigContext } from './provider' | ||
|
||
export const useServerConfig = () => { | ||
const ctx = useContext(ServerConfigContext) | ||
if (!ctx) { | ||
throw new Error('ServerConfig provider is missing') | ||
} | ||
return ctx | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { ServerConfig } from '@/api' | ||
import { useApiUrl } from '@/api/apiUrl' | ||
import { getServerConfig } from '@/api/requests' | ||
import { FC, PropsWithChildren, createContext, useEffect, useState } from 'react' | ||
|
||
export const ServerConfigContext = createContext<ServerConfig>(null as never) | ||
|
||
export const ServerConfigProvider: FC<PropsWithChildren> = ({ children }) => { | ||
const apiUrl = useApiUrl() | ||
const [loading, setLoading] = useState(true) | ||
const [serverConfig, setServerConfig] = useState<ServerConfig | null>(null) | ||
|
||
useEffect(() => { | ||
if (!apiUrl) { | ||
return | ||
} | ||
|
||
getServerConfig(apiUrl) | ||
.then((response) => { | ||
setServerConfig(response) | ||
}) | ||
.catch((e) => { | ||
console.error('Error during fetching server config', e) | ||
}) | ||
.finally(() => setLoading(false)) | ||
}, [apiUrl]) | ||
|
||
if (loading) { | ||
return null | ||
} | ||
|
||
if (!serverConfig) { | ||
throw new Error('Cannot fetch server configuration. Please try again later.') | ||
} | ||
|
||
return <ServerConfigContext.Provider value={serverConfig}>{children}</ServerConfigContext.Provider> | ||
} |