Skip to content

Commit e3c2951

Browse files
authored
Pass url as query string param (#623)
* feat: allow url to be passed in qs * chore: tidy up
1 parent 11744e7 commit e3c2951

File tree

4 files changed

+47
-38
lines changed

4 files changed

+47
-38
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// taken from react-router-dom examples
2+
import { useMemo } from 'react';
3+
import { useLocation } from 'react-router-dom';
4+
5+
export const useQuery = () => {
6+
const { search } = useLocation();
7+
8+
return useMemo(() => new URLSearchParams(search), [search]);
9+
};

apps/router/src/hooks/index.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,4 @@ export * from './guardian/useGuardian';
6363
export * from './guardian/useGuardianSetup';
6464
export * from './gateway/useGateway';
6565
export * from './custom/useTrimmedInput';
66+
export * from './custom/useQuery';

apps/router/src/pages/Home/Home.test.tsx

+29-34
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import HomePage from './Home';
1111

1212
// Mock AppContext
1313
const mockedUseAppContext = jest.fn();
14+
const mockedUseQuery = jest.fn();
1415
jest.mock('../../hooks', () => ({
1516
useAppContext: () => mockedUseAppContext(),
17+
useQuery: () => mockedUseQuery(),
1618
}));
1719

1820
// Mock sha256Hash
@@ -22,20 +24,24 @@ jest.mock('@fedimint/utils', () => ({
2224
}));
2325

2426
describe('pages/Home', () => {
27+
const mockDispatch = jest.fn();
28+
29+
beforeEach(() => {
30+
mockedUseAppContext.mockImplementation(() => ({
31+
service: null,
32+
dispatch: mockDispatch,
33+
}));
34+
35+
mockedUseQuery.mockReturnValue({
36+
get: jest.fn(),
37+
});
38+
});
39+
2540
afterEach(() => {
2641
jest.clearAllMocks();
2742
});
2843

2944
describe('When a user clicks the connect button with an empty input value', () => {
30-
const mockDispatch = jest.fn();
31-
32-
beforeEach(() => {
33-
mockedUseAppContext.mockImplementation(() => ({
34-
service: null,
35-
dispatch: mockDispatch,
36-
}));
37-
});
38-
3945
it('should not call dispatch', async () => {
4046
render(<HomePage />);
4147

@@ -52,15 +58,6 @@ describe('pages/Home', () => {
5258

5359
// We don't support gateway urls at this time so check for this
5460
describe('When a user clicks the connect button with a gateway url input value', () => {
55-
const mockDispatch = jest.fn();
56-
57-
beforeEach(() => {
58-
mockedUseAppContext.mockImplementation(() => ({
59-
service: null,
60-
dispatch: mockDispatch,
61-
}));
62-
});
63-
6461
it('should not call dispatch', async () => {
6562
render(<HomePage />);
6663

@@ -82,15 +79,6 @@ describe('pages/Home', () => {
8279
});
8380

8481
describe('When a user clicks the connect button with a guardian url input value', () => {
85-
const mockDispatch = jest.fn();
86-
87-
beforeEach(() => {
88-
mockedUseAppContext.mockImplementation(() => ({
89-
service: null,
90-
dispatch: mockDispatch,
91-
}));
92-
});
93-
9482
it('should call dispatch', async () => {
9583
render(<HomePage />);
9684

@@ -112,13 +100,6 @@ describe('pages/Home', () => {
112100
});
113101

114102
describe('When there is no service in LocalStorage', () => {
115-
beforeEach(() => {
116-
mockedUseAppContext.mockImplementation(() => ({
117-
service: null,
118-
dispatch: jest.fn(),
119-
}));
120-
});
121-
122103
it('should render an input without a value', () => {
123104
render(<HomePage />);
124105
const input = screen.getByPlaceholderText('Guardian URL');
@@ -170,4 +151,18 @@ describe('pages/Home', () => {
170151
expect(url).toBeInTheDocument();
171152
});
172153
});
154+
155+
describe('When a url is passed as a query param', () => {
156+
beforeEach(() => {
157+
mockedUseQuery.mockReturnValue({
158+
get: jest.fn().mockReturnValue('wss://guardian-url.test.com:8174'),
159+
});
160+
});
161+
162+
it('should set query param value in input box', () => {
163+
render(<HomePage />);
164+
const url = screen.getByDisplayValue('wss://guardian-url.test.com:8174');
165+
expect(url).toBeInTheDocument();
166+
});
167+
});
173168
});

apps/router/src/pages/Home/Home.tsx

+8-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { FaDiscord, FaGithub } from 'react-icons/fa';
2121
import { FiX } from 'react-icons/fi';
2222
import { APP_ACTION_TYPE } from '../../context/AppContext';
2323
import { useAppContext } from '../../hooks';
24+
import { useQuery } from '../../hooks';
2425
import HeroSvg from '../../images/hero-1.svg';
2526
import { getServiceType } from '../../helpers/service';
2627
import { LATEST_RELEASE_TAG } from '../../constants';
@@ -29,6 +30,8 @@ import { Logo } from '../../components/Logo';
2930
const HomePage: React.FC = () => {
3031
const { t } = useTranslation();
3132
const navigate = useNavigate();
33+
const query = useQuery();
34+
3235
const { dispatch, service } = useAppContext();
3336

3437
const [serviceUrl, setServiceUrl] = useState<string>('');
@@ -38,11 +41,12 @@ const HomePage: React.FC = () => {
3841
setIsGateway(false);
3942
}, [serviceUrl]);
4043

44+
// If url query param provided then set this in input
45+
// as priority, otherwise fallback to url in state and then empty string
4146
useEffect(() => {
42-
if (service?.config) {
43-
setServiceUrl(service.config.baseUrl);
44-
}
45-
}, [service]);
47+
const url = query.get('url') || service?.config.baseUrl || '';
48+
setServiceUrl(url);
49+
}, [query, service]);
4650

4751
const handleOnChange = (e: React.ChangeEvent<HTMLInputElement>) => {
4852
const url = e.target.value;

0 commit comments

Comments
 (0)