UNPKG

@selfcommunity/react-ui

Version:

React UI Components to integrate a Community created with SelfCommunity Platform.

111 lines (102 loc) 4.92 kB
import { __rest } from "tslib"; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { useState } from 'react'; import { styled } from '@mui/material/styles'; import { useSCUser } from '@selfcommunity/react-core'; import { Box, Button, Alert } from '@mui/material'; import classNames from 'classnames'; import { FormattedMessage, useIntl } from 'react-intl'; import EmailTextField from '../../shared/EmailTextField'; import { useThemeProps } from '@mui/system'; import { AccountService, formatHttpErrorCode } from '@selfcommunity/api-services'; const PREFIX = 'SCAccountRecover'; const classes = { root: `${PREFIX}-root`, form: `${PREFIX}-form`, email: `${PREFIX}-email`, success: `${PREFIX}-success` }; const Root = styled(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) } })); /** * > API documentation for the Community-JS Categories component. Learn about the available props and the CSS API. * * * The Categories component renders the list of all available categories. * Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-ui/Components/AccountRecover) #### Import ```jsx import {AccountVerify} from '@selfcommunity/react-ui'; ``` #### Component Name The name `SCAccountRecover` can be used when providing style overrides in the theme. #### CSS |Rule Name|Global class|Description| |---|---|---| |root|.SCAccountRecover-root|Styles applied to the root element.| |form|.SCAccountRecover-form|Styles applied to the form element.| |email|.SCAccountRecover-email|Styles applied to the email TextField.| |success|.SCAccountRecover-success|Styles applied to the success Typography.| * * @param inProps */ export default function AccountRecover(inProps) { const props = useThemeProps({ props: inProps, name: PREFIX }); // PROPS const { className, onSuccess = null, TextFieldProps = { variant: 'outlined', fullWidth: true }, ButtonProps = { variant: 'contained' }, successAction = null } = props, rest = __rest(props, ["className", "onSuccess", "TextFieldProps", "ButtonProps", "successAction"]); // STATE const [email, setEmail] = useState(''); const [emailError, setEmailError] = useState(''); const [isSubmitting, setIsSubmitting] = useState(false); const [isSucceed, setIsSucceed] = useState(false); // CONTEXT const scUserContext = useSCUser(); const intl = useIntl(); // HANDLERS const handleChange = (event) => { setEmail(event.target.value); setEmailError(''); }; const handleSubmit = (event) => { event.preventDefault(); event.stopPropagation(); setIsSubmitting(true); AccountService.recover({ email }) .then(() => { setIsSucceed(true); onSuccess && onSuccess(); }) .catch((error) => { const _error = formatHttpErrorCode(error); if (_error.emailError) { setEmailError(_error.emailError.error); } }) .then(() => setIsSubmitting(false)); return false; }; if (scUserContext.user !== null) { // User already logged in return null; } // RENDER return (_jsx(Root, Object.assign({ className: classNames(classes.root, className) }, rest, { children: isSucceed ? (_jsxs(Alert, Object.assign({ severity: "success", className: classes.success }, { children: [intl.formatMessage({ id: 'ui.accountRecover.success', defaultMessage: 'ui.accountRecover.success' }, { email, // eslint-disable-next-line @typescript-eslint/ban-ts-ignore // @ts-ignore b: (chunks) => _jsx("b", { children: chunks }) }), successAction] }))) : (_jsxs("form", Object.assign({ className: classes.form, onSubmit: handleSubmit }, { children: [_jsx(EmailTextField, Object.assign({ className: classes.email, disabled: isSubmitting, label: _jsx(FormattedMessage, { id: "ui.accountRecover.email.label", defaultMessage: "ui.accountRecover.email.label" }) }, TextFieldProps, { value: email, onChange: handleChange, error: Boolean(emailError), helperText: emailError && (_jsx(FormattedMessage, { id: `ui.accountRecover.email.error.${emailError}`, defaultMessage: `ui.accountRecover.email.error.${emailError}` })) })), _jsx(Button, Object.assign({ type: "submit" }, ButtonProps, { disabled: !email || Boolean(emailError) || isSubmitting }, { children: _jsx(FormattedMessage, { id: "ui.accountRecover.submit", defaultMessage: "ui.accountRecover.submit" }) }))] }))) }))); }