Fix rating

This commit is contained in:
koalasat
2025-03-26 17:53:29 +01:00
parent 849e32d1ad
commit 05f4995c93
2 changed files with 37 additions and 37 deletions

View File

@ -1,4 +1,4 @@
import React, { useContext, useState } from 'react'; import React, { useContext, useEffect, useState } from 'react';
import { useTranslation, Trans } from 'react-i18next'; import { useTranslation, Trans } from 'react-i18next';
import { import {
Grid, Grid,
@ -17,7 +17,7 @@ import currencies from '../../../../static/assets/currencies.json';
import TradeSummary from '../TradeSummary'; import TradeSummary from '../TradeSummary';
import { Favorite, RocketLaunch, ContentCopy, Refresh, Info } from '@mui/icons-material'; import { Favorite, RocketLaunch, ContentCopy, Refresh, Info } from '@mui/icons-material';
import { LoadingButton } from '@mui/lab'; import { LoadingButton } from '@mui/lab';
import { finalizeEvent, type Event } from 'nostr-tools';
import { type Order } from '../../../models'; import { type Order } from '../../../models';
import { systemClient } from '../../../services/System'; import { systemClient } from '../../../services/System';
import { import {
@ -25,11 +25,11 @@ import {
type UseFederationStoreType, type UseFederationStoreType,
} from '../../../contexts/FederationContext'; } from '../../../contexts/FederationContext';
import { type UseAppStoreType, AppContext } from '../../../contexts/AppContext'; import { type UseAppStoreType, AppContext } from '../../../contexts/AppContext';
import { GarageContext, type UseGarageStoreType } from '../../../contexts/GarageContext';
interface SuccessfulPromptProps { interface SuccessfulPromptProps {
order: Order; order: Order;
rateUserPlatform: (rating: number) => void; rateUserPlatform: (rating: number) => void;
rateHostPlatform: (rating: number) => void;
onClickStartAgain: () => void; onClickStartAgain: () => void;
onClickRenew: () => void; onClickRenew: () => void;
loadingRenew: boolean; loadingRenew: boolean;
@ -38,7 +38,6 @@ interface SuccessfulPromptProps {
export const SuccessfulPrompt = ({ export const SuccessfulPrompt = ({
order, order,
rateUserPlatform, rateUserPlatform,
rateHostPlatform,
onClickStartAgain, onClickStartAgain,
onClickRenew, onClickRenew,
loadingRenew, loadingRenew,
@ -47,8 +46,39 @@ export const SuccessfulPrompt = ({
const currencyCode: string = currencies[`${order.currency}`]; const currencyCode: string = currencies[`${order.currency}`];
const { settings } = useContext<UseAppStoreType>(AppContext); const { settings } = useContext<UseAppStoreType>(AppContext);
const { federation } = useContext<UseFederationStoreType>(FederationContext); const { federation } = useContext<UseFederationStoreType>(FederationContext);
const { garage } = useContext<UseGarageStoreType>(GarageContext);
const [hostRating, setHostRating] = useState<string>(); 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);
};
useEffect(() => {
rateHostPlatform();
}, [hostRating]);
return ( return (
<Grid <Grid
@ -98,7 +128,6 @@ export const SuccessfulPrompt = ({
size='large' size='large'
onChange={(e) => { onChange={(e) => {
const rate = e.target.value; const rate = e.target.value;
rateHostPlatform(rate);
setHostRating(rate); setHostRating(rate);
}} }}
/> />
@ -113,7 +142,7 @@ export const SuccessfulPrompt = ({
justifyContent: 'center', justifyContent: 'center',
}} }}
> >
{hostRating === '5' ? ( {hostRating === 5 ? (
<> <>
<Typography variant='body2' align='center'> <Typography variant='body2' align='center'>
<b> <b>
@ -130,7 +159,7 @@ export const SuccessfulPrompt = ({
</Typography> </Typography>
)} )}
</div> </div>
{hostRating === '5' ? ( {hostRating === 5 ? (
<Typography variant='body2' align='center'> <Typography variant='body2' align='center'>
{t( {t(
'RoboSats gets better with more liquidity and users. Tell a bitcoiner friend about Robosats!', 'RoboSats gets better with more liquidity and users. Tell a bitcoiner friend about Robosats!',

View File

@ -1,7 +1,6 @@
import React, { useState, useEffect, useContext } from 'react'; import React, { useState, useEffect, useContext } from 'react';
import { Box, Divider, Grid } from '@mui/material'; import { Box, Divider, Grid } from '@mui/material';
import { getWebln, pn } from '../../utils'; import { getWebln, pn } from '../../utils';
import { finalizeEvent, type Event } from 'nostr-tools';
import { import {
ConfirmCancelDialog, ConfirmCancelDialog,
ConfirmCollabCancelDialog, ConfirmCollabCancelDialog,
@ -313,31 +312,6 @@ const TradeBox = ({ currentOrder, onStartAgain }: TradeBoxProps): JSX.Element =>
submitAction({ action: 'rate_platform', rating }); 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 handleWebln = async (order: Order): Promise<void> => {
const webln = await getWebln().catch(() => { const webln = await getWebln().catch(() => {
console.log('WebLN not available'); console.log('WebLN not available');
@ -643,7 +617,6 @@ const TradeBox = ({ currentOrder, onStartAgain }: TradeBoxProps): JSX.Element =>
<SuccessfulPrompt <SuccessfulPrompt
order={order} order={order}
rateUserPlatform={rateUserPlatform} rateUserPlatform={rateUserPlatform}
rateHostPlatform={rateHostPlatform}
onClickStartAgain={onStartAgain} onClickStartAgain={onStartAgain}
loadingRenew={loadingButtons.renewOrder} loadingRenew={loadingButtons.renewOrder}
onClickRenew={() => { onClickRenew={() => {
@ -668,7 +641,6 @@ const TradeBox = ({ currentOrder, onStartAgain }: TradeBoxProps): JSX.Element =>
<SuccessfulPrompt <SuccessfulPrompt
order={order} order={order}
rateUserPlatform={rateUserPlatform} rateUserPlatform={rateUserPlatform}
rateHostPlatform={rateHostPlatform}
onClickStartAgain={onStartAgain} onClickStartAgain={onStartAgain}
loadingRenew={loadingButtons.renewOrder} loadingRenew={loadingButtons.renewOrder}
onClickRenew={() => { onClickRenew={() => {
@ -708,7 +680,6 @@ const TradeBox = ({ currentOrder, onStartAgain }: TradeBoxProps): JSX.Element =>
<SuccessfulPrompt <SuccessfulPrompt
order={order} order={order}
rateUserPlatform={rateUserPlatform} rateUserPlatform={rateUserPlatform}
rateHostPlatform={rateHostPlatform}
onClickStartAgain={onStartAgain} onClickStartAgain={onStartAgain}
loadingRenew={loadingButtons.renewOrder} loadingRenew={loadingButtons.renewOrder}
onClickRenew={() => { onClickRenew={() => {