Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix rating
  • Loading branch information
KoalaSat committed Mar 26, 2025
commit 05f4995c93d8fdbdcc0b0ffac671a5aeaf1f431f
45 changes: 37 additions & 8 deletions frontend/src/components/TradeBox/Prompts/Successful.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -17,19 +17,19 @@ 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 {
FederationContext,
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;
Expand All @@ -38,7 +38,6 @@ interface SuccessfulPromptProps {
export const SuccessfulPrompt = ({
order,
rateUserPlatform,
rateHostPlatform,
onClickStartAgain,
onClickRenew,
loadingRenew,
Expand All @@ -47,8 +46,39 @@ export const SuccessfulPrompt = ({
const currencyCode: string = currencies[`${order.currency}`];
const { settings } = useContext<UseAppStoreType>(AppContext);
const { federation } = useContext<UseFederationStoreType>(FederationContext);
const { garage } = useContext<UseGarageStoreType>(GarageContext);

const [hostRating, setHostRating] = useState<number>();

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);
};

const [hostRating, setHostRating] = useState<string>();
useEffect(() => {
rateHostPlatform();
}, [hostRating]);

return (
<Grid
Expand Down Expand Up @@ -98,7 +128,6 @@ export const SuccessfulPrompt = ({
size='large'
onChange={(e) => {
const rate = e.target.value;
rateHostPlatform(rate);
setHostRating(rate);
}}
/>
Expand All @@ -113,7 +142,7 @@ export const SuccessfulPrompt = ({
justifyContent: 'center',
}}
>
{hostRating === '5' ? (
{hostRating === 5 ? (
<>
<Typography variant='body2' align='center'>
<b>
Expand All @@ -130,7 +159,7 @@ export const SuccessfulPrompt = ({
</Typography>
)}
</div>
{hostRating === '5' ? (
{hostRating === 5 ? (
<Typography variant='body2' align='center'>
{t(
'RoboSats gets better with more liquidity and users. Tell a bitcoiner friend about Robosats!',
Expand Down
29 changes: 0 additions & 29 deletions frontend/src/components/TradeBox/index.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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<void> => {
const webln = await getWebln().catch(() => {
console.log('WebLN not available');
Expand Down Expand Up @@ -643,7 +617,6 @@ const TradeBox = ({ currentOrder, onStartAgain }: TradeBoxProps): JSX.Element =>
<SuccessfulPrompt
order={order}
rateUserPlatform={rateUserPlatform}
rateHostPlatform={rateHostPlatform}
onClickStartAgain={onStartAgain}
loadingRenew={loadingButtons.renewOrder}
onClickRenew={() => {
Expand All @@ -668,7 +641,6 @@ const TradeBox = ({ currentOrder, onStartAgain }: TradeBoxProps): JSX.Element =>
<SuccessfulPrompt
order={order}
rateUserPlatform={rateUserPlatform}
rateHostPlatform={rateHostPlatform}
onClickStartAgain={onStartAgain}
loadingRenew={loadingButtons.renewOrder}
onClickRenew={() => {
Expand Down Expand Up @@ -708,7 +680,6 @@ const TradeBox = ({ currentOrder, onStartAgain }: TradeBoxProps): JSX.Element =>
<SuccessfulPrompt
order={order}
rateUserPlatform={rateUserPlatform}
rateHostPlatform={rateHostPlatform}
onClickStartAgain={onStartAgain}
loadingRenew={loadingButtons.renewOrder}
onClickRenew={() => {
Expand Down