@selfcommunity/react-ui
Version:
React UI Components to integrate a Community created with SelfCommunity Platform.
149 lines (140 loc) • 7.69 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const styles_1 = require("@mui/material/styles");
const react_core_1 = require("@selfcommunity/react-core");
const material_1 = require("@mui/material");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const react_intl_1 = require("react-intl");
const system_1 = require("@mui/system");
const api_services_1 = require("@selfcommunity/api-services");
const PasswordTextField_1 = tslib_1.__importDefault(require("../../shared/PasswordTextField"));
const PREFIX = 'SCAccountReset';
const classes = {
root: `${PREFIX}-root`,
form: `${PREFIX}-form`,
password: `${PREFIX}-password`,
success: `${PREFIX}-success`,
error: `${PREFIX}-error`,
validating: `${PREFIX}-validating`
};
const Root = (0, styles_1.styled)(material_1.Box, {
name: PREFIX,
slot: 'Root',
overridesResolver: (props, styles) => styles.root
})(({ theme }) => ({
[`& .${classes.form} .MuiTextField-root, &.${classes.root} .MuiButton-root`]: {
margin: theme.spacing(1, 0, 1, 0)
},
[`& .${classes.form} .MuiTypography-root`]: {
margin: theme.spacing(1, 0, 1, 0)
},
[`& .${classes.validating}`]: {
[`& span`]: {
marginRight: 12,
float: 'left'
},
[`& p`]: {
paddingTop: 12
}
}
}));
/**
* > API documentation for the Community-JS Account Reset component. Learn about the available props and the CSS API.
*
*
* This component allows users to log in to the application with their usernames and passwords.
* Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-ui/Components/AccountReset)
#### Import
```jsx
import {AccountReset} from '@selfcommunity/react-ui';
```
#### Component Name
The name `SCAccountReset` can be used when providing style overrides in the theme.
#### CSS
|Rule Name|Global class|Description|
|---|---|---|
|root|.SCAccountReset-root|Styles applied to the root element.|
|form|.SCAccountReset-form|Styles applied to the form element.|
|email|.SCAccountReset-password|Styles applied to the password TextField.|
|success|.SCAccountRecover-success|Styles applied to the success Alert.|
|error|.SCAccountRecover-error|Styles applied to the error Alert.|
*
* @param inProps
*/
function AccountReset(inProps) {
const props = (0, system_1.useThemeProps)({
props: inProps,
name: PREFIX
});
// PROPS
const { className, onSuccess, onError, TextFieldProps = { variant: 'outlined', fullWidth: true }, ButtonProps = { variant: 'contained' }, validationCode, successAction = null, errorAction = null } = props, rest = tslib_1.__rest(props, ["className", "onSuccess", "onError", "TextFieldProps", "ButtonProps", "validationCode", "successAction", "errorAction"]);
// STATE
const [isLoading, setIsLoading] = (0, react_1.useState)(true);
const [password, setPassword] = (0, react_1.useState)('');
const [passwordError, setPasswordError] = (0, react_1.useState)('');
const [validationCodeError, setValidationCodeError] = (0, react_1.useState)('');
const [isSubmitting, setIsSubmitting] = (0, react_1.useState)(false);
const [isSucceed, setIsSucceed] = (0, react_1.useState)(false);
// CONTEXT
const scUserContext = (0, react_core_1.useSCUser)();
const intl = (0, react_intl_1.useIntl)();
/**
* Verify the validation code before enable the form
*/
(0, react_1.useEffect)(() => {
if (!scUserContext.user) {
if (validationCode) {
api_services_1.AccountService.verifyValidationCode({ validation_code: validationCode })
.then((res) => {
if (res.status >= 300 || (res && !res.is_valid)) {
setValidationCodeError(intl.formatMessage({ id: 'ui.accountReset.code.error', defaultMessage: 'ui.accountReset.code.error' }));
}
setIsLoading(false);
})
.catch((error) => {
const _error = (0, api_services_1.formatHttpErrorCode)(error);
if (_error.validationCodeError) {
setValidationCodeError(_error.validationCodeError.error);
}
setIsLoading(false);
});
}
}
}, [validationCode]);
// HANDLERS
const handleChange = (event) => {
setPassword(event.target.value);
};
const handleSubmit = (event) => {
event.preventDefault();
event.stopPropagation();
setIsSubmitting(true);
api_services_1.AccountService.reset({ validation_code: validationCode, password })
.then((res) => {
setIsSucceed(true);
onSuccess && onSuccess(res);
})
.catch((error) => {
const _error = (0, api_services_1.formatHttpErrorCode)(error);
if (_error.passwordError) {
setPasswordError(_error.passwordError.error);
}
if (_error.validationCodeError) {
setValidationCodeError(_error.validationCodeError.error);
}
onError && onError(error);
})
.then(() => setIsSubmitting(false));
return false;
};
if (scUserContext.user !== null) {
// User already logged in
return null;
}
// RENDER
return ((0, jsx_runtime_1.jsx)(Root, Object.assign({ className: (0, classnames_1.default)(classes.root, className) }, rest, { children: isSucceed ? ((0, jsx_runtime_1.jsxs)(material_1.Alert, Object.assign({ severity: "success", className: classes.success }, { children: [intl.formatMessage({ id: 'ui.accountReset.success', defaultMessage: 'ui.accountReset.success' }), successAction] }))) : validationCodeError ? ((0, jsx_runtime_1.jsxs)(material_1.Alert, Object.assign({ severity: "error", className: classes.error }, { children: [(0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.accountReset.code.error", defaultMessage: "ui.accountReset.code.error" }), errorAction] }))) : isLoading ? ((0, jsx_runtime_1.jsxs)(material_1.Box, Object.assign({ className: classes.validating }, { children: [(0, jsx_runtime_1.jsx)(material_1.CircularProgress, {}), (0, jsx_runtime_1.jsx)("p", { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.accountReset.code.validatingToken", defaultMessage: "ui.accountReset.code.validatingToken" }) })] }))) : ((0, jsx_runtime_1.jsxs)("form", Object.assign({ className: classes.form, onSubmit: handleSubmit }, { children: [(0, jsx_runtime_1.jsx)(PasswordTextField_1.default, Object.assign({ className: classes.password, disabled: isSubmitting, label: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.accountReset.password.label", defaultMessage: "ui.accountReset.password.label" }) }, TextFieldProps, { value: password, onChange: handleChange, error: Boolean(passwordError), helperText: passwordError && ((0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: `ui.accountReset.password.error.${passwordError}`, defaultMessage: `ui.accountReset.password.error.${passwordError}` })) })), (0, jsx_runtime_1.jsx)(material_1.Button, Object.assign({ type: "submit" }, ButtonProps, { disabled: !password || isSubmitting }, { children: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.accountReset.submit", defaultMessage: "ui.accountReset.submit" }) }))] }))) })));
}
exports.default = AccountReset;