Adding username validation for subscriptions
This commit is contained in:
parent
e36f8cf927
commit
90fe60c10c
|
@ -9,6 +9,7 @@ import { put } from '~/lib/put';
|
||||||
import { del } from '~/lib/del';
|
import { del } from '~/lib/del';
|
||||||
import { isFunction } from 'lodash';
|
import { isFunction } from 'lodash';
|
||||||
import { userId } from '~/lib/user-id';
|
import { userId } from '~/lib/user-id';
|
||||||
|
import { userExists } from '~/lib/user-exists';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
values?: () => any;
|
values?: () => any;
|
||||||
|
@ -47,6 +48,7 @@ const User: Component<Props> = (props) => {
|
||||||
roles: [],
|
roles: [],
|
||||||
};
|
};
|
||||||
await put(couchUserId, userDoc, isNew(), '_users');
|
await put(couchUserId, userDoc, isNew(), '_users');
|
||||||
|
|
||||||
if (isNew()) {
|
if (isNew()) {
|
||||||
navigate(`/user/${id}`);
|
navigate(`/user/${id}`);
|
||||||
}
|
}
|
||||||
|
@ -86,28 +88,41 @@ const User: Component<Props> = (props) => {
|
||||||
return errors;
|
return errors;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isNew()) {
|
const subscriptions = values.subscriptions ?? [];
|
||||||
const authString = `${username}:${password}`;
|
|
||||||
const headers = new Headers();
|
for (let i = 0; i < subscriptions.length; i++) {
|
||||||
headers.set('Authorization', 'Basic ' + btoa(authString));
|
let subscription = subscriptions[i];
|
||||||
const response = await fetch(
|
if (
|
||||||
`${database}/_users/org.couchdb.user:${values.username}`,
|
!!subscription.username &&
|
||||||
{
|
!(await userExists(subscription.username))
|
||||||
method: 'HEAD',
|
) {
|
||||||
mode: 'cors',
|
if (!errors.subscriptions) {
|
||||||
headers,
|
errors.subscriptions = [];
|
||||||
}
|
}
|
||||||
);
|
errors.subscriptions[i] = { username: "User doesn't exist" };
|
||||||
if (response.status === 200) {
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isNew()) {
|
||||||
|
if (await userExists(values.username)) {
|
||||||
errors.username = 'The user is already existing';
|
errors.username = 'The user is already existing';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
console.log({ caller: 'Users / validationHandler', values, errors });
|
||||||
return errors;
|
return errors;
|
||||||
};
|
};
|
||||||
|
|
||||||
const { form, data, setData, setInitialValues, reset, addField, unsetField } =
|
const {
|
||||||
createForm({
|
form,
|
||||||
|
data,
|
||||||
|
setData,
|
||||||
|
setInitialValues,
|
||||||
|
reset,
|
||||||
|
addField,
|
||||||
|
unsetField,
|
||||||
|
isDirty,
|
||||||
|
isValid,
|
||||||
|
} = createForm({
|
||||||
onSubmit: submitHandler,
|
onSubmit: submitHandler,
|
||||||
extend: reporter(),
|
extend: reporter(),
|
||||||
validate: validationHandler,
|
validate: validationHandler,
|
||||||
|
@ -226,14 +241,18 @@ const User: Component<Props> = (props) => {
|
||||||
</table>
|
</table>
|
||||||
<Switch>
|
<Switch>
|
||||||
<Match when={!isNew()}>
|
<Match when={!isNew()}>
|
||||||
<Button.Root type='submit'>Save</Button.Root>
|
<Button.Root type='submit' disabled={!isDirty() || !isValid()}>
|
||||||
|
Save
|
||||||
|
</Button.Root>
|
||||||
<Button.Root onclick={deleteHandler}>Delete</Button.Root>
|
<Button.Root onclick={deleteHandler}>Delete</Button.Root>
|
||||||
</Match>
|
</Match>
|
||||||
<Match when={isNew()}>
|
<Match when={isNew()}>
|
||||||
<Button.Root type='submit'>Create</Button.Root>
|
<Button.Root type='submit' isDisabled={!isValid()}>
|
||||||
|
Create
|
||||||
|
</Button.Root>
|
||||||
</Match>
|
</Match>
|
||||||
</Switch>
|
</Switch>
|
||||||
<Button.Root onclick={cancelHandler}>Cancel</Button.Root>
|
<Button.Root onclick={cancelHandler}>Back</Button.Root>
|
||||||
</form>
|
</form>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue