Skip to content

Commit

Permalink
feat: common utils in retail app
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitShogun committed Jun 28, 2024
1 parent d4eb9ee commit 727dc10
Show file tree
Hide file tree
Showing 17 changed files with 45 additions and 90 deletions.
4 changes: 4 additions & 0 deletions .vscode/project.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
"name": "Molecules",
"path": "../packages/molecules"
},
{
"name": "Common",
"path": "../packages/common"
},
{
"name": "Prettier",
"path": "../packages/prettier-config"
Expand Down
2 changes: 1 addition & 1 deletion apps/retail/components/importedOrder/ImportedOrder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useLanguage } from '../../hooks/useLanguage'
import BottomModalScan from '@components/BottomModal/BottomModalScan'
import BecknButton from '@beckn-ui/molecules/src/components/button/Button'
import Typography from '@beckn-ui/molecules/src/components/typography/typography'
import { isEmpty } from '@utils/common-utils'
import { isEmpty } from '@beckn-ui/common/src/utils'
import { ImportedOrderItem } from '@beckn-ui/common/lib/types'

interface ImportedOrderProps {
Expand Down
2 changes: 1 addition & 1 deletion apps/retail/components/signIn/SignIn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useMemo, useState } from 'react'
import Logo from '../../public/images/Logo.svg'
import AlternateLogo from '../../public/images/KuzaLogo.svg'
import { useLanguage } from '@hooks/useLanguage'
import { signInValidateForm } from '@utils/form-utils'
import { signInValidateForm } from '@beckn-ui/common/src/utils'
import { BecknAuth } from '@beckn-ui/becknified-components'
import Router from 'next/router'
import { Box, Text, useBreakpoint } from '@chakra-ui/react'
Expand Down
2 changes: 1 addition & 1 deletion apps/retail/pages/cart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Cart as BecknCart } from '@beckn-ui/becknified-components'
import { Box, useToast } from '@chakra-ui/react'

import { CartItemProps } from '@beckn-ui/becknified-components/src/components/cart/cart.types'
import { getSelectPayload } from '@utils/cart-utils'
import { getSelectPayload } from '@beckn-ui/common/src/utils'
import { DiscoveryRootState, ICartRootState } from '@beckn-ui/common/lib/types'
import { cartActions } from '@beckn-ui/common/src/store/cart-slice'
import { DOMAIN } from '@beckn-ui/common'
Expand Down
41 changes: 24 additions & 17 deletions apps/retail/pages/checkout.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import React, { useEffect, useState } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { Box, useToast, useTheme } from '@chakra-ui/react'
import { Box, Flex, Text, Stack, Checkbox, useToast, useTheme } from '@chakra-ui/react'
import { DOMAIN } from '@lib/config'
import { useLanguage } from '../hooks/useLanguage'

import { CartItemForRequest, DataPerBpp, ICartRootState, TransactionIdRootState } from '@lib/types/cart'
import {
areShippingAndBillingDetailsSame,
getPayloadForInitRequest,
getSubTotalAndDeliveryCharges
} from '@components/checkout/checkout.utils'
import useRequest from '../hooks/useRequest'
import { CustomToast } from '@components/signIn/SignIn'
import { useInitMutation } from '@services/init'
import { getInitPayload } from '@beckn-ui/common/src/utils'

import { Checkout } from '@beckn-ui/becknified-components'
import { FormData, FormField } from '@beckn-ui/molecules'

import { useRouter } from 'next/router'
import { Router, useRouter } from 'next/router'
import { ShippingFormInitialValuesType } from '@beckn-ui/becknified-components'
import { isEmpty } from '@utils/common-utils'
import { areShippingAndBillingDetailsSame, getInitPayload, getSubTotalAndDeliveryCharges } from '@utils/checkout-utils'
import { DiscoveryRootState, ICartRootState, PaymentBreakDownModel, QuoteBreakupInfo } from '@beckn-ui/common/lib/types'
import { checkoutActions, CheckoutRootState } from '@beckn-ui/common/src/store/checkout-slice'
import { cartActions } from '@beckn-ui/common/src/store/cart-slice'
import { DOMAIN } from '@beckn-ui/common'
import { useInitMutation } from '@beckn-ui/common/src/services/init'
import { CheckoutRootState, checkoutActions } from '@store/checkout-slice'
import { cartActions } from '@store/cart-slice'
import { isEmpty } from '@beckn-ui/common/src/utils'
import { FormField } from '@beckn-ui/molecules'

