phanpy/src/components/follow-request-buttons.jsx
Lim Chee Aun 8b74a32168 Fix race conditions when accept/rejecting many follow requests
- No longer reload the whole list of follow requests and notifications for every accept/reject action
- Notifications list now exclude follow requests (experimental)
2023-08-11 18:00:36 +08:00

80 lines
2.2 KiB
JavaScript

import { useState } from 'preact/hooks';
import { api } from '../utils/api';
import Icon from './icon';
import Loader from './loader';
function FollowRequestButtons({ accountID, onChange }) {
const { masto } = api();
const [uiState, setUIState] = useState('default');
const [requestState, setRequestState] = useState(null); // accept, reject
const [relationship, setRelationship] = useState(null);
const hasRelationship = relationship !== null;
return (
<p class="follow-request-buttons">
<button
type="button"
disabled={uiState === 'loading'}
onClick={() => {
setUIState('loading');
setRequestState('accept');
(async () => {
try {
const rel = await masto.v1.followRequests.authorize(accountID);
if (!rel?.followedBy) {
throw new Error('Follow request not accepted');
}
setRelationship(rel);
onChange();
} catch (e) {
console.error(e);
}
setUIState('default');
})();
}}
>
Accept
</button>{' '}
<button
type="button"
disabled={uiState === 'loading'}
class="light danger"
onClick={() => {
setUIState('loading');
setRequestState('reject');
(async () => {
try {
const rel = await masto.v1.followRequests.reject(accountID);
if (rel?.followedBy) {
throw new Error('Follow request not rejected');
}
setRelationship(rel);
onChange();
} catch (e) {
console.error(e);
setUIState('default');
}
})();
}}
>
Reject
</button>
<span class="follow-request-states">
{hasRelationship && requestState ? (
requestState === 'accept' ? (
<Icon icon="check-circle" alt="Accepted" class="follow-accepted" />
) : (
<Icon icon="x-circle" alt="Rejected" class="follow-rejected" />
)
) : (
<Loader hidden={uiState !== 'loading'} />
)}
</span>
</p>
);
}
export default FollowRequestButtons;