Skip to content

Commit

Permalink
Visualisation for mock server
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrikMatiasko committed May 15, 2024
1 parent e139a8b commit e9dfe0b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/common/hooks/use-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const useStreamApi = (url, options = {}) => {
const [refreshIndex, setRefreshIndex] = useState(0)
let apiMethod = get(options, 'streamApi', true) ? streamApi : fetchApi

const urlBase = url.split('?')[0]?.split('/api/')[1]
const urlBase = url.split('/api/')[1]
const mockKey = urlBase.toUpperCase().replace(/\//g, '_').replace(/-/g, '_')

useEffect(
Expand All @@ -51,9 +51,9 @@ export const useStreamApi = (url, options = {}) => {
setState({ ...state, loading: true })
let data = []

if (process.env[`REACT_APP_MOCK_API_${mockKey}`]) {
if (process.env[`REACT_APP_MOCK_API`] || process.env[`REACT_APP_MOCK_API_${mockKey}`]) {
const mockUrl = `${process.env.REACT_APP_MOCK_BASE_URL}/api/${urlBase}`
data = await getData(fetchApi, mockUrl, options)
data = await getData(apiMethod, mockUrl, options)
} else {
data = await getData(apiMethod, url, options)
}
Expand Down
13 changes: 13 additions & 0 deletions src/components/Layout/Layout.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,16 @@ export const content = css`
overflow: auto;
height: 100%;
`

export const mockApiMode = css`
position: fixed;
top: 0;
left: 50%;
transform: translateX(-50%);
z-index: 1000;
padding: 8px;
background: #000;
color: #fff;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
`
3 changes: 2 additions & 1 deletion src/components/Layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { Props } from './Layout.types'
import * as styles from './Layout.styles'

const Layout: FC<Props> = (props) => {
const { leftPanel, header, content, isIframeMode } = props
const { leftPanel, header, content, isIframeMode, mockApiMode } = props

return (
<div css={styles.layout}>
{!isIframeMode && leftPanel}
{mockApiMode && <div css={styles.mockApiMode}>Mock API mode</div>}

<div css={styles.right}>
{!isIframeMode && header}
Expand Down
1 change: 1 addition & 0 deletions src/components/Layout/Layout.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export type Props = {
header: ReactNode
isIframeMode?: boolean
leftPanel?: ReactNode
mockApiMode?: boolean
}

0 comments on commit e9dfe0b

Please sign in to comment.