Skip to content

Commit

Permalink
feat: update post page to show data if token
Browse files Browse the repository at this point in the history
  • Loading branch information
blushi committed Jun 27, 2024
1 parent 9f3c9de commit eb4a6c6
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const SIGNED_BY = 'Signed by';
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ export type PostFilesProps = {
files: Array<PostFile>;
mapboxToken?: string;
isAdmin: boolean;
hasToken?: boolean;
};

const PostFiles = ({
privacyType,
files,
mapboxToken,
isAdmin = false,
hasToken = false,
}: PostFilesProps) => {
const isPublic = privacyType === 'public';
const privateLocations = privacyType === 'private_locations';
Expand All @@ -39,7 +41,7 @@ const PostFiles = ({
(isPublic || privateLocations || isAdmin) && 'lg:h-[550px]',
)}
>
{(isPublic || isAdmin) && (
{(isPublic || isAdmin || hasToken) && (
<PostFilesPublic
files={files}
mapboxToken={mapboxToken}
Expand All @@ -48,10 +50,10 @@ const PostFiles = ({
privateFiles={privateFiles}
/>
)}
{!isAdmin && privateLocations && (
{!isAdmin && !hasToken && privateLocations && (
<PostFilesPrivateLocations files={files} />
)}
{!isAdmin && privateFiles && <PostFilesPrivateFiles />}
{!isAdmin && !hasToken && privateFiles && <PostFilesPrivateFiles />}
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@ import {

export const getPostQuery = ({
iri,
token,
...params
}: ReactQueryGetPostQueryParams): ReactQueryGetPostQueryResponse => ({
queryKey: [GET_POST_QUERY_KEY, iri],
queryKey: [GET_POST_QUERY_KEY, iri, token ?? ''],
queryFn: async () => {
try {
const resp = await fetch(`${apiUri}/marketplace/v1/posts/${iri}`, {
method: 'GET',
credentials: 'include',
});
const resp = await fetch(
`${apiUri}/marketplace/v1/posts/${iri}${
token ? `?token=${token}` : ''
}`,
{
method: 'GET',
credentials: 'include',
},
);
if (resp.status === 200 || resp.status === 401) {
return await resp.json();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ export type ReactQueryGetPostQueryResponse = QueryObserverOptions<Post | null>;

export type ReactQueryGetPostQueryParams = {
iri?: string;
token?: string | null;
} & ReactQueryBuilderResponse<ReactQueryGetPostQueryResponse>;
19 changes: 14 additions & 5 deletions web-marketplace/src/pages/Post/Post.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useMemo } from 'react';
import { useParams } from 'react-router-dom';
import { useParams, useSearchParams } from 'react-router-dom';
import {
ApolloClient,
NormalizedCacheObject,
Expand Down Expand Up @@ -29,10 +29,13 @@ import { PostTimeline } from './Post.Timeline';

function Post(): JSX.Element {
const { iri } = useParams();
const [searchParams] = useSearchParams();
const token = searchParams.get('token');
const { activeAccountId } = useAuth();
const { data, isFetching } = useQuery(
getPostQuery({
iri,
token,
enabled: !!iri,
}),
);
Expand All @@ -56,11 +59,16 @@ function Post(): JSX.Element {
const isAdmin =
!!adminAccountId && !!activeAccountId && adminAccountId === activeAccountId;
const privacyType = data?.privacy;
const privatePost =
data?.privacy === 'private' || data?.error === 'private post';
const privatePostError = data?.error === 'private post';
const privatePost = data?.privacy === 'private';
const privateFiles = data?.privacy === 'private_files';
const privateLocations = data?.privacy === 'private_locations';
const publicPost = data?.privacy === 'public';

const hasToken =
(privateFiles && !!data?.filesUrls) ||
(privateLocations && data?.contents?.files?.some(file => file.location));

const files = useMemo(
() =>
data?.contents?.files?.map((file, i) => {
Expand Down Expand Up @@ -96,12 +104,12 @@ function Post(): JSX.Element {
<NotFoundPage />
) : (
<>
{!loadingOffchainProjectById && !isAdmin && privatePost && (
{!loadingOffchainProjectById && !isAdmin && privatePostError && (
<PostPrivate />
)}
{!loadingOffchainProjectById &&
data?.contents &&
(!privatePost || isAdmin) && (
(!privatePostError || isAdmin) && (
<>
<PostHeader
projectHref={`/project/${
Expand Down Expand Up @@ -133,6 +141,7 @@ function Post(): JSX.Element {
mapboxToken={MAPBOX_TOKEN}
isAdmin={isAdmin}
files={files}
hasToken={hasToken}
/>
{(isAdmin || !privateFiles) && (
<img
Expand Down
46 changes: 46 additions & 0 deletions web-marketplace/src/pages/Post/hooks/useSharePrivateLink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { useCallback } from 'react';
import { useQuery } from '@tanstack/react-query';
import { useSetAtom } from 'jotai';
import { postData } from 'utils/fetch/postData';

import { COPY_SUCCESS } from 'web-components/src/components/organisms/ProfileHeader/ProfileHeader.constants';
import copyTextToClipboard from 'web-components/src/utils/copy';

import { apiUri } from 'lib/apiUri';
import { bannerTextAtom } from 'lib/atoms/banner.atoms';
import { errorBannerTextAtom } from 'lib/atoms/error.atoms';
import { useRetryCsrfRequest } from 'lib/errors/hooks/useRetryCsrfRequest';
import { getCsrfTokenQuery } from 'lib/queries/react-query/registry-server/getCsrfTokenQuery/getCsrfTokenQuery';

type Params = {
iri?: string;
};

export const useSharePrivateLink = ({ iri }: Params) => {
const retryCsrfRequest = useRetryCsrfRequest();
const { data: token } = useQuery(getCsrfTokenQuery({}));
const setErrorBannerTextAtom = useSetAtom(errorBannerTextAtom);
const setBannerText = useSetAtom(bannerTextAtom);

const sharePrivateLink = useCallback(async () => {
if (token && iri) {
try {
await postData({
url: `${apiUri}/marketplace/v1/posts/${iri}/token`,
token,
retryCsrfRequest,
onSuccess: async response => {
const token = response.token;
await copyTextToClipboard(
`${window.location.origin}/post/${iri}?token=${token}`,
);
setBannerText(COPY_SUCCESS);
},
});
} catch (e) {
setErrorBannerTextAtom(String(e));
}
}
}, [iri, retryCsrfRequest, setBannerText, setErrorBannerTextAtom, token]);
return sharePrivateLink;
};

0 comments on commit eb4a6c6

Please sign in to comment.