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;
|
|
|
|
|
}
|
2023-02-26 16:26:01 -05:00
|
|
|
|
|
|
|
|
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-02-26 16:26:01 -05:00
|
|
|
|
2023-12-17 03:39:57 +00:00
|
|
|
onConfirmWrapper(input: string) {
|
|
|
|
|
// @ts-expect-error
|
|
|
|
|
this.props.isLoading = true;
|
2023-02-26 16:26:01 -05:00
|
|
|
|
2023-12-17 03:39:57 +00:00
|
|
|
const asyncOnConfirm = Promise.resolve(this.props.onConfirm(input));
|
2023-02-26 16:26:01 -05:00
|
|
|
|
|
|
|
|
asyncOnConfirm.then(() => {
|
|
|
|
|
Alerts.close();
|
|
|
|
|
}).catch((e: Error) => {
|
2023-12-17 03:39:57 +00:00
|
|
|
this.setState({ error: e.message });
|
2023-02-26 16:26:01 -05:00
|
|
|
});
|
|
|
|
|
|
2023-12-17 03:39:57 +00:00
|
|
|
// @ts-expect-error
|
|
|
|
|
this.props.isLoading = false;
|
|
|
|
|
};
|
|
|
|
|
}
|