Compare commits
3 Commits
master
...
3998d43041
| Author | SHA1 | Date | |
|---|---|---|---|
|
3998d43041
|
|||
|
d680d16a76
|
|||
|
b78929b566
|
1
jest/setEnvVars.js
Normal file
1
jest/setEnvVars.js
Normal file
@@ -0,0 +1 @@
|
||||
process.env.API_ROOT = "http://localhost:8080/";
|
||||
11
package.json
11
package.json
@@ -10,6 +10,9 @@
|
||||
"name": "Deepak Mallubhotla"
|
||||
},
|
||||
"jest": {
|
||||
"setupFiles": [
|
||||
"<rootDir>/jest/setEnvVars.js"
|
||||
],
|
||||
"reporters": [
|
||||
"default",
|
||||
"jest-junit"
|
||||
@@ -17,7 +20,13 @@
|
||||
"coverageReporters": [
|
||||
"text",
|
||||
"cobertura"
|
||||
]
|
||||
],
|
||||
"collectCoverage": true,
|
||||
"collectCoverageFrom": [
|
||||
"<rootDir>/src/**/*.jsx",
|
||||
"!<rootDir>/src/**/*.test.jsx"
|
||||
],
|
||||
"coverageProvider": "babel"
|
||||
},
|
||||
"private": "true",
|
||||
"license": "ISC",
|
||||
|
||||
62
src/context/AuthContext.jsx
Normal file
62
src/context/AuthContext.jsx
Normal file
@@ -0,0 +1,62 @@
|
||||
import React from "react";
|
||||
import { useAsync } from "react-async";
|
||||
import { bootstrapAppData } from "../utils/bootstrap";
|
||||
import * as authClient from "../utils/auth-client";
|
||||
import { FullPageSpinner } from "../components/lib";
|
||||
|
||||
const AuthContext = React.createContext();
|
||||
|
||||
function AuthProvider(props) {
|
||||
const [firstAttemptFinished, setFirstAttemptFinished] = React.useState(false);
|
||||
const {
|
||||
data = { user: null, listItems: [] },
|
||||
error,
|
||||
isRejected,
|
||||
isPending,
|
||||
isSettled,
|
||||
reload,
|
||||
} = useAsync({
|
||||
promiseFn: bootstrapAppData,
|
||||
});
|
||||
|
||||
React.useLayoutEffect(() => {
|
||||
if (isSettled) {
|
||||
setFirstAttemptFinished(true);
|
||||
}
|
||||
}, [isSettled]);
|
||||
|
||||
if (!firstAttemptFinished) {
|
||||
if (isPending) {
|
||||
return <FullPageSpinner />;
|
||||
}
|
||||
if (isRejected) {
|
||||
return (
|
||||
<div css={{ color: "red" }}>
|
||||
<p>Uh oh... There's a problem. Try refreshing the app.</p>
|
||||
<pre>{error.message}</pre>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const login = (form) => authClient.login(form).then(reload);
|
||||
const register = (form) => authClient.register(form).then(reload);
|
||||
const logout = () => authClient.logout().then(reload);
|
||||
|
||||
return (
|
||||
<AuthContext.Provider
|
||||
value={{ data, login, logout, register }}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function useAuth() {
|
||||
const context = React.useContext(AuthContext);
|
||||
if (context === undefined) {
|
||||
throw new Error(`useAuth must be used within a AuthProvider`);
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
export { AuthProvider, useAuth };
|
||||
8
src/context/index.jsx
Normal file
8
src/context/index.jsx
Normal file
@@ -0,0 +1,8 @@
|
||||
import React from "react";
|
||||
import { AuthProvider } from "./AuthContext.jsx";
|
||||
|
||||
function AppProviders({ children }) {
|
||||
return <AuthProvider>{children}</AuthProvider>;
|
||||
}
|
||||
|
||||
export default AppProviders;
|
||||
@@ -2,3 +2,11 @@ import React from "react";
|
||||
import ReactDOM from "react-dom";
|
||||
import App from "./App.jsx";
|
||||
ReactDOM.render(<App />, document.getElementById("root"));
|
||||
|
||||
import AppProviders from "./context";
|
||||
ReactDOM.render(
|
||||
<AppProviders>
|
||||
<App />
|
||||
</AppProviders>,
|
||||
document.getElementById("root")
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user