Skip to content
This repository was archived by the owner on Sep 23, 2022. It is now read-only.

Commit

Permalink
Implement with redux
Browse files Browse the repository at this point in the history
  • Loading branch information
DavraYoung committed Feb 25, 2019
1 parent 6901726 commit 2e6dad7
Show file tree
Hide file tree
Showing 11 changed files with 288 additions and 146 deletions.
279 changes: 155 additions & 124 deletions .idea/workspace.xml

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"react": "^16.8.2",
"react-bootstrap": "^1.0.0-beta.5",
"react-dom": "^16.8.2",
"react-scripts": "2.1.5"
"react-redux": "^6.0.1",
"react-scripts": "2.1.5",
"redux": "^4.0.1"
},
"scripts": {
"start": "react-scripts start",
Expand Down
13 changes: 13 additions & 0 deletions src/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {FILTER_TEXT_CHANGED} from './constants'



const filterTextChanged=(input)=>{
return {
type: FILTER_TEXT_CHANGED,
payload: input
}
}
export {
filterTextChanged
}
12 changes: 6 additions & 6 deletions src/components/App/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,16 @@ class App extends React.Component {
})
};

onSearchStrChanged=(searchStr)=> {
this.setState({searchStr});
}

render() {
let onSearchStrChanged= this.onSearchStrChanged;
return (
<Container className>
<Container className onClick={() => this.setState(() => {
return {
toggler: this.state.toggler || true
}
})}>
<AppHeader className='my-3'/>
<ControlPanel {...{onSearchStrChanged}}/>
<ControlPanel />
<TodoList items={this.state.items}
searchStr={this.state.searchStr}
className='mt-4'
Expand Down
13 changes: 8 additions & 5 deletions src/components/ControlPanel/ControlPanel.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import React from 'react'
import {InputGroup, Button,} from "react-bootstrap";
import SearchPanel from "../SearchPanel";
import FilterButtons from "../FilterButtons";
import FilterButtons from '../FilterButtons';


const ControlPanel = ({className,onSearchStrChanged}) => {

const ControlPanel = ({className}) => {
return (
<InputGroup {...{className}}>
<SearchPanel {...{onSearchStrChanged}}/>
<InputGroup className={className}>
<SearchPanel/>
<FilterButtons/>
</InputGroup>

)
};
export default ControlPanel;


export default (ControlPanel);
20 changes: 17 additions & 3 deletions src/components/SearchPanel/SearchPanel.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
import React from 'react'
import {FormControl} from "react-bootstrap";
import {filterTextChanged} from '../../actions'
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';

const mapStateToProps = (state) => {
return {
filterText: state.filterText
}
};

const mapDispatchToProps = (dispatch) => {
return bindActionCreators({filterTextChanged}, dispatch);
};

const SearchPanel = ({onSearchStrChanged}) => {
const SearchPanel = ({filterTextChanged, filterText}) => {
console.log(filterText);
return (
<FormControl
onChange={(e) => onSearchStrChanged(e.target.value)}
onChange={(e) => filterTextChanged(e.target.value)}
placeholder="Search"
aria-label="Search for words"
aria-describedby="basic-addon2"
/>
);
};

export default SearchPanel;

export default connect(mapStateToProps, mapDispatchToProps)(SearchPanel);
15 changes: 10 additions & 5 deletions src/components/TodoList/TodoList.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ import './TodoList.css'
class TodoList extends React.Component {

render() {
const {items, className, onDeleted, onMarkImportant, onDone, filterFn, searchStr} = this.props;
console.log(searchStr);
const {items,
className,
onDeleted,
onMarkImportant,
onDone,
filterFn} = this.props;


const elements = items
.filter(filterFn)
.filter(item => item.label
Expand All @@ -18,9 +24,8 @@ class TodoList extends React.Component {
.replace(' ', '')))
.map(
item => {
const {label} = item;
return (
<ListGroup.Item key={label} style={{padding: '0.5rem 1rem'}}>
<ListGroup.Item key={item.label} style={{padding: '0.5rem 1rem'}}>
<TodoListItem
{...item}
onDeleted={() => onDeleted(label)}
Expand All @@ -31,7 +36,7 @@ class TodoList extends React.Component {
)
});

return <ListGroup {...{className}}>{elements}</ListGroup>;
return <ListGroup className={className}>{elements}</ListGroup>;
}
}

Expand Down
13 changes: 12 additions & 1 deletion src/components/TodoListItem/TodoListItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,25 @@ class TodoListItem extends Component {


render() {
let {label, important = false, done = false, onDeleted, onMarkImportant, onDone} = this.props;
let {
label,
important = false,
done = false,
onDeleted,
onMarkImportant,
onDone
} = this.props;

let className = 'mr-auto my-auto';

if (done)
className = className + ' done'

let style = {
fontWeight: (important ? 600 : 300),
color: (important ? 'blue' : 'black')
};

//console.log(style);
return (
<div className="d-flex">
Expand Down
7 changes: 7 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@



const FILTER_TEXT_CHANGED='FILTER_TEXT_CHANGED';
export {
FILTER_TEXT_CHANGED
}
23 changes: 22 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@ import React from 'react'
import ReactDOM from 'react-dom'
import 'bootstrap/dist/css/bootstrap.min.css'
import App from './components/App'
import {createStore} from 'redux'
import {Provider} from 'react-redux'
import {FILTER_TEXT_CHANGED} from './constants'

const globalReducer = (state, action) => {
console.log(action.payload)
switch (action) {
case FILTER_TEXT_CHANGED:
return {filterText: action.payload}
default:
return state;
}
};

ReactDOM.render(<App/>, document.getElementById('root'));
const initialState = {
filterText: '',
};

const store = createStore(globalReducer,initialState);
ReactDOM.render(
<Provider store={store}>
<App/>
</Provider>
, document.getElementById('root'));

0 comments on commit 2e6dad7

Please sign in to comment.