Skip to content

Commit

Permalink
feat: Throttle addAlt function
Browse files Browse the repository at this point in the history
  • Loading branch information
Lodimup committed Jun 10, 2024
1 parent 6902c27 commit 3a2c82e
Showing 1 changed file with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { z } from "zod";
import _ from "lodash";

import { Button } from "@/components/ui/button";
import {
Expand All @@ -22,12 +23,15 @@ import { Input } from "@/components/ui/input";
import { toast } from "sonner";
import { useEffect, useState } from "react";
import { addAlt } from "@/actions/intra/wallets";
import { SAResponse } from "@/types/sa-response";

const FormSchema = z.object({
logins: z.string(),
value: z.coerce.number().int(),
});

let throttledAddAlt = _.throttle(addAlt, 1000);

export const LoginsForm = (props: ILoginsForm) => {
const logins = props["logins"]
? [...new Set(props["logins"].split(" ").filter(Boolean))]
Expand Down Expand Up @@ -60,17 +64,16 @@ export const LoginsForm = (props: ILoginsForm) => {
}
setIsPending(true);
for (const user of users) {
const { data, error } = await addAlt({
const { data, error } = (await throttledAddAlt({
value: value,
user_id: user.id,
login: user.login,
});
})) as SAResponse<Boolean>;
if (error) {
toast.error(`Failed to add $Alt to ${user.login}`);
} else {
toast.success(`Added $Alt to ${user.login}`);
}
await new Promise((resolve) => setTimeout(resolve, 100)); // Simple way to prevent rate limit
}
setIsPending(false);
}
Expand Down

0 comments on commit 3a2c82e

Please sign in to comment.