Skip to content

Commit

Permalink
feat: Redirect to the feeds table if the user is not authenticated to…
Browse files Browse the repository at this point in the history
… view the feed
  • Loading branch information
PintoGideon committed Apr 29, 2024
1 parent 29b623e commit 5ae98a5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
26 changes: 24 additions & 2 deletions src/components/Feeds/FeedView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import {
DrawerPanelContent,
} from "@patternfly/react-core";
import { useQuery } from "@tanstack/react-query";
import { notification } from "antd";
import * as React from "react";
import { useDispatch } from "react-redux";
import { useNavigate, useParams, useLocation } from "react-router";
import { useLocation, useNavigate, useParams } from "react-router";
import { clearSelectedFile } from "../../store/explorer/actions";
import {
getFeedSuccess,
Expand Down Expand Up @@ -48,6 +49,7 @@ export default function FeedView() {
const dispatch = useDispatch();
const navigate = useNavigate();
const location = useLocation();
const [api, contextHolder] = notification.useNotification();
const { id } = params;
const selectedPlugin = useTypedSelector(
(state) => state.instance.selectedPlugin,
Expand All @@ -73,7 +75,11 @@ export default function FeedView() {
enabled: type === "public",
});

const { data: privateFeed } = useQuery({
const {
data: privateFeed,
isError: isPrivateFeedError,
error: privateFeedError,
} = useQuery({
queryKey: ["authenticatedFeed", id],
queryFn: () => fetchAuthenticatedFeed(id),
enabled: type === "private" && isLoggedIn,
Expand All @@ -88,6 +94,21 @@ export default function FeedView() {
}
}, [type, navigate, isLoggedIn]);

React.useEffect(() => {
if (isPrivateFeedError) {
// cube does not return a 404 error when the user fetches a
// feed with an incorrect token
api.error({
message: privateFeedError.message,
duration: 1.5,
});

setTimeout(() => {
navigate("/feeds?type=private");
}, 2500);
}
}, [isPrivateFeedError]);

React.useEffect(() => {
const feed: Feed | undefined = privateFeed || publicFeed;
if (feed) {
Expand Down Expand Up @@ -195,6 +216,7 @@ export default function FeedView() {

return (
<WrapperConnect>
{contextHolder}
<Drawer
isInline
position="bottom"
Expand Down
32 changes: 24 additions & 8 deletions src/components/Feeds/utilties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,35 @@ export const fetchPublicFeeds = async (filterState: any) => {

export async function fetchAuthenticatedFeed(id?: string) {
if (!id) return;
const client = ChrisAPIClient.getClient();
const feed = await client.getFeed(+id);
return feed;

try {
const client = ChrisAPIClient.getClient();
const feed = await client.getFeed(+id);

if (!feed) {
throw new Error(
"You do not permissions to view this feed. Redirecting...",
);
}
return feed;
} catch (error) {
throw error;
}
}

export async function fetchPublicFeed(id?: string) {
if (!id) return;
const client = ChrisAPIClient.getClient();
const publicFeed = await client.getPublicFeeds({ id: +id });
try {
const client = ChrisAPIClient.getClient();
const publicFeed = await client.getPublicFeeds({ id: +id });

//@ts-ignore
if (publicFeed && publicFeed.getItems().length > 0) {
//@ts-ignore
return publicFeed.getItems()[0] as any as Feed;
if (publicFeed && publicFeed.getItems().length > 0) {
//@ts-ignore
return publicFeed.getItems()[0] as any as Feed;
}
throw new Error("Failed to fetch this feed...");
} catch (error) {
throw error;
}
}

0 comments on commit 5ae98a5

Please sign in to comment.