export type ShippingFormData = {
name: string
Expand Down Expand Up @@ -96,6 +102,7 @@ const CheckoutPage = () => {
)

const router = useRouter()
const initRequest = useRequest()
const dispatch = useDispatch()
const [initialize, { isLoading, isError }] = useInitMutation()
const { t, locale } = useLanguage()
Expand Down Expand Up @@ -212,7 +219,7 @@ const CheckoutPage = () => {
// setIsBillingSame(isBillingSameRedux)
// },[])

const formSubmitHandler = (data: FormData<FormField[]>) => {
const formSubmitHandler = (data: any) => {
if (data) {
const { id, type } = selectResponse[0].message.order.fulfillments[0]
getInitPayload(submittedDetails, billingFormData, cartItems, transactionId, DOMAIN, { id, type }).then(res => {
Expand Down Expand Up @@ -242,9 +249,9 @@ const CheckoutPage = () => {
}

const createPaymentBreakdownMap = () => {
const paymentBreakdownMap: PaymentBreakDownModel = {}
const paymentBreakdownMap = {}
if (isInitResultPresent()) {
initResponse[0].message.order.quote.breakup.forEach((breakup: QuoteBreakupInfo) => {
initResponse[0].message.order.quote.breakup.forEach(breakup => {
paymentBreakdownMap[breakup.title] = {
value: breakup.price.value,
currency: breakup.price.currency
Expand Down Expand Up @@ -288,7 +295,7 @@ const CheckoutPage = () => {
// priceWithSymbol: `${currencyMap[singleItem.price.currency]}${singleItem.totalPrice}`,
price: singleItem.totalPrice,
currency: singleItem.price.currency,
image: singleItem.images?.[0].url
image: singleItem.images[0].url
}))
},
shipping: {
Expand All @@ -297,7 +304,7 @@ const CheckoutPage = () => {
color: bgColorOfSecondary,
shippingDetails: {
name: submittedDetails.name,
location: submittedDetails.address!,
location: submittedDetails.address,
number: submittedDetails.mobileNumber,
title: t.shipping
},
Expand Down Expand Up @@ -327,7 +334,7 @@ const CheckoutPage = () => {
showDetails: isInitResultPresent() && !isEmpty(submittedDetails),
shippingDetails: {
name: billingFormData.name,
location: billingFormData.address!,
location: billingFormData.address,
number: billingFormData.mobileNumber,
title: t.billing
},
Expand All @@ -347,7 +354,7 @@ const CheckoutPage = () => {
totalText: t.total,
totalValueWithCurrency: {
value: getSubTotalAndDeliveryCharges(initResponse).subTotal.toString(),
currency: getSubTotalAndDeliveryCharges(initResponse).currencySymbol!
currency: getSubTotalAndDeliveryCharges(initResponse).currencySymbol
}
}
},
Expand Down
3 changes: 1 addition & 2 deletions apps/retail/pages/invoiceDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { DetailCard } from '@beckn-ui/becknified-components'
import Typography from '@beckn-ui/molecules/src/components/typography/typography'
import { Box, Flex } from '@chakra-ui/react'
import { useLanguage } from '@hooks/useLanguage'
import { getPaymentBreakDown } from '@utils/checkout-utils'
import { formatTimestamp } from '@utils/confirm-utils'
import { formatTimestamp, getPaymentBreakDown } from '@beckn-ui/common/src/utils'
import { StatusResponseModel } from '@beckn-ui/common/lib/types'

const invoiceDetails = () => {
Expand Down
2 changes: 1 addition & 1 deletion apps/retail/pages/orderConfirmation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import orderConfirmmark from '../public/images/orderConfirmmark.svg'
import { useSelector, useDispatch } from 'react-redux'
import { useLanguage } from '../hooks/useLanguage'
import { ConfirmationPage } from '@beckn-ui/becknified-components'
import { getPayloadForConfirm, getPayloadForOrderHistoryPost } from '@utils/confirm-utils'
import axios from '@services/axios'
import { Box } from '@chakra-ui/react'
import Cookies from 'js-cookie'
Expand All @@ -13,6 +12,7 @@ import LoaderWithMessage from '@components/loader/LoaderWithMessage'
import { ConfirmResponseModel } from '@beckn-ui/common/lib/types'
import { checkoutActions, CheckoutRootState } from '@beckn-ui/common/src/store/checkout-slice'
import { orderActions } from '@beckn-ui/common/src/store/order-slice'
import { getPayloadForConfirm, getPayloadForOrderHistoryPost } from '@beckn-ui/common/src/utils'
import { useConfirmMutation } from '@beckn-ui/common/src/services/confirm'

const OrderConfirmation = () => {
Expand Down
4 changes: 2 additions & 2 deletions apps/retail/pages/orderDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import ViewMoreOrderModal from '@components/orderDetailComponents/ViewMoreOrder'
// import { statusActions } from '@store/status-slice'
import { DetailCard, OrderStatusProgress, OrderStatusProgressProps } from '@beckn-ui/becknified-components'
import useResponsive from '@beckn-ui/becknified-components/src/hooks/useResponsive'
import { isEmpty } from '@utils/common-utils'
import { isEmpty } from '@beckn-ui/common/src/utils'
import { useLanguage } from '@hooks/useLanguage'
import { formatTimestamp, getPayloadForOrderStatus } from '@utils/confirm-utils'
import { getPayloadForOrderStatus, formatTimestamp } from '@beckn-ui/common/src/utils'
import BecknButton from '@beckn-ui/molecules/src/components/button/Button'
import BottomModalScan from '@components/BottomModal/BottomModalScan'
import LoaderWithMessage from '@components/loader/LoaderWithMessage'
Expand Down
2 changes: 1 addition & 1 deletion apps/retail/pages/orderHistory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Box, Text, Flex, Image } from '@chakra-ui/react'
import React, { useEffect, useState } from 'react'
import pendingIcon from '../public/images/pendingStatus.svg'
import { useDispatch } from 'react-redux'
import { formatTimestamp } from '@utils/confirm-utils'
import { formatTimestamp } from '@beckn-ui/common/src/utils'
import { useRouter } from 'next/router'
import EmptyOrder from '@components/orderHistory/emptyOrder'
import { orderHistoryData } from '@beckn-ui/common/lib/types'
Expand Down
4 changes: 2 additions & 2 deletions apps/retail/pages/profile.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { BecknAuth } from '@beckn-ui/becknified-components'
import { Box, useToast } from '@chakra-ui/react'
import { useLanguage } from '@hooks/useLanguage'
import { profileValidateForm } from '@utils/form-utils'
import { profileValidateForm } from '@beckn-ui/common/src/utils'
import Cookies from 'js-cookie'
import React, { useEffect, useMemo, useState } from 'react'
import Router from 'next/router'
import { isEmpty } from '@utils/common-utils'
import { isEmpty } from '@beckn-ui/common/src/utils'
import { useDispatch } from 'react-redux'
import { FormErrors, ProfileProps } from '@beckn-ui/common/lib/types'
import { feedbackActions } from '@beckn-ui/common/src/store/ui-feedback-slice'
Expand Down
5 changes: 3 additions & 2 deletions apps/retail/pages/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import axios from '@services/axios'
import { useDispatch } from 'react-redux'
import { Box, Flex, Image } from '@chakra-ui/react'
import { useRouter } from 'next/router'
import { parsedSearchlist } from '@utils/search-results.utils'

import { parseSearchlist } from '@beckn-ui/common'
import { Product, ProductCard } from '@beckn-ui/becknified-components'
import { BottomModal } from '@beckn-ui/molecules'
import { useBreakpoint } from '@chakra-ui/react'
Expand Down Expand Up @@ -63,7 +64,7 @@ const Search = () => {
.post(`${apiUrl}/search`, searchPayload)
.then(res => {
dispatch(discoveryActions.addTransactionId({ transactionId: res.data.data[0].context.transaction_id }))
const parsedSearchItems = parsedSearchlist(res.data.data)
const parsedSearchItems = parseSearchlist(res.data.data)
dispatch(discoveryActions.addProducts({ products: parsedSearchItems }))
setItems(parsedSearchItems)
setOriginalItems(parsedSearchItems)
Expand Down
2 changes: 1 addition & 1 deletion apps/retail/pages/signUp.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useMemo, useState } from 'react'
import { useLanguage } from '@hooks/useLanguage'
import AlternateLogo from '../public/images/KuzaLogo.svg'
import { signUpValidateForm } from '@utils/form-utils'
import { signUpValidateForm } from '@beckn-ui/common/src/utils'
import { BecknAuth } from '@beckn-ui/becknified-components'
import Router from 'next/router'
import { Box, useBreakpoint } from '@chakra-ui/react'
Expand Down
1 change: 1 addition & 0 deletions packages/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './lib/types'
export * from './lib/config'
export * from './src/services'
export * from './src/hooks'
export * from './src/utils'
9 changes: 0 additions & 9 deletions packages/common/src/utils/currency-format.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/common/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ export * from './payload'
export * from './general'
export * from './checkout-utils'
export * from './confirm-utils'
export * from './currency-format'
export * from './search-utils'
export * from './form-utils'
48 changes: 0 additions & 48 deletions packages/common/src/utils/payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,54 +85,6 @@ export const getSelectPayload = (
return { data: resultData }
}

export const getPayloadForSelectRequest = (selectedProduct: ParsedItemModel) => {
const {
bppId,
bppUri,
transactionId,
domain,
providerId,
item: { id, fulfillments, tags }
} = selectedProduct

const selectPayload = {
data: [
{
context: {
transaction_id: transactionId,
bpp_id: bppId,
bpp_uri: bppUri,
domain: domain
},
message: {
orders: [
{
provider: {
id: providerId
},
items: [
{
id
}
],
fulfillments,
tags: [
{
descriptor: {
name: 'select-1'
}
}
]
}
]
}
}
]
}

return selectPayload
}

export const getInitPayload = async (
deliveryAddress: ShippingFormInitialValuesType,
billingAddress: ShippingFormInitialValuesType,
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/utils/search-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const dummyLocation = [
}
]

export const parsedSearchlist = (data: SearchResponseModel[]) => {
export const parseSearchlist = (data: SearchResponseModel[]) => {
const itemsarray: ParsedItemModel[] = []
data.forEach(entry => {
const context = entry.context
Expand Down

0 comments on commit 727dc10

Please sign in to comment.