Account editing and change password.

This commit is contained in:
Mike Cao 2020-08-09 02:03:37 -07:00
parent b5cf9f8719
commit b392a51676
23 changed files with 230 additions and 102 deletions

View file

@ -14,13 +14,13 @@ const initialValues = {
password: '',
};
const validate = ({ username, password }) => {
const validate = ({ user_id, username, password }) => {
const errors = {};
if (!username) {
errors.username = 'Required';
}
if (!password) {
if (!user_id && !password) {
errors.password = 'Required';
}
@ -33,10 +33,10 @@ export default function AccountEditForm({ values, onSave, onClose }) {
const handleSubmit = async values => {
const response = await post(`/api/account`, values);
if (response) {
if (typeof response !== 'string') {
onSave();
} else {
setMessage('Something went wrong.');
setMessage(response || 'Something went wrong');
}
};
@ -56,7 +56,7 @@ export default function AccountEditForm({ values, onSave, onClose }) {
</FormRow>
<FormRow>
<label htmlFor="password">Password</label>
<Field name="password" type="text" />
<Field name="password" type="password" />
<FormError name="password" />
</FormRow>
<FormButtons>

View file

@ -10,24 +10,24 @@ import FormLayout, {
} from 'components/layout/FormLayout';
const initialValues = {
password: '',
newPassword: '',
defaultPassword: '',
current_password: '',
new_password: '',
confirm_password: '',
};
const validate = ({ password, newPassword, confirmPassword }) => {
const validate = ({ current_password, new_password, confirm_password }) => {
const errors = {};
if (!password) {
errors.password = 'Required';
if (!current_password) {
errors.current_password = 'Required';
}
if (!newPassword) {
errors.newPassword = 'Required';
if (!new_password) {
errors.new_password = 'Required';
}
if (!confirmPassword) {
errors.confirmPassword = 'Required';
} else if (newPassword !== confirmPassword) {
errors.confirmPassword = `Passwords don't match`;
if (!confirm_password) {
errors.confirm_password = 'Required';
} else if (new_password !== confirm_password) {
errors.confirm_password = `Passwords don't match`;
}
return errors;
@ -37,12 +37,12 @@ export default function ChangePasswordForm({ values, onSave, onClose }) {
const [message, setMessage] = useState();
const handleSubmit = async values => {
const response = await post(`/api/website`, values);
const response = await post(`/api/account/password`, values);
if (response) {
if (typeof response !== 'string') {
onSave();
} else {
setMessage('Something went wrong.');
setMessage(response || 'Something went wrong');
}
};
@ -56,19 +56,19 @@ export default function ChangePasswordForm({ values, onSave, onClose }) {
{() => (
<Form>
<FormRow>
<label htmlFor="password">Current password</label>
<Field name="password" type="password" />
<FormError name="password" />
<label htmlFor="current_password">Current password</label>
<Field name="current_password" type="password" />
<FormError name="current_password" />
</FormRow>
<FormRow>
<label htmlFor="newPassword">New password</label>
<Field name="newPassword" type="password" />
<FormError name="newPassword" />
<label htmlFor="new_password">New password</label>
<Field name="new_password" type="password" />
<FormError name="new_password" />
</FormRow>
<FormRow>
<label htmlFor="confirmPassword">Confirm password</label>
<Field name="confirmPassword" type="password" />
<FormError name="confirmPassword" />
<label htmlFor="confirm_password">Confirm password</label>
<Field name="confirm_password" type="password" />
<FormError name="confirm_password" />
</FormRow>
<FormButtons>
<Button type="submit" variant="action">

View file

@ -19,16 +19,16 @@ const validate = ({ confirmation }) => {
return errors;
};
export default function WebsiteDeleteForm({ values, onSave, onClose }) {
export default function DeleteForm({ values, onSave, onClose }) {
const [message, setMessage] = useState();
const handleSubmit = async ({ website_id }) => {
const response = await del(`/api/website/${website_id}`);
const handleSubmit = async ({ type, id }) => {
const response = await del(`/api/${type}/${id}`);
if (response) {
if (typeof response !== 'string') {
onSave();
} else {
setMessage('Something went wrong.');
setMessage('Something went wrong');
}
};

View file

@ -27,7 +27,7 @@ export default function LoginForm() {
if (response?.token) {
await Router.push('/');
} else {
setMessage('Incorrect username/password.');
setMessage('Incorrect username/password');
}
};

View file

@ -33,10 +33,10 @@ export default function WebsiteEditForm({ values, onSave, onClose }) {
const handleSubmit = async values => {
const response = await post(`/api/website`, values);
if (response) {
if (typeof response !== 'string') {
onSave();
} else {
setMessage('Something went wrong.');
setMessage('Something went wrong');
}
};