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 { importEntities } from 'soapbox/entity-store/actions.ts';
|
||||||
import { Entities } from 'soapbox/entity-store/entities.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 { useApi } from 'soapbox/hooks/useApi.ts';
|
||||||
import { useAppDispatch } from 'soapbox/hooks/useAppDispatch.ts';
|
import { useAppDispatch } from 'soapbox/hooks/useAppDispatch.ts';
|
||||||
import { useLoggedIn } from 'soapbox/hooks/useLoggedIn.ts';
|
import { useLoggedIn } from 'soapbox/hooks/useLoggedIn.ts';
|
||||||
|
@ -21,6 +22,15 @@ function useBookmark() {
|
||||||
const { isLoggedIn } = useLoggedIn();
|
const { isLoggedIn } = useLoggedIn();
|
||||||
const { transaction } = useTransaction();
|
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) {
|
function bookmarkEffect(statusId: string) {
|
||||||
transaction({
|
transaction({
|
||||||
Statuses: {
|
Statuses: {
|
||||||
|
@ -43,8 +53,8 @@ function useBookmark() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function bookmark(statusId: string) {
|
async function bookmark(statusId: string): Promise<Success> {
|
||||||
if (!isLoggedIn) return;
|
if (!isLoggedIn) return { success: false };
|
||||||
bookmarkEffect(statusId);
|
bookmarkEffect(statusId);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -53,19 +63,23 @@ function useBookmark() {
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
dispatch(importEntities([result.data], Entities.STATUSES, 'bookmarks'));
|
dispatch(importEntities([result.data], Entities.STATUSES, 'bookmarks'));
|
||||||
}
|
}
|
||||||
|
return { success: true };
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
unbookmarkEffect(statusId);
|
unbookmarkEffect(statusId);
|
||||||
|
return { success: false };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function unbookmark(statusId: string) {
|
async function unbookmark(statusId: string): Promise<Success> {
|
||||||
if (!isLoggedIn) return;
|
if (!isLoggedIn) return { success: false };
|
||||||
unbookmarkEffect(statusId);
|
unbookmarkEffect(statusId);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await api.post(`/api/v1/statuses/${statusId}/unbookmark`);
|
await dismissEntity(statusId);
|
||||||
|
return { success: true };
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
bookmarkEffect(statusId);
|
bookmarkEffect(statusId);
|
||||||
|
return { success: false };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue