Created
January 25, 2020 15:46
-
-
Save ac205/2f3c129cea7291c62da6c684a7fb7aa1 to your computer and use it in GitHub Desktop.
Shopping Filters
This file contains 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 from "react"; | |
import { connect } from "react-redux"; | |
import { | |
makeStyles, | |
Grid, | |
Typography, | |
FormControlLabel, | |
Checkbox | |
} from "@material-ui/core"; | |
import Rating from "@material-ui/lab/Rating"; | |
const useStyles = makeStyles(theme => ({ | |
root: { | |
flexGrow: 1, | |
padding: theme.spacing(2) | |
}, | |
paper: { | |
padding: theme.spacing(2), | |
textAlign: "center", | |
color: theme.palette.text.secondary | |
} | |
})); | |
const RatingFilter = ({ filterProducts }) => { | |
const classes = useStyles(); | |
const [selected, setSelected] = React.useState({ | |
5: false, | |
4: false, | |
3 : false, | |
2 : false, | |
1 : false | |
}); | |
const handleChange = event => { | |
let rating = event.currentTarget.value | |
setSelected({...selected, [rating]: !selected[rating]}) | |
filterProducts(Number(rating)); | |
}; | |
return ( | |
<div className={classes.root}> | |
<Grid container spacing={3} direction="column" alignItems="flex-start"> | |
<Typography variant="button">Rating</Typography> | |
<FormControlLabel | |
control={ | |
<Checkbox | |
checked={selected.FiveStar} | |
onChange={handleChange} | |
value={5} | |
color="primary" | |
/> | |
} | |
label={<Rating name="5 Stars" value={5} readOnly />} | |
/> | |
<FormControlLabel | |
control={ | |
<Checkbox | |
checked={selected.FourStar} | |
onChange={handleChange} | |
value={4} | |
color="primary" | |
/> | |
} | |
label={<Rating name="4 Stars" value={4} readOnly />} | |
/> | |
<FormControlLabel | |
control={ | |
<Checkbox | |
checked={selected.ThreeStar} | |
onChange={handleChange} | |
value={3} | |
color="primary" | |
/> | |
} | |
label={<Rating name="3 Stars" value={3} readOnly />} | |
/> | |
<FormControlLabel | |
control={ | |
<Checkbox | |
checked={selected.TwoStar} | |
onChange={handleChange} | |
value={2} | |
color="primary" | |
/> | |
} | |
label={<Rating name="2 Stars" value={2} readOnly />} | |
/> | |
<FormControlLabel | |
control={ | |
<Checkbox | |
checked={selected.OneStar} | |
onChange={handleChange} | |
value={1} | |
color="primary" | |
/> | |
} | |
label={<Rating name="1 Star" value={1} readOnly />} | |
/> | |
</Grid> | |
</div> | |
); | |
}; | |
const mapDispatchToProps = dispatch => { | |
return { | |
filterProducts: filter => { | |
dispatch({ type: "ADD_RATING_FILTER", payload: filter }); | |
} | |
}; | |
}; | |
export default connect(null, mapDispatchToProps)(RatingFilter); |
This file contains 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
const productsList = [ | |
{ | |
id: 0, | |
productCode: "TS1000", | |
name: "Sample T-Shirt", | |
subname: "High Quality Shirt", | |
category: "Shirts", | |
description: | |
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut vel odio vitae dolor condimentum efficitur id ullamcorper massa. Proin eleifend euismod justo sed dictum. Aliquam luctus, justo eget hendrerit fringilla, dolor magna rhoncus ante, et tincidunt neque odio a arcu. Quisque accumsan pellentesque risus ut blandit", | |
price: "25.99", | |
salePrice: undefined, | |
img: [ | |
"https://img.represent.com/uploads/8590b79f04a01bbdfd6fa32311382a2a.png?auto=format&w=750", | |
"https://img.represent.com/uploads/8590b79f04a01bbdfd6fa32311382a2a.png?auto=format&w=750", | |
"https://img.represent.com/uploads/8590b79f04a01bbdfd6fa32311382a2a.png?auto=format&w=750", | |
"https://img.represent.com/uploads/8590b79f04a01bbdfd6fa32311382a2a.png?auto=format&w=750" | |
], | |
tags: ["tshirt", "gray"], | |
rating: 4, | |
available: false, | |
count: 1 | |
}, | |
{ | |
id: 1, | |
productCode: "TS1001", | |
name: "Sample T-Shirt 2", | |
subname: "Medium Quality Shirt", | |
category: "Shirts", | |
description: | |
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut vel odio vitae dolor condimentum efficitur id ullamcorper massa. Proin eleifend euismod justo sed dictum. Aliquam luctus, justo eget hendrerit fringilla, dolor magna rhoncus ante, et tincidunt neque odio a arcu. Quisque accumsan pellentesque risus ut blandit", | |
price: "25.99", | |
salePrice: "21.99", | |
img: [ | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false", | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false", | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false", | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false" | |
], | |
tags: ["tshirt", "white"], | |
rating: 5, | |
available: true, | |
count: 1 | |
}, | |
{ | |
id: 2, | |
productCode: "TS1001", | |
name: "Sample T-Shirt 3", | |
subname: "Medium Quality Shirt", | |
category: "Shirts", | |
description: | |
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut vel odio vitae dolor condimentum efficitur id ullamcorper massa. Proin eleifend euismod justo sed dictum. Aliquam luctus, justo eget hendrerit fringilla, dolor magna rhoncus ante, et tincidunt neque odio a arcu. Quisque accumsan pellentesque risus ut blandit", | |
price: "29.99", | |
salePrice: "21.99", | |
img: [ | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false", | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false", | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false", | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false" | |
], | |
tags: ["tshirt", "white"], | |
rating: 1, | |
available: true, | |
count: 1 | |
}, | |
{ | |
id: 3, | |
productCode: "TS1001", | |
name: "Sample T-Shirt 4", | |
subname: "Medium Quality Shirt", | |
category: "Shirts", | |
description: | |
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut vel odio vitae dolor condimentum efficitur id ullamcorper massa. Proin eleifend euismod justo sed dictum. Aliquam luctus, justo eget hendrerit fringilla, dolor magna rhoncus ante, et tincidunt neque odio a arcu. Quisque accumsan pellentesque risus ut blandit", | |
price: "25.99", | |
salePrice: "", | |
img: [ | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false", | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false", | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false", | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false" | |
], | |
tags: ["tshirt", "white"], | |
rating: 3, | |
available: true, | |
count: 1 | |
}, | |
{ | |
id: 4, | |
productCode: "TS1001", | |
name: "Sample T-Shirt 5", | |
subname: "Medium Quality Shirt", | |
category: "Shirts", | |
description: | |
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut vel odio vitae dolor condimentum efficitur id ullamcorper massa. Proin eleifend euismod justo sed dictum. Aliquam luctus, justo eget hendrerit fringilla, dolor magna rhoncus ante, et tincidunt neque odio a arcu. Quisque accumsan pellentesque risus ut blandit", | |
price: "25.99", | |
salePrice: "", | |
img: [ | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false", | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false", | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false", | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false" | |
], | |
tags: ["tshirt", "white"], | |
rating: 4, | |
available: true, | |
count: 1 | |
}, | |
{ | |
id: 5, | |
productCode: "TS1001", | |
name: "Sample T-Shirt 6", | |
subname: "Medium Quality Shirt", | |
category: "Shirts", | |
description: | |
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut vel odio vitae dolor condimentum efficitur id ullamcorper massa. Proin eleifend euismod justo sed dictum. Aliquam luctus, justo eget hendrerit fringilla, dolor magna rhoncus ante, et tincidunt neque odio a arcu. Quisque accumsan pellentesque risus ut blandit", | |
price: "25.99", | |
salePrice: "12.99", | |
img: [ | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false", | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false", | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false", | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false" | |
], | |
tags: ["tshirt", "white"], | |
rating: 4, | |
available: true, | |
count: 1 | |
}, | |
{ | |
id: 6, | |
productCode: "TS1001", | |
name: "Sample T-Shirt 7", | |
subname: "Medium Quality Shirt", | |
category: "Shirts", | |
description: | |
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut vel odio vitae dolor condimentum efficitur id ullamcorper massa. Proin eleifend euismod justo sed dictum. Aliquam luctus, justo eget hendrerit fringilla, dolor magna rhoncus ante, et tincidunt neque odio a arcu. Quisque accumsan pellentesque risus ut blandit", | |
price: "45.99", | |
salePrice: "", | |
img: [ | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false", | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false", | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false", | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false" | |
], | |
tags: ["tshirt", "white"], | |
rating: 3, | |
available: true, | |
count: 1 | |
}, | |
{ | |
id: 7, | |
productCode: "TS1001", | |
name: "Sample T-Shirt 8", | |
subname: "Medium Quality Shirt", | |
category: "Shirts", | |
description: | |
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut vel odio vitae dolor condimentum efficitur id ullamcorper massa. Proin eleifend euismod justo sed dictum. Aliquam luctus, justo eget hendrerit fringilla, dolor magna rhoncus ante, et tincidunt neque odio a arcu. Quisque accumsan pellentesque risus ut blandit", | |
price: "29.99", | |
salePrice: "21.99", | |
img: [ | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false", | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false", | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false", | |
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false" | |
], | |
tags: ["tshirt", "white"], | |
rating: 5, | |
available: true, | |
count: 1 | |
} | |
]; | |
const shopReducer = ( | |
state = { | |
products: productsList, | |
filteredProducts: null | |
}, | |
action | |
) => { | |
switch (action.type) { | |
case "ADD_PRODUCT": | |
console.log("test"); | |
break; | |
case "ADD_RATING_FILTER": | |
console.log(action.payload) | |
let newFilter = state.products.filter(product => { | |
return product.rating === action.payload; | |
}); | |
state = { | |
...state, | |
filteredProducts: newFilter | |
}; | |
break; | |
default: | |
} | |
return state; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment