- remove useless optimization boilerplate - use ref in useEffet for variables that are not denpencies - add NexUserModal component - disable submission while feedback - reset form after validating feeback - update i18n "accountStatus" keypull/12/head
parent
c7127b14f9
commit
2b793b8b72
@ -0,0 +1,78 @@ |
||||
import React, { useRef, useState } from "react"; |
||||
import { useMutate } from "restful-react"; |
||||
import { useTranslation } from "react-i18next"; |
||||
|
||||
import NewItemModal from "./NewItemModal"; |
||||
import NewUserForm from "./NewUserForm"; |
||||
|
||||
export default ({ modalShow, setModalShow, userList, setUserList }) => { |
||||
const { t } = useTranslation("usersTab"); |
||||
|
||||
const [feedback, setFeedback] = useState(null); |
||||
|
||||
const { mutate: post, loading } = useMutate({ |
||||
verb: "POST", |
||||
path: "watcha_register", |
||||
}); |
||||
|
||||
const onSubmit = data => { |
||||
const payload = makePayload(data); |
||||
post(payload) |
||||
.then(response => { |
||||
const user = makeUser(data); |
||||
setUserList([...userList, user]); |
||||
setFeedback({ variant: "success", message: t("success") }); |
||||
}) |
||||
.catch(error => |
||||
setFeedback({ variant: "danger", message: t("danger") }) |
||||
); |
||||
}; |
||||
|
||||
const onHide = () => { |
||||
setModalShow(false); |
||||
setFeedback(null); |
||||
}; |
||||
|
||||
const submitFormRef = useRef(); |
||||
const bindSubmitForm = submitForm => { |
||||
submitFormRef.current = submitForm; |
||||
}; |
||||
|
||||
return ( |
||||
<NewItemModal |
||||
show={modalShow} |
||||
title={t("button")} |
||||
onSave={() => submitFormRef.current()} |
||||
onClick={() => setFeedback(null)} |
||||
{...{ feedback, loading, onHide }} |
||||
> |
||||
<NewUserForm |
||||
{...{ userList, onSubmit, bindSubmitForm, feedback }} |
||||
/> |
||||
</NewItemModal> |
||||
); |
||||
}; |
||||
|
||||
const makePayload = data => ({ |
||||
admin: data.isSynapseAdministrator ? "admin" : false, |
||||
email: data.emailAddress, |
||||
full_name: data.fullName, |
||||
user: computeUserIdFromEmailAddress(data.emailAddress), |
||||
}); |
||||
|
||||
const makeUser = data => ({ |
||||
userId: computeUserIdFromEmailAddress(data.emailAddress), |
||||
displayName: data.fullName, |
||||
emailAddress: data.emailAddress, |
||||
lastSeen: null, |
||||
role: data.isSynapseAdministrator ? "administrator" : "collaborator", |
||||
status: "active", |
||||
}); |
||||
|
||||
const computeUserIdFromEmailAddress = emailAddress => |
||||
emailAddress |
||||
.replace("@", "/") |
||||
.normalize("NFKD") |
||||
.replace(/[\u0300-\u036F]/g, "") |
||||
.toLowerCase() |
||||
.replace(/[^\w=\-./]/g, ""); |
||||
Loading…
Reference in new issue