Skip to content

Instantly share code, notes, and snippets.

@spinda
Last active December 30, 2022 05:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spinda/5b6f3ae7fbda52cc68aaf862b25cf5e3 to your computer and use it in GitHub Desktop.
Save spinda/5b6f3ae7fbda52cc68aaf862b25cf5e3 to your computer and use it in GitHub Desktop.
React/Redux ChatGPT experiment - frontend generation

Give me JavaScript code implementing a React frontend for the app.

import React from 'react';
import { connect } from 'react-redux';
const TodoList = ({ todos, visibilityFilter, dispatch }) => {
let visibleTodos;
switch (visibilityFilter) {
case 'show_all':
visibleTodos = todos;
break;
case 'show_active':
visibleTodos = todos.filter(todo => !todo.completed);
break;
case 'show_completed':
visibleTodos = todos.filter(todo => todo.completed);
break;
default:
visibleTodos = todos;
}
return (
<div>
<ul>
{visibleTodos.map(todo => (
<li key={todo.id}>
{todo.text}
<button
onClick={() =>
dispatch({ type: 'COMPLETE_TODO', id: todo.id })
}
>
Complete
</button>
</li>
))}
</ul>
<button
onClick={() =>
dispatch({ type: 'ADD_TODO', text: 'New todo' })
}
>
Add Todo
</button>
<button onClick={() => dispatch({ type: 'CLEAR_COMPLETED' })}>
Clear Completed
</button>
<div>
<span>Show: </span>
<button
onClick={() =>
dispatch({ type: 'SET_VISIBILITY_FILTER', filter: 'show_all' })
}
>
All
</button>
<button
onClick={() =>
dispatch({ type: 'SET_VISIBILITY_FILTER', filter: 'show_active' })
}
>
Active
</button>
<button
onClick={() =>
dispatch({
type: 'SET_VISIBILITY_FILTER',
filter: 'show_completed'
})
}
>
Completed
</button>
</div>
</div>
);
};
const mapStateToProps = state => {
return {
todos: state.todos,
visibilityFilter: state.visibilityFilter
};
};
export default connect(mapStateToProps)(TodoList);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment