Skip to content

Commit

Permalink
Sesion 4
Browse files Browse the repository at this point in the history
adición de últimos detalles de lógica y +
persistencia de datos
y así ggg
  • Loading branch information
Guarnerman committed May 11, 2023
1 parent 434b740 commit 1cd3682
Show file tree
Hide file tree
Showing 31 changed files with 480 additions and 154 deletions.
1 change: 1 addition & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<div id="modal"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
Expand Down
38 changes: 0 additions & 38 deletions src/App.css

This file was deleted.

74 changes: 0 additions & 74 deletions src/App.js

This file was deleted.

61 changes: 61 additions & 0 deletions src/App/AppUI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React from 'react';
import { ToDoCounter } from '../ToDoCounter';
import { ToDoSearch } from "../ToDoSearch";
import { ToDoList } from '../ToDoList';
import { ToDoItem } from '../ToDoItem';
import { CreateToDoButton } from '../CreateToDoButton'
import { ToDosLoading } from "../ToDosLoading";
import { ToDosError } from "../ToDosError";
import { EmptyToDos } from "../EmptyToDos";
import { Modal } from "../Modal";
import { ToDoContext } from "../ToDoContext";
import { ToDoForm } from '../ToDoForm';

function AppUI() {
const {
loading,
error,
searchedToDos,
completeToDo,
deleteToDo,
openModal,
setOpenModal,
} = React.useContext(ToDoContext)
return (
<>
<ToDoCounter />
<ToDoSearch />
<ToDoList >
{loading && (
<>
<ToDosLoading />
<ToDosLoading />
<ToDosLoading />
</>
)}
{error && <ToDosError />}
{(!loading && searchedToDos.length === 0) && <EmptyToDos />}
{searchedToDos.map(toDo => (
<ToDoItem
key={toDo.text}
text={toDo.text}
completed={toDo.completed}
onComplete={() => completeToDo(toDo.text)}
onDelete={() => deleteToDo(toDo.text)}
/>
))}
</ToDoList>
<CreateToDoButton
setOpenModal={setOpenModal}
/>

{openModal && (
<Modal>
<ToDoForm />
</Modal>
)}
</ >
);
}

export { AppUI }
13 changes: 13 additions & 0 deletions src/App/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';
import { AppUI } from './AppUI';
import { ToDoProvider } from '../ToDoContext';

function App() {
return (
<ToDoProvider>
<AppUI />
</ToDoProvider>
)
}

export default App;
16 changes: 0 additions & 16 deletions src/CreateToDoButton.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
align-items: center;
height: 64px;
width: 64px;
z-index: 1;

transform: rotate(0);
transition: .3s ease;
Expand Down
14 changes: 14 additions & 0 deletions src/CreateToDoButton/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import './CreateToDoButton.css'

function CreateToDoButton({ setOpenModal }) {
return (
<button
className="CreateToDoButton"
onClick={() => {
setOpenModal(state => !state)
}}
>+</button>
);
}

export {CreateToDoButton}
9 changes: 9 additions & 0 deletions src/EmptyToDos/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'

function EmptyToDos() {
return (
<p>Crea tu primer To-Do!</p>
);
}

export { EmptyToDos };
12 changes: 12 additions & 0 deletions src/Modal/Modal.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.ModalBackground {
background-color: rgba(32,35,41,.8);
display: flex;
justify-content: center;
align-items: center;
color: white;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
14 changes: 14 additions & 0 deletions src/Modal/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from "react";
import ReactDOM from "react-dom";
import "./Modal.css";

function Modal({ children }) {
return ReactDOM.createPortal(
<div className="ModalBackground">
{children}
</div>,
document.getElementById('modal')
)
}

export { Modal }
71 changes: 71 additions & 0 deletions src/ToDoContext/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import React from 'react';
import { useLocalStorage } from './useLocalStorage';

const ToDoContext = React.createContext()

function ToDoProvider( { children } ) {
const {
item: toDos,
saveItem: saveToDos,
loading,
error,
} = useLocalStorage('ToDos_V1', [])
const [searchValue, setSearchValue] = React.useState('')
const [openModal, setOpenModal] = React.useState(false)

const completedToDos = toDos.filter(
toDo => !!toDo.completed).length
const totalToDos = toDos.length

const searchedToDos = toDos.filter(
(toDo) => {
const toDoText = toDo.text.toLowerCase()
const searchText = searchValue.toLocaleLowerCase()
return toDoText.includes(searchText)})

const addToDo = (text) => {
const newToDos = [...toDos]
newToDos.push({
text,
completed: false,
})
saveToDos(newToDos)
}

const completeToDo = (text) => {
const newToDos = [...toDos]
const toDoIndex = newToDos.findIndex(
(toDo) => toDo.text === text
)
newToDos[toDoIndex].completed = true
saveToDos(newToDos)
}
const deleteToDo = (text) => {
const newToDos = [...toDos]
const toDoIndex = newToDos.findIndex(
(toDo) => toDo.text === text
)
newToDos.splice(toDoIndex,1)
saveToDos(newToDos)
}
return (
<ToDoContext.Provider value={{
loading,
error,
completedToDos,
totalToDos,
searchValue,
setSearchValue,
searchedToDos,
completeToDo,
deleteToDo,
openModal,
setOpenModal,
addToDo,
}}>
{ children }
</ToDoContext.Provider>
)
}

export { ToDoContext, ToDoProvider }
Loading

0 comments on commit 1cd3682

Please sign in to comment.