Skip to content

Commit

Permalink
persist selection state in url
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Huang committed Oct 12, 2024
1 parent d7675ec commit 4e42679
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,43 @@ import ApplicationForm from "app/opportunities/_components/ApplicationForm";
import { Input } from "@components/ui/input";
import { Separator } from "components/ui/separator";
import { AvatarStack } from "@components/user/users-stack";
import { usePathname, useRouter } from "next/navigation";

interface SelectionState {
questionnaire?: string;
application?: number;
}
interface Props {
phases: OpportunityPhase[];
isAdmin: boolean;
opportunityId: number;
initialSelection: SelectionState;
}

export const ApplicationOverview = ({ phases, isAdmin }: Props) => {
const [
{ questionnaire: questionnaireId, application: applicationId },
setSelectionState,
] = useState<{
export const ApplicationOverview = ({
phases,
isAdmin,
initialSelection,
}: Props) => {
const [selectionState, setSelectionState] = useState<{
questionnaire?: string;
application?: number;
}>({
questionnaire: undefined,
application: undefined,
});
}>(initialSelection);

const { questionnaire: questionnaireId, application: applicationId } =
selectionState;

const pathname = usePathname();
const router = useRouter();

const updateSelection = (data: typeof selectionState) => {
setSelectionState(data);

const params = new URLSearchParams();
if (data.questionnaire) params.set("questionnaire", data.questionnaire);
if (data.application) params.set("application", String(data.application));
router.push(pathname + "?" + params.toString());
};

const selectedQuestionnaire = useMemo(() => {
return phases
Expand Down Expand Up @@ -122,7 +141,7 @@ export const ApplicationOverview = ({ phases, isAdmin }: Props) => {
<Card
key={`questionnaire-${questionnaire.id}`}
onClick={() =>
setSelectionState({
updateSelection({
questionnaire: questionnaire.id,
application: undefined,
})
Expand Down Expand Up @@ -175,10 +194,10 @@ export const ApplicationOverview = ({ phases, isAdmin }: Props) => {
{filteredApplications?.map((application) => (
<Card
onClick={() =>
setSelectionState((prev) => ({
...prev,
updateSelection({
questionnaire: questionnaireId,
application: application.id,
}))
})
}
key={`application-${application.id}`}
className={cn(
Expand Down
13 changes: 12 additions & 1 deletion app/opportunities/[opportunity_id]/applications/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,16 @@ interface Props {
params: {
opportunity_id: string;
};
searchParams: {
application?: string;
questionnaire?: string;
};
}

export default async function ApplicationsPage({ params }: Props) {
export default async function ApplicationsPage({
params,
searchParams,
}: Props) {
const session = await getServerAuthSession();
const userId = session?.user.id;
if (!userId) redirect("/auth");
Expand Down Expand Up @@ -112,6 +119,10 @@ export default async function ApplicationsPage({ params }: Props) {
opportunityId={opportunityId}
phases={phases}
isAdmin={isAdmin}
initialSelection={{
questionnaire: searchParams.questionnaire,
application: Number(searchParams.application),
}}
/>
</div>
);
Expand Down

0 comments on commit 4e42679

Please sign in to comment.