-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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 #1541
Open
save-v
wants to merge
8
commits into
mate-academy:master
Choose a base branch
from
save-v:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
solution #1541
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6d55969
solution
save-v c10a67c
Solution
save-v b39ea1b
refactoring
save-v 5a1fdd3
refactoring
save-v 5af2f6b
refactoring
save-v bc1054b
refactoring
save-v f04c272
refactoring
save-v c5cce3c
Refactor: split code into three new components
save-v File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Todo } from '../types/Todo'; | ||
import { client } from '../utils/fetchClient'; | ||
|
||
export const USER_ID = 2148; | ||
|
||
export const getTodos = () => { | ||
return client.get<Todo[]>(`/todos?userId=${USER_ID}`); | ||
}; | ||
|
||
// Add more methods here | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* eslint-disable jsx-a11y/label-has-associated-control */ | ||
/* eslint-disable jsx-a11y/control-has-associated-label */ | ||
import React, { Dispatch, SetStateAction } from 'react'; | ||
import cN from 'classnames'; | ||
import { Error } from '../../types/Error'; | ||
|
||
type Props = { | ||
errorMessage: Error | null; | ||
setErrorMessage: Dispatch<SetStateAction<Error | null>>; | ||
}; | ||
|
||
export const ErrorNotification: React.FC<Props> = ({ | ||
errorMessage, | ||
setErrorMessage, | ||
}) => { | ||
return ( | ||
<div | ||
data-cy="ErrorNotification" | ||
className={cN('notification is-danger is-light has-text-weight-normal', { | ||
hidden: !errorMessage, | ||
})} | ||
> | ||
<button | ||
onClick={() => setErrorMessage(null)} | ||
data-cy="HideErrorButton" | ||
type="button" | ||
className="delete" | ||
/> | ||
{/* show only one message at a time +++*/} | ||
{errorMessage} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './ErrorNotification'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* eslint-disable jsx-a11y/label-has-associated-control */ | ||
/* eslint-disable jsx-a11y/control-has-associated-label */ | ||
import React, { | ||
Dispatch, | ||
SetStateAction, | ||
RefObject, | ||
useState, | ||
useRef, | ||
useEffect, | ||
} from 'react'; | ||
import { Todo } from '../../types/Todo'; | ||
import cN from 'classnames'; | ||
|
||
type Props = { | ||
todo: Todo; | ||
isTemp?: boolean; | ||
handleTodoStatusChange?: (id: number) => void; | ||
onDelete?: (id: number) => Promise<void>; | ||
isDeletingTodo?: boolean; | ||
todoToDeleteIds?: number[] | null; | ||
setTodoToDeleteIds?: Dispatch<SetStateAction<number[] | null>>; | ||
addTodoField?: RefObject<HTMLInputElement>; | ||
isUpdatingStatus?: boolean; | ||
statusChangeId?: number | null; | ||
setEditingTodoId?: Dispatch<SetStateAction<number | null>>; | ||
editingTodoId?: number | null; | ||
handleTitleChange?: (newTitle: string) => void; | ||
isUpdatingTitle?: boolean | null; | ||
}; | ||
|
||
export const TodoItem: React.FC<Props> = ({ | ||
todo, | ||
isTemp = false, | ||
handleTodoStatusChange, | ||
onDelete, | ||
isDeletingTodo, | ||
todoToDeleteIds, | ||
setTodoToDeleteIds, | ||
addTodoField, | ||
isUpdatingStatus, | ||
statusChangeId, | ||
setEditingTodoId, | ||
editingTodoId, | ||
handleTitleChange, | ||
isUpdatingTitle, | ||
}) => { | ||
const { title, id, completed } = todo; | ||
const [inputValue, setInputValue] = useState<string>(title); | ||
const editTodoField = useRef<HTMLInputElement>(null); | ||
|
||
useEffect(() => { | ||
if (editTodoField.current !== null) { | ||
editTodoField.current.focus(); | ||
} | ||
}, [editingTodoId]); | ||
|
||
return ( | ||
<div key={id} data-cy="Todo" className={cN('todo', { completed })}> | ||
<label className="todo__status-label"> | ||
<input | ||
data-cy="TodoStatus" | ||
type="checkbox" | ||
className="todo__status" | ||
checked={completed} | ||
onChange={() => (isTemp ? null : handleTodoStatusChange?.(id))} | ||
/> | ||
</label> | ||
{id === editingTodoId ? ( | ||
<form onSubmit={() => handleTitleChange?.(inputValue)}> | ||
<input | ||
onBlur={() => { | ||
handleTitleChange?.(inputValue); | ||
}} | ||
ref={editTodoField} | ||
data-cy="TodoTitleField" | ||
type="text" | ||
className="todo__title-field" | ||
placeholder="Empty todo will be deleted" | ||
value={inputValue} | ||
onChange={event => setInputValue(event.target.value)} | ||
/> | ||
</form> | ||
) : ( | ||
<> | ||
<span | ||
onDoubleClick={() => setEditingTodoId?.(id)} | ||
data-cy="TodoTitle" | ||
className="todo__title" | ||
> | ||
{title} | ||
</span> | ||
<button | ||
onClick={() => { | ||
setTodoToDeleteIds?.([id]); | ||
onDelete?.(id).then(() => { | ||
if (addTodoField?.current !== null) { | ||
addTodoField?.current.focus(); | ||
} | ||
}); | ||
}} | ||
type="button" | ||
className="todo__remove" | ||
data-cy="TodoDelete" | ||
> | ||
× | ||
</button> | ||
</> | ||
)} | ||
{/* isDeletingTodo спробувати прибрати*/} | ||
<div | ||
data-cy="TodoLoader" | ||
className={cN('modal overlay', { | ||
'is-active': | ||
isTemp || | ||
(isDeletingTodo && todoToDeleteIds?.includes(id)) || | ||
(isUpdatingStatus && id === statusChangeId) || | ||
(isUpdatingTitle && id === editingTodoId), | ||
})} | ||
> | ||
<div className="modal-background has-background-white-ter" /> | ||
<div className="loader" /> | ||
</div> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './TodoItem'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export enum Error { | ||
LoadError = 'Unable to load todos', | ||
EmptyTitleError = 'Title should not be empty', | ||
AddError = 'Unable to add a todo', | ||
DeleteError = 'Unable to delete a todo', | ||
UpdateError = 'Unable to update a todo', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export enum Filter { | ||
All = 'All', | ||
Completed = 'Completed', | ||
Active = 'Active', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface Todo { | ||
id: number; | ||
userId: number; | ||
title: string; | ||
completed: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
const BASE_URL = 'https://mate.academy/students-api'; | ||
|
||
// returns a promise resolved after a given delay + | ||
function wait(delay: number) { | ||
return new Promise(resolve => { | ||
setTimeout(resolve, delay); | ||
}); | ||
} | ||
|
||
// To have autocompletion and avoid mistypes + | ||
type RequestMethod = 'GET' | 'POST' | 'PATCH' | 'DELETE'; | ||
|
||
function request<T>( | ||
url: string, | ||
method: RequestMethod = 'GET', | ||
data: any = null, // we can send any data to the server + | ||
): Promise<T> { | ||
const options: RequestInit = { method }; | ||
|
||
if (data) { | ||
// We add body and Content-Type only for the requests with data + | ||
options.body = JSON.stringify(data); | ||
options.headers = { | ||
'Content-Type': 'application/json; charset=UTF-8', | ||
}; | ||
} | ||
|
||
// DON'T change the delay it is required for tests + | ||
return wait(100) | ||
.then(() => fetch(BASE_URL + url, options)) | ||
.then(response => { | ||
if (!response.ok) { | ||
throw new Error(); | ||
} | ||
|
||
return response.json(); | ||
}); | ||
} | ||
|
||
export const client = { | ||
get: <T>(url: string) => request<T>(url), | ||
post: <T>(url: string, data: any) => request<T>(url, 'POST', data), | ||
patch: <T>(url: string, data: any) => request<T>(url, 'PATCH', data), | ||
delete: (url: string) => request(url, 'DELETE'), | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Add more methods here