refactor error mgmt for creating new password
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { Button, Card, Field, Input, Stack } from "@chakra-ui/react";
|
import { Button, Card, Field, Input, Stack, Alert } from "@chakra-ui/react";
|
||||||
import { changePW } from "@/utils/userActions";
|
import { changePW } from "@/utils/userActions";
|
||||||
|
import { useState } from "react";
|
||||||
|
|
||||||
type ChangePWformProps = {
|
type ChangePWformProps = {
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
@@ -17,6 +18,14 @@ const ChangePWform: React.FC<ChangePWformProps> = ({
|
|||||||
alert,
|
alert,
|
||||||
username,
|
username,
|
||||||
}) => {
|
}) => {
|
||||||
|
const [showSubAlert, setShowSubAlert] = useState(false);
|
||||||
|
const [subAlertMessage, setSubAlertMessage] = useState("");
|
||||||
|
|
||||||
|
const subAlert = (message: string) => {
|
||||||
|
setSubAlertMessage(message);
|
||||||
|
setShowSubAlert(true);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm p-4">
|
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm p-4">
|
||||||
<Card.Root maxW="sm">
|
<Card.Root maxW="sm">
|
||||||
@@ -64,7 +73,10 @@ const ChangePWform: React.FC<ChangePWformProps> = ({
|
|||||||
) as HTMLInputElement
|
) as HTMLInputElement
|
||||||
)?.value.trim() || "";
|
)?.value.trim() || "";
|
||||||
|
|
||||||
if (!newPassword || newPassword !== confirmNewPassword) return;
|
if (!newPassword || newPassword !== confirmNewPassword) {
|
||||||
|
subAlert("Passwörter stimmen nicht überein!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const res = await changePW(newPassword, username);
|
const res = await changePW(newPassword, username);
|
||||||
if (res.success) {
|
if (res.success) {
|
||||||
@@ -86,6 +98,14 @@ const ChangePWform: React.FC<ChangePWformProps> = ({
|
|||||||
>
|
>
|
||||||
Ändern
|
Ändern
|
||||||
</Button>
|
</Button>
|
||||||
|
{showSubAlert && (
|
||||||
|
<Alert.Root status="error">
|
||||||
|
<Alert.Indicator />
|
||||||
|
<Alert.Content>
|
||||||
|
<Alert.Title>{subAlertMessage}</Alert.Title>
|
||||||
|
</Alert.Content>
|
||||||
|
</Alert.Root>
|
||||||
|
)}
|
||||||
</Card.Footer>
|
</Card.Footer>
|
||||||
</Card.Root>
|
</Card.Root>
|
||||||
</div>
|
</div>
|
||||||
|
Reference in New Issue
Block a user