From 05f4995c93d8fdbdcc0b0ffac671a5aeaf1f431f Mon Sep 17 00:00:00 2001 From: koalasat Date: Wed, 26 Mar 2025 17:53:29 +0100 Subject: [PATCH] Fix rating --- .../TradeBox/Prompts/Successful.tsx | 45 +++++++++++++++---- frontend/src/components/TradeBox/index.tsx | 29 ------------ 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/frontend/src/components/TradeBox/Prompts/Successful.tsx b/frontend/src/components/TradeBox/Prompts/Successful.tsx index 6cc3b2f0..b67b14e1 100644 --- a/frontend/src/components/TradeBox/Prompts/Successful.tsx +++ b/frontend/src/components/TradeBox/Prompts/Successful.tsx @@ -1,4 +1,4 @@ -import React, { useContext, useState } from 'react'; +import React, { useContext, useEffect, useState } from 'react'; import { useTranslation, Trans } from 'react-i18next'; import { Grid, @@ -17,7 +17,7 @@ import currencies from '../../../../static/assets/currencies.json'; import TradeSummary from '../TradeSummary'; import { Favorite, RocketLaunch, ContentCopy, Refresh, Info } from '@mui/icons-material'; import { LoadingButton } from '@mui/lab'; - +import { finalizeEvent, type Event } from 'nostr-tools'; import { type Order } from '../../../models'; import { systemClient } from '../../../services/System'; import { @@ -25,11 +25,11 @@ import { type UseFederationStoreType, } from '../../../contexts/FederationContext'; import { type UseAppStoreType, AppContext } from '../../../contexts/AppContext'; +import { GarageContext, type UseGarageStoreType } from '../../../contexts/GarageContext'; interface SuccessfulPromptProps { order: Order; rateUserPlatform: (rating: number) => void; - rateHostPlatform: (rating: number) => void; onClickStartAgain: () => void; onClickRenew: () => void; loadingRenew: boolean; @@ -38,7 +38,6 @@ interface SuccessfulPromptProps { export const SuccessfulPrompt = ({ order, rateUserPlatform, - rateHostPlatform, onClickStartAgain, onClickRenew, loadingRenew, @@ -47,8 +46,39 @@ export const SuccessfulPrompt = ({ const currencyCode: string = currencies[`${order.currency}`]; const { settings } = useContext(AppContext); const { federation } = useContext(FederationContext); + const { garage } = useContext(GarageContext); - const [hostRating, setHostRating] = useState(); + const [hostRating, setHostRating] = useState(); + + const rateHostPlatform = function (): void { + if (!hostRating) return; + + const slot = garage.getSlot(); + const coordinatorPubKey = federation.getCoordinator(order.shortAlias)?.nostrHexPubkey; + + if (!slot?.nostrPubKey || !slot.nostrSecKey || !coordinatorPubKey || !order.id) return; + + const eventTemplate: Event = { + kind: 31986, + created_at: Math.floor(Date.now() / 1000), + tags: [ + ['d', `${order.shortAlias}:${order.id}`], + ['p', coordinatorPubKey], + ['rating', String(hostRating / 5)], + ], + content: '', + pubkey: slot.nostrPubKey, + id: '', + sig: '', + }; + + const signedEvent = finalizeEvent(eventTemplate, slot.nostrSecKey); + federation.roboPool.sendEvent(signedEvent); + }; + + useEffect(() => { + rateHostPlatform(); + }, [hostRating]); return ( { const rate = e.target.value; - rateHostPlatform(rate); setHostRating(rate); }} /> @@ -113,7 +142,7 @@ export const SuccessfulPrompt = ({ justifyContent: 'center', }} > - {hostRating === '5' ? ( + {hostRating === 5 ? ( <> @@ -130,7 +159,7 @@ export const SuccessfulPrompt = ({ )} - {hostRating === '5' ? ( + {hostRating === 5 ? ( {t( 'RoboSats gets better with more liquidity and users. Tell a bitcoiner friend about Robosats!', diff --git a/frontend/src/components/TradeBox/index.tsx b/frontend/src/components/TradeBox/index.tsx index a573c9f5..56fc5ffe 100644 --- a/frontend/src/components/TradeBox/index.tsx +++ b/frontend/src/components/TradeBox/index.tsx @@ -1,7 +1,6 @@ import React, { useState, useEffect, useContext } from 'react'; import { Box, Divider, Grid } from '@mui/material'; import { getWebln, pn } from '../../utils'; -import { finalizeEvent, type Event } from 'nostr-tools'; import { ConfirmCancelDialog, ConfirmCollabCancelDialog, @@ -313,31 +312,6 @@ const TradeBox = ({ currentOrder, onStartAgain }: TradeBoxProps): JSX.Element => submitAction({ action: 'rate_platform', rating }); }; - const rateHostPlatform = function (rating: number): void { - const slot = garage.getSlot(); - const coordinatorPubKey = federation.getCoordinator(currentOrder.shortAlias)?.nostrHexPubkey; - - if (!slot?.nostrPubKey || !slot.nostrSecKey || !coordinatorPubKey || !currentOrder.id) return; - - const eventTemplate: Event = { - kind: 31986, - created_at: Math.floor(Date.now() / 1000), - tags: [ - ['d', `${coordinatorPubKey}:${currentOrder.id}`], - ['e', ''], - ['p', coordinatorPubKey], - ['rating', String(rating / 5)], - ], - content: '', - pubkey: slot.nostrPubKey, - id: '', - sig: '', - }; - - const signedEvent = finalizeEvent(eventTemplate, slot.nostrSecKey); - federation.roboPool.sendEvent(signedEvent); - }; - const handleWebln = async (order: Order): Promise => { const webln = await getWebln().catch(() => { console.log('WebLN not available'); @@ -643,7 +617,6 @@ const TradeBox = ({ currentOrder, onStartAgain }: TradeBoxProps): JSX.Element => { @@ -668,7 +641,6 @@ const TradeBox = ({ currentOrder, onStartAgain }: TradeBoxProps): JSX.Element => { @@ -708,7 +680,6 @@ const TradeBox = ({ currentOrder, onStartAgain }: TradeBoxProps): JSX.Element => {