Add useVerify hook

This commit is contained in:
Alex Gleason 2023-07-21 12:48:47 -05:00
parent 31e5f860d9
commit 99e8f6912d
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
3 changed files with 83 additions and 19 deletions

View file

@ -488,14 +488,6 @@ const setBadges = (accountId: string, oldTags: string[], newTags: string[]) =>
return dispatch(setTags(accountId, oldBadges, newBadges)); return dispatch(setTags(accountId, oldBadges, newBadges));
}; };
const verifyUser = (accountId: string) =>
(dispatch: AppDispatch) =>
dispatch(tagUsers([accountId], ['verified']));
const unverifyUser = (accountId: string) =>
(dispatch: AppDispatch) =>
dispatch(untagUsers([accountId], ['verified']));
const addPermission = (accountIds: string[], permissionGroup: string) => const addPermission = (accountIds: string[], permissionGroup: string) =>
(dispatch: AppDispatch, getState: () => RootState) => { (dispatch: AppDispatch, getState: () => RootState) => {
const nicknames = nicknamesFromIds(getState, accountIds); const nicknames = nicknamesFromIds(getState, accountIds);
@ -772,8 +764,6 @@ export {
untagUsers, untagUsers,
setTags, setTags,
setBadges, setBadges,
verifyUser,
unverifyUser,
addPermission, addPermission,
removePermission, removePermission,
promoteToAdmin, promoteToAdmin,

View file

@ -0,0 +1,76 @@
import { Entities } from 'soapbox/entity-store/entities';
import { useTransaction } from 'soapbox/entity-store/hooks';
import { EntityCallbacks } from 'soapbox/entity-store/hooks/types';
import { findEntity } from 'soapbox/entity-store/selectors';
import { useApi, useGetState } from 'soapbox/hooks';
import type { Account } from 'soapbox/schemas';
import type { RootState } from 'soapbox/store';
function useVerify() {
const api = useApi();
const getState = useGetState();
const { transaction } = useTransaction();
function verifyEffect(accts: string[], verified: boolean) {
const ids = selectIdsForAccts(getState(), accts);
const updater = (account: Account): Account => {
if (account.pleroma) {
const tags = account.pleroma.tags.filter((tag) => tag !== 'verified');
if (verified) {
tags.push('verified');
}
account.pleroma.tags = tags;
}
account.verified = verified;
return account;
};
transaction({
Accounts: ids.reduce<Record<string, (account: Account) => Account>>(
(result, id) => ({ ...result, [id]: updater }),
{}),
});
}
async function verify(accts: string[], callbacks?: EntityCallbacks<void, unknown>) {
verifyEffect(accts, true);
try {
await api.put('/api/v1/pleroma/admin/users/tag', { nicknames: accts, tags: ['verified'] });
callbacks?.onSuccess?.();
} catch (e) {
callbacks?.onError?.(e);
verifyEffect(accts, false);
}
}
async function unverify(accts: string[], callbacks?: EntityCallbacks<void, unknown>) {
verifyEffect(accts, false);
try {
await api.delete('/api/v1/pleroma/admin/users/tag', { data: { nicknames: accts, tags: ['verified'] } });
callbacks?.onSuccess?.();
} catch (e) {
callbacks?.onError?.(e);
verifyEffect(accts, true);
}
}
return {
verify,
unverify,
};
}
function selectIdsForAccts(state: RootState, accts: string[]): string[] {
return accts.map((acct) => {
const account = findEntity<Account>(
state,
Entities.ACCOUNTS,
(account) => account.acct === acct,
);
return account!.id;
});
}
export { useVerify };

View file

@ -1,14 +1,11 @@
import React, { ChangeEventHandler, useState } from 'react'; import React, { ChangeEventHandler, useState } from 'react';
import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
import { import { setBadges as saveBadges } from 'soapbox/actions/admin';
verifyUser,
unverifyUser,
setBadges as saveBadges,
} from 'soapbox/actions/admin';
import { deactivateUserModal, deleteUserModal } from 'soapbox/actions/moderation'; import { deactivateUserModal, deleteUserModal } from 'soapbox/actions/moderation';
import { useAccount } from 'soapbox/api/hooks'; import { useAccount } from 'soapbox/api/hooks';
import { useSuggest } from 'soapbox/api/hooks/admin/useSuggest'; import { useSuggest } from 'soapbox/api/hooks/admin/useSuggest';
import { useVerify } from 'soapbox/api/hooks/admin/useVerify';
import Account from 'soapbox/components/account'; import Account from 'soapbox/components/account';
import List, { ListItem } from 'soapbox/components/list'; import List, { ListItem } from 'soapbox/components/list';
import MissingIndicator from 'soapbox/components/missing-indicator'; import MissingIndicator from 'soapbox/components/missing-indicator';
@ -45,6 +42,7 @@ const AccountModerationModal: React.FC<IAccountModerationModal> = ({ onClose, ac
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const { suggest, unsuggest } = useSuggest(); const { suggest, unsuggest } = useSuggest();
const { verify, unverify } = useVerify();
const { account: ownAccount } = useOwnAccount(); const { account: ownAccount } = useOwnAccount();
const features = useFeatures(); const features = useFeatures();
const { account } = useAccount(accountId); const { account } = useAccount(accountId);
@ -70,11 +68,11 @@ const AccountModerationModal: React.FC<IAccountModerationModal> = ({ onClose, ac
const { checked } = e.target; const { checked } = e.target;
const message = checked ? messages.userVerified : messages.userUnverified; const message = checked ? messages.userVerified : messages.userUnverified;
const action = checked ? verifyUser : unverifyUser; const action = checked ? verify : unverify;
dispatch(action(account.id)) action([account.acct], {
.then(() => toast.success(intl.formatMessage(message, { acct: account.acct }))) onSuccess: () => toast.success(intl.formatMessage(message, { acct: account.acct })),
.catch(() => {}); });
}; };
const handleSuggestedChange: ChangeEventHandler<HTMLInputElement> = (e) => { const handleSuggestedChange: ChangeEventHandler<HTMLInputElement> = (e) => {