Skip to content

Commit a20edbd

Browse files
committed
migrate remote config to TS
1 parent ccf28ce commit a20edbd

8 files changed

Lines changed: 68 additions & 72 deletions

File tree

src/configuration/AppLoadingHOC.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/configuration/ConfigurationWrapper.js

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { useQuery } from '@tanstack/react-query';
2+
import { ExtractFnReturnType, QueryConfig } from 'src/lib/react-query';
3+
import { RemoteConfig } from "../types";
4+
import { axios } from 'src/lib/axios';
5+
6+
const getAd = async (): Promise<RemoteConfig> => {
7+
return axios.get('/data/remoteConfiguration.json');
8+
}
9+
10+
type QueryFnType = typeof getAd;
11+
12+
type UseGetAdOptions = {
13+
config?: QueryConfig<QueryFnType>;
14+
};
15+
export const useGetRemoteConfig = ({ config }: UseGetAdOptions = {}) => {
16+
return useQuery<ExtractFnReturnType<QueryFnType>>({
17+
...config,
18+
queryKey: ['remote-config'],
19+
queryFn: () => getAd(),
20+
});
21+
}

src/features/remoteConfig/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './providers/ConfigurationWrapper';
2+
export * from './types';
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from 'react'
2+
import useAsyncError from 'src/hooks/useAsyncError'
3+
import { useGetRemoteConfig } from '../api/getRemoteConfig'
4+
import BeatLoader from 'react-spinners/BeatLoader'
5+
6+
export const ConfigurationWrapper = (props) => {
7+
const { isLoading, isError, data: remoteConfig } = useGetRemoteConfig()
8+
const throwError = useAsyncError()
9+
10+
if (isLoading) {
11+
return (
12+
<div className="appLoading">
13+
<BeatLoader color={'#A9B2BD'} loading={true} size={15} />
14+
</div>
15+
)
16+
}
17+
18+
if (isError || !remoteConfig) {
19+
throwError('Could not fetch configuration data, Make sure you are connected to the internet')
20+
}
21+
22+
return <>{props.children}</>
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export type Tag = {
2+
confsValues: string[],
3+
devtoValues: string[],
4+
hashnodeValues: string[],
5+
redditValues: string[],
6+
freecodecampValues: string[],
7+
mediumValues: string[],
8+
label: string,
9+
value: string
10+
}
11+
12+
export type MarketingBannerConfig = {
13+
show: boolean,
14+
campaign_name: string,
15+
htmlContent: string
16+
}
17+
18+
export type RemoteConfig = {
19+
supportedTags: Tag[],
20+
marketingBannerConfig: MarketingBannerConfig
21+
}

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import './index.css';
55
import App from './App';
66
import AppWrapper from './configuration/AppWrapper'
77
import AppErrorBoundary from './configuration/AppErrorBoundary'
8-
import ConfigurationWrapper from './configuration/ConfigurationWrapper'
8+
import { ConfigurationWrapper } from 'src/features/remoteConfig/'
99
import { QueryClientProvider } from '@tanstack/react-query'
1010
import { queryClient } from 'src/lib/react-query'
1111

src/services/remoteConfiguration.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)