@ssplib/react-components 0.0.46 → 0.0.47

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,7 @@
1
1
  /// <reference types="react" />
2
- export declare function Login({ imgURL, name, children, loginURL, onSuccess, onFail, }: {
2
+ export declare function Login({ imgURL, name, children, loginURL }: {
3
3
  imgURL?: string;
4
4
  loginURL: string;
5
5
  children: JSX.Element | JSX.Element[];
6
6
  name?: string;
7
- onSuccess: () => void;
8
- onFail: () => void;
9
7
  }): JSX.Element;
@@ -36,11 +36,13 @@ const React = __importStar(require("react"));
36
36
  const react_1 = require("react");
37
37
  const FormProvider_1 = __importDefault(require("../providers/FormProvider"));
38
38
  const auth_1 = require("../../context/auth");
39
- function Login({ imgURL = '', name = 'Login', children, loginURL, onSuccess, onFail, }) {
39
+ const Input_1 = require("../form/input/Input");
40
+ function Login({ imgURL = '', name = 'Login', children, loginURL }) {
40
41
  const [loading, setLoading] = (0, react_1.useState)(false);
42
+ const [error, setError] = (0, react_1.useState)(false);
41
43
  const { adLogin } = (0, react_1.useContext)(auth_1.AuthContext);
42
44
  function onLogin(data) {
43
- adLogin(loginURL, data);
45
+ adLogin(loginURL, data, setLoading, setError);
44
46
  }
45
47
  return (React.createElement(FormProvider_1.default, { onSubmit: onLogin },
46
48
  React.createElement(Container_1.default, { component: 'main', maxWidth: 'xs' },
@@ -53,7 +55,11 @@ function Login({ imgURL = '', name = 'Login', children, loginURL, onSuccess, onF
53
55
  imgURL && React.createElement("img", { src: imgURL, alt: '', height: 100, width: 100 }),
54
56
  React.createElement(Typography_1.default, { component: 'h1', variant: 'h5', paddingY: 3 }, name),
55
57
  React.createElement(material_1.Stack, { spacing: 3, width: 300 },
56
- React.createElement(material_1.Stack, { spacing: 1 }, children),
57
- React.createElement(lab_1.LoadingButton, { type: 'submit', fullWidth: true, variant: 'contained', loading: loading }, "Login"))))));
58
+ React.createElement(material_1.Stack, { spacing: 1 },
59
+ children,
60
+ React.createElement(Input_1.Input, { name: 'dsa', type: 'input' })),
61
+ React.createElement(lab_1.LoadingButton, { type: 'submit', fullWidth: true, variant: 'contained', loading: loading }, "Login"),
62
+ error && (React.createElement(Box_1.default, { bgcolor: '#ce4257', padding: 2, borderRadius: 2, color: 'white' },
63
+ React.createElement(Typography_1.default, null, "Dados incorretos. Tente novamente!"))))))));
58
64
  }
59
65
  exports.Login = Login;
@@ -1,6 +1,6 @@
1
1
  /// <reference types="react" />
2
2
  export declare const cookieName = "nextauth.token";
3
- export declare function LoginProvider({ children, AUTH_URL, redirectURL, validateTokenRoute, testToken, }: {
3
+ export declare function LoginProvider({ children, AUTH_URL, redirectURL, validateTokenRoute, }: {
4
4
  children: JSX.Element | JSX.Element[];
5
5
  AUTH_URL: string;
6
6
  validateTokenRoute: string;
@@ -43,7 +43,7 @@ const react_1 = __importStar(require("react"));
43
43
  const auth_1 = require("../../context/auth");
44
44
  exports.cookieName = 'nextauth.token';
45
45
  const userImgName = 'user-data.img';
46
- function LoginProvider({ children, AUTH_URL, redirectURL = '/', validateTokenRoute, testToken, }) {
46
+ function LoginProvider({ children, AUTH_URL, redirectURL = '/', validateTokenRoute, }) {
47
47
  const [user, setUser] = (0, react_1.useState)();
48
48
  const [userLoaded, setUserLoaded] = (0, react_1.useState)(false);
49
49
  const router = (0, router_1.useRouter)();
@@ -72,7 +72,8 @@ function LoginProvider({ children, AUTH_URL, redirectURL = '/', validateTokenRou
72
72
  }
73
73
  });
74
74
  }, []);
75
- function adLogin(loginURL, data) {
75
+ function adLogin(loginURL, data, setLoading, setError) {
76
+ setLoading(true);
76
77
  fetch(loginURL, {
77
78
  method: 'POST',
78
79
  body: JSON.stringify(Object.assign(Object.assign({}, data), { cpf: data.cpf.replaceAll(/[.-]/g, '') })),
@@ -82,6 +83,7 @@ function LoginProvider({ children, AUTH_URL, redirectURL = '/', validateTokenRou
82
83
  }).then((res) => {
83
84
  if (res.ok) {
84
85
  res.json().then((j) => {
86
+ setError(false);
85
87
  const token = j.accessToken;
86
88
  const user = (0, jwt_decode_1.default)(token);
87
89
  setUser({
@@ -94,8 +96,11 @@ function LoginProvider({ children, AUTH_URL, redirectURL = '/', validateTokenRou
94
96
  router.replace(redirectURL).finally(() => setUserLoaded(true));
95
97
  });
96
98
  }
97
- else
99
+ else {
100
+ setLoading(false);
98
101
  setUserLoaded(true);
102
+ setError(true);
103
+ }
99
104
  });
100
105
  }
101
106
  // chamado no callback de login
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ssplib/react-components",
3
- "version": "0.0.46",
3
+ "version": "0.0.47",
4
4
  "description": "SSP React Components",
5
5
  "main": "index.js",
6
6
  "author": "Pedro Henrique <sr.hudrick@gmail.com>",
package/types/auth.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  export interface User {
2
3
  name: string;
3
4
  token: string;
@@ -66,7 +67,7 @@ export interface AuthReturnData {
66
67
  user: User | null | undefined;
67
68
  userLoaded: boolean;
68
69
  login: () => void;
69
- adLogin: (loginURL: string, data: any) => void;
70
+ adLogin: (loginURL: string, data: any, setLoading: React.Dispatch<React.SetStateAction<boolean>>, setError: React.Dispatch<React.SetStateAction<boolean>>) => void;
70
71
  saveUserData: (token: AuthReturn) => void;
71
72
  logout: () => void;
72
73
  }