-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add server status check context
- Loading branch information
1 parent
5d89b86
commit d07a56c
Showing
14 changed files
with
123 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { ENV } from '@/conf' | ||
import { | ||
PropsWithChildren, | ||
createContext, | ||
useContext, | ||
useEffect, | ||
useState, | ||
} from 'react' | ||
|
||
export const ServerStatusContext = createContext<string | null>(null) | ||
|
||
export default function ServerStatusProvider({ children }: PropsWithChildren) { | ||
const [serverStatus, setServerStatus] = useState<string | null>('') | ||
|
||
useEffect(() => { | ||
const checkServerStatus = () => { | ||
fetch(`${ENV.SERVER_URL}/api/status`) | ||
.then((response) => response.json()) | ||
.then((data) => { | ||
if (data.status) { | ||
setServerStatus(data.message) | ||
} else { | ||
setServerStatus('Server is not running') | ||
} | ||
}) | ||
.catch(() => setServerStatus('Server is not running')) | ||
} | ||
|
||
// Call immediately | ||
checkServerStatus() | ||
|
||
// Then call every 5 seconds | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,30 @@ | ||
import { useServerStatus } from '@/context/ServerStatusProvider' | ||
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) | ||
const serverStatus = useServerStatus() | ||
console.log(serverStatus) | ||
|
||
if (!cartData?.length) { | ||
return <EmptyCart /> | ||
} | ||
|
||
return ( | ||
<div className={styles.cartContainer}> | ||
<div> | ||
{cartData?.map((product: CartType) => ( | ||
<CartItem product={product} key={product.id} /> | ||
))} | ||
<PlaceOrder cartData={cartData} /> | ||
<> | ||
{serverStatus === 'Server is not running' && <h4>{serverStatus}</h4>} | ||
<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> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,6 @@ | |
interface ImportMeta { | ||
env: { | ||
VITE_API_BASE_URL: string | ||
VITE_SERVER_URL: string | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters