From 90fe60c10c5ad64a01ae0f94e43404efc483d3bf Mon Sep 17 00:00:00 2001 From: evlist Date: Wed, 1 Mar 2023 18:13:13 +0100 Subject: [PATCH] Adding username validation for subscriptions --- src/components/user/User.tsx | 65 +++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/src/components/user/User.tsx b/src/components/user/User.tsx index 89e90a6..a5ffe10 100644 --- a/src/components/user/User.tsx +++ b/src/components/user/User.tsx @@ -9,6 +9,7 @@ import { put } from '~/lib/put'; import { del } from '~/lib/del'; import { isFunction } from 'lodash'; import { userId } from '~/lib/user-id'; +import { userExists } from '~/lib/user-exists'; interface Props { values?: () => any; @@ -47,6 +48,7 @@ const User: Component = (props) => { roles: [], }; await put(couchUserId, userDoc, isNew(), '_users'); + if (isNew()) { navigate(`/user/${id}`); } @@ -86,33 +88,46 @@ const User: Component = (props) => { return errors; } - if (isNew()) { - const authString = `${username}:${password}`; - const headers = new Headers(); - headers.set('Authorization', 'Basic ' + btoa(authString)); - const response = await fetch( - `${database}/_users/org.couchdb.user:${values.username}`, - { - method: 'HEAD', - mode: 'cors', - headers, + const subscriptions = values.subscriptions ?? []; + + for (let i = 0; i < subscriptions.length; i++) { + let subscription = subscriptions[i]; + if ( + !!subscription.username && + !(await userExists(subscription.username)) + ) { + if (!errors.subscriptions) { + errors.subscriptions = []; } - ); - if (response.status === 200) { - errors.username = 'The user is already existing'; + errors.subscriptions[i] = { username: "User doesn't exist" }; } } + if (isNew()) { + if (await userExists(values.username)) { + errors.username = 'The user is already existing'; + } + } + console.log({ caller: 'Users / validationHandler', values, errors }); return errors; }; - const { form, data, setData, setInitialValues, reset, addField, unsetField } = - createForm({ - onSubmit: submitHandler, - extend: reporter(), - validate: validationHandler, - initialValues: getValues(), - }); + const { + form, + data, + setData, + setInitialValues, + reset, + addField, + unsetField, + isDirty, + isValid, + } = createForm({ + onSubmit: submitHandler, + extend: reporter(), + validate: validationHandler, + initialValues: getValues(), + }); const createUserHandler = async () => { console.log({ @@ -226,14 +241,18 @@ const User: Component = (props) => { - Save + + Save + Delete - Create + + Create + - Cancel + Back );