Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ Learn the implemented App and the example and reimplement it with Redux having t
- Implement a solution following the [React task guideline](https://github.com/mate-academy/react_task-guideline#react-tasks-guideline).
- Use the [React TypeScript cheat sheet](https://mate-academy.github.io/fe-program/js/extra/react-typescript).
- This task does not have tests yet!
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://<your_account>.github.io/react_redux-list-of-posts/) and add it to the PR description.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://luchali.github.io/react_redux-list-of-posts/) and add it to the PR description.
45 changes: 31 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"license": "GPL-3.0",
"dependencies": {
"@fortawesome/fontawesome-free": "^6.5.2",
"@reduxjs/toolkit": "^2.2.6",
"@reduxjs/toolkit": "^2.11.2",
"axios": "^1.7.2",
"bulma": "^0.9.4",
"classnames": "^2.5.1",
Expand All @@ -19,7 +19,7 @@
},
"devDependencies": {
"@cypress/react18": "^2.0.1",
"@mate-academy/scripts": "^1.9.12",
"@mate-academy/scripts": "^2.1.3",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/node": "^20.14.10",
Expand Down
63 changes: 34 additions & 29 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useEffect, useState } from 'react';
/* eslint-disable @typescript-eslint/indent */
import { useEffect } from 'react';
import classNames from 'classnames';

import 'bulma/css/bulma.css';
Expand All @@ -9,39 +10,38 @@ import { PostsList } from './components/PostsList';
import { PostDetails } from './components/PostDetails';
import { UserSelector } from './components/UserSelector';
import { Loader } from './components/Loader';
import { getUserPosts } from './api/posts';
import { User } from './types/User';
import { Post } from './types/Post';
import { setAuthor } from './features/author/authorSlice';

export const App: React.FC = () => {
const [posts, setPosts] = useState<Post[]>([]);
const [loaded, setLoaded] = useState(false);
const [hasError, setError] = useState(false);
import {
setSelectedPost,
clearSelectedPost,
} from './features/selectedPost/selectedPostSlice';
import { useAppDispatch, useAppSelector } from './app/hooks';
import { fetchUsers } from './features/users/usersSlice';
import { fetchPosts, clearPosts } from './features/posts/postsSlice';

const [author, setAuthor] = useState<User | null>(null);
const [selectedPost, setSelectedPost] = useState<Post | null>(null);
export const App = () => {
const dispatch = useAppDispatch();

function loadUserPosts(userId: number) {
setLoaded(false);
const { items, loaded, hasError } = useAppSelector(state => state.posts);
const author = useAppSelector(state => state.author.value);
const selectedPost = useAppSelector(state => state.selectedPost.value);

getUserPosts(userId)
.then(setPosts)
.catch(() => setError(true))
// We disable the spinner in any case
.finally(() => setLoaded(true));
}
useEffect(() => {
dispatch(fetchUsers());
}, [dispatch]);

useEffect(() => {
// we clear the post when an author is changed
// not to confuse the user
setSelectedPost(null);
dispatch(clearSelectedPost());

if (author) {
loadUserPosts(author.id);
dispatch(fetchPosts(author.id));
} else {
setPosts([]);
dispatch(clearPosts());
}
}, [author]);
}, [author, dispatch]);

return (
<main className="section">
Expand All @@ -50,15 +50,18 @@ export const App: React.FC = () => {
<div className="tile is-parent">
<div className="tile is-child box is-success">
<div className="block">
<UserSelector value={author} onChange={setAuthor} />
<UserSelector
value={author}
onChange={user => dispatch(setAuthor(user))}
/>
</div>

<div className="block" data-cy="MainContent">
{!author && <p data-cy="NoSelectedUser">No user selected</p>}

{author && !loaded && <Loader />}
{author && loaded && <Loader />}

{author && loaded && hasError && (
{author && !loaded && hasError && (
<div
className="notification is-danger"
data-cy="PostsLoadingError"
Expand All @@ -67,17 +70,17 @@ export const App: React.FC = () => {
</div>
)}

{author && loaded && !hasError && posts.length === 0 && (
{author && !loaded && !hasError && items.length === 0 && (
<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
</div>
)}

{author && loaded && !hasError && posts.length > 0 && (
{author && !loaded && !hasError && items.length > 0 && (
<PostsList
posts={posts}
posts={items}
selectedPostId={selectedPost?.id}
onPostSelected={setSelectedPost}
onPostSelected={post => dispatch(setSelectedPost(post))}
/>
)}
</div>
Expand Down Expand Up @@ -105,3 +108,5 @@ export const App: React.FC = () => {
</main>
);
};

export default App;
10 changes: 10 additions & 0 deletions src/app/store.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import { configureStore, ThunkAction, Action } from '@reduxjs/toolkit';
// eslint-disable-next-line import/no-cycle
import counterReducer from '../features/counter/counterSlice';
import usersReducer from '../features/users/usersSlice';
import postsReducer from '../features/posts/postsSlice';
import authorReducer from '../features/author/authorSlice';
import selectedPostReducer from '../features/selectedPost/selectedPostSlice';
import commentsReducer from '../features/comments/commentsSlice';

export const store = configureStore({
reducer: {
counter: counterReducer,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The counter slice is not part of the task requirements. It seems to be a leftover from the template. Please remove it from the store, along with its import on line 3.

users: usersReducer,
posts: postsReducer,
author: authorReducer,
selectedPost: selectedPostReducer,
comments: commentsReducer,
},
});

Expand Down
Loading