mirror of
https://github.com/leporello-js/leporello-js
synced 2026-01-13 21:14:28 -08:00
redux example rename
This commit is contained in:
21
docs/examples/todos-redux/LICENSE.md
Normal file
21
docs/examples/todos-redux/LICENSE.md
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015-present Dan Abramov
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
2
docs/examples/todos-redux/README.md
Normal file
2
docs/examples/todos-redux/README.md
Normal file
@@ -0,0 +1,2 @@
|
||||
Code in this folder is borrowed from https://github.com/reduxjs/redux/tree/master/examples/todos
|
||||
See [LICENSE.md](./LICENSE.md) for license
|
||||
22
docs/examples/todos-redux/public/index.html
Normal file
22
docs/examples/todos-redux/public/index.html
Normal file
@@ -0,0 +1,22 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Redux Todos Example</title>
|
||||
|
||||
<script src='https://unpkg.com/react/umd/react.development.js'></script>
|
||||
<script src='https://unpkg.com/react-dom/umd/react-dom.development.js'></script>
|
||||
<script src='https://unpkg.com/redux'></script>
|
||||
<script src='https://unpkg.com/react-redux'></script>
|
||||
|
||||
<script type='module'>
|
||||
if(new URLSearchParams(window.location.search).get('leporello') == null) {
|
||||
await import('../src/index.js');
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
</body>
|
||||
</html>
|
||||
28
docs/examples/todos-redux/src/actions/index.js
Normal file
28
docs/examples/todos-redux/src/actions/index.js
Normal file
@@ -0,0 +1,28 @@
|
||||
const nextTodoId = new Function(`
|
||||
let nextId = 0
|
||||
return function() {
|
||||
return nextId++
|
||||
}
|
||||
`)()
|
||||
|
||||
export const addTodo = text => ({
|
||||
type: 'ADD_TODO',
|
||||
id: nextTodoId(),
|
||||
text
|
||||
})
|
||||
|
||||
export const setVisibilityFilter = filter => ({
|
||||
type: 'SET_VISIBILITY_FILTER',
|
||||
filter
|
||||
})
|
||||
|
||||
export const toggleTodo = id => ({
|
||||
type: 'TOGGLE_TODO',
|
||||
id
|
||||
})
|
||||
|
||||
export const VisibilityFilters = {
|
||||
SHOW_ALL: 'SHOW_ALL',
|
||||
SHOW_COMPLETED: 'SHOW_COMPLETED',
|
||||
SHOW_ACTIVE: 'SHOW_ACTIVE'
|
||||
}
|
||||
15
docs/examples/todos-redux/src/components/App.js
Normal file
15
docs/examples/todos-redux/src/components/App.js
Normal file
@@ -0,0 +1,15 @@
|
||||
import Footer from './Footer.js'
|
||||
import AddTodo from '../containers/AddTodo.js'
|
||||
import VisibleTodoList from '../containers/VisibleTodoList.js'
|
||||
|
||||
const h = React.createElement
|
||||
|
||||
const App = () => (
|
||||
h('div', null,
|
||||
h(AddTodo),
|
||||
h(VisibleTodoList),
|
||||
h(Footer),
|
||||
)
|
||||
)
|
||||
|
||||
export default App
|
||||
15
docs/examples/todos-redux/src/components/Footer.js
Normal file
15
docs/examples/todos-redux/src/components/Footer.js
Normal file
@@ -0,0 +1,15 @@
|
||||
import FilterLink from '../containers/FilterLink.js'
|
||||
import { VisibilityFilters } from '../actions/index.js'
|
||||
|
||||
const h = React.createElement
|
||||
|
||||
const Footer = () => (
|
||||
h('div', null,
|
||||
h('span', null, 'Show: '),
|
||||
h(FilterLink, {filter: VisibilityFilters.SHOW_ALL}, 'All'),
|
||||
h(FilterLink, {filter: VisibilityFilters.SHOW_ACTIVE}, 'Active'),
|
||||
h(FilterLink, {filter: VisibilityFilters.SHOW_COMPLETED}, 'Completed'),
|
||||
)
|
||||
)
|
||||
|
||||
export default Footer
|
||||
13
docs/examples/todos-redux/src/components/Link.js
Normal file
13
docs/examples/todos-redux/src/components/Link.js
Normal file
@@ -0,0 +1,13 @@
|
||||
const h = React.createElement
|
||||
|
||||
const Link = ({ active, children, onClick }) => (
|
||||
h('button', {
|
||||
onClick,
|
||||
disabled: active,
|
||||
style:{
|
||||
marginLeft: '4px',
|
||||
}
|
||||
}, children)
|
||||
)
|
||||
|
||||
export default Link
|
||||
12
docs/examples/todos-redux/src/components/Todo.js
Normal file
12
docs/examples/todos-redux/src/components/Todo.js
Normal file
@@ -0,0 +1,12 @@
|
||||
const h = React.createElement
|
||||
|
||||
const Todo = ({ onClick, completed, text }) => (
|
||||
h('li', {
|
||||
onClick,
|
||||
style: {
|
||||
textDecoration: completed ? 'line-through' : 'none'
|
||||
},
|
||||
}, text)
|
||||
)
|
||||
|
||||
export default Todo
|
||||
17
docs/examples/todos-redux/src/components/TodoList.js
Normal file
17
docs/examples/todos-redux/src/components/TodoList.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import Todo from './Todo.js'
|
||||
|
||||
const h = React.createElement
|
||||
|
||||
const TodoList = ({ todos, toggleTodo }) => (
|
||||
h('ul', null,
|
||||
todos.map(todo =>
|
||||
h(Todo, {
|
||||
key: todo.id,
|
||||
...todo,
|
||||
onClick: () => toggleTodo(todo.id),
|
||||
})
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
export default TodoList
|
||||
26
docs/examples/todos-redux/src/containers/AddTodo.js
Normal file
26
docs/examples/todos-redux/src/containers/AddTodo.js
Normal file
@@ -0,0 +1,26 @@
|
||||
import { addTodo } from '../actions/index.js'
|
||||
|
||||
const h = React.createElement
|
||||
|
||||
const AddTodo = ({ dispatch }) => {
|
||||
const inputref = {}
|
||||
|
||||
return (
|
||||
h('div', null,
|
||||
h('form', {
|
||||
onSubmit: e => {
|
||||
e.preventDefault()
|
||||
if (inputref.input.value.trim()) {
|
||||
dispatch(addTodo(inputref.input.value))
|
||||
Object.assign(inputref.input, {value: ''})
|
||||
}
|
||||
}
|
||||
},
|
||||
h('input', {ref: input => Object.assign(inputref, {input})}),
|
||||
h('button', {type: 'submit'}, 'Add Todo')
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
export default ReactRedux.connect()(AddTodo)
|
||||
15
docs/examples/todos-redux/src/containers/FilterLink.js
Normal file
15
docs/examples/todos-redux/src/containers/FilterLink.js
Normal file
@@ -0,0 +1,15 @@
|
||||
import { setVisibilityFilter } from '../actions/index.js'
|
||||
import Link from '../components/Link.js'
|
||||
|
||||
const mapStateToProps = (state, ownProps) => ({
|
||||
active: ownProps.filter === state.visibilityFilter
|
||||
})
|
||||
|
||||
const mapDispatchToProps = (dispatch, ownProps) => ({
|
||||
onClick: () => dispatch(setVisibilityFilter(ownProps.filter))
|
||||
})
|
||||
|
||||
export default ReactRedux.connect(
|
||||
mapStateToProps,
|
||||
mapDispatchToProps
|
||||
)(Link)
|
||||
28
docs/examples/todos-redux/src/containers/VisibleTodoList.js
Normal file
28
docs/examples/todos-redux/src/containers/VisibleTodoList.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import { toggleTodo } from '../actions/index.js'
|
||||
import TodoList from '../components/TodoList.js'
|
||||
import { VisibilityFilters } from '../actions/index.js'
|
||||
|
||||
const getVisibleTodos = (todos, filter) => {
|
||||
if(filter == VisibilityFilters.SHOW_ALL) {
|
||||
return todos
|
||||
} else if(filter == VisibilityFilters.SHOW_COMPLETED) {
|
||||
return todos.filter(t => t.completed)
|
||||
} else if(filter == VisibilityFilters.SHOW_ACTIVE) {
|
||||
return todos.filter(t => !t.completed)
|
||||
} else {
|
||||
throw new Error('Unknown filter: ' + filter)
|
||||
}
|
||||
}
|
||||
|
||||
const mapStateToProps = state => ({
|
||||
todos: getVisibleTodos(state.todos, state.visibilityFilter)
|
||||
})
|
||||
|
||||
const mapDispatchToProps = dispatch => ({
|
||||
toggleTodo: id => dispatch(toggleTodo(id))
|
||||
})
|
||||
|
||||
export default ReactRedux.connect(
|
||||
mapStateToProps,
|
||||
mapDispatchToProps
|
||||
)(TodoList)
|
||||
11
docs/examples/todos-redux/src/index.js
Normal file
11
docs/examples/todos-redux/src/index.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import App from './components/App.js'
|
||||
import rootReducer from './reducers/index.js'
|
||||
|
||||
const h = React.createElement
|
||||
|
||||
const store = Redux.createStore(rootReducer)
|
||||
|
||||
ReactDOM.render(
|
||||
h(ReactRedux.Provider, {store}, h(App)),
|
||||
document.getElementById('root')
|
||||
)
|
||||
7
docs/examples/todos-redux/src/reducers/index.js
Normal file
7
docs/examples/todos-redux/src/reducers/index.js
Normal file
@@ -0,0 +1,7 @@
|
||||
import todos from './todos.js'
|
||||
import visibilityFilter from './visibilityFilter.js'
|
||||
|
||||
export default Redux.combineReducers({
|
||||
todos,
|
||||
visibilityFilter
|
||||
})
|
||||
22
docs/examples/todos-redux/src/reducers/todos.js
Normal file
22
docs/examples/todos-redux/src/reducers/todos.js
Normal file
@@ -0,0 +1,22 @@
|
||||
const todos = (state = [], action) => {
|
||||
if(action.type == 'ADD_TODO') {
|
||||
return [
|
||||
...state,
|
||||
{
|
||||
id: action.id,
|
||||
text: action.text,
|
||||
completed: false
|
||||
}
|
||||
]
|
||||
} else if(action.type == 'TOGGLE_TODO') {
|
||||
return state.map(todo =>
|
||||
(todo.id === action.id)
|
||||
? {...todo, completed: !todo.completed}
|
||||
: todo
|
||||
)
|
||||
} else {
|
||||
return state
|
||||
}
|
||||
}
|
||||
|
||||
export default todos
|
||||
11
docs/examples/todos-redux/src/reducers/visibilityFilter.js
Normal file
11
docs/examples/todos-redux/src/reducers/visibilityFilter.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import { VisibilityFilters } from '../actions/index.js'
|
||||
|
||||
const visibilityFilter = (state = VisibilityFilters.SHOW_ALL, action) => {
|
||||
if(action.type == 'SET_VISIBILITY_FILTER') {
|
||||
return action.filter
|
||||
} else {
|
||||
return state
|
||||
}
|
||||
}
|
||||
|
||||
export default visibilityFilter
|
||||
Reference in New Issue
Block a user