mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
feat(journey): read and write comments and followers in the client hooks
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.
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
// FE-API-JOURNEY-SOCIAL-001
|
||||
//
|
||||
// #1702, #1669: every call pins its method and path and hands back what the server answered.
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { server } from '../../tests/helpers/msw/server';
|
||||
import { journeySocialApi } from './journeySocial';
|
||||
|
||||
describe('journeySocialApi', () => {
|
||||
it('FE-API-JOURNEY-SOCIAL-001: calls the comment, follower and link routes', async () => {
|
||||
const seen: string[] = [];
|
||||
const record =
|
||||
(body: unknown) =>
|
||||
({ request }: { request: Request }) => {
|
||||
seen.push(`${request.method} ${new URL(request.url).pathname}`);
|
||||
return HttpResponse.json(body);
|
||||
};
|
||||
const ok = { success: true };
|
||||
server.use(
|
||||
http.get('/api/journeys/7/comments', record({ comments: [], can_moderate: false })),
|
||||
http.post('/api/journeys/7/entries/2/comments', record({ comment: { id: 1 } })),
|
||||
http.patch('/api/journeys/7/comments/1', record(ok)),
|
||||
http.delete('/api/journeys/7/comments/1', record(ok)),
|
||||
http.get('/api/journeys/7/share-link/subscribers', record({ count: 0, pending: 0, subscribers: [] })),
|
||||
http.delete('/api/journeys/7/share-link/subscribers/4', record(ok)),
|
||||
http.get('/api/public/journey/:token/comments', record({ comments: [] })),
|
||||
http.post('/api/public/journey/:token/entries/2/comments', record(ok)),
|
||||
http.post('/api/public/journey/:token/subscribe', record(ok)),
|
||||
http.post('/api/public/journey-subscriptions/confirm', record({ title: 'Azores', link: 'tok' })),
|
||||
http.post('/api/public/journey-subscriptions/unsubscribe', record(ok))
|
||||
);
|
||||
|
||||
expect(await journeySocialApi.comments(7)).toEqual({ comments: [], can_moderate: false });
|
||||
expect(await journeySocialApi.addComment(7, 2, 'Hi')).toEqual({ comment: { id: 1 } });
|
||||
expect(await journeySocialApi.setCommentHidden(7, 1, true)).toEqual(ok);
|
||||
expect(await journeySocialApi.deleteComment(7, 1)).toEqual(ok);
|
||||
expect(await journeySocialApi.subscribers(7)).toEqual({ count: 0, pending: 0, subscribers: [] });
|
||||
expect(await journeySocialApi.removeSubscriber(7, 4)).toEqual(ok);
|
||||
expect(await journeySocialApi.publicComments('a/b')).toEqual({ comments: [] });
|
||||
expect(await journeySocialApi.addPublicComment('tok', 2, { name: 'Bo', body: 'Hi' })).toEqual(ok);
|
||||
expect(await journeySocialApi.subscribe('tok', { email: 'bo@example.com' })).toEqual(ok);
|
||||
expect(await journeySocialApi.confirmSubscription('c')).toEqual({ title: 'Azores', link: 'tok' });
|
||||
expect(await journeySocialApi.unsubscribe('u')).toEqual(ok);
|
||||
expect(seen).toContain('GET /api/public/journey/a%2Fb/comments');
|
||||
expect(seen).toHaveLength(11);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,49 @@
|
||||
import type {
|
||||
JourneyCommentCreateResponse,
|
||||
JourneyCommentsResponse,
|
||||
JourneyPublicCommentCreateRequest,
|
||||
JourneyPublicCommentsResponse,
|
||||
JourneyPublicSubscribeRequest,
|
||||
JourneyShareSubscribersResponse,
|
||||
JourneySubscriptionConfirmResponse,
|
||||
} from '@trek/shared';
|
||||
import apiClient from './client';
|
||||
|
||||
const pub = (token: string) => `/public/journey/${encodeURIComponent(token)}`;
|
||||
|
||||
/**
|
||||
* The social side of a journey (#1702, #1669): its comments for the people on
|
||||
* it, the followers of its share link for its owner, and what a reader of the
|
||||
* link may do besides reading (comment, follow by email, confirm and stop
|
||||
* following from the mail's links).
|
||||
*/
|
||||
export const journeySocialApi = {
|
||||
comments: (journeyId: number, signal?: AbortSignal): Promise<JourneyCommentsResponse> =>
|
||||
apiClient.get(`/journeys/${journeyId}/comments`, { signal }).then((r) => r.data),
|
||||
addComment: (journeyId: number, entryId: number, body: string): Promise<JourneyCommentCreateResponse> =>
|
||||
apiClient.post(`/journeys/${journeyId}/entries/${entryId}/comments`, { body }).then((r) => r.data),
|
||||
setCommentHidden: (journeyId: number, commentId: number, hidden: boolean): Promise<{ success: true }> =>
|
||||
apiClient.patch(`/journeys/${journeyId}/comments/${commentId}`, { hidden }).then((r) => r.data),
|
||||
deleteComment: (journeyId: number, commentId: number): Promise<{ success: true }> =>
|
||||
apiClient.delete(`/journeys/${journeyId}/comments/${commentId}`).then((r) => r.data),
|
||||
|
||||
subscribers: (journeyId: number, signal?: AbortSignal): Promise<JourneyShareSubscribersResponse> =>
|
||||
apiClient.get(`/journeys/${journeyId}/share-link/subscribers`, { signal }).then((r) => r.data),
|
||||
removeSubscriber: (journeyId: number, subscriberId: number): Promise<{ success: true }> =>
|
||||
apiClient.delete(`/journeys/${journeyId}/share-link/subscribers/${subscriberId}`).then((r) => r.data),
|
||||
|
||||
publicComments: (token: string, signal?: AbortSignal): Promise<JourneyPublicCommentsResponse> =>
|
||||
apiClient.get(`${pub(token)}/comments`, { signal }).then((r) => r.data),
|
||||
addPublicComment: (
|
||||
token: string,
|
||||
entryId: number,
|
||||
body: JourneyPublicCommentCreateRequest
|
||||
): Promise<{ success: true }> =>
|
||||
apiClient.post(`${pub(token)}/entries/${entryId}/comments`, body).then((r) => r.data),
|
||||
subscribe: (token: string, body: JourneyPublicSubscribeRequest): Promise<{ success: true }> =>
|
||||
apiClient.post(`${pub(token)}/subscribe`, body).then((r) => r.data),
|
||||
confirmSubscription: (mailToken: string): Promise<JourneySubscriptionConfirmResponse> =>
|
||||
apiClient.post('/public/journey-subscriptions/confirm', { token: mailToken }).then((r) => r.data),
|
||||
unsubscribe: (mailToken: string): Promise<{ success: true }> =>
|
||||
apiClient.post('/public/journey-subscriptions/unsubscribe', { token: mailToken }).then((r) => r.data),
|
||||
};
|
||||
Reference in New Issue
Block a user