From 001d39d4da2ed4067e7b4af7e18551090a84b242 Mon Sep 17 00:00:00 2001 From: dappsar Date: Wed, 13 Dec 2023 15:29:57 -0300 Subject: [PATCH] [feat] :sparkles: add base match endpoint --- src/components/Navbar/Navbar.jsx | 6 ++- src/components/Navbar/NotificationInfo.jsx | 51 +++++++++++++--------- src/context/NotificationContext.js | 11 +++-- src/context/Web3Context.js | 7 ++- src/pages/api/match.js | 43 ++++++++++++++++++ src/styles/_navbar-notification.scss | 5 +-- 6 files changed, 90 insertions(+), 33 deletions(-) create mode 100644 src/pages/api/match.js diff --git a/src/components/Navbar/Navbar.jsx b/src/components/Navbar/Navbar.jsx index 0d1ed584..bd3f5496 100644 --- a/src/components/Navbar/Navbar.jsx +++ b/src/components/Navbar/Navbar.jsx @@ -56,9 +56,11 @@ function Navbar() { useEffect(() => { const notif = getNotificationsByUser(walletAddress) || [] - const unreadNotifications = notif.filter(notification => !notification.read); + const unreadNotifications = notif.filter((notification) => !notification.read) setNotificationsNbr(unreadNotifications.length) - setNotificationsNbrClass(unreadNotifications.length > 9 ? 'notification__badge__2' : 'notification__badge__1') + setNotificationsNbrClass( + unreadNotifications.length > 9 ? 'notification__badge__2' : 'notification__badge__1' + ) // setNotificationsNbr(20) // setNotificationsNbrClass(20 > 9 ? 'notification__badge__2' : 'notification__badge__1') }, [notifications, walletAddress]) diff --git a/src/components/Navbar/NotificationInfo.jsx b/src/components/Navbar/NotificationInfo.jsx index 08db827f..3879502d 100644 --- a/src/components/Navbar/NotificationInfo.jsx +++ b/src/components/Navbar/NotificationInfo.jsx @@ -40,9 +40,9 @@ const NotificationInfo = ({ showNotificationInfo, setShowNotificationInfo }) => const existsUnreadNotifications = () => { if (!updatedNotifications || updatedNotifications.length === 0) { - return false; + return false } - return updatedNotifications.some(notification => !notification.read) + return updatedNotifications.some((notification) => !notification.read) } const handleRead = (notification, event) => { @@ -85,8 +85,8 @@ const NotificationInfo = ({ showNotificationInfo, setShowNotificationInfo }) => deleteAllNotifications(walletAddress) } - const NotificationTitle = () => ( - updatedNotifications && updatedNotifications.length > 0 ? + const NotificationTitle = () => + updatedNotifications && updatedNotifications.length > 0 ? (

{t('notification_title')}

@@ -95,24 +95,30 @@ const NotificationInfo = ({ showNotificationInfo, setShowNotificationInfo }) => {!existsUnreadNotifications() ? ( ) : ( - { handleReadAll(event) }} + { + handleReadAll(event) + }} /> )}
- { handleDeleteAll(event) }} + { + handleDeleteAll(event) + }} />
- : -
-
-

{t('notification_title')}

