Skip to content

Commit 97b0f66

Browse files
added nested routing for individual categories, added custom collection selector in shop selectors
1 parent 6979a36 commit 97b0f66

File tree

9 files changed

+98
-25
lines changed

9 files changed

+98
-25
lines changed

src/App.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ class App extends Component {
4747
<Header />
4848
<Switch>
4949
<Route exact path='/' component={Homepage} />
50-
<Route exact path='/shop' component={ShopPage} />
51-
<Route exact path='/checkout' component={CheckoutPage} />
52-
<Route exact path='/signin' render={() => this.props.currentUser ? (<Redirect to='/' />) : <SignInAndSignUpPage />} />
50+
<Route path='/shop' component={ShopPage} />
51+
<Route path='/checkout' component={CheckoutPage} />
52+
<Route path='/signin' render={() => this.props.currentUser ? (<Redirect to='/' />) : <SignInAndSignUpPage />} />
5353
</Switch>
5454
</div>;
5555
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react'
2+
import { connect } from 'react-redux'
3+
import { selectCollection } from "../../redux/shop/shop.selector";
4+
import CollectionItem from '../../components/collection-item/collection-item.component'
5+
import './collection.styles.scss'
6+
7+
function CollectionPage({ match, collection }) {
8+
console.log(collection)
9+
return (
10+
<div className="collectionPage">
11+
<h2>CategoryPage</h2>
12+
</div>
13+
)
14+
}
15+
16+
const mapStateToProps = (state, ownProps) => ({
17+
collection: selectCollection(ownProps.match.params.collectionId)(state)
18+
})
19+
20+
export default connect(mapStateToProps)(CollectionPage)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.collection-page {
2+
display: flex;
3+
flex-direction: column;
4+
5+
.title {
6+
font-size: 38px;
7+
margin: 0 auto 30px;
8+
}
9+
10+
.items {
11+
display: grid;
12+
grid-template-columns: 1fr 1fr 1fr 1fr;
13+
grid-gap: 10px;
14+
15+
& .collection-item {
16+
margin-bottom: 30px;
17+
}
18+
}
19+
}

src/Pages/shop/Shop.component.jsx

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
1-
import React, { Component } from 'react'
2-
import { connect } from 'react-redux'
3-
import { selectCollections } from "../../redux/shop/shop.selector";
4-
import { createStructuredSelector } from "reselect";
5-
import CollectionPreview from '../../components/collection-preview/Collection-preview.component'
1+
import React from 'react'
2+
import { Route } from "react-router-dom";
3+
import CollectionPage from '../collection/collection.component'
64

5+
import CollectionOverview from '../../components/collections-overview/collections-overview.component'
76

87

9-
const Shop = ({ collections }) => {
10-
return (<div className="shop-page">
11-
{
12-
collections.map(({ id, ...otherCollectionProps }) => (
13-
<CollectionPreview key={id} {...otherCollectionProps} />
14-
))
15-
}
16-
</div>)
8+
const Shop = ({ match }) => {
9+
console.log(match)
10+
return (
11+
<div className="shop-page">
12+
<Route exact path={`${match.path}`} component={CollectionOverview} />
13+
<Route path={`${match.path}/:collectionId`} component={CollectionPage} />
14+
</div>)
1715
}
1816

19-
const mapStatetoProps = createStructuredSelector({
20-
collections: selectCollections
21-
})
2217

23-
24-
export default connect(mapStatetoProps)(Shop)
18+
export default Shop

src/components/collection-preview/Collection-preview.component.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default function CollectionPreview({ title, items }) {
99
{
1010
items
1111
.filter((item, idx) => idx < 4)
12-
.map( item => (
12+
.map(item => (
1313
<CollectionItem key={item.id} item={item} />
1414
))
1515
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from 'react'
2+
import { connect } from "react-redux";
3+
import { createStructuredSelector } from "reselect";
4+
import './collections-overview.styles.scss'
5+
import { selectCollections } from "../../redux/shop/shop.selector";
6+
import CollectionPreview from "../collection-preview/Collection-preview.component";
7+
8+
function CollectionsOverview({ collections }) {
9+
return (
10+
<div className="collection-overview">
11+
{
12+
collections.map(({ id, ...otherCollectionProps }) => (
13+
<CollectionPreview key={id} {...otherCollectionProps} />
14+
))
15+
}
16+
</div>
17+
)
18+
}
19+
20+
21+
const mapStatetoProps = createStructuredSelector({
22+
collections: selectCollections
23+
})
24+
export default connect(mapStatetoProps)(CollectionsOverview)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.collections-overview {
2+
display: flex;
3+
flex-direction: column;
4+
}

src/components/directory/Directory.component.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component } from 'react'
1+
import React from 'react'
22
import MenuItem from '../menu-item/Menu-item.component'
33
import { connect } from 'react-redux'
44
import { createStructuredSelector } from 'reselect'

src/redux/shop/shop.selector.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,20 @@ import { createSelector } from "reselect";
22

33
const selectShop = state => state.shop
44

5-
5+
const COLLECTION_ID_MAP = {
6+
hats: 1,
7+
sneakers: 2,
8+
jackets: 3,
9+
womens: 4,
10+
mens: 5
11+
}
612
export const selectCollections = createSelector(
713
[selectShop],
814
shop => shop.collections
9-
)
15+
)
16+
17+
export const selectCollection = collectionUrlParam =>
18+
createSelector(
19+
[selectCollections],
20+
collections => collections.find(collection => collection.id === COLLECTION_ID_MAP[collectionUrlParam])
21+
)

0 commit comments

Comments
 (0)