@rtbjs/use-state
Version:
`@rtbjs/use-state` is a state management tool that can act as a local state and be easily turned into a global redux state. It is an innovative approach to state management that combines the advantages of both React's useState and Redux's state management
56 lines • 4.06 kB
JavaScript
"use strict";
exports.__esModule = true;
exports.LoginPage = void 0;
var tslib_1 = require("tslib");
var jsx_runtime_1 = require("react/jsx-runtime");
var react_1 = require("react");
var react_router_dom_1 = require("react-router-dom");
var react_toastify_1 = require("react-toastify");
var auth_api_1 = require("../../redux/api/auth-api");
var antd_1 = require("antd");
var use_is_form_submittable_1 = require("../../components/ui/form/use-is-form-submittable");
var icons_1 = require("@ant-design/icons");
var login_page_styles_1 = require("./login.page.styles");
var LoginPage = function () {
var _a;
// 👇 API Login Mutation
var _b = (0, auth_api_1.useLoginUserMutation)(), loginUser = _b[0], _c = _b[1], isLoading = _c.isLoading, isError = _c.isError, error = _c.error, isSuccess = _c.isSuccess;
var navigate = (0, react_router_dom_1.useNavigate)();
var location = (0, react_router_dom_1.useLocation)();
var from = ((_a = location.state) === null || _a === void 0 ? void 0 : _a.from.pathname) || '/';
(0, react_1.useEffect)(function () {
if (isSuccess) {
react_toastify_1.toast.success('You successfully logged in');
form.resetFields();
navigate(from);
}
if (isError) {
if (Array.isArray(error.data.error)) {
error.data.error.forEach(function (el) {
return react_toastify_1.toast.error(el.message, {
position: 'top-right'
});
});
}
else {
react_toastify_1.toast.error(error.data.message, {
position: 'top-right'
});
var redirectTo = error.data.redirectTo;
if (redirectTo)
navigate("/".concat(redirectTo));
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isLoading]);
var onSubmitHandler = function (values) {
loginUser(values);
};
var form = antd_1.Form.useForm()[0];
var isSubmittable = (0, use_is_form_submittable_1.useIsFormSubmittable)({ form: form }).isSubmittable;
return ((0, jsx_runtime_1.jsxs)(login_page_styles_1.Container, { children: [(0, jsx_runtime_1.jsx)(antd_1.Typography.Title, { children: "Welcome to ToolBox" }), (0, jsx_runtime_1.jsx)(antd_1.Typography.Paragraph, { children: " Please login" }), (0, jsx_runtime_1.jsxs)(antd_1.Form, tslib_1.__assign({ name: "login", style: {
maxWidth: 300
}, onFinish: onSubmitHandler, autoComplete: "off", form: form }, { children: [(0, jsx_runtime_1.jsx)(antd_1.Form.Item, tslib_1.__assign({ name: "email", messageVariables: { name: 'Email' }, rules: [{ required: true, type: 'email' }] }, { children: (0, jsx_runtime_1.jsx)(antd_1.Input, { prefix: (0, jsx_runtime_1.jsx)(icons_1.UserOutlined, { className: "site-form-item-icon" }), placeholder: "Username" }) })), (0, jsx_runtime_1.jsx)(antd_1.Form.Item, tslib_1.__assign({ name: "password", messageVariables: { name: 'Password' }, rules: [{ required: true, min: 8, max: 32 }] }, { children: (0, jsx_runtime_1.jsx)(antd_1.Input, { prefix: (0, jsx_runtime_1.jsx)(icons_1.LockOutlined, { className: "site-form-item-icon" }), type: "password", placeholder: "Password" }) })), (0, jsx_runtime_1.jsx)(antd_1.Form.Item, { children: (0, jsx_runtime_1.jsx)(antd_1.Typography.Link, { children: (0, jsx_runtime_1.jsx)(react_router_dom_1.Link, tslib_1.__assign({ to: "/forgotpassword" }, { children: "Forgot password" })) }) }), (0, jsx_runtime_1.jsx)(antd_1.Form.Item, { children: (0, jsx_runtime_1.jsx)(login_page_styles_1.SubmitButton, tslib_1.__assign({ type: "primary", htmlType: "submit", disabled: !isSubmittable }, { children: "Login" })) }), "Or", ' ', (0, jsx_runtime_1.jsx)(antd_1.Typography.Link, { children: (0, jsx_runtime_1.jsx)(react_router_dom_1.Link, tslib_1.__assign({ to: "/register" }, { children: "Sign Up here" })) })] }))] }));
};
exports.LoginPage = LoginPage;
//# sourceMappingURL=login.page.js.map