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

react_dynamic-list-of-todos_v-01 #1952

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ loaded and show them using `TodoList` (check the code in the `api.ts`);
- Implement a solution following the [React task guideline](https://github.com/mate-academy/react_task-guideline#react-tasks-guideline).
- Use the [React TypeScript cheat sheet](https://mate-academy.github.io/fe-program/js/extra/react-typescript).
- Open one more terminal and run tests with `npm test` to ensure your solution is correct.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://<your_account>.github.io/react_dynamic-list-of-todos/) and add it to the PR description.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://DenysKolbasin.github.io/react_dynamic-list-of-todos/) and add it to the PR description.
60 changes: 53 additions & 7 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
/* eslint-disable max-len */
import React from 'react';
import React, { useState, useEffect } from 'react';
import 'bulma/css/bulma.css';
import '@fortawesome/fontawesome-free/css/all.css';

import { TodoList } from './components/TodoList';
import { TodoFilter } from './components/TodoFilter';
import { TodoModal } from './components/TodoModal';
import { Loader } from './components/Loader';
import { getTodos } from './api';
import { Todo } from './types/Todo';
import { TodoModal } from './components/TodoModal';

enum FilterStatus {
All = 'all',
Completed = 'completed',
Active = 'active',
}

export const App: React.FC = () => {
const [todos, setTodos] = useState<Todo[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [selectedTodo, setSelectedTodo] = useState<Todo | null>(null);
const [query, setQuery] = useState('');
const [status, setStatus] = useState<string>(FilterStatus.All);

useEffect(() => {
setIsLoading(true);
getTodos()
.then(setTodos)
.finally(() => setIsLoading(false));
}, []);

const filteredTodos = todos.filter((todo) => (
(status === FilterStatus.All
|| (status === FilterStatus.Completed && todo.completed)
|| (status === FilterStatus.Active && !todo.completed))
&& todo.title.toLowerCase().includes(query.toLowerCase())
));

return (
<>
<div className="section">
Expand All @@ -17,18 +45,36 @@ export const App: React.FC = () => {
<h1 className="title">Todos:</h1>

<div className="block">
<TodoFilter />
<TodoFilter
query={query}
setQuery={setQuery}
status={status}
setStatus={setStatus}
/>
</div>

<div className="block">
<Loader />
<TodoList />
{isLoading && <Loader />}

{!isLoading && filteredTodos.length > 0 && (
<TodoList
todos={filteredTodos}
onSelect={setSelectedTodo}
selectedTodo={selectedTodo}
/>
)}
</div>
</div>
</div>
</div>

<TodoModal />
{selectedTodo && (
<TodoModal
userId={selectedTodo.userId}
selectedTodo={selectedTodo}
key={selectedTodo.id}
onClose={() => setSelectedTodo(null)}
/>
)}
</>
);
};
3 changes: 0 additions & 3 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { User } from './types/User';
// eslint-disable-next-line max-len
const BASE_URL = 'https://mate-academy.github.io/react_dynamic-list-of-todos/api';

// This function creates a promise
// that is resolved after a given delay
function wait(delay: number): Promise<void> {
return new Promise(resolve => {
setTimeout(resolve, delay);
Expand All @@ -16,7 +14,6 @@ function get<T>(url: string): Promise<T> {
// eslint-disable-next-line prefer-template
const fullURL = BASE_URL + url + '.json';

// we add some delay to see how the loader works
return wait(300)
.then(() => fetch(fullURL))
.then(res => res.json());
Expand Down
87 changes: 56 additions & 31 deletions src/components/TodoFilter/TodoFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,59 @@
export const TodoFilter = () => (
<form className="field has-addons">
<p className="control">
<span className="select">
<select data-cy="statusSelect">
<option value="all">All</option>
<option value="active">Active</option>
<option value="completed">Completed</option>
</select>
</span>
</p>
import React from 'react';

<p className="control is-expanded has-icons-left has-icons-right">
<input
data-cy="searchInput"
type="text"
className="input"
placeholder="Search..."
/>
<span className="icon is-left">
<i className="fas fa-magnifying-glass" />
</span>
interface Props {
query: string;
setQuery: (query: string) => void;
status: string;
setStatus: (status: string) => void;
}

<span className="icon is-right" style={{ pointerEvents: 'all' }}>
{/* eslint-disable-next-line jsx-a11y/control-has-associated-label */}
<button
data-cy="clearSearchButton"
type="button"
className="delete"
export const TodoFilter: React.FC<Props> = ({
query,
setQuery,
status,
setStatus,
}) => {
return (
<form className="field has-addons">
<p className="control">
<span className="select">
<select
data-cy="statusSelect"
value={status}
onChange={(e) => setStatus(e.target.value)}
>
<option value="all">All</option>
<option value="active">Active</option>
<option value="completed">Completed</option>
</select>
</span>
</p>

<p className="control is-expanded has-icons-left has-icons-right">
<input
data-cy="searchInput"
type="text"
className="input"
placeholder="Search..."
value={query}
onChange={(e) => setQuery(e.target.value)}
/>
</span>
</p>
</form>
);
<span className="icon is-left">
<i className="fas fa-magnifying-glass" />
</span>

{query && (
<span className="icon is-right" style={{ pointerEvents: 'all' }}>
{/* eslint-disable-next-line jsx-a11y/control-has-associated-label */}
<button
data-cy="clearSearchButton"
type="button"
className="delete"
onClick={() => setQuery('')}
/>
</span>
)}
</p>
</form>
);
};
62 changes: 62 additions & 0 deletions src/components/TodoItem/TodoItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import React from "react";
import { Todo } from "../../types/Todo"
import cn from 'classnames';


interface Props {
todo: Todo;
onSelect?: (todo: Todo) => void;
selectedTodo: Todo | null;
}

export const TodoItem: React.FC<Props> = ({
todo,
onSelect,
selectedTodo,
}) => {
return (
<tr
data-cy="todo"
key={todo.id}
className={cn({
'has-background-info-light': selectedTodo === todo,
})}
>
<td className="is-vcentered">{todo.id}</td>
<td className="is-vcentered">
{todo.completed && (
<span className="icon" data-cy="iconCompleted">
<i className='fas fa-check' />
</span>
)}
</td>
<td className="is-vcentered is-expanded">
<p
className={cn({
'has-text-success': todo.completed,
'has-text-danger': !todo.completed,
})}
>
{todo.title}
</p>
</td>
<td className="has-text-right is-vcentered">
<button
data-cy="selectButton"
className="button"
type="button"
onClick={() => onSelect && onSelect(todo)}
>
<span className="icon">
<i
className={cn('far', {
'fa-eye-slash': selectedTodo === todo,
'fa-eye': selectedTodo !== todo,
})}
/>
</span>
</button>
</td>
</tr>
)
}
1 change: 1 addition & 0 deletions src/components/TodoItem/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './TodoItem';
Loading