From 727dc1042d67ffd404c053f9cf08950c73d5ba11 Mon Sep 17 00:00:00 2001 From: Ankit Brahmbhatt Date: Fri, 28 Jun 2024 17:41:05 +0530 Subject: [PATCH] feat: common utils in retail app --- .vscode/project.code-workspace | 4 ++ .../importedOrder/ImportedOrder.tsx | 2 +- apps/retail/components/signIn/SignIn.tsx | 2 +- apps/retail/pages/cart.tsx | 2 +- apps/retail/pages/checkout.tsx | 41 +++++++++------- apps/retail/pages/invoiceDetails.tsx | 3 +- apps/retail/pages/orderConfirmation.tsx | 2 +- apps/retail/pages/orderDetails.tsx | 4 +- apps/retail/pages/orderHistory.tsx | 2 +- apps/retail/pages/profile.tsx | 4 +- apps/retail/pages/search.tsx | 5 +- apps/retail/pages/signUp.tsx | 2 +- packages/common/index.ts | 1 + packages/common/src/utils/currency-format.ts | 9 ---- packages/common/src/utils/index.ts | 2 +- packages/common/src/utils/payload.ts | 48 ------------------- packages/common/src/utils/search-utils.ts | 2 +- 17 files changed, 45 insertions(+), 90 deletions(-) delete mode 100644 packages/common/src/utils/currency-format.ts diff --git a/.vscode/project.code-workspace b/.vscode/project.code-workspace index 3ea36a869..4f11403dd 100644 --- a/.vscode/project.code-workspace +++ b/.vscode/project.code-workspace @@ -8,6 +8,10 @@ "name": "Molecules", "path": "../packages/molecules" }, + { + "name": "Common", + "path": "../packages/common" + }, { "name": "Prettier", "path": "../packages/prettier-config" diff --git a/apps/retail/components/importedOrder/ImportedOrder.tsx b/apps/retail/components/importedOrder/ImportedOrder.tsx index 790e8ea03..e0e401be6 100644 --- a/apps/retail/components/importedOrder/ImportedOrder.tsx +++ b/apps/retail/components/importedOrder/ImportedOrder.tsx @@ -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 { diff --git a/apps/retail/components/signIn/SignIn.tsx b/apps/retail/components/signIn/SignIn.tsx index b979817f7..079068c21 100644 --- a/apps/retail/components/signIn/SignIn.tsx +++ b/apps/retail/components/signIn/SignIn.tsx @@ -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' diff --git a/apps/retail/pages/cart.tsx b/apps/retail/pages/cart.tsx index 970f86f3a..13caccce0 100644 --- a/apps/retail/pages/cart.tsx +++ b/apps/retail/pages/cart.tsx @@ -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' diff --git a/apps/retail/pages/checkout.tsx b/apps/retail/pages/checkout.tsx index 5be9c63ef..7523693f3 100644 --- a/apps/retail/pages/checkout.tsx +++ b/apps/retail/pages/checkout.tsx @@ -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 @@ -96,6 +102,7 @@ const CheckoutPage = () => { ) const router = useRouter() + const initRequest = useRequest() const dispatch = useDispatch() const [initialize, { isLoading, isError }] = useInitMutation() const { t, locale } = useLanguage() @@ -212,7 +219,7 @@ const CheckoutPage = () => { // setIsBillingSame(isBillingSameRedux) // },[]) - const formSubmitHandler = (data: FormData) => { + 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 => { @@ -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 @@ -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: { @@ -297,7 +304,7 @@ const CheckoutPage = () => { color: bgColorOfSecondary, shippingDetails: { name: submittedDetails.name, - location: submittedDetails.address!, + location: submittedDetails.address, number: submittedDetails.mobileNumber, title: t.shipping }, @@ -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 }, @@ -347,7 +354,7 @@ const CheckoutPage = () => { totalText: t.total, totalValueWithCurrency: { value: getSubTotalAndDeliveryCharges(initResponse).subTotal.toString(), - currency: getSubTotalAndDeliveryCharges(initResponse).currencySymbol! + currency: getSubTotalAndDeliveryCharges(initResponse).currencySymbol } } }, diff --git a/apps/retail/pages/invoiceDetails.tsx b/apps/retail/pages/invoiceDetails.tsx index bd9e2c393..a92493c8f 100644 --- a/apps/retail/pages/invoiceDetails.tsx +++ b/apps/retail/pages/invoiceDetails.tsx @@ -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 = () => { diff --git a/apps/retail/pages/orderConfirmation.tsx b/apps/retail/pages/orderConfirmation.tsx index 7dff88482..c7b4a104f 100644 --- a/apps/retail/pages/orderConfirmation.tsx +++ b/apps/retail/pages/orderConfirmation.tsx @@ -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' @@ -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 = () => { diff --git a/apps/retail/pages/orderDetails.tsx b/apps/retail/pages/orderDetails.tsx index 897dc79e0..f210be429 100644 --- a/apps/retail/pages/orderDetails.tsx +++ b/apps/retail/pages/orderDetails.tsx @@ -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' diff --git a/apps/retail/pages/orderHistory.tsx b/apps/retail/pages/orderHistory.tsx index dea0e95cf..21ab20ea6 100644 --- a/apps/retail/pages/orderHistory.tsx +++ b/apps/retail/pages/orderHistory.tsx @@ -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' diff --git a/apps/retail/pages/profile.tsx b/apps/retail/pages/profile.tsx index 5091d094b..54ccaa232 100644 --- a/apps/retail/pages/profile.tsx +++ b/apps/retail/pages/profile.tsx @@ -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' diff --git a/apps/retail/pages/search.tsx b/apps/retail/pages/search.tsx index 497c939f8..2e2bf9a21 100644 --- a/apps/retail/pages/search.tsx +++ b/apps/retail/pages/search.tsx @@ -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' @@ -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) diff --git a/apps/retail/pages/signUp.tsx b/apps/retail/pages/signUp.tsx index dcc40ea61..85e7afbd4 100644 --- a/apps/retail/pages/signUp.tsx +++ b/apps/retail/pages/signUp.tsx @@ -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' diff --git a/packages/common/index.ts b/packages/common/index.ts index 2f43acc77..ac81c0ea2 100644 --- a/packages/common/index.ts +++ b/packages/common/index.ts @@ -3,3 +3,4 @@ export * from './lib/types' export * from './lib/config' export * from './src/services' export * from './src/hooks' +export * from './src/utils' diff --git a/packages/common/src/utils/currency-format.ts b/packages/common/src/utils/currency-format.ts deleted file mode 100644 index 00012cd95..000000000 --- a/packages/common/src/utils/currency-format.ts +++ /dev/null @@ -1,9 +0,0 @@ -// Rial currency format -export const irrCurrencyFormat = (price: number | undefined) => { - return price ? new Intl.NumberFormat('fa-IR').format(price) : null -} - -//pound currency format -export const gbpCurrencyFormat = (price: number | undefined) => { - return price ? new Intl.NumberFormat('en-GB').format(price) : null -} diff --git a/packages/common/src/utils/index.ts b/packages/common/src/utils/index.ts index 95e589e74..7dc03e796 100644 --- a/packages/common/src/utils/index.ts +++ b/packages/common/src/utils/index.ts @@ -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' diff --git a/packages/common/src/utils/payload.ts b/packages/common/src/utils/payload.ts index 536387b08..64d3f66c7 100644 --- a/packages/common/src/utils/payload.ts +++ b/packages/common/src/utils/payload.ts @@ -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, diff --git a/packages/common/src/utils/search-utils.ts b/packages/common/src/utils/search-utils.ts index c7f1e0351..e2724077a 100644 --- a/packages/common/src/utils/search-utils.ts +++ b/packages/common/src/utils/search-utils.ts @@ -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