Closed DarkoKrezic closed 1 year ago
The latest updates on your projects. Learn more about Vercel for Git ↗︎
Name | Status | Preview | Comments | Updated (UTC) |
---|---|---|---|---|
capstone-projekt | ✅ Ready (Inspect) | Visit Preview | 💬 Add feedback | May 12, 2023 2:22pm |
Hi Darko, good job in trying to implement all my remarks! 💪 For the implementation of your local storage I would recommend to change the structure of your code. I tried something out and will leave a guide for your. 🗡️
_app.js
NewStoryPage
I recommend to lift your stories state to the _app.js
import GlobalStyle from "../styles";
import { useImmerLocalStorageState } from "@/lib/hook/useImmerLocalStorageState";
import initialStories from "@/db.js";
export default function App({ Component, pageProps }) {
const [stories, setStories] = useImmerLocalStorageState("stories", {
defaultValue: initialStories,
});
function addStory(newStory) {
setStories([newStory, ...stories]);
}
return (
<>
<GlobalStyle />
<Component {...pageProps} stories={stories} addStory={addStory} />
</>
);
}
addStory
function which will add the new story to your stories
data (that will be saved in your local storage too via the useImmerLocalStorageState
)index.js
- HomePage
component
StoryList
component that then handles the data[id].js
- you can also here use the stories
prop directly. It is not necessary to get the data again from the local storage. :)
import { useRouter } from "next/router";
import StoryDetailCard from "@/components/Story";
function StoryDetailPage({ stories }) {
const router = useRouter();
const { id } = router.query;
const story = stories.find((story) => story.id === id);
if (!story) {
return <p>Story not found</p>;
}
return <StoryDetailCard story={story} />;
}
export default StoryDetailPage;
NewStoryPage
_app.js
where the state will be updatedimport NewStoryForm from "@/components/NewStoryForm";
import { useRouter } from "next/router";
export default function NewStoryPage({ addStory }) {
const router = useRouter();
function handleStorySubmit(newStory) {
addStory(newStory);
router.push("/");
}
return <NewStoryForm onSubmit={handleStorySubmit} />;
}
NewStoryPage
. MAybe add-story
or just create
is better. :)NewStoryForm
component
crypto.randomUUID()
to generate a unique id function handleSubmit(event) {
event.preventDefault();
const newStory = {
id: crypto.randomUUID(),
title: title,
coverImage: URL.createObjectURL(coverImage),
textContent: textContent,
dateCreated: new Date().toLocaleDateString(),
};
onSubmit(newStory);
}
db.js
StoryDetailPage
{
id: "1",
title: "Hanni und Hasi",
textContent:
"Hanni liebte ihren Kuschelhasen Hasi über alles. Eines sonnigen Tages beschloss sie, mit ihm in den Park zu gehen. Als sie im Park ankamen, sah Hanni, dass ihre Freunde dort spielten. Sie rannte zu ihnen und stellte Hasi vor. 'Hasi möchte gerne mitspielen', sagte Hanni. Ihre Freunde waren begeistert und bald spielten sie alle zusammen. Sie rannten, lachten und hatten eine Menge Spaß. Doch dann fiel Hanni auf, dass ihr kleiner Bruder, der ebenfalls im Park war, alleine auf einer Bank saß und weinte. Sie rannte zu ihm und fragte, was los war. Ihr Bruder hatte sein Spielzeug verloren und war traurig. Hanni dachte einen Moment nach und sagte dann: 'Ich habe eine Idee. Wie wäre es, wenn wir das Spielzeug teilen und gemeinsam spielen?' Ihr Bruder lächelte und sie gingen zurück zu den anderen Kindern. Hanni erklärte die Situation und bald spielten alle zusammen mit dem Spielzeug. Als es Zeit war, nach Hause zu gehen, sagte einer von Hannis Freunden: 'Danke, dass du uns das Teilen beigebracht hast. Wir hatten eine großartige Zeit!' Hanni lächelte und Hasi knuddelte sie. Sie war glücklich, dass sie eine gute Zeit hatte und ihren Freunden das Teilen beibringen konnte.",
coverImage: "/images/hanni-und-hasi-im-park.png",
dateCreated: "01.05.2023",
}
db.js
to the root of your folder (means one level higher in the folder structure) as it is not necessary to have your db.js
in your public folder❗️When working with local storage it is important to delete the local storage values every time after changing how the local storage data handling in your app. Try this out if something does not work. :)
Think about my remark as these would improve your code! 💪 I hope I did no forget anything. 👀 Feel free to ask me if you have further questions.
Hey Mareike, i did some changes, please feel free to take a look . 😅
US3