mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 00:43:15 +00:00
feat(journey): read and unlock protected share links in the client hooks
The data side of the protected journey link (#2553), without markup yet: - journeyApi.unlockPublicJourney posts a reader's password; the answer sets the link's unlock cookie. - useJourneyPublic tells a locked link (access 'locked') and an expired one ('expired') apart from a broken one, and unlock() reports 'ok', 'wrong', 'rate_limited', 'expired' or 'failed', reloading the journey on success. - useJourneyShareLink carries has_password, expires_at, view_count and last_viewed_at, and setProtection() sets or clears the password and the expiry, shown only once the server took it.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// FE-JRN-SHAREHOOK-001 to FE-JRN-SHAREHOOK-009: the public share link logic behind
|
||||
// FE-JRN-SHAREHOOK-001 to FE-JRN-SHAREHOOK-011: the public share link logic behind
|
||||
// both the desktop share section and the phone settings sheet.
|
||||
import { act, renderHook, waitFor } from '@testing-library/react';
|
||||
import { http, HttpResponse } from 'msw';
|
||||
@@ -134,4 +134,37 @@ describe('useJourneyShareLink', () => {
|
||||
await act(() => result.current.togglePerm('share_recordings'));
|
||||
expect(posted[1]).toEqual({ share_timeline: true, share_gallery: false, share_map: true, share_recordings: true });
|
||||
});
|
||||
|
||||
it('FE-JRN-SHAREHOOK-010: a password and an expiry are saved on their own and only shown once taken (#2553)', async () => {
|
||||
const { result } = await loaded();
|
||||
let saved: boolean | undefined;
|
||||
await act(async () => {
|
||||
saved = await result.current.setProtection({ password: 'secret', expires_at: '2027-01-01T00:00:00.000Z' });
|
||||
});
|
||||
expect(saved).toBe(true);
|
||||
expect(posted).toEqual([{ password: 'secret', expires_at: '2027-01-01T00:00:00.000Z' }]);
|
||||
expect(result.current.link?.has_password).toBe(true);
|
||||
expect(result.current.link?.expires_at).toBe('2027-01-01T00:00:00.000Z');
|
||||
// The password itself is never kept on the client.
|
||||
expect(JSON.stringify(result.current.link)).not.toContain('secret');
|
||||
|
||||
await act(async () => {
|
||||
await result.current.setProtection({ password: null });
|
||||
});
|
||||
expect(posted[1]).toEqual({ password: null });
|
||||
expect(result.current.link?.has_password).toBe(false);
|
||||
expect(result.current.link?.expires_at).toBe('2027-01-01T00:00:00.000Z');
|
||||
});
|
||||
|
||||
it('FE-JRN-SHAREHOOK-011: a refused protection change toasts and leaves the link as it was', async () => {
|
||||
const { result } = await loaded();
|
||||
server.use(http.post('/api/journeys/7/share-link', () => HttpResponse.json({ error: 'x' }, { status: 400 })));
|
||||
let saved: boolean | undefined;
|
||||
await act(async () => {
|
||||
saved = await result.current.setProtection({ expires_at: null });
|
||||
});
|
||||
expect(saved).toBe(false);
|
||||
expect(result.current.link).toEqual(LINK);
|
||||
expect(addToast).toHaveBeenCalledWith('journey.share.updateFailed', 'error', undefined);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -13,6 +13,22 @@ export interface JourneyShareLink {
|
||||
/** JRN-04: the trips' plan and the recorded days on the shared map; off unless switched on. */
|
||||
share_plan?: boolean;
|
||||
share_recordings?: boolean;
|
||||
/** #2553: whether readers have to enter a password first; the password itself never comes back. */
|
||||
has_password?: boolean;
|
||||
/** When the link stops working (ISO, UTC), null for never. */
|
||||
expires_at?: string | null;
|
||||
/** Readers who opened the link, each counted once a day, and when the last one did (ISO, UTC). */
|
||||
view_count?: number;
|
||||
last_viewed_at?: string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* A change to a link's protection (#2553): a new password or null to take it
|
||||
* off, an ISO expiry or null for never. What is left out stays as it is.
|
||||
*/
|
||||
interface JourneyShareProtectionChange {
|
||||
password?: string | null;
|
||||
expires_at?: string | null;
|
||||
}
|
||||
|
||||
/** What the shared map can show besides the entries, each only while the map itself is shared. */
|
||||
@@ -25,6 +41,15 @@ export const SHARE_MAP_EXTRAS: { key: JourneyShareMapExtra; labelKey: string; hi
|
||||
{ key: 'share_recordings', labelKey: 'journey.share.recordings', hintKey: 'journey.share.recordingsHint' },
|
||||
];
|
||||
|
||||
/** The link as it reads after `change` was saved: the password is only ever known as there or not. */
|
||||
function applyProtection(link: JourneyShareLink, change: JourneyShareProtectionChange): JourneyShareLink {
|
||||
return {
|
||||
...link,
|
||||
...(change.password !== undefined ? { has_password: change.password !== null } : {}),
|
||||
...(change.expires_at !== undefined ? { expires_at: change.expires_at } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
/** The public address of a journey share token. */
|
||||
export function journeyShareUrl(link: JourneyShareLink | null, origin: string): string {
|
||||
return link ? `${origin}/public/journey/${link.token}` : '';
|
||||
@@ -101,9 +126,27 @@ export function useJourneyShareLink(journeyId: number) {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Set or take off the password, or change the expiry. Not optimistic: the
|
||||
* link only shows the new state once the server took it, and a refusal toasts
|
||||
* and leaves it as it was. Resolves whether it was saved, so a dialog knows
|
||||
* whether to close.
|
||||
*/
|
||||
const setProtection = async (change: JourneyShareProtectionChange): Promise<boolean> => {
|
||||
if (!link) return false;
|
||||
try {
|
||||
await journeyApi.createShareLink(journeyId, change);
|
||||
setLink((current) => (current ? applyProtection(current, change) : current));
|
||||
return true;
|
||||
} catch {
|
||||
toast.error(t('journey.share.updateFailed'));
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const shareUrl = journeyShareUrl(link, window.location.origin);
|
||||
|
||||
const copyLink = () => copy(shareUrl);
|
||||
|
||||
return { link, loading, manageable, copied, shareUrl, createLink, togglePerm, deleteLink, copyLink };
|
||||
return { link, loading, manageable, copied, shareUrl, createLink, togglePerm, deleteLink, copyLink, setProtection };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user