-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add change password using separate service
- Loading branch information
1 parent
b70d047
commit 9a609ba
Showing
7 changed files
with
192 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { BadRequest, type FeathersError } from "@feathersjs/errors" | ||
import React, { useEffect, useRef, useState } from "react" | ||
import { Form } from "react-bootstrap" | ||
import ErrorManager, { type BadRequestData } from "./ErrorManager" | ||
import { FloppyDiskArrowIn } from "iconoir-react" | ||
|
||
export type ChangePasswordFormPayload = { | ||
currentPassword: string | ||
password: string | ||
verifyPassword: string | ||
} | ||
|
||
export interface ChangePasswordFormProps { | ||
className?: string | ||
onSubmit: (payload: ChangePasswordFormPayload) => void | ||
error?: FeathersError | null | ||
} | ||
|
||
const ChangePasswordForm: React.FC<ChangePasswordFormProps> = ({ | ||
className, | ||
onSubmit, | ||
error, | ||
}) => { | ||
const delayTimerRef = useRef<NodeJS.Timeout | null>(null) | ||
const [formError, setFormError] = useState<Error | null>(null) | ||
|
||
const formPayload = useRef<ChangePasswordFormPayload>({ | ||
currentPassword: "", | ||
password: "", | ||
verifyPassword: "", | ||
}) | ||
|
||
const handleOnSubmit = (e: React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault() | ||
console.info("[ChangePasswordForm] @handleOnSubmit") | ||
const errorsAsData: { [key: string]: BadRequestData } = {} | ||
if (formPayload.current.password !== formPayload.current.verifyPassword) { | ||
errorsAsData.verifyPassword = { | ||
label: "Verify password", | ||
message: "The passwords you entered don't match. Please try again.", | ||
} | ||
} | ||
if (Object.keys(errorsAsData).length > 0) { | ||
setFormError(new BadRequest("Please check your entries.", errorsAsData)) | ||
return false | ||
} | ||
onSubmit(formPayload.current) | ||
} | ||
|
||
const updateValue = (key: keyof ChangePasswordFormPayload, value: string) => { | ||
formPayload.current[key] = value | ||
console.info("[RegisterForm] @updatePreview", key, value) | ||
// handpick the fields to preview | ||
delayTimerRef.current = setTimeout(() => { | ||
setFormError(null) | ||
}, 400) | ||
} | ||
|
||
console.info("[ChangePasswordForm] @render", { error }) | ||
|
||
useEffect(() => { | ||
return () => { | ||
if (delayTimerRef.current) { | ||
clearTimeout(delayTimerRef.current) | ||
} | ||
} | ||
}, []) | ||
|
||
return ( | ||
<> | ||
<Form | ||
onSubmit={handleOnSubmit} | ||
className={`ChangePasswordForm ${className}`} | ||
> | ||
<ErrorManager error={error || formError} /> | ||
|
||
<Form.Group | ||
className="mb-3" | ||
controlId="ModalChangePasswordorm.password" | ||
> | ||
<Form.Label className="font-weight-bold">Current Password</Form.Label> | ||
<Form.Control | ||
onChange={(e) => updateValue("currentPassword", e.target.value)} | ||
type="password" | ||
/> | ||
</Form.Group> | ||
<Form.Group className="mb-3" controlId="ModalChangePassword.password"> | ||
<Form.Label className="font-weight-bold">New Password</Form.Label> | ||
<Form.Control | ||
onChange={(e) => updateValue("password", e.target.value)} | ||
type="password" | ||
/> | ||
</Form.Group> | ||
<Form.Group | ||
className="mb-3" | ||
controlId="ModalChangePassword.verifyPassword" | ||
> | ||
<Form.Label className="font-weight-bold"> | ||
Verify New Password | ||
</Form.Label> | ||
<Form.Control | ||
onChange={(e) => updateValue("verifyPassword", e.target.value)} | ||
type="password" | ||
/> | ||
</Form.Group> | ||
<button type="submit" className="btn btn-primary btn-lg px-4"> | ||
<FloppyDiskArrowIn /> <span className="ms-2">Register</span> | ||
</button> | ||
</Form> | ||
|
||
<p className="mt-2"> | ||
Any Questions? <br /> | ||
Contact us at{" "} | ||
<a href="mailto:[email protected]">[email protected]</a> | ||
</p> | ||
</> | ||
) | ||
} | ||
|
||
export default ChangePasswordForm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Col, Container, Form, Modal, Row } from "react-bootstrap" | ||
import { BrowserViewChangePassword } from "../constants" | ||
import { useBrowserStore } from "../store" | ||
import Alert from "./Alert" | ||
import { useRef, useState } from "react" | ||
import ChangePasswordForm, { | ||
type ChangePasswordFormPayload, | ||
} from "./ChangePasswordForm" | ||
import type { FeathersError } from "@feathersjs/errors" | ||
import { changePasswordService } from "../services" | ||
|
||
const ChangePassword = () => { | ||
const view = useBrowserStore((state) => state.view) | ||
const setView = useBrowserStore((state) => state.setView) | ||
const [error, setError] = useState<FeathersError | null>(null) | ||
|
||
const handleOnSubmit = (payload: ChangePasswordFormPayload) => { | ||
console.info("[ChangePasswordModal] @handleOnSubmit", payload) | ||
changePasswordService | ||
.create({ | ||
password: payload.password, | ||
currentPassword: payload.currentPassword, | ||
}) | ||
.then((data) => { | ||
console.info( | ||
"[ChangePasswordModal] Password changed successfully. data:", | ||
data | ||
) | ||
setView(null) | ||
}) | ||
.catch((err: FeathersError) => { | ||
setError(err) | ||
console.error("[ChangePasswordModal] create", err.message, err.data) | ||
}) | ||
} | ||
|
||
return ( | ||
<Modal | ||
centered | ||
show={view === BrowserViewChangePassword} | ||
onHide={() => setView(null)} | ||
> | ||
<Modal.Header closeButton> | ||
<Modal.Title>Change Password</Modal.Title> | ||
</Modal.Header> | ||
<Modal.Body className="p-3"> | ||
<ChangePasswordForm onSubmit={handleOnSubmit} error={error} /> | ||
</Modal.Body> | ||
</Modal> | ||
) | ||
} | ||
|
||
export default ChangePassword |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters