mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 00:43:15 +00:00
f1e64ef686
The data side of journey comments and following a link by email (#1702, #1669), for the views to draw on, online only like the journey itself: - api/journeySocial and repo/journeySocialRepo, parsed against the shared contracts. - useJourneyComments: a journey's comments grouped by entry, add, hide and delete, read again on journey:entry:commented. - useJourneySubscribers: the owner's list of followers and their removal. - useJourneyPublicSocial: the comments a share link shows, a reader's comment and follow request, each answering what it came to. - useJourneyFollow: the confirm and unsubscribe pages a follower's mail links open, posting the token once. - useJourneyShareLink switches allow_comments and allow_subscriptions on their own, like the map extras.
66 lines
2.2 KiB
TypeScript
66 lines
2.2 KiB
TypeScript
import type { JourneyShareSubscribersResponse } from '@trek/shared';
|
|
import { useCallback, useEffect, useState } from 'react';
|
|
import { journeySocialRepo } from '../../repo/journeySocialRepo';
|
|
|
|
interface Loaded {
|
|
journeyId: number | null;
|
|
data: JourneyShareSubscribersResponse;
|
|
}
|
|
|
|
const EMPTY: JourneyShareSubscribersResponse = { count: 0, pending: 0, subscribers: [] };
|
|
|
|
/**
|
|
* The addresses following a journey's share link by email (#1669), for its
|
|
* owner's share section on the desktop page and the phone sheet. Read only
|
|
* while `enabled` (the owner, with following switched on), and removed one by
|
|
* one; a removal the server refuses leaves the list as it was and resolves
|
|
* false.
|
|
*/
|
|
export function useJourneySubscribers(journeyId: number, enabled: boolean) {
|
|
const [loaded, setLoaded] = useState<Loaded>({ journeyId: null, data: EMPTY });
|
|
const [failed, setFailed] = useState(false);
|
|
const data = enabled && loaded.journeyId === journeyId ? loaded.data : EMPTY;
|
|
const status = !enabled ? 'idle' : loaded.journeyId === journeyId ? 'ready' : failed ? 'failed' : 'loading';
|
|
|
|
useEffect(() => {
|
|
if (!enabled) return;
|
|
const controller = new AbortController();
|
|
journeySocialRepo
|
|
.subscribers(journeyId, controller.signal)
|
|
.then((next) => {
|
|
setFailed(false);
|
|
setLoaded({ journeyId, data: next });
|
|
})
|
|
.catch(() => {
|
|
if (!controller.signal.aborted) setFailed(true);
|
|
});
|
|
return () => controller.abort();
|
|
}, [journeyId, enabled]);
|
|
|
|
const remove = useCallback(
|
|
async (subscriberId: number): Promise<boolean> => {
|
|
try {
|
|
await journeySocialRepo.removeSubscriber(journeyId, subscriberId);
|
|
setLoaded((prev) => {
|
|
const gone = prev.data.subscribers.find((s) => s.id === subscriberId);
|
|
const subscribers = prev.data.subscribers.filter((s) => s.id !== subscriberId);
|
|
return {
|
|
...prev,
|
|
data: {
|
|
subscribers,
|
|
count: prev.data.count - (gone?.confirmed ? 1 : 0),
|
|
pending: prev.data.pending - (gone && !gone.confirmed ? 1 : 0),
|
|
},
|
|
};
|
|
});
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
},
|
|
[journeyId]
|
|
);
|
|
|
|
return { ...data, status, remove };
|
|
}
|