2022-05-30 11:55:46 -07:00
|
|
|
import React from 'react';
|
|
|
|
import { defineMessages, useIntl } from 'react-intl';
|
|
|
|
|
2022-09-10 14:52:06 -07:00
|
|
|
import { addSchedule, removeSchedule } from 'soapbox/actions/compose';
|
2022-09-14 11:01:00 -07:00
|
|
|
import { useAppDispatch, useCompose } from 'soapbox/hooks';
|
2022-09-10 14:52:06 -07:00
|
|
|
|
2022-11-15 09:23:36 -08:00
|
|
|
import ComposeFormButton from './compose-form-button';
|
2022-05-30 11:55:46 -07:00
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
add_schedule: { id: 'schedule_button.add_schedule', defaultMessage: 'Schedule post for later' },
|
|
|
|
remove_schedule: { id: 'schedule_button.remove_schedule', defaultMessage: 'Post immediately' },
|
|
|
|
});
|
|
|
|
|
|
|
|
interface IScheduleButton {
|
2023-02-15 13:26:27 -08:00
|
|
|
composeId: string
|
|
|
|
disabled?: boolean
|
2022-05-30 11:55:46 -07:00
|
|
|
}
|
|
|
|
|
2022-09-10 14:52:06 -07:00
|
|
|
const ScheduleButton: React.FC<IScheduleButton> = ({ composeId, disabled }) => {
|
2022-05-30 11:55:46 -07:00
|
|
|
const intl = useIntl();
|
2022-09-10 14:52:06 -07:00
|
|
|
const dispatch = useAppDispatch();
|
|
|
|
|
2022-09-14 11:01:00 -07:00
|
|
|
const compose = useCompose(composeId);
|
|
|
|
|
2022-09-14 13:05:40 -07:00
|
|
|
const active = !!compose.schedule;
|
|
|
|
const unavailable = !!compose.id;
|
2022-05-30 11:55:46 -07:00
|
|
|
|
|
|
|
const handleClick = () => {
|
2022-09-10 14:52:06 -07:00
|
|
|
if (active) {
|
|
|
|
dispatch(removeSchedule(composeId));
|
|
|
|
} else {
|
|
|
|
dispatch(addSchedule(composeId));
|
|
|
|
}
|
2022-05-30 11:55:46 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (unavailable) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ComposeFormButton
|
2022-07-09 09:20:02 -07:00
|
|
|
icon={require('@tabler/icons/calendar-stats.svg')}
|
2022-05-30 11:55:46 -07:00
|
|
|
title={intl.formatMessage(active ? messages.remove_schedule : messages.add_schedule)}
|
|
|
|
active={active}
|
|
|
|
disabled={disabled}
|
|
|
|
onClick={handleClick}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ScheduleButton;
|