Skip to content

Commit

Permalink
Disabled button in settings when no change made (#1178)
Browse files Browse the repository at this point in the history
* disabled button

* Update packages/files-ui/src/Components/Modules/Settings/Profile.tsx

Co-authored-by: Ryan Noble <ryanjnoble@gmail.com>

Co-authored-by: Ryan Noble <ryanjnoble@gmail.com>
  • Loading branch information
Tbaut and RyRy79261 authored Jun 24, 2021
1 parent 4da1a71 commit d19e0c6
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions packages/files-ui/src/Components/Modules/Settings/Profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
useThemeSwitcher
} from "@chainsafe/common-theme"
import { LockIcon, CopyIcon } from "@chainsafe/common-components"
import { Formik, Form } from "formik"
import { Form, useFormik, FormikProvider } from "formik"
import { useUser } from "../../../Contexts/UserContext"
import { t, Trans } from "@lingui/macro"
import { centerEllipsis } from "../../../Utils/Helpers"
Expand Down Expand Up @@ -177,6 +177,13 @@ const useStyles = makeStyles(({ constants, breakpoints, palette, typography }: C
})
)

const profileValidation = yup.object().shape({
// email: yup.string().email("Email is invalid").required("Email is required"),
firstName: yup.string(),
lastName: yup.string(),
username: yup.string()
})

const ProfileView = () => {
const { themeKey, setTheme } = useThemeSwitcher()
const { addToastMessage } = useToaster()
Expand All @@ -186,7 +193,22 @@ const ProfileView = () => {
const [showUsernameForm, setShowUsernameForm] = useState(false)
const [username, setUsername] = useState("")
const [usernameData, setUsernameData] = useState({ error: "", loading: false })

const formik = useFormik({
initialValues:{
firstName: profile?.firstName || "",
lastName: profile?.lastName || ""
// email: profile?.email || ""
},
onSubmit: (values) => {
onUpdateProfile(
values.firstName || "",
values.lastName || ""
// values.email || ""
)
},
validationSchema: profileValidation,
validateOnChange: false
})
const onUpdateProfile = async (firstName: string, lastName: string) => {
try {
setUpdatingProfile(true)
Expand Down Expand Up @@ -234,13 +256,6 @@ const ProfileView = () => {
}
}

const profileValidation = yup.object().shape({
// email: yup.string().email("Email is invalid").required("Email is required"),
firstName: yup.string(),
lastName: yup.string(),
username: yup.string()
})

const onLookupUsername = useCallback((username: string) => {
lookupOnUsername(username).then((doesUsernameExist) => {
if (doesUsernameExist) {
Expand Down Expand Up @@ -449,22 +464,7 @@ const ProfileView = () => {
}
</div>
}
<Formik
initialValues={{
firstName: profile?.firstName || "",
lastName: profile?.lastName || ""
// email: profile?.email || ""
}}
onSubmit={(values) => {
onUpdateProfile(
values.firstName || "",
values.lastName || ""
// values.email || ""
)
}}
validationSchema={profileValidation}
validateOnChange={false}
>
<FormikProvider value={formik}>
<Form>
<div className={classes.inputBoxContainer}>
<Typography
Expand Down Expand Up @@ -525,6 +525,7 @@ const ProfileView = () => {
variant={themeKey === "dark" ? "outline" : "primary"}
loadingText="Saving"
data-cy="profile-save-button"
disabled={!formik.dirty}
>
<LockIcon className={classes.icon} />
{" "}
Expand All @@ -533,7 +534,7 @@ const ProfileView = () => {
</Typography>
</Button>
</Form>
</Formik>
</FormikProvider>
</div>
</div>
{/* <div id="deletion" className={classes.sectionContainer}>
Expand Down

0 comments on commit d19e0c6

Please sign in to comment.