-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from uwblueprint/F24/Justin/Invite-User-Button…
…-and-Modal F24/justin/invite user button and modal
- Loading branch information
Showing
4 changed files
with
223 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
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,102 @@ | ||
import React, { useState } from "react"; | ||
import { JSONSchema7 } from "json-schema"; | ||
import { Form } from "@rjsf/bootstrap-4"; | ||
import { IChangeEvent, ISubmitEvent } from "@rjsf/core"; | ||
|
||
export interface AddUserRequest { | ||
firstName: string; | ||
lastName: string; | ||
phoneNumber: string; | ||
email: string; | ||
role: "Administrator" | "Animal Behaviourist" | "Staff" | "Volunteer"; | ||
} | ||
|
||
interface AddUserFormModalProps { | ||
onSubmit: (formData: AddUserRequest) => Promise<void>; | ||
} | ||
|
||
const userSchema: JSONSchema7 = { | ||
title: "Invite a user", | ||
description: "Enter user details to send an invite", | ||
type: "object", | ||
required: ["firstName", "lastName", "phoneNumber", "email", "role"], | ||
properties: { | ||
firstName: { type: "string", title: "First Name" }, | ||
lastName: { type: "string", title: "Last Name" }, | ||
phoneNumber: { type: "string", title: "Phone Number" }, | ||
email: { type: "string", format: "email", title: "Email" }, | ||
role: { | ||
type: "string", | ||
title: "Role", | ||
enum: ["Administrator", "Animal Behaviourist", "Staff", "Volunteer"], | ||
default: "Staff", | ||
}, | ||
}, | ||
}; | ||
|
||
const uiSchema = { | ||
role: { | ||
"ui:widget": "select", | ||
}, | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const validate = (formData: AddUserRequest, errors: any) => { | ||
const phoneRegex = /^\d{3}-\d{3}-\d{4}$/; | ||
const phoneRegex2 = /^\d{10}$/; | ||
if ( | ||
!phoneRegex.test(formData.phoneNumber) && | ||
!phoneRegex2.test(formData.phoneNumber) | ||
) { | ||
errors.phoneNumber.addError("Phone number must be in xxx-xxx-xxxx format."); | ||
} | ||
if (!formData.email.includes("@")) { | ||
errors.email.addError("Email must be in address@domain format."); | ||
} | ||
return errors; | ||
}; | ||
|
||
const AddUserFormModal = ({ | ||
onSubmit, | ||
}: AddUserFormModalProps): React.ReactElement => { | ||
const [formFields, setFormFields] = useState<AddUserRequest | null>(null); | ||
const [loading, setLoading] = useState(false); | ||
const [error, setError] = useState<string | null>(null); | ||
|
||
const handleSubmit = async ({ formData }: ISubmitEvent<AddUserRequest>) => { | ||
setLoading(true); | ||
setError(null); | ||
try { | ||
await onSubmit(formData); | ||
setFormFields(null); | ||
} catch (err) { | ||
setError("An error occurred while sending the invite."); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<Form | ||
formData={formFields} | ||
schema={userSchema} | ||
uiSchema={uiSchema} | ||
validate={validate} | ||
onChange={({ formData }: IChangeEvent<AddUserRequest>) => | ||
setFormFields(formData) | ||
} | ||
onSubmit={handleSubmit} | ||
> | ||
<div style={{ textAlign: "center" }}> | ||
<button type="submit" className="btn btn-primary" disabled={loading}> | ||
{loading ? "Sending..." : "Send Invite"} | ||
</button> | ||
</div> | ||
</Form> | ||
{error && <p style={{ color: "red" }}>{error}</p>} | ||
</div> | ||
); | ||
}; | ||
|
||
export default AddUserFormModal; |
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