Add useFollow, useChangeEntity hooks

environments/review-change-ent-6o2wvy/deployments/3528
Alex Gleason 2023-06-22 23:17:40 -05:00
rodzic cff5f96df4
commit ad1718b5f9
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
4 zmienionych plików z 87 dodań i 12 usunięć

Wyświetl plik

@ -0,0 +1,57 @@
import { Entities } from 'soapbox/entity-store/entities';
import { useChangeEntity } from 'soapbox/entity-store/hooks';
import { useApi } from 'soapbox/hooks/useApi';
import { type Account } from 'soapbox/schemas';
function useChangeAccount() {
const { changeEntity: changeAccount } = useChangeEntity<Account>(Entities.ACCOUNTS);
return { changeAccount };
}
function useFollow() {
const api = useApi();
const { changeAccount } = useChangeAccount();
function incrementFollowers(accountId: string) {
changeAccount(accountId, (account) => ({
...account,
followers_count: account.followers_count + 1,
}));
}
function decrementFollowers(accountId: string) {
changeAccount(accountId, (account) => ({
...account,
followers_count: account.followers_count - 1,
}));
}
async function follow(accountId: string, options = {}) {
incrementFollowers(accountId);
try {
await api.post(`/api/v1/accounts/${accountId}/follow`, options);
} catch (e) {
decrementFollowers(accountId);
}
}
async function unfollow(accountId: string, options = {}) {
decrementFollowers(accountId);
try {
await api.post(`/api/v1/accounts/${accountId}/unfollow`, options);
} catch (e) {
incrementFollowers(accountId);
}
}
return {
follow,
unfollow,
incrementFollowers,
decrementFollowers,
};
}
export { useFollow };

Wyświetl plik

@ -1,13 +1,11 @@
/**
* Accounts
*/
// Accounts
export { useAccount } from './accounts/useAccount';
export { useFollow } from './accounts/useFollow';
export { useRelationships } from './accounts/useRelationships';
export { usePatronUser } from './accounts/usePatronUser';
/**
* Groups
*/
// Groups
export { useBlockGroupMember } from './groups/useBlockGroupMember';
export { useCancelMembershipRequest } from './groups/useCancelMembershipRequest';
export { useCreateGroup, type CreateGroupParams } from './groups/useCreateGroup';
@ -34,8 +32,3 @@ export { usePromoteGroupMember } from './groups/usePromoteGroupMember';
export { useSuggestedGroups } from './groups/useSuggestedGroups';
export { useUpdateGroup } from './groups/useUpdateGroup';
export { useUpdateGroupTag } from './groups/useUpdateGroupTag';
/**
* Relationships
*/
export { useRelationships } from './accounts/useRelationships';

Wyświetl plik

@ -5,4 +5,5 @@ export { useEntityLookup } from './useEntityLookup';
export { useCreateEntity } from './useCreateEntity';
export { useDeleteEntity } from './useDeleteEntity';
export { useDismissEntity } from './useDismissEntity';
export { useIncrementEntity } from './useIncrementEntity';
export { useIncrementEntity } from './useIncrementEntity';
export { useChangeEntity } from './useChangeEntity';

Wyświetl plik

@ -0,0 +1,24 @@
import { importEntities } from 'soapbox/entity-store/actions';
import { Entities } from 'soapbox/entity-store/entities';
import { type Entity } from 'soapbox/entity-store/types';
import { useAppDispatch, useGetState } from 'soapbox/hooks';
type ChangeEntityFn<TEntity extends Entity> = (entity: TEntity) => TEntity
function useChangeEntity<TEntity extends Entity = Entity>(entityType: Entities) {
const getState = useGetState();
const dispatch = useAppDispatch();
function changeEntity(entityId: string, change: ChangeEntityFn<TEntity>): void {
if (!entityId) return;
const entity = getState().entities[entityType]?.store[entityId] as TEntity | undefined;
if (entity) {
const newEntity = change(entity);
dispatch(importEntities([newEntity], entityType));
}
}
return { changeEntity };
}
export { useChangeEntity, type ChangeEntityFn };