Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution #1187

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
139 changes: 102 additions & 37 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,118 @@ import { PostsList } from './components/PostsList';
import { PostDetails } from './components/PostDetails';
import { UserSelector } from './components/UserSelector';
import { Loader } from './components/Loader';
import { useEffect, useState } from 'react';
import { User } from './types/User';
import { Post } from './types/Post';
import { getUserPosts, getUsers } from './utils/fetchClient';

export const App = () => (
<main className="section">
<div className="container">
<div className="tile is-ancestor">
<div className="tile is-parent">
<div className="tile is-child box is-success">
<div className="block">
<UserSelector />
</div>
export const App = () => {
const [users, setUsers] = useState<User[]>([]);

<div className="block" data-cy="MainContent">
<p data-cy="NoSelectedUser">No user selected</p>
const [selectedUser, setSelectedUser] = useState<User | null>(null);
const [userPosts, setUserPosts] = useState<Post[]>([]);

<Loader />
const [selectedPost, setSelectedPost] = useState<Post | null>(null);

<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
const [error, setError] = useState('');
const [loading, setLoading] = useState(false);

const [showForm, setShowForm] = useState(false);

useEffect(() => {
getUsers()
.then(setUsers)
.catch(newError => {
throw newError;
});
}, []);

useEffect(() => {
if (selectedUser) {
setLoading(true);

getUserPosts(selectedUser.id)
.then(setUserPosts)
.catch(() => setError('Something went wrong!'))
.finally(() => setLoading(false));
}
}, [selectedUser]);

<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
return (
<main className="section">
<div className="container">
<div className="tile is-ancestor">
<div className="tile is-parent">
<div className="tile is-child box is-success">
<div className="block">
<UserSelector
users={users}
setSelectedUser={setSelectedUser}
selectedUser={selectedUser}
setSelectedPost={setSelectedPost}
/>
</div>

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

{!loading &&
!error &&
selectedUser &&
(userPosts.length > 0 ? (
<PostsList
userPosts={userPosts}
setSelectedPost={setSelectedPost}
setShowForm={setShowForm}
selectedPost={selectedPost}
/>
) : (
<div
className="notification is-warning"
data-cy="NoPostsYet"
>
No posts yet
</div>
))}

{loading && <Loader />}

{error && (
<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
{error}
</div>
)}
</div>
</div>
</div>
</div>

<div
data-cy="Sidebar"
className={classNames(
'tile',
'is-parent',
'is-8-desktop',
'Sidebar',
'Sidebar--open',
)}
>
<div className="tile is-child box is-success ">
<PostDetails />
<div
data-cy="Sidebar"
className={classNames(
'tile',
'is-parent',
'is-8-desktop',
'Sidebar',
{ 'Sidebar--open': selectedPost },
)}
>
<div className="tile is-child box is-success ">
{selectedPost && (
<PostDetails
selectedPost={selectedPost}
showForm={showForm}
setShowForm={setShowForm}
/>
)}
</div>
</div>
</div>
</div>
</div>
</main>
);
</main>
);
};
160 changes: 128 additions & 32 deletions src/components/NewCommentForm.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,78 @@
import React from 'react';
import classNames from 'classnames';
import React, { useState } from 'react';
import { CommentData } from '../types/Comment';

type Props = {
buttonLoader: boolean;
addFunction: (newComment: CommentData) => Promise<void>;
};

export const NewCommentForm: React.FC<Props> = ({
buttonLoader,
addFunction,
}) => {
const [name, setName] = useState('');
const [nameError, setNameError] = useState(false);

const handleNameChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setName(event.target.value);
setNameError(false);
};

const [email, setEmail] = useState('');
const [emailError, setEmailError] = useState(false);

const handleEmailChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setEmail(event.target.value);
setEmailError(false);
};

const [text, setText] = useState('');
const [textError, setTextError] = useState(false);

Choose a reason for hiding this comment

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

All states must be at the beginning of the component

const handleTextChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
setText(event.target.value);
setTextError(false);
};

const normalizeValue = (value: string) => {
return value.trim();
};

const onSubmit = (event: React.FormEvent | React.MouseEvent) => {
event.preventDefault();

setName(normalizeValue(name));
setNameError(!name);

setEmail(normalizeValue(email));
setEmailError(!email);

setText(normalizeValue(text));
setTextError(!text);

if (!name || !email || !text) {
return;
}

const newComment = addFunction({ name, email, body: text });

if (newComment instanceof Promise) {
newComment.then(() => setText(''));
}
};

const clearFunction = () => {
setEmail('');
setEmailError(false);
setName('');
setNameError(false);
setText('');
setTextError(false);
};

export const NewCommentForm: React.FC = () => {
return (
<form data-cy="NewCommentForm">
<form data-cy="NewCommentForm" onSubmit={event => onSubmit(event)}>
<div className="field" data-cy="NameField">
<label className="label" htmlFor="comment-author-name">
Author Name
Expand All @@ -14,24 +84,29 @@ export const NewCommentForm: React.FC = () => {
name="name"
id="comment-author-name"
placeholder="Name Surname"
className="input is-danger"
value={name}
onChange={handleNameChange}
className={classNames('input', { 'is-danger': nameError })}
/>

<span className="icon is-small is-left">
<i className="fas fa-user" />
</span>
{nameError && (
<>
<span
className="icon is-small is-right has-text-danger"
data-cy="ErrorIcon"
>
<i className="fas fa-exclamation-triangle" />
</span>

<span
className="icon is-small is-right has-text-danger"
data-cy="ErrorIcon"
>
<i className="fas fa-exclamation-triangle" />
</span>
<p className="help is-danger" data-cy="ErrorMessage">
Name is required
</p>
</>
)}
</div>

<p className="help is-danger" data-cy="ErrorMessage">
Name is required
</p>
</div>

<div className="field" data-cy="EmailField">
Expand All @@ -45,24 +120,30 @@ export const NewCommentForm: React.FC = () => {
name="email"
id="comment-author-email"
placeholder="[email protected]"
className="input is-danger"
onChange={handleEmailChange}
value={email}
className={classNames('input', { 'is-danger': emailError })}
/>

<span className="icon is-small is-left">
<i className="fas fa-envelope" />
</span>

<span
className="icon is-small is-right has-text-danger"
data-cy="ErrorIcon"
>
<i className="fas fa-exclamation-triangle" />
</span>
</div>
{emailError && (
<>
<span
className="icon is-small is-right has-text-danger"
data-cy="ErrorIcon"
>
<i className="fas fa-exclamation-triangle" />
</span>

<p className="help is-danger" data-cy="ErrorMessage">
Email is required
</p>
<p className="help is-danger" data-cy="ErrorMessage">
Email is required
</p>
</>
)}
</div>
</div>

<div className="field" data-cy="BodyField">
Expand All @@ -75,25 +156,40 @@ export const NewCommentForm: React.FC = () => {
id="comment-body"
name="body"
placeholder="Type comment here"
className="textarea is-danger"
onChange={handleTextChange}
value={text}
className={classNames('input', { 'is-danger': textError })}
/>
</div>

<p className="help is-danger" data-cy="ErrorMessage">
Enter some text
</p>
{textError && (
<p className="help is-danger" data-cy="ErrorMessage">
Enter some text
</p>
)}
</div>

<div className="field is-grouped">
<div className="control">
<button type="submit" className="button is-link is-loading">
<button
type="submit"
onClick={event => {
onSubmit(event);
}}
className={classNames('button is-link', {
'is-loading': buttonLoader,
})}
>
Add
</button>
</div>

<div className="control">
{/* eslint-disable-next-line react/button-has-type */}
<button type="reset" className="button is-link is-light">
<button
type="reset"
onClick={clearFunction}
className="button is-link is-light"
>
Clear
</button>
</div>
Expand Down
Loading
Loading