Skip to content

Commit

Permalink
wip: fixing flow redirections and components visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
mewdev committed Jan 3, 2025
1 parent 5a8ac1a commit bba3281
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 95 deletions.
16 changes: 12 additions & 4 deletions apps/web/app/xyz/[number]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,21 @@ export default function Page() {
};

const yesClick = () => {
answerYes();
skipQuestion();
if (currentQuestion !== questions.length) {
answerYes();
skipQuestion();
} else {
router.push("/xyz/rekapitulace");
}
};

const noClick = () => {
answerNo();
skipQuestion();
if (currentQuestion !== questions.length) {
answerNo();
skipQuestion();
} else {
router.push("/xyz/rekapitulace");
}
};

return (
Expand Down
18 changes: 15 additions & 3 deletions apps/web/app/xyz/navod/[number]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@ import { Button, StepProgress } from "@repo/design-system/ui";
import Link from "next/link";
import { useGuideStore } from "../../store";
import { ArrowIconRight } from "@repo/design-system/icons";
import { useRouter } from "next/navigation";
import { useEffect } from "react";

export default function Page({ params }) {
const router = useRouter();
const guide = useGuideStore((state) => state.guide);
const currentStep = useGuideStore((state) => state.currentStep);
const stepTotal = useGuideStore((state) => state.stepTotal);
const nextStep = useGuideStore((state) => state.nextStep);
const prevStep = useGuideStore((state) => state.prevStep);

useEffect(() => {
// Always do navigations after the first render
// shallow error?
router.push(`/xyz/navod/${currentStep}`, undefined, { shallow: true });
}, [currentStep]);

return (
<div className="grid h-screen w-screen grid-cols-3 place-content-center">
<div className="flex items-center justify-end">
Expand Down Expand Up @@ -41,9 +50,12 @@ export default function Page({ params }) {
/>
</div>
<div className="flex w-auto flex-col items-center">
<Button onClick={nextStep} fitContent kind="outline">
Další krok
</Button>
{currentStep !== stepTotal && (
<Button onClick={nextStep} fitContent kind="outline">
Další krok
</Button>
)}

{currentStep === 4 ? (
<Button
kind="filled"
Expand Down
17 changes: 17 additions & 0 deletions apps/web/app/xyz/navod/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// use middleware for redirection
"use client";
import { usePathname, useRouter } from "next/navigation";
import { useEffect } from "react";
export default function Page() {
const path = usePathname();
const router = useRouter();
console.log(typeof path);
useEffect(() => {
console.log("Use effect");
if (path === "/xyz/navod") {
router.push("/xyz/navod/1");
}
}, []);

return <h1>Redirect to guide</h1>;
}
95 changes: 95 additions & 0 deletions apps/web/app/xyz/oldPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"use client";
import { useQuestionsStore } from "./store";
import { QuestionWrapper, BottomBar } from "@repo/design-system/ui";
import type { ExtendedQuestions } from "./store";
import { useRouter } from "next/navigation";
import { useEffect } from "react";

export default function Page() {
// cleanup and better naming
// const testQuestions = useQuestionsStore((state) => state.testQuestions);
const questions = useQuestionsStore((state) => state.questions);
const currentQuestion = useQuestionsStore((state) => state.currentQuestion);
const questionTotal = useQuestionsStore((state) => state.questionTotal);
const prevQuestion = useQuestionsStore((state) => state.prevQuestion);
const skipQuestion = useQuestionsStore((state) => state.skipQuestion);
const toggleImportant = useQuestionsStore((state) => state.toggleImportant);
const answerYes = useQuestionsStore((state) => state.answerYes);
const answerNo = useQuestionsStore((state) => state.answerNo);
const router = useRouter();

// understand this approach more!
// useEffect(() => {
// if (currentQuestion) {
// const slug = `/xyz/${currentQuestion}`;
// if (window.location.pathname !== slug) {
// window.history.replaceState(null, "", slug);
// }
// }
// }, [currentQuestion]);

const prevClick = () => {
if (currentQuestion === 1) {
router.push("/xyz/navod");
} else {
prevQuestion();
}
};

const skipClick = () => {
if (currentQuestion === questions.length) {
router.push("/xyz/rekapitulace");
} else {
skipQuestion();
}
};

const yesClick = () => {
answerYes();
skipQuestion();
};

const noClick = () => {
answerNo();
skipQuestion();
};

return (
<div>
{/* questions wrapper */}
{questions.map((question: ExtendedQuestions, index) => {
const currentStep = index + 1;
if (currentStep === currentQuestion) {
return (
<QuestionWrapper
key={`Question card id:${question.id}`}
question={question}
currentQuestion={currentQuestion}
questionCount={questions.length}
skipQuestion={skipClick}
prevQuestion={prevClick}
/>
);
}
})}
{/* Bottom bar wrapper */}
{questions.map((question: ExtendedQuestions, index) => {
const currentStep = index + 1;
if (currentStep === currentQuestion) {
return (
<BottomBar
key={`Bottom bar id:${question.id}`}
questions={questions}
currentQuestion={currentQuestion}
questionTotal={questionTotal}
toggleImportant={toggleImportant}
yesClick={yesClick}
noClick={noClick}
starPressed={question.isImportant ? true : undefined}
/>
);
}
})}
</div>
);
}
98 changes: 10 additions & 88 deletions apps/web/app/xyz/page.tsx
Original file line number Diff line number Diff line change
@@ -1,95 +1,17 @@
// use middleware for redirection
"use client";
import { useQuestionsStore } from "./store";
import { QuestionWrapper, BottomBar } from "@repo/design-system/ui";
import type { ExtendedQuestions } from "./store";
import { useRouter } from "next/navigation";
import { usePathname, useRouter } from "next/navigation";
import { useEffect } from "react";

export default function Page() {
// cleanup and better naming
// const testQuestions = useQuestionsStore((state) => state.testQuestions);
const questions = useQuestionsStore((state) => state.questions);
const currentQuestion = useQuestionsStore((state) => state.currentQuestion);
const questionTotal = useQuestionsStore((state) => state.questionTotal);
const prevQuestion = useQuestionsStore((state) => state.prevQuestion);
const skipQuestion = useQuestionsStore((state) => state.skipQuestion);
const toggleImportant = useQuestionsStore((state) => state.toggleImportant);
const answerYes = useQuestionsStore((state) => state.answerYes);
const answerNo = useQuestionsStore((state) => state.answerNo);
const path = usePathname();
const router = useRouter();

// understand this approach more!
// useEffect(() => {
// if (currentQuestion) {
// const slug = `/xyz/${currentQuestion}`;
// if (window.location.pathname !== slug) {
// window.history.replaceState(null, "", slug);
// }
// }
// }, [currentQuestion]);

const prevClick = () => {
if (currentQuestion === 1) {
router.push("/xyz/navod");
} else {
prevQuestion();
console.log(typeof path);
useEffect(() => {
console.log("Use effect");
if (path === "/xyz") {
router.push("/xyz/1");
}
};

const skipClick = () => {
if (currentQuestion === questions.length) {
router.push("/xyz/rekapitulace");
} else {
skipQuestion();
}
};

const yesClick = () => {
answerYes();
skipQuestion();
};

const noClick = () => {
answerNo();
skipQuestion();
};
}, []);

return (
<div>
{/* questions wrapper */}
{questions.map((question: ExtendedQuestions, index) => {
const currentStep = index + 1;
if (currentStep === currentQuestion) {
return (
<QuestionWrapper
key={`Question card id:${question.id}`}
question={question}
currentQuestion={currentQuestion}
questionCount={questions.length}
skipQuestion={skipClick}
prevQuestion={prevClick}
/>
);
}
})}
{/* Bottom bar wrapper */}
{questions.map((question: ExtendedQuestions, index) => {
const currentStep = index + 1;
if (currentStep === currentQuestion) {
return (
<BottomBar
key={`Bottom bar id:${question.id}`}
questions={questions}
currentQuestion={currentQuestion}
questionTotal={questionTotal}
toggleImportant={toggleImportant}
yesClick={yesClick}
noClick={noClick}
starPressed={question.isImportant ? true : undefined}
/>
);
}
})}
</div>
);
return <h1>Redirect to first question</h1>;
}

0 comments on commit bba3281

Please sign in to comment.