Update: Add TSDoc comments

environments/review-create-ui-ume3nq/deployments/4833
danidfra 2024-09-23 11:25:48 -03:00
rodzic ec17e3a722
commit 1a088649aa
2 zmienionych plików z 56 dodań i 1 usunięć

Wyświetl plik

@ -14,12 +14,22 @@ const messages = defineMessages({
sucessMessage: { id: 'manage.zap_split.success_request', defaultMessage: 'Fees updated successfully.' }, sucessMessage: { id: 'manage.zap_split.success_request', defaultMessage: 'Fees updated successfully.' },
}); });
/**
* Custom hook that manages the logic for handling Zap Split data, including fetching, updating, and removing accounts.
* It handles the state for formatted data, weights, and messages associated with the Zap Split accounts.
*
* @returns An object with data, weights, message, and functions to manipulate them.
*/
export const useManageZapSplit = () => { 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 [message, setMessage] = useState<{ [id: string]: string }>({});
/**
* Fetches the Zap Split data from the API, parses it, and sets the state for formatted data, weights, and messages.
* Displays an error toast if the request fails.
*/
const fetchZapSplitData = async () => { const fetchZapSplitData = async () => {
try { try {
const { data } = await api.get<ZapSplitData[]>('/api/v1/ditto/zap_splits'); const { data } = await api.get<ZapSplitData[]>('/api/v1/ditto/zap_splits');
@ -32,6 +42,7 @@ 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) => { const initialMessages = normalizedData.reduce((acc, item) => {
acc[item.account.id] = item.message; acc[item.account.id] = item.message;
return acc; return acc;
@ -47,6 +58,12 @@ export const useManageZapSplit = () => {
fetchZapSplitData(); fetchZapSplitData();
}, []); }, []);
/**
* Updates the weight of a specific account.
*
* @param accountId - The ID of the account whose weight is being changed.
* @param newWeight - The new weight value to be assigned to the account.
*/
const handleWeightChange = (accountId: string, newWeight: number) => { const handleWeightChange = (accountId: string, newWeight: number) => {
setWeights((prevWeights) => ({ setWeights((prevWeights) => ({
...prevWeights, ...prevWeights,
@ -54,6 +71,12 @@ export const useManageZapSplit = () => {
})); }));
}; };
/**
* Updates the message of a specific account.
*
* @param accountId - The ID of the account whose weight is being changed.
* @param newMessage - The new message to be assigned to the account.
*/
const handleMessageChange = (accountId: string, newMessage: string) => { const handleMessageChange = (accountId: string, newMessage: string) => {
setMessage((prevMessage) => ({ setMessage((prevMessage) => ({
...prevMessage, ...prevMessage,
@ -61,7 +84,13 @@ export const useManageZapSplit = () => {
})); }));
}; };
const sendNewSplit = async (newAccount?: INewAccount, newMessage?: string) => { /**
* Sends the updated Zap Split data to the API, including any new account or message changes.
* If the total weight exceeds 50%, displays an error toast and aborts the operation.
*
* @param newAccount - (Optional) A new account object to be added to the Zap Split data.
*/
const sendNewSplit = async (newAccount?: INewAccount) => {
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] = {
@ -97,6 +126,11 @@ export const useManageZapSplit = () => {
toast.success(messages.sucessMessage); toast.success(messages.sucessMessage);
}; };
/**
* Removes an account from the Zap Split by making a DELETE request to the API, and then refetches the updated data.
*
* @param accountId - The ID of the account to be removed.
*/
const removeAccount = async (accountId: string) => { const removeAccount = async (accountId: string) => {
const isToDelete = [(formattedData.find(item => item.account.id === accountId))?.account.id]; const isToDelete = [(formattedData.find(item => item.account.id === accountId))?.account.id];

Wyświetl plik

@ -21,6 +21,12 @@ interface INewAccount{
weight: number; weight: number;
} }
/**
* Main component that handles the logic and UI for managing accounts in Zap Split.
* Allows the user to view and edit associated accounts, adjust weights, and add new accounts.
*
* @returns A component that renders the Zap Split account management interface.
*/
const ManageZapSplit: React.FC = () => { const ManageZapSplit: React.FC = () => {
const intl = useIntl(); const intl = useIntl();
const { formattedData, weights, message, handleMessageChange, handleWeightChange, sendNewSplit, removeAccount } = useManageZapSplit(); const { formattedData, weights, message, handleMessageChange, handleWeightChange, sendNewSplit, removeAccount } = useManageZapSplit();
@ -28,6 +34,10 @@ const ManageZapSplit: React.FC = () => {
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()) });
/**
* Function that handles submitting a new account to Zap Split. It resets the form and triggers
* the submission of the account with the current data.
*/
const handleNewAccount = () => { const handleNewAccount = () => {
setHasNewAccount(false); setHasNewAccount(false);
@ -37,6 +47,11 @@ const ManageZapSplit: React.FC = () => {
setNewAccount(({ acc: '', message: '', weight: Number((newWeight * 100).toFixed()) })); setNewAccount(({ acc: '', message: '', weight: Number((newWeight * 100).toFixed()) }));
}; };
/**
* Updates the weight of the new account and adjusts the `newAccount` state with the new weight value.
*
* @param newWeight - The new weight assigned to the account.
*/
const handleChange = (newWeight: number) => { const handleChange = (newWeight: number) => {
setNewWeight(newWeight); setNewWeight(newWeight);
setNewAccount((previousValue) => ({ setNewAccount((previousValue) => ({
@ -44,6 +59,12 @@ const ManageZapSplit: React.FC = () => {
weight: Number((newWeight * 100).toFixed()) })); weight: Number((newWeight * 100).toFixed()) }));
}; };
/**
* Formats the weight value into an integer representing the percentage.
*
* @param weight - The weight as a decimal number (e.g., 0.05).
* @returns The formatted weight as an integer (e.g., 5 for 5%).
*/
const formattedWeight = (weight: number) =>{ const formattedWeight = (weight: number) =>{
return Number((weight * 100).toFixed()); return Number((weight * 100).toFixed());
}; };