suggest and verify by account IDs, simplify hooks
This commit is contained in:
@@ -1,20 +1,16 @@
|
||||
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 { accountIdsToAccts } from 'soapbox/selectors';
|
||||
|
||||
import type { Account } from 'soapbox/schemas';
|
||||
import type { RootState } from 'soapbox/store';
|
||||
|
||||
function useSuggest() {
|
||||
const api = useApi();
|
||||
const getState = useGetState();
|
||||
const { transaction } = useTransaction();
|
||||
|
||||
function suggestEffect(accts: string[], suggested: boolean) {
|
||||
const ids = selectIdsForAccts(getState(), accts);
|
||||
|
||||
function suggestEffect(accountIds: string[], suggested: boolean) {
|
||||
const updater = (account: Account): Account => {
|
||||
if (account.pleroma) {
|
||||
account.pleroma.is_suggested = suggested;
|
||||
@@ -23,31 +19,33 @@ function useSuggest() {
|
||||
};
|
||||
|
||||
transaction({
|
||||
Accounts: ids.reduce<Record<string, (account: Account) => Account>>(
|
||||
Accounts: accountIds.reduce<Record<string, (account: Account) => Account>>(
|
||||
(result, id) => ({ ...result, [id]: updater }),
|
||||
{}),
|
||||
});
|
||||
}
|
||||
|
||||
async function suggest(accts: string[], callbacks?: EntityCallbacks<void, unknown>) {
|
||||
suggestEffect(accts, true);
|
||||
async function suggest(accountIds: string[], callbacks?: EntityCallbacks<void, unknown>) {
|
||||
const accts = accountIdsToAccts(getState(), accountIds);
|
||||
suggestEffect(accountIds, true);
|
||||
try {
|
||||
await api.patch('/api/v1/pleroma/admin/users/suggest', { nicknames: accts });
|
||||
callbacks?.onSuccess?.();
|
||||
} catch (e) {
|
||||
callbacks?.onError?.(e);
|
||||
suggestEffect(accts, false);
|
||||
suggestEffect(accountIds, false);
|
||||
}
|
||||
}
|
||||
|
||||
async function unsuggest(accts: string[], callbacks?: EntityCallbacks<void, unknown>) {
|
||||
suggestEffect(accts, false);
|
||||
async function unsuggest(accountIds: string[], callbacks?: EntityCallbacks<void, unknown>) {
|
||||
const accts = accountIdsToAccts(getState(), accountIds);
|
||||
suggestEffect(accountIds, false);
|
||||
try {
|
||||
await api.patch('/api/v1/pleroma/admin/users/unsuggest', { nicknames: accts });
|
||||
callbacks?.onSuccess?.();
|
||||
} catch (e) {
|
||||
callbacks?.onError?.(e);
|
||||
suggestEffect(accts, true);
|
||||
suggestEffect(accountIds, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,15 +55,4 @@ function useSuggest() {
|
||||
};
|
||||
}
|
||||
|
||||
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 { useSuggest };
|
||||
@@ -1,20 +1,16 @@
|
||||
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 { accountIdsToAccts } from 'soapbox/selectors';
|
||||
|
||||
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);
|
||||
|
||||
function verifyEffect(accountIds: string[], verified: boolean) {
|
||||
const updater = (account: Account): Account => {
|
||||
if (account.pleroma) {
|
||||
const tags = account.pleroma.tags.filter((tag) => tag !== 'verified');
|
||||
@@ -28,31 +24,33 @@ function useVerify() {
|
||||
};
|
||||
|
||||
transaction({
|
||||
Accounts: ids.reduce<Record<string, (account: Account) => Account>>(
|
||||
Accounts: accountIds.reduce<Record<string, (account: Account) => Account>>(
|
||||
(result, id) => ({ ...result, [id]: updater }),
|
||||
{}),
|
||||
});
|
||||
}
|
||||
|
||||
async function verify(accts: string[], callbacks?: EntityCallbacks<void, unknown>) {
|
||||
verifyEffect(accts, true);
|
||||
async function verify(accountIds: string[], callbacks?: EntityCallbacks<void, unknown>) {
|
||||
const accts = accountIdsToAccts(getState(), accountIds);
|
||||
verifyEffect(accountIds, 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);
|
||||
verifyEffect(accountIds, false);
|
||||
}
|
||||
}
|
||||
|
||||
async function unverify(accts: string[], callbacks?: EntityCallbacks<void, unknown>) {
|
||||
verifyEffect(accts, false);
|
||||
async function unverify(accountIds: string[], callbacks?: EntityCallbacks<void, unknown>) {
|
||||
const accts = accountIdsToAccts(getState(), accountIds);
|
||||
verifyEffect(accountIds, 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);
|
||||
verifyEffect(accountIds, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,15 +60,4 @@ function useVerify() {
|
||||
};
|
||||
}
|
||||
|
||||
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 };
|
||||
Reference in New Issue
Block a user