Commit d4b7f2ce authored by tom's avatar tom

store network info in cookies

parent 357c7ce7
API_HOST=blockscout.com
API_BASE_PATH=/xdai/testnet/api
\ No newline at end of file
API_HOST=blockscout.com
API_BASE_PATH=/xdai/testnet/api
\ No newline at end of file
...@@ -9,7 +9,7 @@ export default function fetch(path: string, init?: RequestInit): Promise<Respons ...@@ -9,7 +9,7 @@ export default function fetch(path: string, init?: RequestInit): Promise<Respons
authorization: `Bearer ${ process.env.API_AUTHORIZATION_TOKEN }`, authorization: `Bearer ${ process.env.API_AUTHORIZATION_TOKEN }`,
'content-type': 'application/json', 'content-type': 'application/json',
}; };
const url = `https://${ process.env.API_HOST }${ process.env.API_BASE_PATH }${ path }`; const url = `https://blockscout.com${ path }`;
return nodeFetch(url, { return nodeFetch(url, {
headers, headers,
......
import type { NextApiRequest, NextApiResponse } from 'next'; import type { NextApiRequest, NextApiResponse } from 'next';
import fetch from 'lib/api/fetch'; import fetch from 'lib/api/fetch';
import * as cookies from 'lib/cookies';
type Methods = 'GET' | 'POST' | 'PUT' | 'DELETE'; type Methods = 'GET' | 'POST' | 'PUT' | 'DELETE';
...@@ -8,7 +9,16 @@ export default function handler<TRes>(getUrl: (_req: NextApiRequest) => string, ...@@ -8,7 +9,16 @@ export default function handler<TRes>(getUrl: (_req: NextApiRequest) => string,
return async(_req: NextApiRequest, res: NextApiResponse<TRes>) => { return async(_req: NextApiRequest, res: NextApiResponse<TRes>) => {
if (_req.method && allowedMethods.includes(_req.method as Methods)) { if (_req.method && allowedMethods.includes(_req.method as Methods)) {
const isBodyDisallowed = _req.method === 'GET' || _req.method === 'HEAD'; const isBodyDisallowed = _req.method === 'GET' || _req.method === 'HEAD';
const response = await fetch(getUrl(_req), { const networkType = _req.cookies[cookies.NAMES.NETWORK_TYPE];
const networkSubType = _req.cookies[cookies.NAMES.NETWORK_SUB_TYPE];
if (!networkType || !networkSubType) {
// eslint-disable-next-line no-console
console.error(`Incorrect network: NETWORK_TYPE=${ networkType } NETWORK_SUB_TYPE=${ networkSubType }`);
}
const url = `/${ networkType }/${ networkSubType }/api${ getUrl(_req) }`;
const response = await fetch(url, {
method: _req.method, method: _req.method,
body: isBodyDisallowed ? undefined : _req.body, body: isBodyDisallowed ? undefined : _req.body,
}); });
......
...@@ -4,7 +4,9 @@ import { Cookies } from 'typescript-cookie'; ...@@ -4,7 +4,9 @@ import { Cookies } from 'typescript-cookie';
import isBrowser from './isBrowser'; import isBrowser from './isBrowser';
export enum NAMES { export enum NAMES {
NAV_BAR_COLLAPSED='nav_bar_collapsed' NAV_BAR_COLLAPSED='nav_bar_collapsed',
NETWORK_TYPE='network_type',
NETWORK_SUB_TYPE='network_sub_type',
} }
export function get(name?: string | undefined | null) { export function get(name?: string | undefined | null) {
......
...@@ -11,7 +11,7 @@ module.exports = withReactSvg({ ...@@ -11,7 +11,7 @@ module.exports = withReactSvg({
return [ return [
{ {
source: '/', source: '/',
destination: '/xdai/mainnet', destination: '/xdai/testnet',
permanent: true, permanent: true,
}, },
]; ];
......
import { Popover, PopoverTrigger, Icon, useColorModeValue, Button } from '@chakra-ui/react'; import { Popover, PopoverTrigger, Icon, useColorModeValue, Button } from '@chakra-ui/react';
import { useRouter } from 'next/router';
import React from 'react'; import React from 'react';
import networksIcon from 'icons/networks.svg'; import networksIcon from 'icons/networks.svg';
import * as cookies from 'lib/cookies';
import getDefaultTransitionProps from 'theme/utils/getDefaultTransitionProps'; import getDefaultTransitionProps from 'theme/utils/getDefaultTransitionProps';
import NetworkMenuPopup from './NetworkMenuPopup'; import NetworkMenuPopup from './NetworkMenuPopup';
...@@ -11,6 +13,19 @@ interface Props { ...@@ -11,6 +13,19 @@ interface Props {
} }
const NetworkMenu = ({ isCollapsed }: Props) => { const NetworkMenu = ({ isCollapsed }: Props) => {
const router = useRouter();
const networkType = router.query.network_type;
const networkSubType = router.query.network_sub_type;
React.useEffect(() => {
if (typeof networkType === 'string') {
cookies.set(cookies.NAMES.NETWORK_TYPE, networkType);
}
if (typeof networkSubType === 'string') {
cookies.set(cookies.NAMES.NETWORK_SUB_TYPE, networkSubType);
}
}, [ networkType, networkSubType ]);
return ( return (
<Popover openDelay={ 300 } placement="right-start" gutter={ 22 } isLazy> <Popover openDelay={ 300 } placement="right-start" gutter={ 22 } isLazy>
<PopoverTrigger> <PopoverTrigger>
......
...@@ -32,7 +32,7 @@ const NetworkMenuLink = ({ name, type, subType, icon, isActive, routeName, isAcc ...@@ -32,7 +32,7 @@ const NetworkMenuLink = ({ name, type, subType, icon, isActive, routeName, isAcc
const pathName = `/${ type }/${ subType }${ localPath }`; const pathName = `/${ type }/${ subType }${ localPath }`;
// will fix later after we agree on CI/CD workflow // will fix later after we agree on CI/CD workflow
const href = type === 'xdai' ? pathName : 'https://blockscout.com' + pathName; const href = type === 'xdai' && subType === 'testnet' ? pathName : 'https://blockscout.com' + pathName;
const hasIcon = Boolean(icon); const hasIcon = Boolean(icon);
const colors = useColors({ hasIcon }); const colors = useColors({ hasIcon });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment