Use BirthdayInput on Edit profile page

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
ci-review-rules
marcin mikołajczak 2022-05-27 19:13:44 +02:00
rodzic 8dfb8c4bb2
commit afe670b8fc
3 zmienionych plików z 73 dodań i 74 usunięć

Wyświetl plik

@ -1,13 +1,10 @@
import PropTypes from 'prop-types'; import React, { useMemo } from 'react';
import React from 'react'; import { defineMessages, useIntl } from 'react-intl';
import ImmutablePureComponent from 'react-immutable-pure-component';
import { defineMessages, injectIntl } from 'react-intl';
import { connect } from 'react-redux';
import IconButton from 'soapbox/components/icon_button'; import IconButton from 'soapbox/components/icon_button';
import BundleContainer from 'soapbox/features/ui/containers/bundle_container'; import BundleContainer from 'soapbox/features/ui/containers/bundle_container';
import { DatePicker } from 'soapbox/features/ui/util/async-components'; import { DatePicker } from 'soapbox/features/ui/util/async-components';
import { getFeatures } from 'soapbox/utils/features'; import { useAppSelector, useFeatures } from 'soapbox/hooks';
const messages = defineMessages({ const messages = defineMessages({
birthdayPlaceholder: { id: 'edit_profile.fields.birthday_placeholder', defaultMessage: 'Your birthday' }, birthdayPlaceholder: { id: 'edit_profile.fields.birthday_placeholder', defaultMessage: 'Your birthday' },
@ -17,29 +14,37 @@ const messages = defineMessages({
nextYear: { id: 'datepicker.next_year', defaultMessage: 'Next year' }, nextYear: { id: 'datepicker.next_year', defaultMessage: 'Next year' },
}); });
const mapStateToProps = state => { interface IBirthdayInput {
const features = getFeatures(state.get('instance')); value?: string,
onChange: (value: string) => void,
required?: boolean,
}
return { const BirthdayInput: React.FC<IBirthdayInput> = ({ value, onChange, required }) => {
supportsBirthdays: features.birthdays, const intl = useIntl();
minAge: state.getIn(['instance', 'pleroma', 'metadata', 'birthday_min_age']), const features = useFeatures();
};
};
export default @connect(mapStateToProps) const supportsBirthdays = features.birthdays;
@injectIntl const minAge = useAppSelector((state) => state.instance.getIn(['pleroma', 'metadata', 'birthday_min_age'])) as number;
class BirthdayInput extends ImmutablePureComponent {
static propTypes = { const maxDate = useMemo(() => {
hint: PropTypes.node, if (!supportsBirthdays) return null;
required: PropTypes.bool,
supportsBirthdays: PropTypes.bool,
minAge: PropTypes.number,
onChange: PropTypes.func.isRequired,
value: PropTypes.instanceOf(Date),
};
renderHeader = ({ let maxDate = new Date();
maxDate = new Date(maxDate.getTime() - minAge * 1000 * 60 * 60 * 24 + maxDate.getTimezoneOffset() * 1000 * 60);
return maxDate;
}, [minAge]);
const selected = useMemo(() => {
if (!supportsBirthdays || !value) return null;
const date = new Date(value);
return new Date(date.getTime() + (date.getTimezoneOffset() * 60000));
}, [value]);
if (!supportsBirthdays) return null;
const renderCustomHeader = ({
decreaseMonth, decreaseMonth,
increaseMonth, increaseMonth,
prevMonthButtonDisabled, prevMonthButtonDisabled,
@ -49,12 +54,20 @@ class BirthdayInput extends ImmutablePureComponent {
prevYearButtonDisabled, prevYearButtonDisabled,
nextYearButtonDisabled, nextYearButtonDisabled,
date, date,
}: {
decreaseMonth(): void,
increaseMonth(): void,
prevMonthButtonDisabled: boolean,
nextMonthButtonDisabled: boolean,
decreaseYear(): void,
increaseYear(): void,
prevYearButtonDisabled: boolean,
nextYearButtonDisabled: boolean,
date: Date,
}) => { }) => {
const { intl } = this.props;
return ( return (
<div className='datepicker__header'> <div className='flex flex-col gap-2'>
<div className='datepicker__months'> <div className='flex items-center justify-between'>
<IconButton <IconButton
className='datepicker__button' className='datepicker__button'
src={require('@tabler/icons/icons/chevron-left.svg')} src={require('@tabler/icons/icons/chevron-left.svg')}
@ -73,7 +86,7 @@ class BirthdayInput extends ImmutablePureComponent {
title={intl.formatMessage(messages.nextMonth)} title={intl.formatMessage(messages.nextMonth)}
/> />
</div> </div>
<div className='datepicker__years'> <div className='flex items-center justify-between'>
<IconButton <IconButton
className='datepicker__button' className='datepicker__button'
src={require('@tabler/icons/icons/chevron-left.svg')} src={require('@tabler/icons/icons/chevron-left.svg')}
@ -94,39 +107,26 @@ class BirthdayInput extends ImmutablePureComponent {
</div> </div>
</div> </div>
); );
} };
render() { const handleChange = (date: Date) => onChange(new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(0, 10));
const { intl, value, onChange, supportsBirthdays, hint, required, minAge } = this.props;
if (!supportsBirthdays) return null;
let maxDate = new Date();
maxDate = new Date(maxDate.getTime() - minAge * 1000 * 60 * 60 * 24 + maxDate.getTimezoneOffset() * 1000 * 60);
return ( return (
<div className='datepicker'> <div className='mt-1 relative rounded-md shadow-sm'>
{hint && (
<div className='datepicker__hint'>
{hint}
</div>
)}
<div className='datepicker__input'>
<BundleContainer fetchComponent={DatePicker}> <BundleContainer fetchComponent={DatePicker}>
{Component => (<Component {Component => (<Component
selected={value} selected={selected}
wrapperClassName='react-datepicker-wrapper' wrapperClassName='react-datepicker-wrapper'
onChange={onChange} onChange={handleChange}
placeholderText={intl.formatMessage(messages.birthdayPlaceholder)} placeholderText={intl.formatMessage(messages.birthdayPlaceholder)}
minDate={new Date('1900-01-01')} minDate={new Date('1900-01-01')}
maxDate={maxDate} maxDate={maxDate}
required={required} required={required}
renderCustomHeader={this.renderHeader} renderCustomHeader={renderCustomHeader}
/>)} />)}
</BundleContainer> </BundleContainer>
</div> </div>
</div>
); );
} };
} export default BirthdayInput;

Wyświetl plik

@ -58,7 +58,6 @@ const RegistrationForm: React.FC<IRegistrationForm> = ({ inviteToken }) => {
const [usernameUnavailable, setUsernameUnavailable] = useState(false); const [usernameUnavailable, setUsernameUnavailable] = useState(false);
const [passwordConfirmation, setPasswordConfirmation] = useState(''); const [passwordConfirmation, setPasswordConfirmation] = useState('');
const [passwordMismatch, setPasswordMismatch] = useState(false); const [passwordMismatch, setPasswordMismatch] = useState(false);
const [birthday, setBirthday] = useState<Date | undefined>(undefined);
const source = useRef(axios.CancelToken.source()); const source = useRef(axios.CancelToken.source());
@ -111,8 +110,8 @@ const RegistrationForm: React.FC<IRegistrationForm> = ({ inviteToken }) => {
setPasswordMismatch(!passwordsMatch()); setPasswordMismatch(!passwordsMatch());
}; };
const onBirthdayChange = (newBirthday: Date) => { const onBirthdayChange = (birthday: string) => {
setBirthday(newBirthday); updateParams({ birthday });
}; };
const launchModal = () => { const launchModal = () => {
@ -187,10 +186,6 @@ const RegistrationForm: React.FC<IRegistrationForm> = ({ inviteToken }) => {
if (inviteToken) { if (inviteToken) {
params.set('token', inviteToken); params.set('token', inviteToken);
} }
if (birthday) {
params.set('birthday', new Date(birthday.getTime() - (birthday.getTimezoneOffset() * 60000)).toISOString().slice(0, 10));
}
}); });
setSubmissionLoading(true); setSubmissionLoading(true);
@ -291,7 +286,7 @@ const RegistrationForm: React.FC<IRegistrationForm> = ({ inviteToken }) => {
{birthdayRequired && ( {birthdayRequired && (
<BirthdayInput <BirthdayInput
value={birthday} value={params.get('birthday')}
onChange={onBirthdayChange} onChange={onBirthdayChange}
required required
/> />

Wyświetl plik

@ -4,6 +4,7 @@ import { defineMessages, useIntl, FormattedMessage } from 'react-intl';
import { updateNotificationSettings } from 'soapbox/actions/accounts'; import { updateNotificationSettings } from 'soapbox/actions/accounts';
import { patchMe } from 'soapbox/actions/me'; import { patchMe } from 'soapbox/actions/me';
import snackbar from 'soapbox/actions/snackbar'; import snackbar from 'soapbox/actions/snackbar';
import BirthdayInput from 'soapbox/components/birthday_input';
import List, { ListItem } from 'soapbox/components/list'; import List, { ListItem } from 'soapbox/components/list';
import { useAppSelector, useAppDispatch, useOwnAccount, useFeatures } from 'soapbox/hooks'; import { useAppSelector, useAppDispatch, useOwnAccount, useFeatures } from 'soapbox/hooks';
import { normalizeAccount } from 'soapbox/normalizers'; import { normalizeAccount } from 'soapbox/normalizers';
@ -242,6 +243,10 @@ const EditProfile: React.FC = () => {
}; };
}; };
const handleBirthdayChange = (date: string) => {
updateData('birthday', date);
};
const handleHideNetworkChange: React.ChangeEventHandler<HTMLInputElement> = e => { const handleHideNetworkChange: React.ChangeEventHandler<HTMLInputElement> = e => {
const hide = e.target.checked; const hide = e.target.checked;
@ -325,10 +330,9 @@ const EditProfile: React.FC = () => {
<FormGroup <FormGroup
labelText={<FormattedMessage id='edit_profile.fields.birthday_label' defaultMessage='Birthday' />} labelText={<FormattedMessage id='edit_profile.fields.birthday_label' defaultMessage='Birthday' />}
> >
<Input <BirthdayInput
type='text'
value={data.birthday} value={data.birthday}
onChange={handleTextChange('birthday')} onChange={handleBirthdayChange}
/> />
</FormGroup> </FormGroup>
)} )}