From 1a088649aa691e3db043264a6950bac7d305e47c Mon Sep 17 00:00:00 2001 From: danidfra Date: Mon, 23 Sep 2024 11:25:48 -0300 Subject: [PATCH] Update: Add TSDoc comments --- src/api/hooks/admin/useManageZapSplit.ts | 36 +++++++++++++++++++++++- src/features/admin/manage-zap-split.tsx | 21 ++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/api/hooks/admin/useManageZapSplit.ts b/src/api/hooks/admin/useManageZapSplit.ts index 3b6d12101..fecb820b4 100644 --- a/src/api/hooks/admin/useManageZapSplit.ts +++ b/src/api/hooks/admin/useManageZapSplit.ts @@ -14,12 +14,22 @@ const messages = defineMessages({ 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 = () => { const api = useApi(); const [formattedData, setFormattedData] = useState([]); const [weights, setWeights] = useState<{ [id: string]: number }>({}); 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 () => { try { const { data } = await api.get('/api/v1/ditto/zap_splits'); @@ -32,6 +42,7 @@ export const useManageZapSplit = () => { return acc; }, {} as { [id: string]: number }); setWeights(initialWeights); + const initialMessages = normalizedData.reduce((acc, item) => { acc[item.account.id] = item.message; return acc; @@ -47,6 +58,12 @@ export const useManageZapSplit = () => { 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) => { setWeights((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) => { setMessage((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 { const updatedZapSplits = formattedData.reduce((acc: { [id: string]: { message: string; weight: number } }, zapData) => { acc[zapData.account.id] = { @@ -97,6 +126,11 @@ export const useManageZapSplit = () => { 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 isToDelete = [(formattedData.find(item => item.account.id === accountId))?.account.id]; diff --git a/src/features/admin/manage-zap-split.tsx b/src/features/admin/manage-zap-split.tsx index 833471357..95d350399 100644 --- a/src/features/admin/manage-zap-split.tsx +++ b/src/features/admin/manage-zap-split.tsx @@ -21,6 +21,12 @@ interface INewAccount{ 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 intl = useIntl(); const { formattedData, weights, message, handleMessageChange, handleWeightChange, sendNewSplit, removeAccount } = useManageZapSplit(); @@ -28,6 +34,10 @@ const ManageZapSplit: React.FC = () => { const [newWeight, setNewWeight] = useState(0.05); const [newAccount, setNewAccount] = useState({ 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 = () => { setHasNewAccount(false); @@ -37,6 +47,11 @@ const ManageZapSplit: React.FC = () => { 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) => { setNewWeight(newWeight); setNewAccount((previousValue) => ({ @@ -44,6 +59,12 @@ const ManageZapSplit: React.FC = () => { 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) =>{ return Number((weight * 100).toFixed()); };