Skip to content

Commit

Permalink
fix: support headless
Browse files Browse the repository at this point in the history
  • Loading branch information
kris-liu-smile authored and CarlLiu2023 committed Aug 15, 2023
1 parent 8cc078b commit 4adedb1
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 27 deletions.
2 changes: 1 addition & 1 deletion apps/storefront/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ declare interface CustomFieldItems {
}

declare interface CustomFieldStringItems {
[key: string]: string | number
[key: string]: string
}

declare interface Window {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Box, Button, Card, CardContent, Typography } from '@mui/material'

interface CustomField {
[key: string]: string | number
}

interface DashboardCardProps {
row: CustomFieldStringItems
row: CustomField
startActing: (id: number) => void
endActing: () => void
salesRepCompanyId?: number
Expand Down
7 changes: 5 additions & 2 deletions apps/storefront/src/pages/login/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ export const loginCheckout = (data: LoginConfig) => {
}),
}

return fetch(`${bcBaseUrl}/internalapi/v1/checkout/customer`, requestOptions)
return fetch(
`${bcBaseUrl()}/internalapi/v1/checkout/customer`,
requestOptions
)
.then((response) => response.text())
.catch((error) => console.log('error', error))
}
Expand All @@ -116,7 +119,7 @@ export const sendEmail = (emailAddress: string) => {
}

