-
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
todo app final part #781
Open
AnVBondar
wants to merge
2
commits into
mate-academy:master
Choose a base branch
from
AnVBondar: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
todo app final part #781
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 |
---|---|---|
@@ -1,24 +1,30 @@ | ||
/* eslint-disable max-len */ | ||
/* eslint-disable jsx-a11y/control-has-associated-label */ | ||
import React from 'react'; | ||
import React, { useContext } from 'react'; | ||
import { UserWarning } from './UserWarning'; | ||
|
||
const USER_ID = 0; | ||
import { Footer } from './components/Footer'; | ||
import { Header } from './components/Header'; | ||
import { Main } from './components/Main'; | ||
import { Errors } from './components/Errors'; | ||
import { TodoContext } from './TodoContext'; | ||
|
||
export const App: React.FC = () => { | ||
const { USER_ID } = useContext(TodoContext); | ||
|
||
if (!USER_ID) { | ||
return <UserWarning />; | ||
} | ||
|
||
return ( | ||
<section className="section container"> | ||
<p className="title is-4"> | ||
Copy all you need from the prev task: | ||
<br /> | ||
<a href="https://github.com/mate-academy/react_todo-app-add-and-delete#react-todo-app-add-and-delete">React Todo App - Add and Delete</a> | ||
</p> | ||
<div className="todoapp"> | ||
<h1 className="todoapp__title">todos</h1> | ||
|
||
<div className="todoapp__content"> | ||
<Header /> | ||
<Main /> | ||
<Footer /> | ||
</div> | ||
|
||
<p className="subtitle">Styles are already copied</p> | ||
</section> | ||
<Errors /> | ||
</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,92 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import React, { useEffect, useState } from 'react'; | ||
import { TodoType } from './types/TodoType'; | ||
import { Filter } from './types/Filter'; | ||
import { ErrorsType } from './types/ErrorsType'; | ||
import { getTodos } from './api/todos'; | ||
|
||
const USER_ID = 10236; | ||
|
||
type TodoContextType = { | ||
USER_ID: number; | ||
todos: TodoType[]; | ||
filterTodo: Filter; | ||
errorMessage: string; | ||
tempTodo: TodoType | null; | ||
selectedTodos: TodoType[], | ||
setTempTodo: (tempTodo: TodoType | null) => void; | ||
getFilteredTodo: (filter: Filter) => void; | ||
setErrorMessage: (error: string) => void; | ||
addTodo: (newTodo: TodoType) => void; | ||
setTodos: React.Dispatch<React.SetStateAction<TodoType[]>>; | ||
setHasDelete: React.Dispatch<React.SetStateAction<boolean>>; | ||
setSelectedTodos: React.Dispatch<React.SetStateAction<TodoType[]>>; | ||
}; | ||
|
||
export const TodoContext = React.createContext<TodoContextType>({ | ||
USER_ID, | ||
todos: [], | ||
filterTodo: Filter.All, | ||
errorMessage: '', | ||
tempTodo: null, | ||
selectedTodos: [], | ||
setTempTodo: () => {}, | ||
getFilteredTodo: () => {}, | ||
setErrorMessage: () => {}, | ||
setTodos: () => {}, | ||
addTodo: () => {}, | ||
setHasDelete: () => {}, | ||
setSelectedTodos: () => {}, | ||
}); | ||
|
||
type Props = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
export const GlobalStateProvider: React.FC<Props> = ({ children }) => { | ||
const [todos, setTodos] = useState<TodoType[]>([]); | ||
const [filterTodo, setFilterTodo] = useState<Filter>(Filter.All); | ||
const [errorMessage, setErrorMessage] = useState<string>(''); | ||
const [tempTodo, setTempTodo] = useState<TodoType | null>(null); | ||
const [hasDelete, setHasDelete] = useState<boolean>(false); | ||
const [selectedTodos, setSelectedTodos] = useState<TodoType[]>([]); | ||
|
||
useEffect(() => { | ||
getTodos(USER_ID) | ||
.then(setTodos) | ||
.catch(() => { | ||
setErrorMessage(ErrorsType.Load); | ||
setTimeout(() => { | ||
setErrorMessage(''); | ||
}, 3000); | ||
}); | ||
}, [hasDelete]); | ||
|
||
function addTodo(newTodo: TodoType) { | ||
setTodos(currentTodos => [...currentTodos, newTodo]); | ||
} | ||
|
||
const value = { | ||
USER_ID, | ||
todos, | ||
filterTodo, | ||
errorMessage, | ||
tempTodo, | ||
setTempTodo, | ||
setErrorMessage, | ||
getFilteredTodo: (filter: Filter) => { | ||
setFilterTodo(filter); | ||
}, | ||
setTodos, | ||
addTodo, | ||
setHasDelete, | ||
selectedTodos, | ||
setSelectedTodos, | ||
}; | ||
|
||
return ( | ||
<TodoContext.Provider value={value}> | ||
{children} | ||
</TodoContext.Provider> | ||
); | ||
}; |
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,29 @@ | ||
import { TodoType } from '../types/TodoType'; | ||
import { client } from '../utils/fetchClient'; | ||
|
||
export const getTodos = (userId: number) => { | ||
return client.get<TodoType[]>(`/todos?userId=${userId}`); | ||
}; | ||
|
||
export const postTodos = ( | ||
{ | ||
userId, | ||
title, | ||
completed, | ||
}: Omit<TodoType, 'id'>, | ||
) => { | ||
return client.post<TodoType>('/todos', { userId, title, completed }); | ||
}; | ||
|
||
export const deleteTodos = (todoId: number) => { | ||
return client.delete(`/todos/${todoId}`); | ||
}; | ||
|
||
export const patchTodos = ({ | ||
id, | ||
completed, | ||
userId, | ||
title, | ||
}: TodoType) => { | ||
return client.patch(`/todos/${id}`, { userId, title, completed }); | ||
}; |
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,49 @@ | ||
import cn from 'classnames'; | ||
import { useContext } from 'react'; | ||
import { TodoContext } from '../../TodoContext'; | ||
import { ErrorsType } from '../../types/ErrorsType'; | ||
|
||
export const Errors = () => { | ||
const { errorMessage, setErrorMessage } = useContext(TodoContext); | ||
|
||
let message = ''; | ||
|
||
switch (errorMessage) { | ||
case ErrorsType.Load: | ||
message = 'Unable to load a todo'; | ||
break; | ||
case ErrorsType.Add: | ||
message = 'Unable to add a todo'; | ||
break; | ||
case ErrorsType.Delete: | ||
message = 'Unable to delete a todo'; | ||
break; | ||
case ErrorsType.Update: | ||
message = 'Unable to update a todo'; | ||
break; | ||
case ErrorsType.Title: | ||
message = 'Title can\'t be empty'; | ||
break; | ||
default: | ||
} | ||
|
||
return ( | ||
<div className={cn( | ||
'notification', | ||
'is-danger', | ||
'is-light', | ||
'has-text-weight-normal', { | ||
hidden: !errorMessage, | ||
}, | ||
)} | ||
> | ||
{/* eslint-disable-next-line jsx-a11y/control-has-associated-label */} | ||
<button | ||
type="button" | ||
className="delete" | ||
onClick={() => setErrorMessage('')} | ||
/> | ||
{message} | ||
</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 './Errors'; |
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.
why do you need
unused-vars
?