Skip to content

Commit

Permalink
517 protect results page (#520)
Browse files Browse the repository at this point in the history
* Update back button to recieve a fallback route

* Create a loader to redirect to cycle id if cycle is open (protect /results)

* Add fallback route to cycle back button
  • Loading branch information
camilovegag authored May 20, 2024
1 parent bd09454 commit a926501
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
18 changes: 18 additions & 0 deletions packages/berlin/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ async function redirectToCycleResultsLoader(
return null;
}

/**
* Redirects the user to the cycle page if the cycle is open
*/
async function redirectToCycleIfOpen(queryClient: QueryClient, eventId?: string, cycleId?: string) {
const cycle = await queryClient.fetchQuery({
queryKey: ['cycles', cycleId],
queryFn: () => fetchCycle(cycleId || ''),
});

if (cycle?.status === 'OPEN') {
return redirect(`/events/${eventId}/cycles/${cycleId}`);
}

return null;
}

const router = (queryClient: QueryClient) =>
createBrowserRouter([
{ path: '/popup', element: <PassportPopupRedirect /> },
Expand Down Expand Up @@ -211,6 +227,8 @@ const router = (queryClient: QueryClient) =>
},
{
path: ':cycleId/results',
loader: ({ params }) =>
redirectToCycleIfOpen(queryClient, params.eventId, params.cycleId),
Component: Results,
},
{
Expand Down
19 changes: 16 additions & 3 deletions packages/berlin/src/components/back-button/BackButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@ import { useNavigate } from 'react-router-dom';
import { useAppStore } from '../../store';
import IconButton from '../icon-button';

function BackButton() {
type BackButtonProps = {
fallbackRoute?: string;
};

function BackButton({ fallbackRoute }: BackButtonProps) {
const navigate = useNavigate();
const theme = useAppStore((state) => state.theme);

const handleBackClick = () => {
if (fallbackRoute) {
navigate(fallbackRoute);
} else {
navigate(-1);
}
};

return (
<IconButton
onClick={() => navigate(-1)}
onClick={handleBackClick}
$color="secondary"
icon={{ src: `/icons/arrow-back-${theme}.svg`, alt: 'Trash icon' }}
icon={{ src: `/icons/arrow-back-${theme}.svg`, alt: 'Back icon' }}
$padding={0}
/>
);
Expand Down
4 changes: 2 additions & 2 deletions packages/berlin/src/pages/Cycle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function Cycle() {
const queryClient = useQueryClient();

const { user } = useUser();
const { cycleId } = useParams();
const { eventId, cycleId } = useParams();
const { data: cycle } = useQuery({
queryKey: ['cycles', cycleId],
queryFn: () => fetchCycle(cycleId || ''),
Expand Down Expand Up @@ -268,7 +268,7 @@ function Cycle() {
return (
<FlexColumn $gap="2rem">
<FlexColumn>
<BackButton />
<BackButton fallbackRoute={`/events/${eventId}/cycles`} />
<Title>{currentCycle?.questionTitle}</Title>
<Body>{voteInfo}</Body>
<Body>
Expand Down

0 comments on commit a926501

Please sign in to comment.