return fetch(
`${bcBaseUrl}/login.php?action=send_password_email`,
`${bcBaseUrl()}/login.php?action=send_password_email`,
requestOptions
)
.then((response) => response.text())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,15 @@ export default function OrderDialog({
headers: {
'content-type': 'application/x-www-form-urlencoded',
},
referrer: `${bcBaseUrl}/account.php?action=new_return&order_id=${orderId}`,
referrer: `${bcBaseUrl()}/account.php?action=new_return&order_id=${orderId}`,
body: urlencoded,
mode: 'no-cors',
}

try {
setIsRequestLoading(true)
const returnResult = await fetch(
`${bcBaseUrl}/account.php?action=save_new_return`,
`${bcBaseUrl()}/account.php?action=save_new_return`,
requestOptions
)
if (
Expand Down
14 changes: 9 additions & 5 deletions apps/storefront/src/shared/service/bc/api/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,33 @@ import B3Request from '../../request/b3Fetch'
import { RequestType } from '../../request/base'

export const getCartInfo = (): CustomFieldItems =>
B3Request.get(`${bcBaseUrl}/api/storefront/carts`, RequestType.BCRest)
B3Request.get(`${bcBaseUrl()}/api/storefront/carts`, RequestType.BCRest)

export const createCart = (data: CustomFieldItems): CustomFieldItems =>
B3Request.post(`${bcBaseUrl}/api/storefront/carts`, RequestType.BCRest, data)
B3Request.post(
`${bcBaseUrl()}/api/storefront/carts`,
RequestType.BCRest,
data
)

export const addProductToCart = (
data: CustomFieldItems,
cartId: string
): CustomFieldItems =>
B3Request.post(
`${bcBaseUrl}/api/storefront/carts/${cartId}/items`,
`${bcBaseUrl()}/api/storefront/carts/${cartId}/items`,
RequestType.BCRest,
data
)

export const getCartInfoWithOptions = (): CustomFieldItems =>
B3Request.get(
`${bcBaseUrl}/api/storefront/carts?include=lineItems.digitalItems.options,lineItems.physicalItems.options`,
`${bcBaseUrl()}/api/storefront/carts?include=lineItems.digitalItems.options,lineItems.physicalItems.options`,
RequestType.BCRest
)

export const deleteCart = (cartId: string) =>
B3Request.delete(
`${bcBaseUrl}/api/storefront/carts/${cartId}`,
`${bcBaseUrl()}/api/storefront/carts/${cartId}`,
RequestType.BCRest
)
4 changes: 2 additions & 2 deletions apps/storefront/src/shared/service/bc/api/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { RequestType } from '../../request/base'

export const getBCForgotPassword = (data: CustomFieldItems): CustomFieldItems =>
B3Request.post(
`${bcBaseUrl}/login.php?action=send_password_email`,
`${bcBaseUrl()}/login.php?action=send_password_email`,
RequestType.BCRest,
data
)

export const getBcCurrentJWT = (data: CustomFieldItems) =>
B3Request.get(`${bcBaseUrl}/customer/current.jwt`, RequestType.BCRest, data)
B3Request.get(`${bcBaseUrl()}/customer/current.jwt`, RequestType.BCRest, data)
2 changes: 1 addition & 1 deletion apps/storefront/src/shared/service/bc/api/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const getBCProductVariantId = (
data: CustomFieldItems
): CustomFieldItems =>
B3Request.post(
`${bcBaseUrl}/remote/v1/product-attributes/${productId}`,
`${bcBaseUrl()}/remote/v1/product-attributes/${productId}`,
RequestType.BCRest,
data
)
Expand Down
2 changes: 1 addition & 1 deletion apps/storefront/src/shared/service/bc/api/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import B3Request from '../../request/b3Fetch'
import { RequestType } from '../../request/base'

const getBCRegisterCustomFields = (): CustomFieldItems =>
B3Request.get(`${bcBaseUrl}/api/storefront/form-fields`, RequestType.BCRest)
B3Request.get(`${bcBaseUrl()}/api/storefront/form-fields`, RequestType.BCRest)

export default getBCRegisterCustomFields
18 changes: 11 additions & 7 deletions apps/storefront/src/shared/service/request/b3Fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ interface Config {
}
}

const GraphqlEndpoints = {
B2BGraphql: `${B2B_BASIC_URL}/graphql`,
BCGraphql: `${bcBaseUrl}/graphql`,
BCProxyGraphql: `${B2B_BASIC_URL}/api/v3/proxy/bc-storefront/graphql`,
} as const
const GraphqlEndpointsFn = (type: string): string => {
const GraphqlEndpoints: CustomFieldStringItems = {
B2BGraphql: `${B2B_BASIC_URL}/graphql`,
BCGraphql: `${bcBaseUrl()}/graphql`,
BCProxyGraphql: `${B2B_BASIC_URL}/api/v3/proxy/bc-storefront/graphql`,
}

return GraphqlEndpoints[type] || ''
}

function request<T>(path: string, config?: T & Config, type?: string) {
const url = RequestType.B2BRest === type ? `${B2B_BASIC_URL}${path}` : path
Expand Down Expand Up @@ -75,7 +79,7 @@ function request<T>(path: string, config?: T & Config, type?: string) {
}

function graphqlRequest<T, Y>(
type: keyof typeof GraphqlEndpoints,
type: string,
data: T,
config?: Y,
customMessage = false
Expand All @@ -89,7 +93,7 @@ function graphqlRequest<T, Y>(
body: JSON.stringify(data),
}

const url = GraphqlEndpoints[type]
const url = GraphqlEndpointsFn(type)
return b3Fetch(url, init, type, customMessage)
}

Expand Down
14 changes: 9 additions & 5 deletions apps/storefront/src/utils/basicConfig.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import globalB3 from '@b3/global-b3'

import { store } from '@/store'

const storeHash = globalB3?.setting?.store_hash

const bcUrl = sessionStorage.getItem('sf-bcUrl')
? JSON.parse(sessionStorage.getItem('sf-bcUrl') || '')
: ''
const bcBaseUrl = () => {
const {
global: { bcUrl },
} = store.getState()

const isLocalDebugging = globalB3?.setting?.is_local_debugging
const isLocalDebugging = globalB3?.setting?.is_local_debugging

const bcBaseUrl = isLocalDebugging ? '/bigcommerce' : bcUrl
return isLocalDebugging ? '/bigcommerce' : bcUrl
}

export { bcBaseUrl, storeHash }

0 comments on commit 4adedb1

Please sign in to comment.