Skip to content

Commit

Permalink
wip: remove unused components and restructure question handling in th…
Browse files Browse the repository at this point in the history
…e store, introduced mockup guide
  • Loading branch information
mewdev committed Jan 9, 2025
1 parent 7829734 commit 4162d12
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,15 @@ import { useQuestionsStore } from "../providers/storeProvider";

export default function Page() {
const questions = useQuestionsStore((state) => state.questions);
const collection = useQuestionsStore((state) => state.collection);
const currentQuestion = useQuestionsStore((state) => state.currentQuestion);
const prevQuestion = useQuestionsStore((state) => state.prevQuestion);
const skipQuestion = useQuestionsStore((state) => state.skipQuestion);

return (
<main className="flex min-h-screen flex-col items-center p-24">
<div className="bg-red-200">
<h2>Questions</h2>
{questions?.map((question: string, index) => {
if (currentQuestion === index + 1) {
return (
<div key={index} className="m-4 bg-red-400">
<span>{question}</span>
</div>
);
}
})}
</div>
<div className="bg-red-200">
<h2>Collection</h2>
{collection?.map((item: string, index) => {
{questions?.map((item: string, index) => {
if (currentQuestion === index + 1) {
return (
<div key={index} className="m-4 bg-red-400">
Expand Down
22 changes: 0 additions & 22 deletions apps/web/app/abc/components/counter.tsx

This file was deleted.

11 changes: 0 additions & 11 deletions apps/web/app/abc/counter/page.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions apps/web/app/abc/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import getQuestions from "./utils/getQuestions";

const baseUrl = "https://dummyjson.com/c/7ee4-a7f4-4977-bb54";

const collection = await getQuestions(baseUrl);
const questions = await getQuestions(baseUrl);

export default async function RootLayout({
children,
Expand All @@ -16,7 +16,7 @@ export default async function RootLayout({
}) {
return (
<html lang="en">
<StoreProvider collection={collection}>
<StoreProvider questions={questions}>
<UrlUpdater>
<body>{children}</body>
</UrlUpdater>
Expand Down
29 changes: 29 additions & 0 deletions apps/web/app/abc/navod/[guideNumber]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use client";

import { useQuestionsStore } from "../../providers/storeProvider";

export default function Page() {
const guideNumber = useQuestionsStore((state) => state.guideNumber);
const guideContent = useQuestionsStore((state) => state.guideContent);
const prevGuide = useQuestionsStore((state) => state.prevGuide);
const nextGuide = useQuestionsStore((state) => state.nextGuide);
return (
<main className="flex min-h-screen flex-col items-center p-24">
<div className="bg-red-200">
<h2>Collection</h2>
{guideContent?.map((guide: string, index) => {
if (guideNumber === index + 1) {
return (
<div key={index} className="m-4 bg-red-400">
<span>{guide}</span>
</div>
);
}
})}
</div>

<button onClick={nextGuide}>Skip question</button>
<button onClick={prevGuide}>Prev question</button>
</main>
);
}
19 changes: 19 additions & 0 deletions apps/web/app/abc/navod/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// use middleware for redirection
"use client";
import { usePathname, useRouter } from "next/navigation";
import { useEffect } from "react";
import { useQuestionsStore } from "../providers/storeProvider";
export default function Page() {
const path = usePathname();
const router = useRouter();
const guideNumber = useQuestionsStore((state) => state.guideNumber);
console.log(typeof path);
useEffect(() => {
console.log("Use effect");
if (path === "/abc/navod") {
router.push(`/abc/navod/${guideNumber}`);
}
}, []);

return <h1>Redirect to guide</h1>;
}
24 changes: 13 additions & 11 deletions apps/web/app/abc/providers/storeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
import { type ReactNode, createContext, useRef, useContext } from "react";
import { type StoreApi, createStore, useStore } from "zustand";

// divide store, to the external file?

type Store = {
count: number;
questions: string[];
currentQuestion: number;
collection: string[];
decrementCount: () => void;
incrementCount: () => void;
prevQuestion: () => void;
skipQuestion: () => void;
nextGuide: () => void;
prevGuide: () => void;
guideNumber: number;
guideContent: string[];
};

export const StoreContext = createContext<StoreApi<Store> | undefined>(
Expand All @@ -21,23 +23,23 @@ export const StoreContext = createContext<StoreApi<Store> | undefined>(
// update to props with children
export interface StoreProviderProps {
children: ReactNode;
collection: string[];
questions: string[];
}

export const StoreProvider = ({ children, collection }: StoreProviderProps) => {
export const StoreProvider = ({ children, questions }: StoreProviderProps) => {
const storeRef = useRef<StoreApi<Store> | undefined>();
if (!storeRef.current) {
storeRef.current = createStore<Store>((set) => ({
collection,
count: 1,
questions: ["Item 1", "Item 2", "Item 3"],
questions,
currentQuestion: 1,
decrementCount: () => set((state) => ({ count: state.count - 1 })),
incrementCount: () => set((state) => ({ count: state.count + 1 })),
prevQuestion: () =>
set((state) => ({ currentQuestion: state.currentQuestion - 1 })),
skipQuestion: () =>
set((state) => ({ currentQuestion: state.currentQuestion + 1 })),
guideNumber: 1,
guideContent: ["Guide item 1", "Guide item 2", "Guide item 3"],
prevGuide: () => set((state) => ({ guideNumber: state.guideNumber - 1 })),
nextGuide: () => set((state) => ({ guideNumber: state.guideNumber + 1 })),
}));
}

Expand Down
17 changes: 0 additions & 17 deletions apps/web/app/abc/store.ts

This file was deleted.

0 comments on commit 4162d12

Please sign in to comment.