Skip to content

Commit

Permalink
refactor: use provider to get posts data
Browse files Browse the repository at this point in the history
  • Loading branch information
Javimtib92 committed Sep 30, 2024
1 parent 4055d2a commit cd90181
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
7 changes: 6 additions & 1 deletion app/routes/posts/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import { useLoaderData } from "@remix-run/react";
import { json } from "@remix-run/node";

import PostsListPage from "@/src/posts/presentation/pages/posts-list/posts-list";
import { usePostsListProvider } from "@/src/posts/presentation/providers/posts-list.provider";

export default function PostsPage() {
const data = useLoaderData<typeof loader>();

return <PostsListPage posts={data} />;
return (
<usePostsListProvider.State initialState={{ posts: data }}>
<PostsListPage />
</usePostsListProvider.State>
);
}

export async function loader() {
Expand Down
6 changes: 4 additions & 2 deletions src/posts/presentation/pages/posts-list/posts-list.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import css from "@/src/shared/presentation/styles/wrapper.css";
import { SimpleCard } from "@/src/shared/presentation/components/simple-card/simple-card";
import type { Post } from "@/src/posts/domain/models/post";
import { useTranslation } from "react-i18next";
import PageTitle from "@/src/shared/presentation/components/page-title/page-title";
import { usePostsListProvider } from "../../providers/posts-list.provider";

export default function PostsPage() {
const posts = usePostsListProvider((state) => state.posts);

export default function PostsPage({ posts }: { posts: Array<Post> }) {
const { t } = useTranslation();

return (
Expand Down
6 changes: 6 additions & 0 deletions src/posts/presentation/providers/posts-list.provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createProvider } from "@/src/shared/presentation/utils/zustand";
import type { PostsListStateViewModel } from "../view-models/posts-list-state";

export const usePostsListProvider = createProvider<PostsListStateViewModel>(() => (_set) => ({
posts: []
}));
5 changes: 5 additions & 0 deletions src/posts/presentation/view-models/posts-list-state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Post } from "@/src/posts/domain/models/post";

export interface PostsListStateViewModel {
posts: Array<Post>;
}

0 comments on commit cd90181

Please sign in to comment.