Skip to content

Commit

Permalink
[CastIt.Server.ClientApp] Added a hub context
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Aug 21, 2021
1 parent 4fb2d17 commit 140eaa5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
15 changes: 9 additions & 6 deletions CastIt.Server/ClientApp/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { AppRoutes, PlayerRoutes } from './routes';
import { Suspense } from 'react';
import ServerMessage from './components/server_message';
import { TranslationContextProvider } from './context/translations.context';
import { CastItHubContextProvider } from './context/castit_hub.context';

const theme = createTheme({
palette: {
Expand Down Expand Up @@ -42,12 +43,14 @@ function App() {
<ThemeProvider theme={theme}>
<CssBaseline />
<TranslationContextProvider>
<ServerMessage>
<Suspense fallback={loading}>
<AppRoutes />
<PlayerRoutes />
</Suspense>
</ServerMessage>
<CastItHubContextProvider>
<ServerMessage>
<Suspense fallback={loading}>
<AppRoutes />
<PlayerRoutes />
</Suspense>
</ServerMessage>
</CastItHubContextProvider>
</TranslationContextProvider>
</ThemeProvider>
</BrowserRouter>
Expand Down
34 changes: 34 additions & 0 deletions CastIt.Server/ClientApp/src/context/castit_hub.context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { createContext, Dispatch, SetStateAction, useContext, useEffect } from 'react';
import { CastItHubService } from '../services/castithub.service';

interface ICastItHubContext {
connection: CastItHubService;
isConnected: boolean;
isError: boolean;
}

const initialValue: ICastItHubContext = {
connection: new CastItHubService(),
isConnected: false,
isError: false,
};

export const CastItHubContext = createContext<[ICastItHubContext, Dispatch<SetStateAction<ICastItHubContext>>]>([initialValue, (s) => s]);

export const CastItHubContextProvider = (children: any): JSX.Element => {
const [hub, setHub] = useContext(CastItHubContext);

useEffect(() => {
hub.connection
.connect()
.then(() => {
setHub((s) => ({ ...s, isConnected: true, isError: false }));
})
.catch((error) => {
console.log(error);
setHub((s) => ({ ...s, isConnected: false, isError: true }));
});
}, [hub, setHub]);

return <CastItHubContext.Provider value={[hub, setHub]}>{children.children}</CastItHubContext.Provider>;
};
1 change: 1 addition & 0 deletions CastIt.Server/ClientApp/src/utils/app_constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const defaultImg = `${process.env.PUBLIC_URL}/no_img.png`;

0 comments on commit 140eaa5

Please sign in to comment.