Skip to content

Commit

Permalink
feat(besitz): add returnTo for edit & add
Browse files Browse the repository at this point in the history
Co-Authored-By: Pram Gurusinga <9930966+pgurusinga@users.noreply.github.com>
  • Loading branch information
chohner and pgurusinga committed Feb 12, 2024
1 parent af2e5eb commit 7ff338a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app/components/ArraySummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const ArraySummary = ({
const deleteFetcher = useFetcher();

const pathroot = pathname.slice(0, pathname.lastIndexOf("/"));
const addButtonDestination = `${pathroot}/${arrayKey}${arrayData.length > 0 ? "" : "-frage"}`;
const addButtonDestination = `${pathroot}/${arrayKey}${arrayData.length > 0 ? "" : "-frage"}?returnTo=${pathname}`;

return (
<div className="ds-stack-8 scroll-my-40">
Expand Down Expand Up @@ -70,7 +70,7 @@ const ArraySummary = ({
<Button
iconLeft={<EditButton />}
look="tertiary"
href={`${arrayKey}/${index}`}
href={`${arrayKey}/${index}?returnTo=${pathname}`}
>
{editButtonText}
</Button>
Expand Down
2 changes: 2 additions & 0 deletions app/routes/shared/components/StepWithPreHeading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function StepWithPreHeading() {
buttonNavigationProps,
translations,
navItems,
returnTo,
} = useLoaderData<typeof loader>();
const stepId = splatFromParams(useParams());
const { pathname } = useLocation();
Expand Down Expand Up @@ -101,6 +102,7 @@ export function StepWithPreHeading() {
{formContent && formContent.length != 0 && (
<PageContent content={formContent} className="ds-stack-40" />
)}
<input type="hidden" name="_returnTo" value={returnTo} />
{postFormContent && postFormContent.length != 0 && (
<PageContent
content={postFormContent}
Expand Down
14 changes: 9 additions & 5 deletions app/routes/shared/step.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const loader = async ({
// eslint-disable-next-line sonarjs/cognitive-complexity
}: LoaderFunctionArgs) => {
await throw404IfFeatureFlagEnabled(request);
const { pathname } = new URL(request.url);
const { pathname, searchParams } = new URL(request.url);
const { flowId, stepId, arrayIndex } = parsePathname(pathname);
const cookieId = request.headers.get("Cookie");
const { data, id } = await getSessionForContext(flowId).getSession(cookieId);
Expand Down Expand Up @@ -197,6 +197,7 @@ export const loader = async ({
flowController,
navTranslations,
),
returnTo: searchParams.get("returnTo") ?? undefined,
},
{ headers },
);
Expand Down Expand Up @@ -269,8 +270,11 @@ export const action = async ({ request }: ActionFunctionArgs) => {
if (customEventName)
void sendCustomEvent(customEventName, validationResult.data, request);

return redirect(
flowController.getNext(stepId)?.url ?? flowController.getInitial().url,
{ headers },
);
const returnTo = formData.get("_returnTo");
const destination =
returnTo && typeof returnTo === "string" && returnTo !== ""
? returnTo
: flowController.getNext(stepId)?.url ?? flowController.getInitial().url;

return redirect(destination, { headers });
};

0 comments on commit 7ff338a

Please sign in to comment.