-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CastIt.Server.ClientApp] Added a hub context
- Loading branch information
Showing
3 changed files
with
44 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
CastIt.Server/ClientApp/src/context/castit_hub.context.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const defaultImg = `${process.env.PUBLIC_URL}/no_img.png`; |