Skip to content

Commit

Permalink
Merge pull request #18 from builtbysuraj/style-refactor-client
Browse files Browse the repository at this point in the history
Style refactor client
  • Loading branch information
builtbysuraj authored Feb 6, 2024
2 parents 5e7c892 + 4ae9f3f commit 0ef4aa7
Show file tree
Hide file tree
Showing 21 changed files with 184 additions and 39 deletions.
2 changes: 1 addition & 1 deletion client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="./public/favicon.ico" />
<link rel="icon" type="image/svg+xml" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>
Online Shopping Site for Mobiles, Electronics, Furniture, Grocery,
Expand Down
24 changes: 24 additions & 0 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"lodash.debounce": "^4.0.8",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-hot-toast": "^2.4.1",
"react-image-gallery": "^1.3.0",
"react-redux": "^9.0.4",
"react-router-dom": "^6.21.0"
Expand Down
File renamed without changes
File renamed without changes
1 change: 1 addition & 0 deletions client/src/conf/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const ENV = {
API_BASE_URL: String(import.meta.env.VITE_API_BASE_URL),
SERVER_URL: String(import.meta.env.VITE_SERVER_URL),
}
64 changes: 64 additions & 0 deletions client/src/context/ServerStatusProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import axios from 'axios'
import {
PropsWithChildren,
createContext,
useContext,
useEffect,
useState,
} from 'react'

import { ENV } from '@/conf'

type ServerStateType = {
status: boolean
message: string
}

export const ServerStatusContext = createContext<ServerStateType | null>(null)

export default function ServerStatusProvider({ children }: PropsWithChildren) {
const [serverStatus, setServerStatus] = useState<ServerStateType>({
status: false,
message: '',
})

useEffect(() => {
const checkServerStatus = async () => {
try {
const { data } = await axios.get(`${ENV.SERVER_URL}/api/status`)

if (data.status) {
setServerStatus(data)
} else {
setServerStatus({ status: false, message: 'Server is not running' })
}
} catch (error) {
setServerStatus({ status: false, message: 'Wait for server to start' })
}
}

checkServerStatus()

const intervalId = setInterval(checkServerStatus, 10000)

// Clear interval on component unmount
return () => clearInterval(intervalId)
}, [])

return (
<ServerStatusContext.Provider value={serverStatus}>
{children}
</ServerStatusContext.Provider>
)
}

// eslint-disable-next-line react-refresh/only-export-components
export const useServerStatus = () => {
const context = useContext(ServerStatusContext)
if (context === undefined) {
throw new Error(
'useServerStatus must be used within an ServerStatusProvider'
)
}
return context
}
17 changes: 9 additions & 8 deletions client/src/layouts/filters/SidebarFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { memo } from 'react'

import CategoryFilter from './components/CategoryFilter'
import PriceSliderFilter from './components/PriceSliderFilter'
import RatingFilter from './components/RatingFilter'
import SortProductsFilter from './components/SortProductsFilter'
import AppliedFilters from './components/applied-filters/AppliedFilters'
import BrandFilter from './components/brand-filter/BrandFilter'
import ClearFilter from './components/clear-filter/ClearFilter'
import {
AppliedFilters,
BrandFilter,
CategoryFilter,
ClearFilter,
PriceSliderFilter,
RatingFilter,
SortProductsFilter,
} from './components'

