Skip to content

Commit

Permalink
fix: add Calculate the price and tax of the product
Browse files Browse the repository at this point in the history
  • Loading branch information
kris-liu-smile committed Apr 23, 2023
1 parent ca25cfc commit ac72444
Show file tree
Hide file tree
Showing 18 changed files with 381 additions and 175 deletions.
4 changes: 3 additions & 1 deletion apps/storefront/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
getQuoteEnabled,
getTemPlateConfig,
setStorefrontConfig,
getStoreTaxZoneRates,
} from '@/utils'

import {
Expand Down Expand Up @@ -167,7 +168,8 @@ export default function App() {
await loginInfo()
}
setChannelStoreType(currentChannelId)
await Promise.all([setStorefrontConfig(dispatch), getTemPlateConfig(currentChannelId, styleDispatch, dispatch)])
// await getTaxZoneRates()
await Promise.all([getStoreTaxZoneRates(), setStorefrontConfig(dispatch), getTemPlateConfig(currentChannelId, styleDispatch, dispatch)])
const userInfo = {
role: +role,
isAgenting,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import {
} from '@/shared/global'

import {
searchB2BProducts,
searchBcProducts,
getB2BShoppingListDetails,
getBcShoppingListDetails,
deleteB2BShoppingListItem,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {

import {
ShoppingListProductItem,
ShoppingListProductItemVariants,
Variant,
SimpleObject,
} from '../../../types'

Expand Down Expand Up @@ -127,7 +127,7 @@ export const ChooseOptionsDialog = (props: ChooseOptionsDialogProps) => {

const [quantity, setQuantity] = useState<number | string>(1)
const [formFields, setFormFields] = useState<CustomFieldItems[]>([])
const [variantInfo, setVariantInfo] = useState<ShoppingListProductItemVariants | null>(null)
const [variantInfo, setVariantInfo] = useState<Partial<Variant> | null>(null)
const [variantSku, setVariantSku] = useState('')
const [additionalProducts, setAdditionalProducts] = useState<CustomFieldItems>({})

Expand Down
22 changes: 22 additions & 0 deletions apps/storefront/src/shared/service/b2b/graphql/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,24 @@ const storefrontConfigs = (channelId: number, keys: string[]) => `{
}
}`

const taxZoneRates = () => `{
taxZoneRates(storeHash: "${storeHash}") {
rates {
id,
name,
enabled,
priority,
classRates {
rate,
taxClassId,
}
},
enabled,
id,
name,
}
}`

export const getB2BToken = (bcJwtToken: string, channelId: number = 1): CustomFieldItems => B3Request.graphqlB2B({
query: getB2BTokenQl(bcJwtToken, channelId),
})
Expand Down Expand Up @@ -188,3 +206,7 @@ export const getBcCurrencies = (channelId: string): CustomFieldItems => B3Reques
export const getStorefrontConfigs = (channelId: number, keys: string[]): CustomFieldItems => B3Request.graphqlB2B({
query: storefrontConfigs(channelId, keys),
})

export const getTaxZoneRates = (): CustomFieldItems => B3Request.graphqlB2B({
query: taxZoneRates(),
})
1 change: 1 addition & 0 deletions apps/storefront/src/shared/service/b2b/graphql/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const searchProducts = (data: CustomFieldItems) => `{
optionsV3,
channelId,
productUrl,
taxClassId,
}
}`

Expand Down
2 changes: 2 additions & 0 deletions apps/storefront/src/shared/service/b2b/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import {
getCurrencies,
getBcCurrencies,
getStorefrontConfigs,
getTaxZoneRates,
} from './graphql/global'

import {
Expand Down Expand Up @@ -234,4 +235,5 @@ export {
B2BProductsBulkUploadCSV,
BcProductsBulkUploadCSV,
getStorefrontConfigs,
getTaxZoneRates,
}
1 change: 1 addition & 0 deletions apps/storefront/src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './reducer'
export * from './selectors'
export * from './slices/theme'
export * from './slices/glabol'
2 changes: 2 additions & 0 deletions apps/storefront/src/store/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
setupStore,
} from '@b3/store'
import theme from './slices/theme'
import glabol from './slices/glabol'

export const middlewareOptions = {
serializableCheck: {
Expand All @@ -12,6 +13,7 @@ export const middlewareOptions = {

export const store = setupStore({
reducers: {
glabol,
theme,
},
middlewareOptions,
Expand Down
1 change: 1 addition & 0 deletions apps/storefront/src/store/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ import {
} from './reducer'

const themeSelector = (state: RootState) => state.theme

export const themeFrameSelector = createSelector(themeSelector, (theme) => theme.themeFrame)
55 changes: 55 additions & 0 deletions apps/storefront/src/store/slices/glabol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {
Draft,
createSlice,
} from '@reduxjs/toolkit'
import type {
PayloadAction,
} from '@reduxjs/toolkit'

export interface TaxZoneRates {
rate?: number,
taxClassId?: number
}

interface Rates {
enabled: boolean,
id: number,
name: string,
priority: number,
classRates: TaxZoneRates[],
}

export interface TaxZoneRatesProps {
enabled: boolean,
id: number,
name: string,
rates: Rates[]
}

export interface glabolState {
taxZoneRates?: TaxZoneRatesProps[]
}

const initialState: glabolState = {
taxZoneRates: [],
}

export const glabolSlice = createSlice({
name: 'glabol',
initialState,
reducers: {
clearglabol: () => initialState,
setTaxZoneRates: (state, {
payload,
}: PayloadAction<TaxZoneRatesProps[]>) => {
state.taxZoneRates = payload as Draft<TaxZoneRatesProps[]>
},
},
})

export const {
clearglabol,
setTaxZoneRates,
} = glabolSlice.actions

export default glabolSlice.reducer
109 changes: 52 additions & 57 deletions apps/storefront/src/types/products.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import {
ShoppingListProductItemModifiers,
ShoppingListProductItemOption,
} from '@/types/shoppingList'

export interface ProductOptionsItem {
option_id: number,
display_name: string,
Expand Down Expand Up @@ -51,60 +56,35 @@ export interface ProductVariantSkuInfo{
variantSku: string,
}

interface OptionValue {
id: number;
label: string;
option_id: number;
option_display_name: string;
export interface OptionValue {
id: number,
label: string,
option_display_name: string,
option_id: number,
}

interface Variant {
export interface Variant {
variant_id: number;
product_id: number;
sku: string;
price: number,
option_values: OptionValue[];
calculated_price: number;
image_url: string;
has_price_list: boolean;
bulk_prices: any[]; // not sure about the type
bulk_prices?: any[]; // not sure about the type
purchasing_disabled: boolean;
cost_price: number;
cost_price?: number;
inventory_level: number;
bc_calculated_price: {
as_entered: number;
tax_inclusive: number;
tax_exclusive: number;
entered_inclusive: boolean;
};
}

interface Modifier {
id: number;
display_name: string;
type: string;
required: boolean;
config: any; // not sure about the type
option_values: any[]; // not sure about the type
bc_calculated_price: BcCalculatedPrice;
}

interface Option {
option_id: number;
display_name: string;
sort_order: number;
is_required: boolean;
export interface AdjustersPrice {
adjuster: string;
adjuster_value: number;
}

interface OptionV3 {
id: number;
product_id: number;
name: string;
display_name: string;
type: string;
sort_order: number;
option_values: OptionValue[];
}

interface ALlOptionValue {
export interface ALlOptionValue {
id: number;
label: string;
sort_order: number;
Expand All @@ -129,27 +109,40 @@ interface ALlOptionValue {
message: string;
};
} | null;
product_id?: number,
}

export interface AllOptionProps {
id: number;
id: number | string;
product_id?: number;
name: string;
display_name: string;
type: string;
sort_order: number;
option_values: ALlOptionValue[];
config: {
product_list_adjusts_inventory?: boolean;
product_list_adjusts_pricing?: boolean;
product_list_shipping_calc?: string;
default_value?: string;
text_characters_limited?: boolean;
text_min_length?: number;
text_max_length?: number;
checkbox_label?: string;
checked_by_default?: boolean;
} | null;
option_values: Partial<ALlOptionValue>[];
config?: {
default_value?: string,
text_characters_limited?: boolean,
text_max_length?: number,
text_min_length?: number,
text_lines_limited?: boolean,
text_max_lines?: number,
date_earliest_value?: string,
date_latest_value?: string,
date_limit_mode?: string,
date_limited?: boolean,
number_highest_value?: number,
number_integers_only?: boolean,
number_limit_mode?: string,
number_limited?: boolean,
number_lowest_value?: number,
checkbox_label?: string,
checked_by_default?: boolean,
file_max_size?: number,
file_types_mode?: string,
file_types_other?: string[],
file_types_supported?: string[],
};
required: boolean;
isVariantOption?: boolean;
}
Expand All @@ -158,20 +151,22 @@ export interface Product {
id: number;
name: string;
sku: string;
base_price: string,
costPrice: string;
channelId: number[],
selectOptions: string,
inventoryLevel: number;
inventoryTracking: string;
availability: string;
orderQuantityMinimum: number;
orderQuantityMaximum: number;
variants: Variant[];
variants?: Partial<Variant>[];
currencyCode: string;
imageUrl: string;
modifiers: Modifier[];
options: Option[];
optionsV3: OptionV3[];
allOptions: AllOptionProps
modifiers: ShoppingListProductItemModifiers[];
options?: ShoppingListProductItemOption[];
optionsV3?: ShoppingListProductItemModifiers[];
allOptions?: Partial<AllOptionProps>[]
productUrl: string;
quantity: number;
[key:string]: any;
Expand Down
22 changes: 5 additions & 17 deletions apps/storefront/src/types/shoppingList.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ProductItem,
BcCalculatedPrice,
Variant,
AllOptionProps,
} from './products'

export interface ShoppingListItem {
Expand Down Expand Up @@ -64,7 +65,7 @@ export interface ShoppingListProductItemModifiers {
file_types_supported?: string[],
},
display_name: string,
id: number | string,
id?: number | string,
option_values: ShoppingListProductItemModifiersOption[],
required: boolean,
type: string,
Expand All @@ -78,26 +79,13 @@ export interface ShoppingListProductItemVariantsOption {
option_id: number,
}

export interface ShoppingListProductItemVariants {
product_id: number,
sku: string,
variant_id: number,
purchasing_disabled: boolean,
option_values: ShoppingListProductItemVariantsOption[],
cost_price: number,
image_url: string,
calculated_price: number,
price: number,
bc_calculated_price: BcCalculatedPrice,
}

export interface ShoppingListProductItem extends ProductItem{
options?: ShoppingListProductItemOption[],
optionsV3?: ShoppingListProductItemModifiers[],
modifiers?: ShoppingListProductItemModifiers[],
costPrice?: string,
variants?: ShoppingListProductItemVariants[],
allOptions?: ShoppingListProductItemModifiers[],
variants?: Variant[],
allOptions?: Partial<AllOptionProps>[],
selectOptions?: string,
orderQuantityMaximum?: number,
orderQuantityMinimum?: number,
Expand Down
Loading

0 comments on commit ac72444

Please sign in to comment.