2023-05-06 12:13:39 +03:00
|
|
|
import { useState } from 'preact/hooks';
|
|
|
|
|
|
|
|
import { api } from '../utils/api';
|
|
|
|
|
2023-08-11 13:00:36 +03:00
|
|
|
import Icon from './icon';
|
2023-05-06 12:13:39 +03:00
|
|
|
import Loader from './loader';
|
|
|
|
|
|
|
|
function FollowRequestButtons({ accountID, onChange }) {
|
|
|
|
const { masto } = api();
|
|
|
|
const [uiState, setUIState] = useState('default');
|
2023-08-11 13:00:36 +03:00
|
|
|
const [requestState, setRequestState] = useState(null); // accept, reject
|
|
|
|
const [relationship, setRelationship] = useState(null);
|
|
|
|
|
|
|
|
const hasRelationship = relationship !== null;
|
|
|
|
|
2023-05-06 12:13:39 +03:00
|
|
|
return (
|
|
|
|
<p class="follow-request-buttons">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
disabled={uiState === 'loading'}
|
|
|
|
onClick={() => {
|
|
|
|
setUIState('loading');
|
2023-08-11 13:00:36 +03:00
|
|
|
setRequestState('accept');
|
2023-05-06 12:13:39 +03:00
|
|
|
(async () => {
|
|
|
|
try {
|
2023-08-11 13:00:36 +03:00
|
|
|
const rel = await masto.v1.followRequests.authorize(accountID);
|
|
|
|
if (!rel?.followedBy) {
|
|
|
|
throw new Error('Follow request not accepted');
|
|
|
|
}
|
|
|
|
setRelationship(rel);
|
2023-05-06 12:13:39 +03:00
|
|
|
onChange();
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
2023-08-11 13:00:36 +03:00
|
|
|
setUIState('default');
|
2023-05-06 12:13:39 +03:00
|
|
|
})();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Accept
|
|
|
|
</button>{' '}
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
disabled={uiState === 'loading'}
|
|
|
|
class="light danger"
|
|
|
|
onClick={() => {
|
|
|
|
setUIState('loading');
|
2023-08-11 13:00:36 +03:00
|
|
|
setRequestState('reject');
|
2023-05-06 12:13:39 +03:00
|
|
|
(async () => {
|
|
|
|
try {
|
2023-08-11 13:00:36 +03:00
|
|
|
const rel = await masto.v1.followRequests.reject(accountID);
|
|
|
|
if (rel?.followedBy) {
|
|
|
|
throw new Error('Follow request not rejected');
|
|
|
|
}
|
|
|
|
setRelationship(rel);
|
2023-05-06 12:13:39 +03:00
|
|
|
onChange();
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
setUIState('default');
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Reject
|
|
|
|
</button>
|
2023-08-11 13:00:36 +03:00
|
|
|
<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>
|
2023-05-06 12:13:39 +03:00
|
|
|
</p>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FollowRequestButtons;
|