TypeScript

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2022-06-09 21:22:19 +02:00
parent 2786fa257b
commit d1a5da97b8
3 changed files with 27 additions and 34 deletions

View file

@ -1,11 +1,10 @@
import PropTypes from 'prop-types'; import React from 'react';
import * as React from 'react';
import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
import { useDispatch, useSelector } from 'react-redux';
import snackbar from 'soapbox/actions/snackbar'; import snackbar from 'soapbox/actions/snackbar';
import { verifyAge } from 'soapbox/actions/verification'; import { verifyAge } from 'soapbox/actions/verification';
import { Button, Datepicker, Form, Text } from 'soapbox/components/ui'; import { Button, Datepicker, Form, Text } from 'soapbox/components/ui';
import { useAppDispatch, useAppSelector } from 'soapbox/hooks';
const messages = defineMessages({ const messages = defineMessages({
fail: { fail: {
@ -14,7 +13,7 @@ const messages = defineMessages({
}, },
}); });
function meetsAgeMinimum(birthday, ageMinimum) { function meetsAgeMinimum(birthday: Date, ageMinimum: number) {
const month = birthday.getUTCMonth(); const month = birthday.getUTCMonth();
const day = birthday.getUTCDate(); const day = birthday.getUTCDate();
const year = birthday.getUTCFullYear(); const year = birthday.getUTCFullYear();
@ -24,11 +23,11 @@ function meetsAgeMinimum(birthday, ageMinimum) {
const AgeVerification = () => { const AgeVerification = () => {
const intl = useIntl(); const intl = useIntl();
const dispatch = useDispatch(); const dispatch = useAppDispatch();
const isLoading = useSelector((state) => state.verification.get('isLoading')); const isLoading = useAppSelector((state) => state.verification.get('isLoading')) as boolean;
const ageMinimum = useSelector((state) => state.verification.get('ageMinimum')); const ageMinimum = useAppSelector((state) => state.verification.get('ageMinimum')) as any;
const siteTitle = useSelector((state) => state.instance.get('title')); const siteTitle = useAppSelector((state) => state.instance.title);
const [date, setDate] = React.useState(''); const [date, setDate] = React.useState('');
const isValid = typeof date === 'object'; const isValid = typeof date === 'object';
@ -44,9 +43,7 @@ const AgeVerification = () => {
dispatch(verifyAge(birthday)); dispatch(verifyAge(birthday));
} else { } else {
dispatch( dispatch(
snackbar.error(intl.formatMessage(messages.fail, { snackbar.error(intl.formatMessage(messages.fail, { ageMinimum })),
ageMinimum,
})),
); );
} }
}, [date, ageMinimum]); }, [date, ageMinimum]);
@ -78,8 +75,4 @@ const AgeVerification = () => {
); );
}; };
AgeVerification.propTypes = {
verifyAge: PropTypes.func,
};
export default AgeVerification; export default AgeVerification;

View file

