Enable editing of zap split messages

This commit is contained in:
danidfra 2024-09-23 10:53:08 -03:00
parent a66dd2b263
commit 275513ea16
2 changed files with 36 additions and 10 deletions

View File

@ -1,11 +1,11 @@
import { useState, useEffect } from 'react'; import { useState, useEffect } from 'react';
import { defineMessages } from 'react-intl'; import { defineMessages } from 'react-intl';
import { type INewAccount } from 'soapbox/features/admin/manage-zap-split';
import { useApi } from 'soapbox/hooks'; import { useApi } from 'soapbox/hooks';
import { baseZapAccountSchema, ZapSplitData } from 'soapbox/schemas/zap-split'; import { baseZapAccountSchema, ZapSplitData } from 'soapbox/schemas/zap-split';
import toast from 'soapbox/toast'; import toast from 'soapbox/toast';
import { type INewAccount } from '../../../features/admin/manage-zap-split';
const messages = defineMessages({ const messages = defineMessages({
zapSplitFee: { id: 'manage.zap_split.fees_error_message', defaultMessage: 'The fees cannot exceed 50% of the total zap.' }, zapSplitFee: { id: 'manage.zap_split.fees_error_message', defaultMessage: 'The fees cannot exceed 50% of the total zap.' },
@ -18,6 +18,7 @@ export const useManageZapSplit = () => {
const api = useApi(); const api = useApi();
const [formattedData, setFormattedData] = useState<ZapSplitData[]>([]); const [formattedData, setFormattedData] = useState<ZapSplitData[]>([]);
const [weights, setWeights] = useState<{ [id: string]: number }>({}); const [weights, setWeights] = useState<{ [id: string]: number }>({});
const [message, setMessage] = useState<{ [id: string]: string }>({});
const fetchZapSplitData = async () => { const fetchZapSplitData = async () => {
try { try {
@ -31,6 +32,11 @@ export const useManageZapSplit = () => {
return acc; return acc;
}, {} as { [id: string]: number }); }, {} as { [id: string]: number });
setWeights(initialWeights); setWeights(initialWeights);
const initialMessages = normalizedData.reduce((acc, item) => {
acc[item.account.id] = item.message;
return acc;
}, {} as { [id: string]: string });
setMessage(initialMessages);
} }
} catch (error) { } catch (error) {
toast.error(messages.fetchErrorMessage); toast.error(messages.fetchErrorMessage);
@ -48,11 +54,18 @@ export const useManageZapSplit = () => {
})); }));
}; };
const sendNewSplit = async (newAccount?: INewAccount) => { const handleMessageChange = (accountId: string, newMessage: string) => {
setMessage((prevMessage) => ({
...prevMessage,
[accountId]: newMessage,
}));
};
const sendNewSplit = async (newAccount?: INewAccount, newMessage?: string) => {
try { try {
const updatedZapSplits = formattedData.reduce((acc: { [id: string]: { message: string; weight: number } }, zapData) => { const updatedZapSplits = formattedData.reduce((acc: { [id: string]: { message: string; weight: number } }, zapData) => {
acc[zapData.account.id] = { acc[zapData.account.id] = {
message: zapData.message, message: message[zapData.account.id] || zapData.message,
weight: weights[zapData.account.id] || zapData.weight, weight: weights[zapData.account.id] || zapData.weight,
}; };
return acc; return acc;
@ -95,6 +108,8 @@ export const useManageZapSplit = () => {
return { return {
formattedData, formattedData,
weights, weights,
message,
handleMessageChange,
handleWeightChange, handleWeightChange,
sendNewSplit, sendNewSplit,
removeAccount, removeAccount,

View File

@ -3,7 +3,7 @@ import { FormattedMessage, defineMessages, useIntl } from 'react-intl';
import Account from 'soapbox/components/account'; import Account from 'soapbox/components/account';
import List, { ListItem } from 'soapbox/components/list'; import List, { ListItem } from 'soapbox/components/list';
import { Button, Column, HStack, Stack } from 'soapbox/components/ui'; import { Button, Column, HStack, Input, Stack } from 'soapbox/components/ui';
import { useManageZapSplit } from '../../api/hooks/admin/useManageZapSplit'; import { useManageZapSplit } from '../../api/hooks/admin/useManageZapSplit';
import AddNewAccount from '../ui/components/new-account-zap-split'; import AddNewAccount from '../ui/components/new-account-zap-split';
@ -23,7 +23,7 @@ interface INewAccount{
const ManageZapSplit: React.FC = () => { const ManageZapSplit: React.FC = () => {
const intl = useIntl(); const intl = useIntl();
const { formattedData, weights, handleWeightChange, sendNewSplit, removeAccount } = useManageZapSplit(); const { formattedData, weights, message, handleMessageChange, handleWeightChange, sendNewSplit, removeAccount } = useManageZapSplit();
const [hasNewAccount, setHasNewAccount] = useState(false); const [hasNewAccount, setHasNewAccount] = useState(false);
const [newWeight, setNewWeight] = useState(0.05); const [newWeight, setNewWeight] = useState(0.05);
const [newAccount, setNewAccount] = useState<INewAccount>({ acc: '', message: '', weight: Number((newWeight * 100).toFixed()) }); const [newAccount, setNewAccount] = useState<INewAccount>({ acc: '', message: '', weight: Number((newWeight * 100).toFixed()) });
@ -54,17 +54,28 @@ const ManageZapSplit: React.FC = () => {
<List> <List>
{formattedData.map((data) => ( {formattedData.map((data) => (
<ListItem key={data.account.id} label={''}> <ListItem key={data.account.id} label={''}>
<div className='relative flex w-full flex-col items-start justify-center gap-4 sm:flex-row sm:justify-between'> <div className='relative flex w-full flex-col items-start justify-center gap-4 pt-2 md:flex-row md:items-center md:justify-between md:pt-0'>
<div className='flex w-60 justify-center sm:justify-start '> <div className='flex min-w-60 items-center justify-center md:justify-start'>
<Account account={data.account} showProfileHoverCard={false} /> <Account account={data.account} showProfileHoverCard={false} />
</div> </div>
<HStack space={2} className='w-full sm:justify-end'>
<div className='flex w-[96%] flex-col justify-center md:w-full'>
<FormattedMessage id='manage.zap_split.new_account_message' defaultMessage='Message:' />
<Input
className='md:-mt-1'
value={message[data.account.id] || ''}
onChange={(e) =>
handleMessageChange(data.account.id, e.target.value)
}
/>
</div>
<HStack space={2} className='w-full md:justify-end'>
<ZapSplitSlider <ZapSplitSlider
width='w-[96%] sm:w-40' width='w-[96%] md:w-40'
initialWeight={weights[data.account.id] || 0} initialWeight={weights[data.account.id] || 0}
onWeightChange={(weight) => handleWeightChange(data.account.id, weight)} onWeightChange={(weight) => handleWeightChange(data.account.id, weight)}
/> />
<Button type='button' className='absolute right-0 top-2 sm:relative sm:right-0 sm:top-0' size='xs' icon={closeIcon} theme='transparent' onClick={() => removeAccount(data.account.id)} /> <Button type='button' className='absolute right-0 top-0 md:relative' size='xs' icon={closeIcon} theme='transparent' onClick={() => removeAccount(data.account.id)} />
</HStack> </HStack>
</div> </div>
</ListItem> </ListItem>