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.' },
});
/**
* 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<ZapSplitData[]>([]);
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<ZapSplitData[]>('/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];

Wyświetl plik

@ -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<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 = () => {
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());
};