+ ) : ( +
+
+

{t('notification_title')}

+
-
- ) + ) const NotificationMessage = ({ notification }) => ( @@ -131,14 +137,20 @@ const NotificationInfo = ({ showNotificationInfo, setShowNotificationInfo }) => {notification.read ? ( ) : ( - { handleRead(notification, event) }} + { + handleRead(notification, event) + }} /> )}
- { handleDelete(notification, event) }} + { + handleDelete(notification, event) + }} />
@@ -151,8 +163,7 @@ const NotificationInfo = ({ showNotificationInfo, setShowNotificationInfo }) => const NotificationMessages = () => ( - {updatedNotifications.slice(0, 7).map((notification, index) => - ( + {updatedNotifications.slice(0, 7).map((notification, index) => ( ))} {/* diff --git a/src/context/NotificationContext.js b/src/context/NotificationContext.js index 64d5ab3c..980b1aae 100644 --- a/src/context/NotificationContext.js +++ b/src/context/NotificationContext.js @@ -10,8 +10,9 @@ export const NotificationProvider = ({ children }) => { const getNotificationsByUser = (user) => { if (!user) return notifications - return notifications.filter((notification) => - notification.walletAddress === user && notification.deleted === false) + return notifications.filter( + (notification) => notification.walletAddress === user && notification.deleted === false + ) } useEffect(() => { @@ -51,13 +52,15 @@ export const NotificationProvider = ({ children }) => { const readAllNotifications = (user) => { const updatedNotifs = notifications.map((notification) => - notification.walletAddress === user ? { ...notification, read: true } : notification + notification.walletAddress === user ? { ...notification, read: true } : notification ) setNotifications(updatedNotifs) } const deleteAllNotifications = (user) => { - const updatedNotifs = notifications.filter((notification) => notification.walletAddress !== user) + const updatedNotifs = notifications.filter( + (notification) => notification.walletAddress !== user + ) setNotifications(updatedNotifs) } diff --git a/src/context/Web3Context.js b/src/context/Web3Context.js index 19ce80df..523fded5 100644 --- a/src/context/Web3Context.js +++ b/src/context/Web3Context.js @@ -12,7 +12,6 @@ import { CONTRACTS, NETWORK } from '../config' import { NotificationContext } from './NotificationContext' import { getAccountAddressText } from '../utils/stringUtils' - const initialState = { connectWallet: () => {} } @@ -100,8 +99,8 @@ function Web3ContextProvider({ children }) { gammaPacksContractInstance.on('PackTransfer', (from, to, tokenId) => { const packNbr = ethers.BigNumber.from(tokenId).toNumber() addNotification(to, 'notification_pack_transfer', [ - {item: 'PACK', value: packNbr, valueShort: packNbr }, - {item: 'WALLET', value: from, valueShort: getAccountAddressText(from) } + { item: 'PACK', value: packNbr, valueShort: packNbr }, + { item: 'WALLET', value: from, valueShort: getAccountAddressText(from) } ]) }) @@ -109,7 +108,7 @@ function Web3ContextProvider({ children }) { addNotification(to, 'notification_exchange', [ { item: 'CARD_RECEIVED', value: cNFrom, valueShort: cNFrom }, { item: 'CARD_SENT', value: cNTo, valueShort: cNTo }, - { item: 'WALLET', value: from, valueShort: getAccountAddressText(from) }, + { item: 'WALLET', value: from, valueShort: getAccountAddressText(from) } ]) addNotification(from, 'notification_exchange', [ { item: 'CARD_RECEIVED', value: cNTo, valueShort: cNTo }, diff --git a/src/pages/api/match.js b/src/pages/api/match.js new file mode 100644 index 00000000..3742aa66 --- /dev/null +++ b/src/pages/api/match.js @@ -0,0 +1,43 @@ +import { ethers } from 'ethers' +import { NETWORK, CONTRACTS } from '../../config' +import gammaCardsAbi from '../../context/abis/GammaCards.v5.sol/NofGammaCardsV5.json' +import { getCardsByUser } from '../../services/gamma' + +// example call: http://localhost:3000/api/match?w1=0x117b706DEF40310eF5926aB57868dAcf46605b8d&w2=0x35dad65F60c1A32c9895BE97f6bcE57D32792E83 +export default async function handler(req, res) { + try { + const { w1, w2 } = req.query + + if (!w1 || !w2) { + res.status(400).json({ + error: 'Please provide the wallet addresses (w1 and w2) as query parameters' + }) + return + } + + const provider = new ethers.providers.JsonRpcProvider(NETWORK.chainNodeProviderUrl) + const gammaCardsContractInstance = new ethers.Contract( + CONTRACTS.gammaCardsAddress, + gammaCardsAbi.abi, + provider + ) + const u1Cards = (await getCardsByUser(gammaCardsContractInstance, w1)).user + const u2Cards = (await getCardsByUser(gammaCardsContractInstance, w2)).user + + // TODO: obtener repetidas + // TODO: ver las que ambos no tienen + // TODO: obtener match + + res.setHeader('Content-Type', 'application/json') + res.status(200).json({ + user1: u1Cards, + user2: u2Cards, + match: false + }) + } catch (error) { + console.error(error) + res.status(500).json({ + error: 'An error occurred while processing the request.' + }) + } +} diff --git a/src/styles/_navbar-notification.scss b/src/styles/_navbar-notification.scss index 8544539b..3debe963 100644 --- a/src/styles/_navbar-notification.scss +++ b/src/styles/_navbar-notification.scss @@ -99,7 +99,6 @@ display: flex; align-items: center; justify-content: space-between; - } .notification__info__icon__link, @@ -173,7 +172,7 @@ width: 23px; height: 23px; font-size: 8px; - } + } } .notification__badge__2 { @@ -185,4 +184,4 @@ width: 25px; height: 25px; } -} \ No newline at end of file +}