function SidebarFilters() {
return (
Expand Down
17 changes: 17 additions & 0 deletions client/src/layouts/filters/components/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import CategoryFilter from './CategoryFilter'
import PriceSliderFilter from './PriceSliderFilter'
import RatingFilter from './RatingFilter'
import SortProductsFilter from './SortProductsFilter'
import AppliedFilters from './applied-filters/AppliedFilters'
import BrandFilter from './brand-filter/BrandFilter'
import ClearFilter from './clear-filter/ClearFilter'

export {
AppliedFilters,
BrandFilter,
CategoryFilter,
ClearFilter,
PriceSliderFilter,
RatingFilter,
SortProductsFilter,
}
2 changes: 1 addition & 1 deletion client/src/layouts/header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import Input from '@/components/ui/Input'
import useGetParams from '@/hooks/useGetParams'
import useHandleDispatch from '@/hooks/useHandleDispatch'
import { useAppSelector } from '@/state/store'
import flipkart from '../../assets/img/flipkart.png'
import styles from './Header.module.css'
import flipkart from '/flipkart.png'

function Header() {
const cartData = useAppSelector((state) => state.cart)
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions client/src/lazyComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ const ProductListingPage = lazy(
)
const LoginPage = lazy(() => import('./pages/login/LoginPage'))
const SignUpPage = lazy(() => import('./pages/signup/SignUpPage'))
const PaymentSuccess = lazy(
() => import('./pages/payment-success/PaymentSuccess')
const PaymentSuccessPage = lazy(
() => import('./pages/payment-success/PaymentSuccessPage')
)

export {
CartPage,
HomePage,
LoginPage,
PaymentSuccess,
PaymentSuccessPage,
ProductDetailsPage,
ProductListingPage,
SignUpPage,
Expand Down
9 changes: 8 additions & 1 deletion client/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createRoot } from 'react-dom/client'
import { Toaster } from 'react-hot-toast'
import { Provider } from 'react-redux'
import { RouterProvider } from 'react-router-dom'

Expand All @@ -13,6 +14,9 @@ import router from './routes'
// Store
import store from './state/store'

// Context
import ServerStatusProvider from './context/ServerStatusProvider'

if (process.env.NODE_ENV === 'production') {
console.log = () => {}
console.warn = () => {}
Expand All @@ -21,6 +25,9 @@ if (process.env.NODE_ENV === 'production') {

createRoot(document.querySelector('#root')!).render(
<Provider store={store}>
<RouterProvider router={router} />
<ServerStatusProvider>
<Toaster />
<RouterProvider router={router} />
</ServerStatusProvider>
</Provider>
)
27 changes: 12 additions & 15 deletions client/src/pages/cart/CartPage.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
import { useAppSelector } from '@/state/store'
import type { CartType } from '@/types'
import styles from './CartPage.module.css'
import CartItem from './components/cart-item/CartItem'
import CartPriceDetails from './components/cart-price-details/CartPriceDetails'
import EmptyCart from './components/empty-cart/EmptyCart'
import PlaceOrder from './components/place-order/PlaceOrder'
import { CartItem, CartPriceDetails, EmptyCart, PlaceOrder } from './components'

export default function CartPage() {
const cartData = useAppSelector((state) => state.cart)

if (!cartData?.length) {
return <EmptyCart />
}
if (!cartData?.length) return <EmptyCart />

return (
<div className={styles.cartContainer}>
<div>
{cartData?.map((product: CartType) => (
<CartItem product={product} key={product.id} />
))}
<PlaceOrder cartData={cartData} />
<>
<div className={styles.cartContainer}>
<div>
{cartData?.map((product: CartType) => (
<CartItem product={product} key={product.id} />
))}
<PlaceOrder cartData={cartData} />
</div>
<CartPriceDetails cartData={cartData} />
</div>
<CartPriceDetails cartData={cartData} />
</div>
</>
)
}
6 changes: 6 additions & 0 deletions client/src/pages/cart/components/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import CartItem from './cart-item/CartItem'
import CartPriceDetails from './cart-price-details/CartPriceDetails'
import EmptyCart from './empty-cart/EmptyCart'
import PlaceOrder from './place-order/PlaceOrder'

export { CartItem, CartPriceDetails, EmptyCart, PlaceOrder }
28 changes: 26 additions & 2 deletions client/src/pages/cart/components/place-order/PlaceOrder.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import axios from 'axios'

import { useServerStatus } from '@/context/ServerStatusProvider'
import { totalCartPrice } from '@/utils'
import toast from 'react-hot-toast'
import styles from './PlaceOrder.module.css'

declare global {
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Razorpay: any
}
}

type Props = {
cartData: []
}

export default function PlaceOrder({ cartData }: Props) {
const serverStatus = useServerStatus()

const amount = totalCartPrice(cartData)

const handleCheckout = async () => {
const performCheckout = async () => {
const {
data: { key },
} = await axios.get('http://www.localhost:5000/api/v1/get-key')
Expand All @@ -34,12 +45,25 @@ export default function PlaceOrder({ cartData }: Props) {
address: 'Razorpay Corporate Office',
},
}
// @ts-expect-error Custom Razorpay script in index.html

const razor = new window.Razorpay(options)

razor.open()
}

const handleCheckout = async () => {
if (!serverStatus?.status) {
// @ts-expect-error ...
return toast.error(serverStatus?.message)
}
if (serverStatus?.status)
toast.promise(performCheckout(), {
loading: 'Processing...',
success: <b>Done!</b>,
error: 'Server is not running',
})
}

return (
<div className={styles.placeOrder}>
<button onClick={handleCheckout}>PLACE ORDER</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useAppDispatch } from '@/state/store'
import { useEffect } from 'react'
import { useSearchParams } from 'react-router-dom'

export default function PaymentSuccess() {
export default function PaymentSuccessPage() {
const seachQuery = useSearchParams()[0]
const referenceNum = seachQuery.get('reference')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import Paper from '@mui/material/Paper'
import Rating from '@mui/material/Rating'
import { Link } from 'react-router-dom'

import { ProductType } from '@/types'
import type { ProductType } from '@/types'
import styles from './ProductCard.module.css'

type Props = {
type ProductProps = {
product: ProductType
}

export default function ProductCard({ product }: Props) {
export default function ProductCard({ product }: ProductProps) {
return (
<div>
<Link key={product.id} to={`/products/${product.id}`}>
Expand Down
4 changes: 2 additions & 2 deletions client/src/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
CartPage,
HomePage,
LoginPage,
PaymentSuccess,
PaymentSuccessPage,
ProductDetailsPage,
ProductListingPage,
SignUpPage,
Expand All @@ -24,7 +24,7 @@ const router = createBrowserRouter(
<Route path="/cart" element={<CartPage />} />
<Route path="/login" element={<LoginPage />} />
<Route path="/signup" element={<SignUpPage />} />
<Route path="/payment-success" element={<PaymentSuccess />} />
<Route path="/payment-success" element={<PaymentSuccessPage />} />
</Route>
)
)
Expand Down
1 change: 1 addition & 0 deletions client/src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
interface ImportMeta {
env: {
VITE_API_BASE_URL: string
VITE_SERVER_URL: string
}
}
Loading

0 comments on commit 0ef4aa7

Please sign in to comment.