Skip to content

Commit

Permalink
fix: Handle S&R by adding layout wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed May 31, 2024
1 parent 3bd1049 commit 587de80
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
16 changes: 16 additions & 0 deletions editor.planx.uk/src/pages/layout/OfflineLayout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useStore } from "pages/FlowEditor/lib/store";
import { OfflinePage } from "pages/OfflinePage";
import React, { PropsWithChildren } from "react";

const OfflineLayout = ({ children }: PropsWithChildren) => {
const isFlowOnline = useStore.getState().flowStatus === "online";
const searchParams = new URLSearchParams(window.location.search);
const isUserResuming = Boolean(searchParams.get("sessionId"));

// Allow users to complete Save & Return journeys, even if a flow is offline
const isFlowAccessible = isFlowOnline || isUserResuming;

return isFlowAccessible ? children : <OfflinePage />;
};

export default OfflineLayout;
20 changes: 3 additions & 17 deletions editor.planx.uk/src/routes/published.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { compose, map, mount, route, withData, withView } from "navi";
import { useStore } from "pages/FlowEditor/lib/store";
import { OfflinePage } from "pages/OfflinePage";
import ContentPage from "pages/Preview/ContentPage";
import Questions from "pages/Preview/Questions";
import React from "react";
Expand All @@ -26,21 +24,9 @@ const routes = compose(
}),

mount({
"/": route((req) => ({
view: () => {
const isFlowOnline = useStore.getState().flowStatus === "online";
const isUserResuming = Boolean(req.params.sessionId);

// Allow users to complete Save & Return journeys, even if a flow is offline
const isFlowAccessible = isFlowOnline || isUserResuming;

return isFlowAccessible ? (
<Questions previewEnvironment="standalone" />
) : (
<OfflinePage />
);
},
})),
"/": route({
view: <Questions previewEnvironment="standalone" />,
}),
"/pages/:page": map((req) => {
return route({
view: () => <ContentPage page={req.params.page} />,
Expand Down
9 changes: 6 additions & 3 deletions editor.planx.uk/src/routes/views/published.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { NaviRequest } from "navi";
import { NotFoundError } from "navi";
import { useStore } from "pages/FlowEditor/lib/store";
import { Store } from "pages/FlowEditor/lib/store";
import OfflineLayout from "pages/layout/OfflineLayout";
import PublicLayout from "pages/layout/PublicLayout";
import SaveAndReturnLayout from "pages/layout/SaveAndReturnLayout";
import React from "react";
Expand Down Expand Up @@ -55,9 +56,11 @@ export const publishedView = async (req: NaviRequest) => {

return (
<PublicLayout>
<SaveAndReturnLayout>
<View />
</SaveAndReturnLayout>
<OfflineLayout>
<SaveAndReturnLayout>
<View />
</SaveAndReturnLayout>
</OfflineLayout>
</PublicLayout>
);
};
Expand Down

0 comments on commit 587de80

Please sign in to comment.