Parcourir la source

Wait for no custom errors before submitting

tags/Baseline_180220205_Frontend
Wayne il y a 8 mois
Parent
révision
0ed51ed325
3 fichiers modifiés avec 42 ajouts et 3 suppressions
  1. +4
    -1
      src/components/TimeLeaveModal/TimeLeaveInputTable.tsx
  2. +8
    -2
      src/components/TimeLeaveModal/TimeLeaveModal.tsx
  3. +30
    -0
      src/components/utils/waitFor.ts

+ 4
- 1
src/components/TimeLeaveModal/TimeLeaveInputTable.tsx Voir le fichier

@@ -637,7 +637,10 @@ const TimeLeaveInputTable: React.FC<Props> = ({
setValue(day, newEntries);

if (entries.some((e) => e._isNew)) {
setError(day, { message: "There are some unsaved entries." });
setError(day, {
message: "There are some unsaved entries.",
type: "custom",
});
} else {
clearErrors(day);
}


+ 8
- 2
src/components/TimeLeaveModal/TimeLeaveModal.tsx Voir le fichier

@@ -39,6 +39,7 @@ import DateHoursList from "../DateHoursTable/DateHoursList";
import TimeLeaveInputTable from "./TimeLeaveInputTable";
import TimeLeaveMobileEntry from "./TimeLeaveMobileEntry";
import { Task } from "@/app/api/tasks";
import waitForCondition from "../utils/waitFor";

interface Props {
isOpen: boolean;
@@ -240,9 +241,14 @@ const TimeLeaveModal: React.FC<Props> = ({
<Button
variant="contained"
startIcon={<Check />}
type="submit"
onClick={() => {
onClick={async () => {
await waitForCondition(async () => {
return !Object.values(formProps.formState.errors).some(
(err) => err?.type === "custom",
);
});
formProps.clearErrors();
formProps.handleSubmit(onSubmit)();
}}
>
{t("Save")}


+ 30
- 0
src/components/utils/waitFor.ts Voir le fichier

@@ -0,0 +1,30 @@
/**
* Wait until a condition to be true with a default timeout of 1 second (and checking every 100ms).
*/
export const waitForCondition = async (
condition: () => Promise<boolean>,
waitOptions: {
timeout: number;
interval: number;
} = { timeout: 1000, interval: 100 },
) => {
const startTime = Date.now();

const check = async () => {
if (await condition()) {
return;
} else {
if (Date.now() - startTime < waitOptions.timeout) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(check());
}, waitOptions.interval);
});
}
}
};

return check();
};

export default waitForCondition;

Chargement…
Annuler
Enregistrer