GoonCord/src/ui/components/InputAlert.tsx

43 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-04-15 19:54:23 +01:00
import { InputAlertProps } from "@types";
2023-12-17 03:39:57 +00:00
import { findByName, findByProps } from "@metro/filters";
interface InternalInputAlertProps extends InputAlertProps {
onClose?: () => void;
onSubmit?: (input: string) => void;
isLoading?: boolean;
}
const Alerts = findByProps("openLazy", "close");
2023-12-17 03:39:57 +00:00
const UserSettingsInputAlert = findByName("UserSettingsInputAlert") as React.ComponentClass<InternalInputAlertProps>;
// TODO: Moving to Discord's UserSettingsInputAlert here has some issues:
//* - Errors are not cleared upon typing
//* - The confirmText does not apply
//* - The confirm button is not disabled when there is an error
export default class InputAlert extends UserSettingsInputAlert {
constructor(props: InternalInputAlertProps) {
super(props);
props.secureTextEntry = false;
props.onClose = Alerts.close;
props.onSubmit = (i: string) => this.onConfirmWrapper(i);
this.state = { input: props.initialValue };
}
2023-12-17 03:39:57 +00:00
onConfirmWrapper(input: string) {
// @ts-expect-error
this.props.isLoading = true;
2023-12-17 03:39:57 +00:00
const asyncOnConfirm = Promise.resolve(this.props.onConfirm(input));
asyncOnConfirm.then(() => {
Alerts.close();
}).catch((e: Error) => {
2023-12-17 03:39:57 +00:00
this.setState({ error: e.message });
});
2023-12-17 03:39:57 +00:00
// @ts-expect-error
this.props.isLoading = false;
};
}