Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
piyopiyo0 committed Oct 16, 2024
1 parent dec4411 commit 341ee70
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 45 deletions.
4 changes: 2 additions & 2 deletions cypress/integration/page.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ describe('', () => {
page.waitForRequest('@usersRequest');
});

it('should should empty text by default', () => {
it('should show empty text by default', () => {
button().should('have.text', 'Choose a user');
});

Expand Down Expand Up @@ -861,7 +861,7 @@ describe('', () => {
describe('', () => {
beforeEach(() => {
cy.clock();
page.spyOn('**/comments?postId=2', 'post2Coments', { fixture: 'post2Comments' });
page.spyOn('**/comments?postId=2', 'post2Comments', { fixture: 'post2Comments' });
page.postButton(1).click();
});

Expand Down
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import { useEffect, useState } from 'react';
import 'bulma/css/bulma.css';
import '@fortawesome/fontawesome-free/css/all.css';
import './App.scss';
Expand Down
4 changes: 2 additions & 2 deletions src/components/MainContent/PostList/PostItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const PostItem: React.FC<PostItemProps> = ({ post }) => {
const { setIsError } = useContext(ErrorsContext);
const { setIsActiveForm } = useContext(CommentFormContext);

function handleOpenButton() {
function handleOpenButtonClick() {
if (activePost?.id !== post.id) {
setActivePost(post);
} else {
Expand Down Expand Up @@ -57,7 +57,7 @@ export const PostItem: React.FC<PostItemProps> = ({ post }) => {
className={classNames('button is-link', {
'is-light': activePost?.id !== post.id,
})}
onClick={() => handleOpenButton()}
onClick={() => handleOpenButtonClick()}
>
{activePost?.id === post.id ? 'Close' : 'Open'}
</button>
Expand Down
10 changes: 6 additions & 4 deletions src/components/Sidebar/CommentList/CommentItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ interface CommentItemProps {
export const CommentItem: React.FC<CommentItemProps> = ({ comment }) => {
const { comments, setComments } = useContext(CommentListContext);

function handleDeleteButton() {
async function handleDelete() {
const newList = comments.filter(item => item.id !== comment.id);
const copy = [...comments];

setComments(newList);

deleteComment(comment.id).catch(() => {
try {
await deleteComment(comment.id);
} catch (error) {
alert('Failed to delete comment, please try again!');

setComments(copy);
});
}
}

return (
Expand All @@ -34,7 +36,7 @@ export const CommentItem: React.FC<CommentItemProps> = ({ comment }) => {
type="button"
className="delete is-small"
aria-label="delete"
onClick={handleDeleteButton}
onClick={handleDelete}
>
delete button
</button>
Expand Down
4 changes: 2 additions & 2 deletions src/components/Sidebar/CommentList/CommentList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export const CommentList: React.FC<CommentListProps> = ({ comments }) => {
<>
<p className="title is-4">Comments:</p>

{comments?.map(item => (
<CommentItem key={item.id} comment={item} />
{comments?.map(comment => (
<CommentItem key={comment.id} comment={comment} />
))}
</>
)}
Expand Down
19 changes: 10 additions & 9 deletions src/components/Sidebar/PostDetails/NewCommentForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const NewCommentForm: React.FC = () => {
});
}

function handleAdd(event: React.FormEvent<HTMLFormElement>) {
const handleAdd = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();

const newError = { ...isErrorField };
Expand Down Expand Up @@ -100,10 +100,10 @@ export const NewCommentForm: React.FC = () => {
setIsProcessing(false);
});
}
}
};

return (
<form data-cy="NewCommentForm" onSubmit={event => handleAdd(event)}>
<form data-cy="NewCommentForm" onSubmit={handleAdd}>
<div className="field" data-cy="NameField">
<label className="label" htmlFor="comment-author-name">
Author Name
Expand All @@ -119,8 +119,8 @@ export const NewCommentForm: React.FC = () => {
className={classNames('input', {
'is-danger': isErrorField.nameField,
})}
onChange={e =>
onChangeHandler(Keys.nameField, e.target.value.trimStart())
onChange={event =>
onChangeHandler(Keys.nameField, event.target.value.trimStart())
}
/>

Expand Down Expand Up @@ -160,8 +160,8 @@ export const NewCommentForm: React.FC = () => {
className={classNames('input', {
'is-danger': isErrorField.emailField,
})}
onChange={e =>
onChangeHandler(Keys.emailField, e.target.value.trimStart())
onChange={event =>
onChangeHandler(Keys.emailField, event.target.value.trimStart())
}
/>

Expand Down Expand Up @@ -200,8 +200,8 @@ export const NewCommentForm: React.FC = () => {
className={classNames('textarea', {
'is-danger': isErrorField.textArea,
})}
onChange={e =>
onChangeHandler(Keys.textArea, e.target.value.trimStart())
onChange={event =>
onChangeHandler(Keys.textArea, event.target.value.trimStart())
}
/>
</div>
Expand All @@ -220,6 +220,7 @@ export const NewCommentForm: React.FC = () => {
className={classNames('button is-link', {
'is-loading': isProcessing,
})}
disabled={isProcessing}
>
Add
</button>
Expand Down
14 changes: 6 additions & 8 deletions src/components/Sidebar/PostDetails/PostDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@ export const PostDetails: React.FC = () => {

return (
<div className="content" data-cy="PostDetails">
<div className="content" data-cy="PostDetails">
<div className="block">
<h2 data-cy="PostTitle">{`#${activePost?.id}: ${activePost?.title}`}</h2>
<div className="block">
<h2 data-cy="PostTitle">{`#${activePost?.id}: ${activePost?.title}`}</h2>

<p data-cy="PostBody">{activePost?.body}</p>
</div>
<p data-cy="PostBody">{activePost?.body}</p>
</div>

{activePost && <CommentList comments={comments} />}
{activePost && <CommentList comments={comments} />}

{isActiveForm && <NewCommentForm />}
</div>
{isActiveForm && <NewCommentForm />}
</div>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const DropdownItem: React.FC<DropdownItemProps> = ({
const { setIsError } = useContext(ErrorsContext);
const { setActivePost } = useContext(ActivePostContext);

function handleMouseDown(
function handleMouseDownEvent(
event: React.MouseEvent<HTMLAnchorElement, MouseEvent>,
) {
event.preventDefault();
Expand Down Expand Up @@ -54,7 +54,7 @@ export const DropdownItem: React.FC<DropdownItemProps> = ({
className={classNames('dropdown-item', {
'is-active': activeUser?.id === user.id,
})}
onMouseDown={event => handleMouseDown(event)}
onMouseDown={handleMouseDownEvent}
>
{user.name}
</a>
Expand Down
21 changes: 19 additions & 2 deletions src/components/UserSelector/DropdownMenu/DropdownMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import { DropdownItem } from './DropdownItem';
import { User } from '../../../types/User';

Expand All @@ -7,14 +7,31 @@ interface DropdownMenuProps {
setIsActive: React.Dispatch<React.SetStateAction<boolean>>;
}

interface UserWithKey extends User {
key: string;
}

export const DropdownMenu: React.FC<DropdownMenuProps> = ({
users,
setIsActive,
}) => {
const [usersWithKeys, setUsersWithKeys] = useState<UserWithKey[]>([]);

useEffect(() => {
const generateUniqueKeys = (userList: User[]): UserWithKey[] => {
return userList.map(user => ({
...user,
key: `${user.id}-${Math.random().toString(36).substr(2, 9)}`,
}));
};

setUsersWithKeys(generateUniqueKeys(users));
}, [users]);

return (
<div tabIndex={1} className="dropdown-menu" id="dropdown-menu" role="menu">
<div className="dropdown-content">
{users.map(user => (
{usersWithKeys.map(user => (
<DropdownItem key={user.id} user={user} setIsActive={setIsActive} />
))}
</div>
Expand Down
24 changes: 12 additions & 12 deletions src/utils/Store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ type ActiveUser = User | null;

interface ActiveUserContextType {
activeUser: ActiveUser;
setActiveUser: React.Dispatch<SetStateAction<ActiveUser>>;
setActiveUser: React.Dispatch<SetStateAction<ActiveUser>> | undefined;
}

export const ActiveUserContext = createContext<ActiveUserContextType>({
activeUser: null,
setActiveUser: () => {},
setActiveUser: undefined,
});

export const ActiveUserProvider: React.FC<Children> = ({ children }) => {
Expand All @@ -34,12 +34,12 @@ export const ActiveUserProvider: React.FC<Children> = ({ children }) => {

interface Loader {
isLoading: boolean;
setIsLoading: React.Dispatch<SetStateAction<boolean>>;
setIsLoading: React.Dispatch<SetStateAction<boolean>> | undefined;
}

export const LoaderContext = createContext<Loader>({
isLoading: false,
setIsLoading: () => {},
setIsLoading: undefined,
});

export const LoaderProvider: React.FC<Children> = ({ children }) => {
Expand All @@ -59,12 +59,12 @@ type PostsType = Post[] | null;

interface Posts {
posts: PostsType;
setPosts: React.Dispatch<SetStateAction<PostsType>>;
setPosts: React.Dispatch<SetStateAction<PostsType>> | undefined;
}

export const PostsContext = createContext<Posts>({
posts: null,
setPosts: () => {},
setPosts: undefined,
});

export const PostsProvider: React.FC<Children> = ({ children }) => {
Expand All @@ -81,12 +81,12 @@ type Error = ErrorTypes | null;

interface Errors {
isError: Error;
setIsError: React.Dispatch<SetStateAction<Error>>;
setIsError: React.Dispatch<SetStateAction<Error>> | undefined;
}

export const ErrorsContext = createContext<Errors>({
isError: null,
setIsError: () => {},
setIsError: undefined,
});

export const ErrorsProvider: React.FC<Children> = ({ children }) => {
Expand Down Expand Up @@ -128,12 +128,12 @@ export const ActivePostProvider: React.FC<Children> = ({ children }) => {

interface CommentList {
comments: Comment[];
setComments: React.Dispatch<SetStateAction<Comment[]>>;
setComments: React.Dispatch<SetStateAction<Comment[]>> | undefined;
}

export const CommentListContext = createContext<CommentList>({
comments: [],
setComments: () => {},
setComments: undefined,
});

export const CommentListProvider: React.FC<Children> = ({ children }) => {
Expand All @@ -153,12 +153,12 @@ export const CommentListProvider: React.FC<Children> = ({ children }) => {

interface Form {
isActiveForm: boolean;
setIsActiveForm: React.Dispatch<SetStateAction<boolean>>;
setIsActiveForm: React.Dispatch<SetStateAction<boolean>> | undefined;
}

export const CommentFormContext = createContext<Form>({
isActiveForm: false,
setIsActiveForm: () => {},
setIsActiveForm: undefined,
});

export const CommentFormProvider: React.FC<Children> = ({ children }) => {
Expand Down
4 changes: 3 additions & 1 deletion src/utils/fetchClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ function request<T>(
.then(() => fetch(BASE_URL + url, options))
.then(response => {
if (!response.ok) {
throw new Error();
throw new Error(
`Request failed with status ${response.status}: ${response.statusText}`,
);
}

return response.json();
Expand Down

0 comments on commit 341ee70

Please sign in to comment.