Skip to content

Commit

Permalink
fix(react wrapper): set raw state also in bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan committed Aug 24, 2022
1 parent 48147ad commit 3d4479a
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions packages/storefront-react/src/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,25 @@ const ShopProvider: React.FC<ShopProviderProps> = ({

// load cart if not already loaded by the default cart get method
if (cart === undefined && bootstrap.cart === true) {
const res = await client.cart
.getCart({ input: {} })
.catch(() => ({ data: undefined, error: new Error('unknown error') }))
const res = await client.cart.getCart({ input: {} }).catch(() => ({
data: undefined,
raw: undefined,
error: new Error('unknown error'),
}))

setCart(res.data ?? null)
setRawCart(res.raw ?? null)
}
// load cart if not already loaded by the custom cart loader
else if (cart === undefined && typeof bootstrap.cart === 'function') {
const res = await bootstrap
.cart()
.catch(() => ({ data: undefined, error: new Error('unknown error') }))
const res = await bootstrap.cart().catch(() => ({
data: undefined,
raw: undefined,
error: new Error('unknown error'),
}))

setCart(res.data ?? null)
setRawCart(res.raw ?? null)
}

// mark bootstrapping as completed
Expand All @@ -262,19 +268,25 @@ const ShopProvider: React.FC<ShopProviderProps> = ({

// load user if not already loaded by the default user get method
if (user === undefined && bootstrap.user === true) {
const res = await client.user
.getUser({ input: {} })
.catch(() => ({ data: undefined, error: new Error('unknown error') }))
const res = await client.user.getUser({ input: {} }).catch(() => ({
data: undefined,
raw: undefined,
error: new Error('unknown error'),
}))

setUser(res.data ?? null)
setRawUser(res.raw ?? null)
}
// load user if not already loaded by the custom user loader
else if (user === undefined && typeof bootstrap.user === 'function') {
const res = await bootstrap
.user()
.catch(() => ({ data: undefined, error: new Error('unknown error') }))
const res = await bootstrap.user().catch(() => ({
data: undefined,
raw: undefined,
error: new Error('unknown error'),
}))

setUser(res.data ?? null)
setRawUser(res.raw ?? null)
}

// mark bootstrapping as completed
Expand All @@ -297,20 +309,28 @@ const ShopProvider: React.FC<ShopProviderProps> = ({
if (wishlist === undefined && bootstrap.wishlist === true) {
const res = await client.wishlist
.getWishlist({ input: {} })
.catch(() => ({ data: undefined, error: new Error('unknown error') }))
.catch(() => ({
data: undefined,
raw: undefined,
error: new Error('unknown error'),
}))

setWishlist(res.data ?? null)
setRawWishlist(res.raw ?? null)
}
// load wishlist if not already loaded by the custom wishlist loader
else if (
wishlist === undefined &&
typeof bootstrap.wishlist === 'function'
) {
const res = await bootstrap
.wishlist()
.catch(() => ({ data: undefined, error: new Error('unknown error') }))
const res = await bootstrap.wishlist().catch(() => ({
data: undefined,
raw: undefined,
error: new Error('unknown error'),
}))

setWishlist(res.data ?? null)
setRawWishlist(res.raw ?? null)
}

// mark bootstrapping as completed
Expand Down

0 comments on commit 3d4479a

Please sign in to comment.