Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
c2cb824
Adds signIn, signOut, signUp, and addToCart to SessionProvider.
markbrocato Jun 15, 2020
086bd44
updated based on feedback
markbrocato Jun 16, 2020
2bca1ba
more feedback
markbrocato Jun 16, 2020
3ad245d
fix addToCart `others` prop
Jun 16, 2020
7093cf0
Fix syntax error.
markbrocato Jun 16, 2020
9bec1bd
Merge branch 'enhance-session-provider' of github.com:react-storefron…
markbrocato Jun 16, 2020
cf437b7
SessionProvider: initial state
Jun 16, 2020
8e264ec
useCartTotal helper hook
Jun 16, 2020
63b87b9
Merge branch 'enhance-session-provider' of github.com:react-storefron…
Jun 16, 2020
10cb070
SessionProvider: add to cart
Jun 17, 2020
e3670b9
connector(s): sign in
Jun 17, 2020
d278e62
SessionProvider: sign out
Jun 17, 2020
af9248c
SessionProvider: signUp
Jun 17, 2020
b12a98b
Merge remote-tracking branch 'origin/master' into enhance-session-pro…
markbrocato Jun 22, 2020
b79b5d7
Image: prevent render "onerror" as a DOM prop in case of AMP
Jun 25, 2020
921e629
Add tests
markbrocato Jun 23, 2020
2f7a1c3
SessionProvider actions: checking if responses are ok
Jul 8, 2020
813c4c5
merged latest from master
markbrocato Jul 10, 2020
0c4c7b5
Merge branch 'enhance-session-provider' of github.com:react-storefron…
markbrocato Jul 10, 2020
595760f
More fixes
markbrocato Jul 13, 2020
682779b
test coverage
markbrocato Jul 13, 2020
c42e304
update/remove cart
Jul 15, 2020
19e6174
tests
markbrocato Jul 16, 2020
23bd661
Formatting.
markbrocato Jul 16, 2020
6e41a36
bump version
markbrocato Jul 16, 2020
9d939d3
Update mock-connector to match interface.
markbrocato Jul 16, 2020
b5c16a4
type
markbrocato Jul 16, 2020
a5df5db
bump version.
markbrocato Jul 16, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
760 changes: 379 additions & 381 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-storefront",
"version": "8.12.2",
"version": "8.14.0-alpha.0",
"description": "Build and deploy e-commerce progressive web apps (PWAs) in record time.",
"module": "./index.js",
"license": "Apache-2.0",
Expand Down Expand Up @@ -78,7 +78,7 @@
"enzyme-adapter-react-16": "^1.15.1",
"jest": "^25.4.0",
"jest-enzyme": "^7.1.2",
"jest-fetch-mock": "^2.1.2",
"jest-fetch-mock": "^3.0.3",
"jsdom": "^16.2.2",
"next": "^9.4.4",
"npm-watch": "^0.6.0",
Expand Down
7 changes: 6 additions & 1 deletion src/Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ export default function Image({
[classes.fit]: aspectRatio != null,
})}
{...imgAttributes}
onError={() => setPrimaryNotFound(true)}
// prevent render "onerror" as a DOM prop in case of amp-img tag
onError={
ImgElement !== 'amp-img'
? () => setPrimaryNotFound(true)
: /* istanbul ignore next */ undefined
}
/>
)}
</div>
Expand Down
3 changes: 3 additions & 0 deletions src/LazyHydrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ if (isBrowser()) {
will be removed.
*/
export function LazyStyles() {
/* istanbul ignore next */
if (useAmp()) return null

let styles = null
Expand Down Expand Up @@ -212,7 +213,9 @@ function LazyHydrateInstance({ id, className, ssrOnly, children, on, ...props })
*/

function LazyHydrate({ children, ...props }) {
/* istanbul ignore next */
if (useAmp()) return children

return (
<LazyHydrateInstance {...props}>
{/* LazyStylesProvider should not be used in the browser. Once components
Expand Down
14 changes: 14 additions & 0 deletions src/hooks/useCartTotal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useContext, useMemo } from 'react';
import get from 'lodash/get';
import SessionContext from '../session/SessionContext';

function useCartTotal() {
const context = useContext(SessionContext);
const items = get(context, 'session.cart.items', []);
const total = useMemo(() => items
.reduce((totalAcc, item) => item.quantity + totalAcc, 0),
[items]);
return total;
}

export default useCartTotal;
19 changes: 19 additions & 0 deletions src/mock-connector/account.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import fulfillAPIRequest from '..//props/fulfillAPIRequest'
import createAppData from './utils/createAppData'

export default async function account(req, res) {
return await fulfillAPIRequest(req, {
appData: createAppData,
pageData: () =>
Promise.resolve({
title: 'My Account',
account: {},
breadcrumbs: [
{
text: 'Home',
href: '/',
},
],
}),
})
}
10 changes: 10 additions & 0 deletions src/mock-connector/addToCart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default async function addToCart(product, quantity, req, res) {
const { id, color, size } = product

console.log('product id: ', id)
console.log('color id: ', color || 'Not provided')
console.log('size id: ', size || 'Not provided')
console.log('quantity ', quantity)

return {}
}
54 changes: 22 additions & 32 deletions src/mock-connector/cart.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,27 @@
import createProduct from './utils/createProduct'
import withAmpFormParser from '../middlewares/withAmpFormParser'
import fulfillAPIRequest from '../props/fulfillAPIRequest'
import createAppData from './utils/createAppData'

const cart = (req, res) => {
if (req.method === 'POST') {
const { id, color, size, quantity } = req.body
export default async function cart(req, res) {
const products = [createProduct(1), createProduct(2), createProduct(3)]

console.log('product id: ', id)
console.log('color id: ', color || 'Not provided')
console.log('size id: ', size || 'Not provided')
console.log('quantity ', quantity)

if (req.query['__amp_source_origin']) {
res.setHeader('AMP-Redirect-To', `${req.query['__amp_source_origin']}/cart`)
}

setTimeout(() => {
res.end(JSON.stringify({ response: 'success', cartCount: 4 }))
}, 1)
} else {
const products = [createProduct(1), createProduct(2), createProduct(3)]

res.end(
JSON.stringify({
pageData: { items: products.map((item, i) => ({ ...item, quantity: i + 1 })) },
return fulfillAPIRequest(req, {
appData: createAppData,
pageData: () =>
Promise.resolve({
title: 'My Cart',
breadcrumbs: [
{
text: 'Home',
href: '/',
},
],
cart: {
items: products.map((item, i) => ({
...item,
quantity: 1,
})),
},
}),
)
}
})
}

export const config = {
api: {
bodyParser: false,
},
}

export default withAmpFormParser(cart)
3 changes: 3 additions & 0 deletions src/mock-connector/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export { default as cart } from './cart.js'
export { default as account } from './account.js'
export { default as addToCart } from './addToCart.js'
export { default as updateCartItem } from './updateCartItem.js'
export { default as home } from './home.js'
export { default as product } from './product.js'
export { default as productMedia } from './productMedia.js'
Expand Down
3 changes: 3 additions & 0 deletions src/mock-connector/removeCartItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function removeCartItem(item, req, res) {
return {}
}
2 changes: 0 additions & 2 deletions src/mock-connector/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@ export default [
{ source: '/', destination: '/' },
{ source: '/s/:subcategoryId', destination: '/s/[subcategoryId]' },
{ source: '/p/:productId', destination: '/p/[productId]' },
{ source: '/products/:productId', destination: '/p/[productId]' },
{ source: '/p/:productId/suggestions', destination: '/p/[productId]/suggestions' },
{ source: '/foo', destination: '/' },
]
11 changes: 10 additions & 1 deletion src/mock-connector/session.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import createProduct from './utils/createProduct'

export default async function session(req, res) {
const products = [createProduct(1), createProduct(2), createProduct(3)]

return {
name: 'Mark',
email: 'mark@domain.com',
itemsInCart: 0,
cart: {
items: products.map((item, i) => ({
...item,
quantity: 1,
})),
},
currency: 'USD',
}
}
8 changes: 4 additions & 4 deletions src/mock-connector/subcategory.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import fulfillAPIRequest from 'react-storefront/props/fulfillAPIRequest'
import createAppData from './utils/createAppData'

export default async function subcategory(params, req, res) {
let { q, id = '1', page = 0, filters, sort, more = false } = params
let { q, slug = '1', page = 0, filters, sort, more = false } = params

if (filters) {
filters = JSON.parse(filters)
Expand All @@ -18,9 +18,9 @@ export default async function subcategory(params, req, res) {
appData: createAppData,
pageData: () =>
Promise.resolve({
id: id,
name: q != null ? `Results for "${q}"` : `Subcategory ${id}`,
title: q != null ? `Results for "${q}"` : `Subcategory ${id}`,
id: slug,
name: q != null ? `Results for "${q}"` : `Subcategory ${slug}`,
title: q != null ? `Results for "${q}"` : `Subcategory ${slug}`,
total: 100,
page: parseInt(page),
totalPages: 5,
Expand Down
3 changes: 3 additions & 0 deletions src/mock-connector/updateCartItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function updateCartItem(item, quantity, req, res) {
return {}
}
2 changes: 1 addition & 1 deletion src/mock-connector/utils/createMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function createSubcategoryItem(i) {

return {
text: `Subcategory ${i}`,
href: `/s/[subcategoryId]`,
href: `/s/[...categorySlug]`,
as: `/s/${i}`,
expanded: i === 1,
items,
Expand Down
9 changes: 7 additions & 2 deletions src/mock-connector/utils/createTabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ export default function createTabs() {
const subcategories = []

for (let i = 1; i <= 3; i++) {
subcategories.push({ as: `/s/${i}`, href: '/s/[subcategoryId]', text: `Subcategory ${i}` })
subcategories.push({ as: `/s/${i}`, href: '/s/[...categorySlug]', text: `Subcategory ${i}` })
}

for (let i = 1; i <= 10; i++) {
tabs.push({ as: `/s/${i}`, href: '/s/[subcategoryId]', text: `Category ${i}`, subcategories })
tabs.push({
as: `/s/${i}`,
href: '/s/[...categorySlug]',
text: `Category ${i}`,
items: subcategories,
})
}

return tabs
Expand Down
Loading