kopia lustrzana https://gitlab.com/soapbox-pub/soapbox
fix: use useDismissEntity when undoing a bookmark
rodzic
a339bb93ea
commit
cfce081063
|
@ -1,6 +1,7 @@
|
|||
import { importEntities } from 'soapbox/entity-store/actions.ts';
|
||||
import { Entities } from 'soapbox/entity-store/entities.ts';
|
||||
import { useTransaction } from 'soapbox/entity-store/hooks/index.ts';
|
||||
import { useDismissEntity, useTransaction } from 'soapbox/entity-store/hooks/index.ts';
|
||||
import { ExpandedEntitiesPath } from 'soapbox/entity-store/hooks/types.ts';
|
||||
import { useApi } from 'soapbox/hooks/useApi.ts';
|
||||
import { useAppDispatch } from 'soapbox/hooks/useAppDispatch.ts';
|
||||
import { useLoggedIn } from 'soapbox/hooks/useLoggedIn.ts';
|
||||
|
@ -21,6 +22,15 @@ function useBookmark() {
|
|||
const { isLoggedIn } = useLoggedIn();
|
||||
const { transaction } = useTransaction();
|
||||
|
||||
type Success = { success: boolean }
|
||||
|
||||
const path: ExpandedEntitiesPath = [Entities.STATUSES, 'bookmarks'];
|
||||
|
||||
const { dismissEntity } = useDismissEntity(path, async (statusId: string) => {
|
||||
const response = await api.post(`/api/v1/statuses/${statusId}/unbookmark`);
|
||||
return response;
|
||||
});
|
||||
|
||||
function bookmarkEffect(statusId: string) {
|
||||
transaction({
|
||||
Statuses: {
|
||||
|
@ -43,8 +53,8 @@ function useBookmark() {
|
|||
});
|
||||
}
|
||||
|
||||
async function bookmark(statusId: string) {
|
||||
if (!isLoggedIn) return;
|
||||
async function bookmark(statusId: string): Promise<Success> {
|
||||
if (!isLoggedIn) return { success: false };
|
||||
bookmarkEffect(statusId);
|
||||
|
||||
try {
|
||||
|
@ -53,19 +63,23 @@ function useBookmark() {
|
|||
if (result.success) {
|
||||
dispatch(importEntities([result.data], Entities.STATUSES, 'bookmarks'));
|
||||
}
|
||||
return { success: true };
|
||||
} catch (e) {
|
||||
unbookmarkEffect(statusId);
|
||||
return { success: false };
|
||||
}
|
||||
}
|
||||
|
||||
async function unbookmark(statusId: string) {
|
||||
if (!isLoggedIn) return;
|
||||
async function unbookmark(statusId: string): Promise<Success> {
|
||||
if (!isLoggedIn) return { success: false };
|
||||
unbookmarkEffect(statusId);
|
||||
|
||||
try {
|
||||
await api.post(`/api/v1/statuses/${statusId}/unbookmark`);
|
||||
await dismissEntity(statusId);
|
||||
return { success: true };
|
||||
} catch (e) {
|
||||
bookmarkEffect(statusId);
|
||||
return { success: false };
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue