Skip to content

Commit

Permalink
expandedCards
Browse files Browse the repository at this point in the history
  • Loading branch information
chribjel committed Jan 20, 2025
1 parent c4dc2e6 commit b67a4c7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/components/delete-function-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Props = {
export function DeleteFunctionModal({ onClose, isOpen, functionId }: Props) {
const { func, removeFunction } = useFunction(functionId);
const navigate = Route.useNavigate();
const { path, flags, filters } = Route.useSearch();
const { path, flags, filters, expandedCards } = Route.useSearch();

return (
<Modal onClose={onClose} isOpen={isOpen} isCentered>
Expand Down Expand Up @@ -62,6 +62,7 @@ export function DeleteFunctionModal({ onClose, isOpen, functionId }: Props) {
: pathString,
) ?? ["1"],
filters,
expandedCards,
flags: flags,
},
});
Expand Down
16 changes: 13 additions & 3 deletions src/components/function-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Card, Flex, IconButton, Skeleton, Stack, Text } from "@kvib/react";
import { FunctionCardEdit } from "./function-card-edit";
import { FunctionCardExpandableContent } from "./function-card-expandable-content";
import { EditAndSelectButtons } from "./edit-and-select-buttons";
import { useEffect, useState } from "react";
import { useEffect, useMemo, useState } from "react";
import { useHasFunctionAccess } from "@/hooks/use-has-function-access";

export function FunctionCard({
Expand All @@ -17,10 +17,19 @@ export function FunctionCard({
const search = Route.useSearch();
const navigate = Route.useNavigate();

const [isCardExpanded, setIsCardExpanded] = useState(false);
const isCardExpanded = useMemo(() => {
return search.expandedCards?.includes(functionId) ?? false;
}, [functionId, search.expandedCards]);

const toggleCardExpanded = () => {
setIsCardExpanded((prev) => !prev);
navigate({
search: {
...search,
expandedCards: isCardExpanded
? search.expandedCards?.filter((id) => id !== functionId)
: [...(search.expandedCards ?? []), functionId],
},
});
};

const [bottomMargin, setBottomMargin] = useState(0);
Expand Down Expand Up @@ -65,6 +74,7 @@ export function FunctionCard({
? [func?.data?.path.slice(0, func?.data?.path.lastIndexOf("."))]
: [`${func?.data?.path}`]),
],
expandedCards: search.expandedCards,
filters: search.filters,
edit: search.edit,
flags: search.flags,
Expand Down
10 changes: 9 additions & 1 deletion src/components/search-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ export function SearchField() {
}}
onChange={(newFunc: { value: string } | null) => {
if (newFunc) {
navigate({ search: { ...search, path: [newFunc.value] } });
const newPath = newFunc.value;
const newId = Number.parseInt(newPath.split(".").pop() ?? "1");
navigate({
search: {
...search,
path: [newFunc.value],
expandedCards: [newId],
},
});
}
}}
placeholder="Søk på funksjonsnavnet her..."
Expand Down
10 changes: 9 additions & 1 deletion src/effects/create-and-redirect-effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function CreateAndRedirectEffect() {
newMetadataKey,
newMetadataValue,
redirect,
expandedCards,
} = Route.useSearch();

const { func } = useFunction(functionId ?? 1);
Expand All @@ -35,7 +36,13 @@ export function CreateAndRedirectEffect() {
if (redirect) {
window.location.href = redirect;
} else {
navigate({ search: { path: path, flags: flags } });
navigate({
search: {
path: path,
flags: flags,
expandedCards: expandedCards,
},
});
}
});
}
Expand All @@ -47,6 +54,7 @@ export function CreateAndRedirectEffect() {
path,
flags,
addMetadata,
expandedCards,
navigate,
]);

Expand Down
1 change: 1 addition & 0 deletions src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const functionSearchSchema = object({
),
["1"],
),
expandedCards: array(number()).optional(),
functionId: number().optional(),
edit: number().optional(),
newMetadataKey: string().optional(),
Expand Down

0 comments on commit b67a4c7

Please sign in to comment.