Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
[UI > InputAlert] Revert and fix (#211)
Browse files Browse the repository at this point in the history
* Revert "[UI > InputAlert] Fix for new UI"

This reverts commit 963ddc5.

* real fix

* unrevert some changes
  • Loading branch information
pylixonly authored Dec 19, 2023
1 parent 963ddc5 commit 0e48cbc
Showing 1 changed file with 38 additions and 31 deletions.
69 changes: 38 additions & 31 deletions src/ui/components/InputAlert.tsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,49 @@
import { InputAlertProps } from "@types";
import { findByName, findByProps } from "@metro/filters";

interface InternalInputAlertProps extends InputAlertProps {
onClose?: () => void;
onSubmit?: (input: string) => void;
isLoading?: boolean;
}
import { findByProps } from "@metro/filters";
import { Forms, Alert } from "@ui/components";

const { FormInput } = Forms;
const Alerts = findByProps("openLazy", "close");
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 };
}

onConfirmWrapper(input: string) {
// @ts-expect-error
this.props.isLoading = true;
export default function InputAlert({ title, confirmText, confirmColor, onConfirm, cancelText, placeholder, initialValue = "", secureTextEntry }: InputAlertProps) {
const [value, setValue] = React.useState(initialValue);
const [error, setError] = React.useState("");

const asyncOnConfirm = Promise.resolve(this.props.onConfirm(input));
function onConfirmWrapper() {
const asyncOnConfirm = Promise.resolve(onConfirm(value))

asyncOnConfirm.then(() => {
Alerts.close();
}).catch((e: Error) => {
this.setState({ error: e.message });
setError(e.message);
});

// @ts-expect-error
this.props.isLoading = false;
};
}

return (
<Alert
title={title}
confirmText={confirmText}
confirmColor={confirmColor}
isConfirmButtonDisabled={error.length !== 0}
onConfirm={onConfirmWrapper}
cancelText={cancelText}
onCancel={() => Alerts.close()}
>
<FormInput
placeholder={placeholder}
value={value}
onChange={(v: string | { text: string }) => {
setValue(typeof v === "string" ? v : v.text);
if (error) setError("");
}}
returnKeyType="done"
onSubmitEditing={onConfirmWrapper}
error={error || undefined}
secureTextEntry={secureTextEntry}
autoFocus={true}
showBorder={true}
style={{ paddingVertical: 5, alignSelf: "stretch", paddingHorizontal: 0 }}
/>
</Alert>
);
};

0 comments on commit 0e48cbc

Please sign in to comment.