Skip to content

Commit

Permalink
Merge pull request #245 from P4-Games/feature/match-endpoint
Browse files Browse the repository at this point in the history
[feat] ✨ add base match endpoint
  • Loading branch information
dappsar authored Dec 13, 2023
2 parents 74d9280 + 001d39d commit a47473d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 33 deletions.
6 changes: 4 additions & 2 deletions src/components/Navbar/Navbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
51 changes: 31 additions & 20 deletions src/components/Navbar/NotificationInfo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -85,8 +85,8 @@ const NotificationInfo = ({ showNotificationInfo, setShowNotificationInfo }) =>
deleteAllNotifications(walletAddress)
}

const NotificationTitle = () => (
updatedNotifications && updatedNotifications.length > 0 ?
const NotificationTitle = () =>
updatedNotifications && updatedNotifications.length > 0 ? (
<div className='notification__info__icon__and__link'>
<div className='notification__info__link__container'>
<p className={`notification__info__title`}>{t('notification_title')}</p>
Expand All @@ -95,24 +95,30 @@ const NotificationInfo = ({ showNotificationInfo, setShowNotificationInfo }) =>
{!existsUnreadNotifications() ? (
<VscMailRead className='notification__info__icon__read' />
) : (
<IoMailUnreadOutline className='notification__info__icon'
onClick={(event) => { handleReadAll(event) }}
<IoMailUnreadOutline
className='notification__info__icon'
onClick={(event) => {
handleReadAll(event)
}}
/>
)}
</div>
<div className='notification__info__icon__container'>
<RiDeleteBin2Line className='notification__info__icon'
onClick={(event) => { handleDeleteAll(event) }}
<RiDeleteBin2Line
className='notification__info__icon'
onClick={(event) => {
handleDeleteAll(event)
}}
/>
</div>
</div>
:
<div className='notification__info__icon__and__link'>
<div>
<p className={`notification__info__title`}>{t('notification_title')}</p>
) : (
<div className='notification__info__icon__and__link'>
<div>
<p className={`notification__info__title`}>{t('notification_title')}</p>
</div>
</div>
</div>
)
)

const NotificationMessage = ({ notification }) => (
<React.Fragment>
Expand All @@ -131,14 +137,20 @@ const NotificationInfo = ({ showNotificationInfo, setShowNotificationInfo }) =>
{notification.read ? (
<VscMailRead className='notification__info__icon__read' />
) : (
<IoMailUnreadOutline className='notification__info__icon'
onClick={(event) => { handleRead(notification, event) }}
<IoMailUnreadOutline
className='notification__info__icon'
onClick={(event) => {
handleRead(notification, event)
}}
/>
)}
</div>
<div className='notification__info__icon__container'>
<RiDeleteBin2Line className='notification__info__icon'
onClick={(event) => { handleDelete(notification, event) }}
<RiDeleteBin2Line
className='notification__info__icon'
onClick={(event) => {
handleDelete(notification, event)
}}
/>
</div>
</div>
Expand All @@ -151,8 +163,7 @@ const NotificationInfo = ({ showNotificationInfo, setShowNotificationInfo }) =>

const NotificationMessages = () => (
<React.Fragment>
{updatedNotifications.slice(0, 7).map((notification, index) =>
(
{updatedNotifications.slice(0, 7).map((notification, index) => (
<NotificationMessage key={`notif-${index}`} notification={notification} />
))}
{/*
Expand Down
11 changes: 7 additions & 4 deletions src/context/NotificationContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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)
}

Expand Down
7 changes: 3 additions & 4 deletions src/context/Web3Context.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { CONTRACTS, NETWORK } from '../config'
import { NotificationContext } from './NotificationContext'
import { getAccountAddressText } from '../utils/stringUtils'


const initialState = {
connectWallet: () => {}
}
Expand Down Expand Up @@ -100,16 +99,16 @@ 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) }
])
})

gammaCardsContractInstance.on('ExchangeCardOffer', (from, to, cNFrom, cNTo) => {
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 },
Expand Down
43 changes: 43 additions & 0 deletions src/pages/api/match.js
Original file line number Diff line number Diff line change
@@ -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.'
})
}
}
5 changes: 2 additions & 3 deletions src/styles/_navbar-notification.scss
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@
display: flex;
align-items: center;
justify-content: space-between;

}

.notification__info__icon__link,
Expand Down Expand Up @@ -173,7 +172,7 @@
width: 23px;
height: 23px;
font-size: 8px;
}
}
}

.notification__badge__2 {
Expand All @@ -185,4 +184,4 @@
width: 25px;
height: 25px;
}
}
}

0 comments on commit a47473d

Please sign in to comment.