@ -1,12 +1,12 @@
import PropTypes from 'prop-types'; import { AxiosError } from 'axios';
import * as React from 'react'; import React from 'react';
import { useIntl } from 'react-intl'; import { useIntl } from 'react-intl';
import { useDispatch, useSelector } from 'react-redux';
import snackbar from 'soapbox/actions/snackbar'; import snackbar from 'soapbox/actions/snackbar';
import { checkEmailVerification, postEmailVerification, requestEmailVerification } from 'soapbox/actions/verification'; import { checkEmailVerification, postEmailVerification, requestEmailVerification } from 'soapbox/actions/verification';
import Icon from 'soapbox/components/icon'; import Icon from 'soapbox/components/icon';
import { Button, Form, FormGroup, Input, Text } from 'soapbox/components/ui'; import { Button, Form, FormGroup, Input, Text } from 'soapbox/components/ui';
import { useAppDispatch, useAppSelector } from 'soapbox/hooks';
const Statuses = { const Statuses = {
IDLE: 'IDLE', IDLE: 'IDLE',
@ -16,8 +16,12 @@ const Statuses = {
const EMAIL_REGEX = /^[^@\s]+@[^@\s]+$/; const EMAIL_REGEX = /^[^@\s]+@[^@\s]+$/;
const EmailSent = ({ handleSubmit }) => { interface IEmailSent {
const dispatch = useDispatch(); handleSubmit: React.FormEventHandler,
}
const EmailSent: React.FC<IEmailSent> = ({ handleSubmit }) => {
const dispatch = useAppDispatch();
const checkEmailConfirmation = () => { const checkEmailConfirmation = () => {
dispatch(checkEmailVerification()) dispatch(checkEmailVerification())
@ -47,19 +51,19 @@ const EmailSent = ({ handleSubmit }) => {
const EmailVerification = () => { const EmailVerification = () => {
const intl = useIntl(); const intl = useIntl();
const dispatch = useDispatch(); const dispatch = useAppDispatch();
const isLoading = useSelector((state) => state.verification.get('isLoading')); const isLoading = useAppSelector((state) => state.verification.get('isLoading')) as boolean;
const [email, setEmail] = React.useState(''); const [email, setEmail] = React.useState('');
const [status, setStatus] = React.useState(Statuses.IDLE); const [status, setStatus] = React.useState(Statuses.IDLE);
const [errors, setErrors] = React.useState([]); const [errors, setErrors] = React.useState<Array<string>>([]);
const isValid = email.length > 0 && EMAIL_REGEX.test(email); const isValid = email.length > 0 && EMAIL_REGEX.test(email);
const onChange = React.useCallback((event) => setEmail(event.target.value), []); const onChange = React.useCallback((event) => setEmail(event.target.value), []);
const handleSubmit = React.useCallback((event) => { const handleSubmit: React.FormEventHandler = React.useCallback((event) => {
event.preventDefault(); event.preventDefault();
setErrors([]); setErrors([]);
@ -80,8 +84,8 @@ const EmailVerification = () => {
), ),
); );
}) })
.catch(error => { .catch((error: AxiosError) => {
const isEmailTaken = error.response?.data?.error === 'email_taken'; const isEmailTaken = (error.response?.data as any)?.error === 'email_taken';
const message = isEmailTaken ? ( const message = isEmailTaken ? (
intl.formatMessage({ id: 'email_verification.exists', defaultMessage: 'This email has already been taken.' }) intl.formatMessage({ id: 'email_verification.exists', defaultMessage: 'This email has already been taken.' })
@ -130,8 +134,4 @@ const EmailVerification = () => {
); );
}; };
EmailSent.propTypes = {
handleSubmit: PropTypes.func.isRequired,
};
export default EmailVerification; export default EmailVerification;

View file

@ -1,11 +1,11 @@
import * as React from 'react'; import React from 'react';
import { useIntl } from 'react-intl'; import { useIntl } from 'react-intl';
import OtpInput from 'react-otp-input'; import OtpInput from 'react-otp-input';
import { useDispatch, useSelector } from 'react-redux';
import snackbar from 'soapbox/actions/snackbar'; import snackbar from 'soapbox/actions/snackbar';
import { confirmPhoneVerification, requestPhoneVerification } from 'soapbox/actions/verification'; import { confirmPhoneVerification, requestPhoneVerification } from 'soapbox/actions/verification';
import { Button, Form, FormGroup, Input, Text } from 'soapbox/components/ui'; import { Button, Form, FormGroup, Input, Text } from 'soapbox/components/ui';
import { useAppDispatch, useAppSelector } from 'soapbox/hooks';
import { formatPhoneNumber } from 'soapbox/utils/phone'; import { formatPhoneNumber } from 'soapbox/utils/phone';
const Statuses = { const Statuses = {
@ -18,9 +18,9 @@ const validPhoneNumberRegex = /^\+1\s\(\d{3}\)\s\d{3}-\d{4}/;
const SmsVerification = () => { const SmsVerification = () => {
const intl = useIntl(); const intl = useIntl();
const dispatch = useDispatch(); const dispatch = useAppDispatch();
const isLoading = useSelector((state) => state.verification.get('isLoading')); const isLoading = useAppSelector((state) => state.verification.get('isLoading')) as boolean;
const [phone, setPhone] = React.useState(''); const [phone, setPhone] = React.useState('');
const [status, setStatus] = React.useState(Statuses.IDLE); const [status, setStatus] = React.useState(Statuses.IDLE);