diff --git a/.gitignore b/.gitignore index 5ef6a52..e2764f9 100644 --- a/.gitignore +++ b/.gitignore @@ -39,3 +39,6 @@ yarn-error.log* # typescript *.tsbuildinfo next-env.d.ts + +# clerk configuration (can include secrets) +/.clerk/ diff --git a/src/app/market/page.tsx b/src/app/market/page.tsx new file mode 100644 index 0000000..cf228d7 --- /dev/null +++ b/src/app/market/page.tsx @@ -0,0 +1,153 @@ +"use client" +import { useEffect, useState, useRef } from 'react'; +import axios from 'axios'; +import { MagnifyingGlassIcon } from '@heroicons/react/24/outline'; +import style from './style.module.css'; + +interface CryptoData { + id: string; + name: string; + symbol: string; + current_price: number; + market_cap: number; + total_volume: number; + price_change_percentage_24h: number; + image: string; +} + +const Home = () => { + const [cryptoData, setCryptoData] = useState([]); + const [searchTerm, setSearchTerm] = useState(''); + const [minPrice, setMinPrice] = useState(''); + const [maxPrice, setMaxPrice] = useState(''); + const [loading, setLoading] = useState(true); + const [sortOption, setSortOption] = useState(''); + const widgetRef = useRef(null); + + const fetchCryptoData = async () => { + setLoading(true); // شروع بارگذاری داده‌ها + try { + const response = await axios.get('https://api.coingecko.com/api/v3/coins/markets', { + params: { + vs_currency: 'usd', + order: sortOption, + per_page: 1000, + page: 1, + sparkline: false, + }, + }); + setCryptoData(response.data); + } catch (error) { + console.error('Error fetching data:', error); + } finally { + setLoading(false); // پایان بارگذاری داده‌ها + } + }; + + useEffect(() => { + fetchCryptoData(); + const interval = setInterval(fetchCryptoData, 30000); // هر ۵ ثانیه به‌روز شود + + return () => clearInterval(interval); // پاک کردن interval هنگام unmount + }, [sortOption]); + + const formatPrice = (price: number): string => { + return price < 1 ? price.toFixed(8) : price.toFixed(2); + }; + + const filteredData = cryptoData.filter(crypto => { + const matchesSearchTerm = crypto.name.toLowerCase().includes(searchTerm.toLowerCase()) || + crypto.symbol.toLowerCase().includes(searchTerm.toLowerCase()); + const matchesMinPrice = minPrice ? crypto.current_price >= parseFloat(minPrice) : true; + const matchesMaxPrice = maxPrice ? crypto.current_price <= parseFloat(maxPrice) : true; + + return matchesSearchTerm && matchesMinPrice && matchesMaxPrice; + }); + + // useEffect(() => { + // fetchCryptoData(); + // const interval = setInterval(fetchCryptoData, 30000); // هر 30 ثانیه داده‌ها به‌روز شوند + // return () => clearInterval(interval); + // }, [sortOption]); + + + // const filteredData = cryptoData.filter(crypto => { + // const matchesSearchTerm = crypto.name.toLowerCase().includes(searchTerm.toLowerCase()) || + // crypto.symbol.toLowerCase().includes(searchTerm.toLowerCase()); + // const matchesMinPrice = minPrice ? crypto.current_price >= parseFloat(minPrice) : true; + // const matchesMaxPrice = maxPrice ? crypto.current_price <= parseFloat(maxPrice) : true; + // return matchesSearchTerm && matchesMinPrice && matchesMaxPrice; + // }); + + + return ( +
+ {/* جستجو */} +
+
+ setSearchTerm(e.target.value)} + placeholder="Search by name or symbol..." + className="w-full px-4 py-3 pl-12 rounded-xl + dark:bg-white/5 bg-white + dark:text-white text-gray-900 + dark:border-white/10 border-gray-200 border + focus:outline-none focus:ring-2 focus:ring-[#1890ff] + placeholder:dark:text-gray-500 placeholder:text-gray-400 + transition-all duration-200"/> + +
+
+ {/* نمایش جدول */} + + + + + + + + + + + + {loading ? ( + + + + + + ) : ( + + { + filteredData.length === 0 ? ( + + + + ) : ( + filteredData.map(crypto => ( + + + + + + + + + )) + ) + } + + )} +
IconNamePriceMarket Cap24h Volume24h Change

loading

No results found ☹️
{crypto.name} {crypto.name} ${crypto.current_price}${crypto.market_cap.toLocaleString()}${crypto.total_volume.toLocaleString()} + {crypto.price_change_percentage_24h.toFixed(2)}% +
+ +
+ ); +}; + +export default Home; + + diff --git a/src/app/market/style.module.css b/src/app/market/style.module.css new file mode 100644 index 0000000..1d66260 --- /dev/null +++ b/src/app/market/style.module.css @@ -0,0 +1,20 @@ +.loader { + width: 24px; + height: 24px; + border: 5px solid #FFF; + border-bottom-color: transparent; + border-radius: 50%; + display: inline-block; + box-sizing: border-box; + animation: rotation 1s linear infinite; + } + + @keyframes rotation { + 0% { + transform: rotate(0deg); + } + 100% { + transform: rotate(360deg); + } + } + \ No newline at end of file