Skip to content

Commit a75cf59

Browse files
refactor: convert JavaScript files to TypeScript with type annotations
1 parent de581e3 commit a75cf59

File tree

21 files changed

+537
-0
lines changed

21 files changed

+537
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import React, { Suspense, useEffect, useState } from "react";
2+
import { useRoutes } from "react-router-dom";
3+
import { IntlProvider } from "react-intl";
4+
import { useLocale, useConfig, getLocaleMessages } from "@ecronix/base-shell";
5+
import {
6+
AddToHomeScreenProvider,
7+
AuthProvider,
8+
UpdateProvider,
9+
OnlineProvider,
10+
SimpleValuesProvider,
11+
LocaleProvider,
12+
} from "@ecronix/base-shell";
13+
14+
interface LayoutContentProps {
15+
appConfig?: any;
16+
}
17+
18+
export const LayoutContent: React.FC<LayoutContentProps> = ({ appConfig = {} }) => {
19+
const [messages, setMessages] = useState<any[]>([]);
20+
const {
21+
components,
22+
routes = [],
23+
containers,
24+
locale: confLocale,
25+
getDefaultRoutes,
26+
auth,
27+
update,
28+
} = appConfig || {};
29+
const { persistKey } = auth || {};
30+
const { checkInterval = 5000 } = update || {};
31+
const { Menu, Loading = () => <div>Loading...</div> } = components || {};
32+
const { locales, onError } = confLocale || {};
33+
const { LayoutContainer = React.Fragment } = containers || {};
34+
const defaultRoutes = getDefaultRoutes ? getDefaultRoutes(appConfig) : [];
35+
const { locale = {} } = useLocale();
36+
37+
useEffect(() => {
38+
const loadPolyfills = async () => {
39+
if (locale.locales && locale.locales.length > 0) {
40+
for (let i = 0; i < locales.length; i++) {
41+
const l = locales[i];
42+
if (l.locale === locale) {
43+
if (l.loadData) {
44+
await l.loadData;
45+
}
46+
}
47+
}
48+
}
49+
};
50+
loadPolyfills();
51+
}, [locale, locales]);
52+
53+
useEffect(() => {
54+
const loadMessages = async () => {
55+
const messages = await getLocaleMessages(locale, locales);
56+
setMessages(messages);
57+
};
58+
loadMessages();
59+
}, [locale, locales]);
60+
61+
return (
62+
<AuthProvider persistKey={persistKey}>
63+
<SimpleValuesProvider>
64+
<AddToHomeScreenProvider>
65+
<UpdateProvider checkInterval={checkInterval}>
66+
<OnlineProvider>
67+
<IntlProvider
68+
locale={locale}
69+
key={locale}
70+
messages={messages}
71+
onError={onError}
72+
>
73+
<LayoutContainer>
74+
<Suspense fallback={<Loading />}>{Menu && <Menu />}</Suspense>
75+
<Suspense fallback={<Loading />}>
76+
{useRoutes([...routes, ...defaultRoutes])}
77+
</Suspense>
78+
</LayoutContainer>
79+
</IntlProvider>
80+
</OnlineProvider>
81+
</UpdateProvider>
82+
</AddToHomeScreenProvider>
83+
</SimpleValuesProvider>
84+
</AuthProvider>
85+
);
86+
};
87+
88+
export const LayoutContainer: React.FC = () => {
89+
const { appConfig } = useConfig();
90+
const { locale } = appConfig || {};
91+
const { defaultLocale, persistKey } = locale || {};
92+
return (
93+
<LocaleProvider defaultLocale={defaultLocale} persistKey={persistKey}>
94+
<LayoutContent appConfig={appConfig} />
95+
</LocaleProvider>
96+
);
97+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from "react";
2+
3+
const Context = React.createContext<any>(null);
4+
5+
export default Context;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import React, { useState } from "react";
2+
import Context from "./Context";
3+
4+
interface A2HPState {
5+
deferredPrompt: any;
6+
isAppInstallable: boolean;
7+
isAppInstalled: boolean;
8+
}
9+
10+
const initialState: A2HPState = {
11+
deferredPrompt: () => {},
12+
isAppInstallable: false,
13+
isAppInstalled: false,
14+
};
15+
16+
const Provider: React.FC = ({ children }) => {
17+
const [state, setA2HPState] = useState<A2HPState>(initialState);
18+
19+
window.addEventListener("beforeinstallprompt", (e) => {
20+
e.preventDefault();
21+
setA2HPState({ deferredPrompt: e, isAppInstallable: true });
22+
});
23+
24+
window.addEventListener("appinstalled", () => {
25+
setA2HPState({ isAppInstalled: true });
26+
});
27+
28+
return (
29+
<Context.Provider value={{ ...state, setA2HPState }}>
30+
{children}
31+
</Context.Provider>
32+
);
33+
};
34+
35+
export default Provider;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { useContext } from "react";
2+
import Context from "./Context";
3+
import Provider from "./Provider";
4+
5+
function useAddToHomeScreen() {
6+
return useContext(Context);
7+
}
8+
9+
export {
10+
useAddToHomeScreen,
11+
Context as AddToHomeScreenContext,
12+
Provider as AddToHomeScreenProvider,
13+
};
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import React, { useEffect, useReducer } from "react";
2+
import Context from "./Context";
3+
4+
interface AuthState {
5+
[key: string]: any;
6+
}
7+
8+
interface AuthAction {
9+
type: "SET_AUTH" | "UPDATE_AUTH";
10+
auth: AuthState;
11+
}
12+
13+
function reducer(state: AuthState, action: AuthAction): AuthState {
14+
const { type, auth } = action;
15+
switch (type) {
16+
case "SET_AUTH":
17+
return auth;
18+
case "UPDATE_AUTH":
19+
return { ...state, ...auth };
20+
default:
21+
throw new Error();
22+
}
23+
}
24+
25+
interface ProviderProps {
26+
persistKey?: string;
27+
children: React.ReactNode;
28+
}
29+
30+
const Provider: React.FC<ProviderProps> = ({ persistKey = "auth", children }) => {
31+
const persistAuth = JSON.parse(
32+
localStorage.getItem(persistKey)?.replace("undefined", "{}") || "{}"
33+
);
34+
35+
const [auth, dispatch] = useReducer(reducer, persistAuth || {});
36+
37+
useEffect(() => {
38+
try {
39+
localStorage.setItem(persistKey, JSON.stringify(auth));
40+
} catch (error) {
41+
console.warn(error);
42+
}
43+
}, [auth, persistKey]);
44+
45+
const setAuth = (auth: AuthState) => {
46+
dispatch({ type: "SET_AUTH", auth });
47+
};
48+
49+
const updateAuth = (auth: AuthState) => {
50+
dispatch({ type: "UPDATE_AUTH", auth });
51+
};
52+
53+
return (
54+
<Context.Provider value={{ auth, setAuth, updateAuth }}>
55+
{children}
56+
</Context.Provider>
57+
);
58+
};
59+
60+
export default Provider;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from "react";
2+
3+
const Context = React.createContext<any>(null);
4+
5+
export default Context;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import React, { useState, useEffect } from "react";
2+
import Context from "./Context";
3+
4+
interface ProviderProps {
5+
children: React.ReactNode;
6+
defaultLocale?: string;
7+
persistKey?: string;
8+
}
9+
10+
const Provider: React.FC<ProviderProps> = ({
11+
children,
12+
defaultLocale = "en",
13+
persistKey = "locale",
14+
}) => {
15+
const persistLocale = localStorage.getItem(persistKey);
16+
const [locale, setLocale] = useState<string>(persistLocale || defaultLocale);
17+
18+
useEffect(() => {
19+
try {
20+
localStorage.setItem(persistKey, locale);
21+
} catch (error) {
22+
console.warn(error);
23+
}
24+
}, [locale, persistKey]);
25+
26+
return (
27+
<Context.Provider value={{ locale, setLocale }}>
28+
{children}
29+
</Context.Provider>
30+
);
31+
};
32+
33+
export default Provider;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { useContext } from "react";
2+
import Context from "./Context";
3+
import Provider from "./Provider";
4+
5+
function useLocale() {
6+
return useContext(Context);
7+
}
8+
9+
export { useLocale, Context as LocaleContext, Provider as LocaleProvider };
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from "react";
2+
3+
const Context = React.createContext<boolean | null>(null);
4+
5+
export default Context;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React, { useState } from 'react'
2+
import Context from './Context'
3+
4+
interface ProviderProps {
5+
children: React.ReactNode;
6+
}
7+
8+
const Provider: React.FC<ProviderProps> = ({ children }) => {
9+
const [isOnline, setOnline] = useState<boolean>(navigator.onLine)
10+
11+
window.addEventListener('online', () => setOnline(true))
12+
window.addEventListener('offline', () => setOnline(false))
13+
14+
return <Context.Provider value={isOnline}>{children}</Context.Provider>
15+
}
16+
17+
export default Provider

0 commit comments

Comments
 (0)