Skip to content

Commit

Permalink
add loader property
Browse files Browse the repository at this point in the history
  • Loading branch information
milukove committed Aug 7, 2023
1 parent b83b6df commit e2773d6
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.2.0] 2022-08-08

### Added

- `loader` provider property, to define custom component for loading state

## [0.1.1] 2022-08-04

### Added
Expand Down
3 changes: 2 additions & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export declare type GrinderyLoginContextProps = {
};
export declare type GrinderyLoginProviderProps = {
children: React.ReactNode;
loader?: React.ReactNode;
};
/** Grindery Nexus Context */
export declare const GrinderyLoginContext: React.Context<GrinderyLoginContextProps>;
Expand All @@ -34,7 +35,7 @@ export declare const GrinderyLoginContext: React.Context<GrinderyLoginContextPro
*
* It also exposes the context via the useGrinderyLogin hook.
*/
export declare const GrinderyLoginProvider: ({ children, }: GrinderyLoginProviderProps) => React.JSX.Element;
export declare const GrinderyLoginProvider: ({ children, loader, }: GrinderyLoginProviderProps) => React.JSX.Element;
/** Grindery Login Hook */
export declare const useGrinderyLogin: () => GrinderyLoginContextProps;
export default GrinderyLoginProvider;
25 changes: 23 additions & 2 deletions dist/use-grindery-login.cjs.development.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/use-grindery-login.cjs.development.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/use-grindery-login.cjs.production.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/use-grindery-login.cjs.production.min.js.map

Large diffs are not rendered by default.

25 changes: 23 additions & 2 deletions dist/use-grindery-login.esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/use-grindery-login.esm.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "use-grindery-login",
"author": "milukove",
"version": "0.1.1",
"version": "0.2.0",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
25 changes: 24 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export type GrinderyLoginContextProps = {

export type GrinderyLoginProviderProps = {
children: React.ReactNode;
loader?: React.ReactNode;
};

// Default context properties
Expand Down Expand Up @@ -77,6 +78,7 @@ export const GrinderyLoginContext = createContext<GrinderyLoginContextProps>(
*/
export const GrinderyLoginProvider = ({
children,
loader,
}: GrinderyLoginProviderProps) => {
// Authentication token object
const [token, setToken] = useState<AuthToken | null>(null);
Expand All @@ -90,6 +92,9 @@ export const GrinderyLoginProvider = ({
// User authentication loading state
const [isAuthenticating, setIsAuthenticating] = useState<boolean>(true);

// Loading state
const [loading, setLoading] = React.useState(true);

// Connect user
const connect = async () => {

Check warning on line 99 in src/index.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and ubuntu-latest

The 'connect' function makes the dependencies of useEffect Hook (at line 192) change on every render. To fix this, wrap the 'connect' definition into its own useCallback() Hook

Check warning on line 99 in src/index.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and windows-latest

The 'connect' function makes the dependencies of useEffect Hook (at line 192) change on every render. To fix this, wrap the 'connect' definition into its own useCallback() Hook

Check warning on line 99 in src/index.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and macOS-latest

The 'connect' function makes the dependencies of useEffect Hook (at line 192) change on every render. To fix this, wrap the 'connect' definition into its own useCallback() Hook

Check warning on line 99 in src/index.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and ubuntu-latest

The 'connect' function makes the dependencies of useEffect Hook (at line 192) change on every render. To fix this, wrap the 'connect' definition into its own useCallback() Hook

Check warning on line 99 in src/index.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and windows-latest

The 'connect' function makes the dependencies of useEffect Hook (at line 192) change on every render. To fix this, wrap the 'connect' definition into its own useCallback() Hook

Check warning on line 99 in src/index.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 14.x and macOS-latest

The 'connect' function makes the dependencies of useEffect Hook (at line 192) change on every render. To fix this, wrap the 'connect' definition into its own useCallback() Hook
const currentUrl = window.location.href.split('?')[0];
Expand Down Expand Up @@ -174,6 +179,24 @@ export const GrinderyLoginProvider = ({
identifyUser();
}, [identifyUser]);

useEffect(() => {
if (!isAuthenticating) {
setTimeout(() => {
if (token?.access_token) {
setLoading(false);
} else {
connect();
}
}, 1000);
}
}, [token, isAuthenticating, connect]);

useEffect(() => {
if (!token?.access_token) {
setLoading(true);
}
}, [token]);

return (
<GrinderyLoginContext.Provider
value={{
Expand All @@ -192,7 +215,7 @@ export const GrinderyLoginProvider = ({
src={`${LOGIN_URL}/session`}
style={{ display: 'none' }}
/>
{children}
{loading && loader ? loader : children}
</GrinderyLoginContext.Provider>
);
};
Expand Down

0 comments on commit e2773d6

Please sign in to comment.