-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from syncosaurus/todo-list
Todo List Interactive Component
- Loading branch information
Showing
9 changed files
with
248 additions
and
26 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { useState, useEffect } from 'react' | ||
import { TodoListClient } from './TodoListClient.js' | ||
import Signal from '../Puzzle/Signal.jsx' | ||
import SignalVertical from '../Puzzle/SignalVertical.jsx' | ||
|
||
export default function TodoList() { | ||
const [todoList, setTodoList] = useState([]) | ||
const [windowWidth, setWindowWidth] = useState() | ||
|
||
useEffect(() => { | ||
setWindowWidth(window.innerWidth) | ||
window.addEventListener('resize', e => setWindowWidth(e.target.innerWidth)) | ||
}, []) | ||
|
||
function handleSubmit(newTodo) { | ||
let newTodoList = [...todoList, newTodo] | ||
|
||
setTodoList(newTodoList) | ||
} | ||
|
||
function handleDelete(id) { | ||
let filteredTodoList = todoList.filter(todo => todo.id !== id) | ||
setTodoList(filteredTodoList) | ||
} | ||
|
||
const divStyle = { | ||
border: '40px solid', | ||
borderImage: 'url(' + '/landing/Client-window2.svg' + ') 25 25', | ||
} | ||
if (windowWidth < 1094) { | ||
return ( | ||
<div className="grid grid-cols-1 gap-0 justify-center"> | ||
<TodoListClient | ||
serverTodoList={todoList} | ||
onSubmit={handleSubmit} | ||
onDelete={handleDelete} | ||
latency={0} | ||
interactable={true} | ||
narrow={true} | ||
client={1} | ||
/> | ||
<SignalVertical /> | ||
<TodoListClient | ||
serverTodoList={todoList} | ||
onSubmit={handleSubmit} | ||
onDelete={handleDelete} | ||
latency={1000} | ||
interactable={false} | ||
narrow={true} | ||
client={2} | ||
/> | ||
</div> | ||
) | ||
} | ||
|
||
return ( | ||
<div className="flex"> | ||
<TodoListClient | ||
serverTodoList={todoList} | ||
onSubmit={handleSubmit} | ||
onDelete={handleDelete} | ||
latency={0} | ||
interactable={true} | ||
narrow={false} | ||
/> | ||
<Signal /> | ||
<TodoListClient | ||
serverTodoList={todoList} | ||
onSubmit={handleSubmit} | ||
onDelete={handleDelete} | ||
latency={1000} | ||
interactable={false} | ||
narrow={false} | ||
/> | ||
</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,130 @@ | ||
import { useEffect, useState } from 'react' | ||
import { v4 as uuidv4 } from 'uuid' | ||
import addIcon from './icon_add.png' | ||
import deleteIcon from './icon_delete.png' | ||
|
||
export function TodoListClient({ | ||
serverTodoList, | ||
onSubmit, | ||
onDelete, | ||
latency, | ||
interactable, | ||
narrow, | ||
}) { | ||
const [inputValue, setInputValue] = useState('') | ||
const [localTodoList, setLocalTodoList] = useState(serverTodoList) | ||
const [displayErrorMessage, setDisplayErrorMessage] = useState(false) | ||
|
||
useEffect(() => { | ||
setTimeout(() => { | ||
setLocalTodoList(serverTodoList) | ||
}, latency) | ||
}, [serverTodoList]) | ||
|
||
function handleChange(e) { | ||
setInputValue(e.target.value) | ||
} | ||
|
||
function handleSubmit(e) { | ||
e.preventDefault() | ||
if (inputValue === '') { | ||
setDisplayErrorMessage(true) | ||
setTimeout(() => { | ||
setDisplayErrorMessage(false) | ||
}, 3000) | ||
|
||
return | ||
} | ||
|
||
let newTodo = { | ||
id: uuidv4(), | ||
text: inputValue, | ||
} | ||
|
||
let newLocalTodoList = [...localTodoList, newTodo] | ||
|
||
setLocalTodoList(newLocalTodoList) | ||
onSubmit(newTodo) | ||
setInputValue('') | ||
} | ||
|
||
function handleDelete(id) { | ||
let filteredLocalTodoList = localTodoList.filter(todo => todo.id !== id) | ||
setLocalTodoList(filteredLocalTodoList) | ||
onDelete(id) | ||
} | ||
|
||
return ( | ||
<div | ||
className={ | ||
narrow | ||
? 'container inline-block w-[300px] m-0 p-0 min-h-80' | ||
: 'container inline-block w-[400px] m-0 p-0 min-h-80' | ||
} | ||
style={ | ||
!interactable | ||
? { ...divStyle, pointerEvents: 'none', opacity: '.5' } | ||
: divStyle | ||
} | ||
> | ||
<h3>{!interactable ? 'Todo List (Client 2)' : 'Todo List (Client 1)'}</h3> | ||
{displayErrorMessage ? ( | ||
<p style={errorStyling}>Must enter a value</p> | ||
) : ( | ||
<></> | ||
)} | ||
<form> | ||
<input | ||
type="text" | ||
placeholder="Enter new todo" | ||
value={inputValue} | ||
onChange={handleChange} | ||
/> | ||
<button style={addButtonStyle} onClick={handleSubmit}> | ||
<img src={addIcon} /> | ||
</button> | ||
</form> | ||
<ul style={listStyle}> | ||
{localTodoList.map(todo => ( | ||
<li key={todo.id}> | ||
{todo.text} | ||
<button | ||
style={deleteButtonStyle} | ||
onClick={() => handleDelete(todo.id)} | ||
> | ||
<img src={deleteIcon} /> | ||
</button> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
) | ||
} | ||
|
||
const divStyle = { | ||
border: '40px solid', | ||
borderImage: 'url(' + '/landing/Client-window2.svg' + ') 25 25', | ||
} | ||
|
||
const deleteButtonStyle = { | ||
backgroundColor: 'white', | ||
border: 'none', | ||
cursor: 'pointer', | ||
textAlign: 'right', | ||
} | ||
|
||
const addButtonStyle = { | ||
backgroundColor: 'white', | ||
border: 'none', | ||
cursor: 'pointer', | ||
verticalAlign: '-3px', | ||
} | ||
|
||
const listStyle = { | ||
listStyleType: 'none', | ||
paddingLeft: '0px', | ||
} | ||
|
||
const errorStyling = { | ||
color: 'red', | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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