Skip to content

Commit

Permalink
refactor: rename testQuestions to questions for clarity and consisten…
Browse files Browse the repository at this point in the history
…cy, added todos
  • Loading branch information
mewdev committed Jan 6, 2025
1 parent 54d1ded commit 5a180e2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
13 changes: 7 additions & 6 deletions apps/web/app/xyz/KalkulackaInitializer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import { useQuestionsStore } from "./store";
import type { ExtendedQuestions } from "./store";

type Props = {
testQuestions: any;
questions: any;
children: React.ReactNode;
};

export default function KalkulackaInitializer({
testQuestions,
children,
}: Props) {
export default function KalkulackaInitializer({ questions, children }: Props) {
useQuestionsStore.setState(() => {
const updatedTestQuestions = testQuestions.map(
const updatedTestQuestions = questions.map(
(question: ExtendedQuestions) => {
return {
...question,
Expand All @@ -28,3 +25,7 @@ export default function KalkulackaInitializer({

return children;
}

// TODOS:
// 1. Solve warning: Cannot update a component (`useQuestionsStoreProvider`) while rendering a different component (`KalkulackaInitializer`). To locate the bad setState() call inside `KalkulackaInitializer`, follow the stack trace as described in https://reactjs.org/link/setstate-in-render
// see: https://github.com/facebook/react/issues/18147#issuecomment-592267650
4 changes: 1 addition & 3 deletions apps/web/app/xyz/[number]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ export default function Page() {
const params = useParams();
const router = useRouter();
// 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);
Expand Down Expand Up @@ -95,7 +93,7 @@ export default function Page() {
key={`Bottom bar id:${question.id}`}
questions={questions}
currentQuestion={currentQuestion}
questionTotal={questionTotal}
questionTotal={questions.length}
toggleImportant={toggleImportant}
yesClick={yesClick}
noClick={noClick}
Expand Down
4 changes: 2 additions & 2 deletions apps/web/app/xyz/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ export default async function RootLayout({
return data;
}

const testQuestions = await fetchQuestions();
const questions = await fetchQuestions();
return (
<html lang="en">
<body>
<KalkulackaInitializer testQuestions={testQuestions}>
<KalkulackaInitializer questions={questions}>
{children}
</KalkulackaInitializer>
</body>
Expand Down
10 changes: 6 additions & 4 deletions apps/web/app/xyz/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ type ExtendedQuestions = Question & {
type QuestionsStore = {
questions: ExtendedQuestions[];
currentQuestion: number;
questionTotal: number;
skipQuestion: () => void;
prevQuestion: () => void;
toggleImportant: () => void;
Expand All @@ -30,6 +29,7 @@ type GuideStore = {
prevStep: () => void;
};

// guide content as HTML rather than plain text in object ???
export const useGuideStore = create<GuideStore>((set) => ({
guide: [
{
Expand All @@ -55,12 +55,9 @@ export const useGuideStore = create<GuideStore>((set) => ({
prevStep: () => set((state) => ({ currentStep: state.currentStep - 1 })),
}));

// swtich off debug mode

export const useQuestionsStore = create<QuestionsStore>((set) => ({
questions: [],
currentQuestion: 0,
questionTotal: 4,
skipQuestion: () =>
set((state) => ({ currentQuestion: state.currentQuestion + 1 })),
prevQuestion: () =>
Expand Down Expand Up @@ -147,3 +144,8 @@ export const useQuestionsStore = create<QuestionsStore>((set) => ({
}));

export type { ExtendedQuestions };

// TODOS:
// 1. Use Immer for better performance and cleaner code for updating state
// 2. Fix reduntant function
// 3. Fix no unused vars error

0 comments on commit 5a180e2

Please sign in to comment.