Created
January 3, 2023 14:42
-
-
Save deleteman/6b18704f38228cd85b213fb70ca8070d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import React, { useState } from 'react'; | |
import Sentiment from 'sentiment'; | |
import 'bootstrap/dist/css/bootstrap.min.css'; | |
const sentiment = new Sentiment(); | |
function TodoApp() { | |
const [todos, setTodos] = useState([]); | |
const [input, setInput] = useState(''); | |
function handleChange(event) { | |
setInput(event.target.value); | |
} | |
function handleSubmit(event) { | |
event.preventDefault(); | |
const sentimentResult = sentiment.analyze(input); | |
const todo = { task: input, sentiment: sentimentResult }; | |
setTodos([...todos, todo]); | |
setInput(''); | |
} | |
function getSentimentString(score) { | |
if (score > 0) { | |
return 'positive'; | |
} else if (score < 0) { | |
return 'negative'; | |
} else { | |
return 'neutral'; | |
} | |
} | |
return ( | |
<div className="container mt-5"> | |
<form onSubmit={handleSubmit}> | |
<div className="form-group"> | |
<label htmlFor="todo-input">To-do:</label> | |
<input | |
type="text" | |
className="form-control" | |
id="todo-input" | |
value={input} | |
onChange={handleChange} | |
/> | |
</div> | |
<button type="submit" className="btn btn-primary"> | |
Add | |
</button> | |
</form> | |
<ul className="list-group mt-3"> | |
{todos.map((todo, index) => ( | |
<li | |
key={index} | |
className={`list-group-item d-flex justify-content-between align-items-center ${ | |
todo.sentiment.score > 0 | |
? 'bg-success text-white' | |
: todo.sentiment.score < 0 | |
? 'bg-danger text-white' | |
: '' | |
}`} | |
> | |
{todo.task} | |
<span className={`badge badge-pill ${ | |
todo.sentiment.score === 0 ? 'bg-secondary' : '' | |
}`}> | |
{getSentimentString(todo.sentiment.score)} | |
</span> | |
</li> | |
))} | |
</ul> | |
</div> | |
); | |
} | |
export default TodoApp; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment