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 #858

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions src/App.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@
.message-body {
white-space: pre-line;
}

iframe {
display: none;
}
69 changes: 45 additions & 24 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
import React from 'react';
import 'bulma/bulma.sass';
/* eslint-disable max-len */
import '@fortawesome/fontawesome-free/css/all.css';
import 'bulma/bulma.sass';
import React from 'react';
import './App.scss';

import classNames from 'classnames';
import { PostsList } from './components/PostsList';
import { Loader } from './components/Loader';
import { PostDetails } from './components/PostDetails';
import { PostsList } from './components/PostsList';
import { UserSelector } from './components/UserSelector';
import { Loader } from './components/Loader';
import { usePostsContext } from './hooks/usePostsContext';
import { useUserContext } from './hooks/useUserContext';

export const App: React.FC = () => {
const {
posts,
isPostsError,
isPostsPending,
selectedPost,
} = usePostsContext();
const { selectedUser } = useUserContext();

const hasError = !isPostsPending && isPostsError;
const withoutErrors = selectedUser && !isPostsError;
const fetchedWithoutErrors = withoutErrors && !isPostsPending;

return (
<main className="section">
<div className="container">
Expand All @@ -21,24 +36,28 @@ export const App: React.FC = () => {
</div>

<div className="block" data-cy="MainContent">
<p data-cy="NoSelectedUser">
No user selected
</p>

<Loader />

<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>

<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
</div>

<PostsList />
{!selectedUser && (
<p data-cy="NoSelectedUser">
No user selected
</p>
)}
{withoutErrors && isPostsPending && (<Loader />)}
{fetchedWithoutErrors && (posts.length > 0
? <PostsList />
: (
<div className="notification is-warning" data-cy="NoPostsYet">
No posts yet
</div>
)
)}
{hasError && (
<div
className="notification is-danger"
data-cy="PostsLoadingError"
>
Something went wrong!
</div>
)}
</div>
</div>
</div>
Expand All @@ -50,11 +69,13 @@ export const App: React.FC = () => {
'is-parent',
'is-8-desktop',
'Sidebar',
'Sidebar--open',
{
'Sidebar--open': selectedPost,
},
)}
>
<div className="tile is-child box is-success ">
<PostDetails />
{selectedPost && <PostDetails />}
</div>
</div>
</div>
Expand Down
16 changes: 16 additions & 0 deletions src/Root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { App } from './App';
import { CommentsContextProvider } from './context/CommentsContext';
import { PostsContextProvider } from './context/PostsContext';
import { UserContextProvider } from './context/UserContext';

const Root: React.FC = () => (
<UserContextProvider>
<PostsContextProvider>
<CommentsContextProvider>
<App />
</CommentsContextProvider>
</PostsContextProvider>
</UserContextProvider>
);

export default Root;
145 changes: 117 additions & 28 deletions src/components/NewCommentForm.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,67 @@
import React from 'react';
import classNames from 'classnames';
import React, { useState } from 'react';
import { useCommentsContext } from '../hooks/useCommentsContext';

const initialDetails = {
name: '',
email: '',
body: '',
};

export const NewCommentForm: React.FC = () => {
const { addingComment, handleAddComment } = useCommentsContext();
const [commentDetails, setCommentDetails] = useState(initialDetails);
const [nameError, setNameError] = useState(false);
const [emailError, setEmailError] = useState(false);
const [bodyError, setBodyError] = useState(false);

Comment on lines +14 to +16

Choose a reason for hiding this comment

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

Many states. Try to unite them.

const handleResetForm = () => {
setNameError(false);
setEmailError(false);
setBodyError(false);
setCommentDetails(prev => ({
...prev,
body: '',
}));
};

const handleSubmitForm = (event: React.FormEvent) => {
event.preventDefault();

if (!commentDetails.name) {
setNameError(true);
}

if (!commentDetails.email) {
setEmailError(true);
}

if (!commentDetails.body) {
setBodyError(true);
}

if (commentDetails.name
&& commentDetails.email
&& commentDetails.body
) {
handleAddComment(commentDetails);
handleResetForm();
}
};

const handleDetailsChange = (fieldName: string, value: string) => {
setCommentDetails(prev => ({
...prev,
[fieldName]: value,
}));
};

return (
<form data-cy="NewCommentForm">
<form
data-cy="NewCommentForm"
onReset={handleResetForm}
onSubmit={handleSubmitForm}
>
<div className="field" data-cy="NameField">
<label className="label" htmlFor="comment-author-name">
Author Name
Expand All @@ -14,24 +73,32 @@ export const NewCommentForm: React.FC = () => {
name="name"
id="comment-author-name"
placeholder="Name Surname"
className="input is-danger"
className={classNames('input', {
'is-danger': nameError,
})}
value={commentDetails.name}
onChange={e => handleDetailsChange(e.target.name, e.target.value)}
/>

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

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

<p className="help is-danger" data-cy="ErrorMessage">
Name is required
</p>
{nameError && (
<p className="help is-danger" data-cy="ErrorMessage">
Name is required
</p>
)}
</div>

<div className="field" data-cy="EmailField">
Expand All @@ -45,24 +112,32 @@ export const NewCommentForm: React.FC = () => {
name="email"
id="comment-author-email"
placeholder="email@test.com"
className="input is-danger"
className={classNames('input', {
'is-danger': emailError,
})}
value={commentDetails.email}
onChange={e => handleDetailsChange(e.target.name, e.target.value)}
/>

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

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

<div className="field" data-cy="BodyField">
Expand All @@ -75,25 +150,39 @@ export const NewCommentForm: React.FC = () => {
id="comment-body"
name="body"
placeholder="Type comment here"
className="textarea is-danger"
className={classNames('textarea', {
'is-danger': bodyError,
})}
value={commentDetails.body}
onChange={e => handleDetailsChange(e.target.name, e.target.value)}
/>
</div>

<p className="help is-danger" data-cy="ErrorMessage">
Enter some text
</p>
{bodyError && (
<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"
className={classNames('button is-link', {
'is-loading': addingComment,
})}
>
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"
className="button is-link is-light"
>
Clear
</button>
</div>
Expand Down
Loading
Loading