Skip to content

Commit

Permalink
Enable navigating on paths outcome block
Browse files Browse the repository at this point in the history
  • Loading branch information
terotik committed Oct 13, 2024
1 parent 236be2a commit c2318ec
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions components/paths/contentblocks/PathsOutcomeBlock.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use client';
import { useState } from 'react';
import { useCallback, useEffect, useState } from 'react';

import { useTranslations } from 'next-intl';
import { usePathname, useRouter, useSearchParams } from 'next/navigation';
import { Card, CardBody, Col, Container, Row } from 'reactstrap';
import styled from 'styled-components';

Expand Down Expand Up @@ -64,9 +65,37 @@ export default function PathsOutcomeBlock() {
const yearRange = useReactiveVar(yearRangeVar);
const activeScenario = useReactiveVar(activeScenarioVar);
const path = '';
const searchParams = useSearchParams();
const router = useRouter();
const pathname = usePathname();
const queryNodeId = searchParams.get('node') ?? undefined;

const [lastActiveNodeId, setLastActiveNodeId] = useState<string | undefined>(
undefined
queryNodeId
);

const createQueryString = useCallback(
(name: string, value: string) => {
const params = new URLSearchParams(searchParams.toString());
params.set(name, value);

return params.toString();
},
[searchParams]
);

useEffect(() => {
if (queryNodeId === lastActiveNodeId) return;
if (lastActiveNodeId) {
router.replace(
pathname + '?' + createQueryString('node', lastActiveNodeId),
{ scroll: false }
);
}
// router clear query ?
}, [lastActiveNodeId, queryNodeId]);

// router.push(pathname + '?' + createQueryString('sort', 'asc'))
const queryResp = useQuery<GetPageQuery, GetPageQueryVariables>(GET_PAGE, {
variables: { path, goal: null },
fetchPolicy: 'cache-and-network',
Expand Down Expand Up @@ -96,7 +125,10 @@ export default function PathsOutcomeBlock() {

allNodes.set(outcomeNode.id, outcomeNode);
//setLastActiveNodeId(outcomeNode.id);
const activeNodeId = outcomeNode.id;
const activeNodeId =
lastActiveNodeId && allNodes.has(lastActiveNodeId)
? lastActiveNodeId
: outcomeNode.id;
// TODO: filtering out empty nodes, in some instances there are some -> investigate why
const visibleNodes = findVisibleNodes(allNodes, activeNodeId, []).filter(
(node) => node?.id
Expand Down

0 comments on commit c2318ec

Please sign in to comment.