Skip to content

Commit

Permalink
improvedCode
Browse files Browse the repository at this point in the history
  • Loading branch information
Shevchuchka committed Oct 30, 2024
1 parent fd30f85 commit 2d206f7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
14 changes: 6 additions & 8 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,12 @@ export const App = () => {
{ 'Sidebar--open': selectedPost },
)}
>
<div className="tile is-child box is-success ">
{selectedPost && (
<PostDetails
selectedPost={selectedPost}
showForm={showForm}
setShowForm={setShowForm}
/>
)}
<div className="tile is-child box is-success">
<PostDetails
selectedPost={selectedPost}
showForm={showForm}
setShowForm={setShowForm}
/>
</div>
</div>
</div>
Expand Down
39 changes: 21 additions & 18 deletions src/components/NewCommentForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,48 @@ export const NewCommentForm: React.FC<Props> = ({
}) => {
const [name, setName] = useState('');
const [nameError, setNameError] = useState(false);
const [email, setEmail] = useState('');
const [emailError, setEmailError] = useState(false);
const [text, setText] = useState('');
const [textError, setTextError] = 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);

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

const normalizeValue = (value: string) => {
return value.trim();
const checkQuery = (
query: string,
setQuery: (query: string) => void,
error: (value: boolean) => void,
) => {
const normalizedQuery = query.trim();
const isError = normalizedQuery.length === 0 || !query;

error(isError);
setQuery(normalizedQuery);

return isError;
};

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

setName(normalizeValue(name));
setNameError(!name);
const isNameError = checkQuery(name, setName, setNameError);
const isEmailError = checkQuery(email, setEmail, setEmailError);
const isTextError = checkQuery(text, setText, setTextError);

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

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

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

Expand Down Expand Up @@ -116,7 +119,7 @@ export const NewCommentForm: React.FC<Props> = ({

<div className="control has-icons-left has-icons-right">
<input
type="text"
type="email"
name="email"
id="comment-author-email"
placeholder="[email protected]"
Expand Down

0 comments on commit 2d206f7

Please sign in to comment.