Skip to content

Instantly share code, notes, and snippets.

@cford-14
Created April 8, 2020 19:12
Show Gist options
  • Save cford-14/e4c93be84c137952076d4b1620a8d5c1 to your computer and use it in GitHub Desktop.
Save cford-14/e4c93be84c137952076d4b1620a8d5c1 to your computer and use it in GitHub Desktop.
List App connection errors in ListList component
import React from 'react';
import ReactDOM from 'react-dom';
import { Route, Link, BrowserRouter as Router, Switch } from 'react-router-dom';
import { Provider } from 'react-redux';
import { store } from './REDUX/store';
import './index.css';
import App from './App';
import { Manage } from './components/Delta/Manage';
import { NewList } from './components/Delta/NewList';
import { Share } from './components/Delta/Share';
import { Banner } from './components/Banner';
import { AddTo } from './components/AddTo';
import ListList from './components/ListList';
import * as serviceWorker from './serviceWorker';
const routing = (
<Provider store={store}>
<Router>
<div className="App">
<Banner />
<div className="nav">
<Link to="/" className="menuOption"><h3>New List</h3></Link>
<Link to="/manage" className="menuOption"><h3>Manage</h3></Link>
<Link to="/share" className="menuOption"><h3>Share Lists</h3></Link>
</div>
<ListList /> {/*wrap export in withRouter*/}
<div class="functionBox">
<div class="functionBoxGrid">
<AddTo />
<Switch className="Delta">
<Route exact path="/" component={NewList} />
<Route path="/manage" component={Manage} />
<Route path="/share" component={Share} />
</Switch>
</div>
</div>
</div>
</Router>
</Provider>
);
ReactDOM.render(routing, document.getElementById('root'));
import React from 'react';
import { connect } from 'react-redux';
//import PropTypes from 'prop-types'
import '../App.css';
class ListList extends React.Component {
constructor(props) {
super(props);
}
render() {
let allShops = this.props.masterList.map(listItem => listItem.stores); //generates error
let shops = [];
for (let itemList of allShops) {
for (let shop of itemList) {
if (shops.indexOf(shop) === -1) {
shops.push(shop);
}
}
};
return(
<div className="listBox">
<div className="listBoxTitle">
<h3>Shopping <br/> Lists</h3>
</div>
{shops.map(shop =>
<div className="listButton">
<p>{shop}</p>
</div>
)}
</div>
)
}
};
const mapStateToProps = (state) => {
return {
masterList: state.masterList //generates error
}
}
export default connect(mapStateToProps)(ListList)
import { constants } from './actionTypes';
//list actions
let nextId = 10001;
export const addItem = (itemName, quantity, units, notes, stores) => {
//payload = itemName, quantity, units, notes, stores
return {
type: constants.ADD_ITEM,
id: nextId++,
checked: false,
itemName,
quantity,
units,
notes,
stores //array
}
};
export const toggleCheck = id => {
return {
type: constants.TOGGLE_CHECK,
id
}
};
export const removeChecked = () => {
return {
type: constants.REMOVE_CHECKED,
}
};
export const deleteList = shop => {
return {
type: constants.DELETE_LIST,
shop
}
};
import { constants } from './actionTypes';
export const reducer = (state, action) => {
switch (action.type) {
case constants.ADD_ITEM:
return Object.assign({}, state, {
masterList: [
...state.masterList,
{
id: action.id,
checked: false,
item: action.item,
quantity: action.quantity,
units: action.units,
notes: action.notes,
stores: action.stores
}
]
})
case constants.TOGGLE_CHECK:
return Object.assign({}, state, {
masterList: state.masterList.map((listItem) => {
return listItem.id === action.id ?
Object.assign({}, {checked: !listItem.checked}) : listItem
})
})
case constants.REMOVE_CHECKED:
return Object.assign({}, state, {
masterList: state.masterList.filter((listItem)=> {
return listItem.checked === true
})
})
case constants.DELETE_LIST:
return Object.assign({}, state, {
masterList: state.masterlist.map((listItem)=> {
listItem.stores.filter(shop => {
return shop !== action.shop
})
return listItem.stores.length > 0
})
})
}
}
import { createStore } from 'redux';
import { reducer } from './reducers';
import { InitialState } from './initialState';
export const store = createStore(
reducer,
InitialState)
const InitialState = {
masterList: [
{
"id": 10000,
"checked": false,
"item": "item name",
"quantity": 1,
"units": "boxes",
"notes": "always optional",
"stores": ["Your", "Stores", "Here"]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment