From ff8ff649f61565cd2cfdaea51d6d89721032ac7d Mon Sep 17 00:00:00 2001 From: Oleksii-Ivan Stetsyk Date: Mon, 17 Feb 2025 19:18:47 +0200 Subject: [PATCH] dynamic-list-of-posts --- src/App.tsx | 91 +++++++------- src/components/NewCommentForm.tsx | 180 +++++++++++++++++++++------- src/components/PostDetails.tsx | 189 +++++++++++++++++------------- src/components/PostsList.tsx | 168 ++++++++++++++------------ src/components/UserSelector.tsx | 88 ++++++++++---- 5 files changed, 450 insertions(+), 266 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 017957182..0fc86134d 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,60 +1,63 @@ +import { useState } from 'react'; import classNames from 'classnames'; import 'bulma/css/bulma.css'; import '@fortawesome/fontawesome-free/css/all.css'; import './App.scss'; +import { UserSelector } from './components/UserSelector'; import { PostsList } from './components/PostsList'; import { PostDetails } from './components/PostDetails'; -import { UserSelector } from './components/UserSelector'; -import { Loader } from './components/Loader'; - -export const App = () => ( -
-
-
-
-
-
- -
- -
-

No user selected

- - - -
- Something went wrong! +import { Post } from './types/Post'; + +export const App = () => { + const [selectedUserId, setSelectedUserId] = useState(null); + const [selectedPost, setSelectedPost] = useState(null); + + const handleUserSelect = (userId: number | null) => { + setSelectedUserId(userId); + setSelectedPost(null); + }; + + return ( +
+
+
+
+
+
+
- -
- No posts yet +
+ {selectedUserId === null ? ( +

No user selected

+ ) : ( + + )}
- -
-
-
-
- +
+
+ +
-
-
-); +
+ ); +}; diff --git a/src/components/NewCommentForm.tsx b/src/components/NewCommentForm.tsx index 73a8a0b45..61517dd4e 100644 --- a/src/components/NewCommentForm.tsx +++ b/src/components/NewCommentForm.tsx @@ -1,99 +1,189 @@ -import React from 'react'; +/* eslint-disable no-console */ +import React, { useState } from 'react'; +import { CommentData } from '../types/Comment'; + +interface NewCommentFormProps { + onSubmit: (data: CommentData) => void; +} + +export const NewCommentForm: React.FC = ({ onSubmit }) => { + const [name, setName] = useState(''); + const [email, setEmail] = useState(''); + const [body, setBody] = useState(''); + const [errors, setErrors] = useState<{ + name?: string; + email?: string; + body?: string; + }>({}); + const [isSubmitting, setIsSubmitting] = useState(false); + const [submitted, setSubmitted] = useState(false); + + const validate = () => { + const newErrors: { name?: string; email?: string; body?: string } = {}; + + if (!name.trim()) { + newErrors.name = 'Name is required'; + } + + if (!email.trim()) { + newErrors.email = 'Email is required'; + } + + if (!body.trim()) { + newErrors.body = 'Enter some text'; + } + + return newErrors; + }; + + const handleSubmit = async (e: React.FormEvent) => { + e.preventDefault(); + setSubmitted(true); + const newErrors = validate(); + + if (Object.keys(newErrors).length > 0) { + setErrors(newErrors); + + return; + } + + setIsSubmitting(true); + try { + await onSubmit({ name, email, body }); + setBody(''); + setErrors({}); + setSubmitted(false); + } catch (err) { + console.error('Failed to add comment', err); + } finally { + setIsSubmitting(false); + } + }; + + const handleClear = () => { + setErrors({}); + setBody(''); + }; -export const NewCommentForm: React.FC = () => { return ( -
+
-
{ + setName(e.target.value); + if (errors.name) { + setErrors(prev => ({ ...prev, name: undefined })); + } + }} /> - - - - - - + + {submitted && errors.name && ( + + + + )}
- -

- Name is required -

+ {submitted && errors.name && ( +

+ {errors.name} +

+ )}
-
{ + setEmail(e.target.value); + if (errors.email) { + setErrors(prev => ({ ...prev, email: undefined })); + } + }} /> - - - - - - + + {submitted && errors.email && ( + + + + )}
- -

- Email is required -

+ {submitted && errors.email && ( +

+ {errors.email} +

+ )}
-