parent
cff5f96df4
commit
ad1718b5f9
@ -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 };
|
@ -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 };
|
Loading…
Reference in new issue