From 9592ac3d1dc8d928bde78a2bf4f579d9fba8b854 Mon Sep 17 00:00:00 2001 From: Riccardo Graziosi <31478034+riggraz@users.noreply.github.com> Date: Fri, 22 Jul 2022 16:50:36 +0200 Subject: [PATCH] Form refactoring (#142) --- app/controllers/tenants_controller.rb | 12 + app/javascript/actions/Tenant/submitTenant.ts | 4 + .../actions/Tenant/tenantSignUpFormActions.ts | 124 ---------- .../actions/changeSiteSettingsGeneralForm.ts | 65 ------ .../SiteSettings/Boards/BoardForm.tsx | 145 +++++------- .../Boards/BoardsSiteSettingsP.tsx | 2 +- .../General/GeneralSiteSettingsP.tsx | 214 ++++++++---------- .../components/SiteSettings/General/index.tsx | 2 +- .../PostStatuses/PostStatusForm.tsx | 147 +++++------- .../PostStatusesSiteSettingsP.tsx | 2 +- .../TenantSignUp/TenantSignUpForm.tsx | 121 +++++----- .../components/TenantSignUp/TenantSignUpP.tsx | 172 +++++++------- .../TenantSignUp/UserSignUpForm.tsx | 214 ++++++++---------- .../containers/GeneralSiteSettings.tsx | 32 +-- app/javascript/containers/TenantSignUp.tsx | 50 +--- .../reducers/SiteSettings/generalReducer.ts | 92 +------- .../reducers/siteSettingsReducer.ts | 16 -- .../reducers/tenantSignUpReducer.ts | 99 +------- config/locales/en.yml | 11 + config/routes.rb | 2 + package.json | 5 +- yarn.lock | 86 +++++-- 22 files changed, 557 insertions(+), 1060 deletions(-) delete mode 100644 app/javascript/actions/Tenant/tenantSignUpFormActions.ts delete mode 100644 app/javascript/actions/changeSiteSettingsGeneralForm.ts diff --git a/app/controllers/tenants_controller.rb b/app/controllers/tenants_controller.rb index e16de276..e842a0a5 100644 --- a/app/controllers/tenants_controller.rb +++ b/app/controllers/tenants_controller.rb @@ -47,6 +47,18 @@ class TenantsController < ApplicationController end end + # Given a new_subdomain + # Returns true if it is available, false otherwise + def is_available + subdomain = params[:new_subdomain] + + return unless subdomain.present? + return if RESERVED_SUBDOMAINS.include?(subdomain) + return if Tenant.exists?(subdomain: subdomain) + + render json: { is_available: 'true' } + end + private def tenant_create_params diff --git a/app/javascript/actions/Tenant/submitTenant.ts b/app/javascript/actions/Tenant/submitTenant.ts index eb128662..679f19a3 100644 --- a/app/javascript/actions/Tenant/submitTenant.ts +++ b/app/javascript/actions/Tenant/submitTenant.ts @@ -76,7 +76,11 @@ export const submitTenant = ( } else { dispatch(tenantSubmitFailure(json.error)); } + + return Promise.resolve(res); } catch (e) { dispatch(tenantSubmitFailure(e)); + + return Promise.resolve(e); } }; \ No newline at end of file diff --git a/app/javascript/actions/Tenant/tenantSignUpFormActions.ts b/app/javascript/actions/Tenant/tenantSignUpFormActions.ts deleted file mode 100644 index ea50d9a9..00000000 --- a/app/javascript/actions/Tenant/tenantSignUpFormActions.ts +++ /dev/null @@ -1,124 +0,0 @@ -export const TENANT_SIGN_UP_TOGGLE_EMAIL_AUTH = 'TENANT_SIGN_UP_TOGGLE_EMAIL_AUTH'; - -interface TenantSignUpToggleEmailAuth { - type: typeof TENANT_SIGN_UP_TOGGLE_EMAIL_AUTH, -} - -export const toggleEmailAuthTenantSignUp = ( - -): TenantSignUpToggleEmailAuth => ({ - type: TENANT_SIGN_UP_TOGGLE_EMAIL_AUTH, -}); - - -// User full name -export const TENANT_SIGN_UP_CHANGE_USER_FULL_NAME = 'TENANT_SIGN_UP_CHANGE_USER_FULL_NAME'; - -interface TenantSignUpChangeUserFullName { - type: typeof TENANT_SIGN_UP_CHANGE_USER_FULL_NAME, - fullName: string, -} - -export const changeUserFullNameTenantSignUp = ( - fullName: string -): TenantSignUpChangeUserFullName => ({ - type: TENANT_SIGN_UP_CHANGE_USER_FULL_NAME, - fullName, -}); - -// User email -export const TENANT_SIGN_UP_CHANGE_USER_EMAIL = 'TENANT_SIGN_UP_CHANGE_USER_EMAIL'; - -interface TenantSignUpChangeUserEmail { - type: typeof TENANT_SIGN_UP_CHANGE_USER_EMAIL, - email: string, -} - -export const changeUserEmailTenantSignUp = ( - email: string -): TenantSignUpChangeUserEmail => ({ - type: TENANT_SIGN_UP_CHANGE_USER_EMAIL, - email, -}); - -// User password -export const TENANT_SIGN_UP_CHANGE_USER_PASSWORD = 'TENANT_SIGN_UP_CHANGE_USER_PASSWORD'; - -interface TenantSignUpChangeUserPassword { - type: typeof TENANT_SIGN_UP_CHANGE_USER_PASSWORD, - password: string, -} - -export const changeUserPasswordTenantSignUp = ( - password: string -): TenantSignUpChangeUserPassword => ({ - type: TENANT_SIGN_UP_CHANGE_USER_PASSWORD, - password, -}); - -// User password confirmation -export const TENANT_SIGN_UP_CHANGE_USER_PASSWORD_CONFIRMATION = 'TENANT_SIGN_UP_CHANGE_USER_PASSWORD_CONFIRMATION'; - -interface TenantSignUpChangeUserPasswordConfirmation { - type: typeof TENANT_SIGN_UP_CHANGE_USER_PASSWORD_CONFIRMATION, - passwordConfirmation: string, -} - -export const changeUserPasswordConfirmationTenantSignUp = ( - passwordConfirmation: string -): TenantSignUpChangeUserPasswordConfirmation => ({ - type: TENANT_SIGN_UP_CHANGE_USER_PASSWORD_CONFIRMATION, - passwordConfirmation, -}); - -// Confirm user data, proceed to step 2 -export const TENANT_SIGN_UP_CONFIRM_USER_FORM = 'TENANT_SIGN_UP_CONFIRM_USER_FORM'; - -interface TenantSignUpConfirmUserForm { - type: typeof TENANT_SIGN_UP_CONFIRM_USER_FORM; -} - -export const confirmUserFormTenantSignUp = (): TenantSignUpConfirmUserForm => ({ - type: TENANT_SIGN_UP_CONFIRM_USER_FORM, -}); - -// Tenant site name -export const TENANT_SIGN_UP_CHANGE_TENANT_SITE_NAME = 'TENANT_SIGN_UP_CHANGE_TENANT_SITE_NAME'; - -interface TenantSignUpChangeTenantSiteName { - type: typeof TENANT_SIGN_UP_CHANGE_TENANT_SITE_NAME, - siteName: string, -} - -export const changeTenantSiteNameTenantSignUp = ( - siteName: string -): TenantSignUpChangeTenantSiteName => ({ - type: TENANT_SIGN_UP_CHANGE_TENANT_SITE_NAME, - siteName, -}); - -// Tenant site name -export const TENANT_SIGN_UP_CHANGE_TENANT_SUBDOMAIN = 'TENANT_SIGN_UP_CHANGE_TENANT_SUBDOMAIN'; - -interface TenantSignUpChangeTenantSubdomain { - type: typeof TENANT_SIGN_UP_CHANGE_TENANT_SUBDOMAIN, - subdomain: string, -} - -export const changeTenantSubdomainTenantSignUp = ( - subdomain: string -): TenantSignUpChangeTenantSubdomain => ({ - type: TENANT_SIGN_UP_CHANGE_TENANT_SUBDOMAIN, - subdomain, -}); - - -export type TenantSignUpFormActions = - TenantSignUpToggleEmailAuth | - TenantSignUpChangeUserFullName | - TenantSignUpChangeUserEmail | - TenantSignUpChangeUserPassword | - TenantSignUpChangeUserPasswordConfirmation | - TenantSignUpConfirmUserForm | - TenantSignUpChangeTenantSiteName | - TenantSignUpChangeTenantSubdomain; \ No newline at end of file diff --git a/app/javascript/actions/changeSiteSettingsGeneralForm.ts b/app/javascript/actions/changeSiteSettingsGeneralForm.ts deleted file mode 100644 index 0146612b..00000000 --- a/app/javascript/actions/changeSiteSettingsGeneralForm.ts +++ /dev/null @@ -1,65 +0,0 @@ -// siteName -export const SITE_SETTINGS_CHANGE_GENERAL_FORM_SITE_NAME = 'SITE_SETTINGS_CHANGE_GENERAL_FORM_SITE_NAME'; - -interface SiteSettingsChangeGeneralFormSiteName { - type: typeof SITE_SETTINGS_CHANGE_GENERAL_FORM_SITE_NAME, - siteName: string, -} - -export const changeSiteSettingsGeneralFormSiteName = ( - siteName: string -): SiteSettingsChangeGeneralFormSiteName => ({ - type: SITE_SETTINGS_CHANGE_GENERAL_FORM_SITE_NAME, - siteName, -}); - -// siteLogo -export const SITE_SETTINGS_CHANGE_GENERAL_FORM_SITE_LOGO = 'SITE_SETTINGS_CHANGE_GENERAL_FORM_SITE_LOGO'; - -interface SiteSettingsChangeGeneralFormSiteLogo { - type: typeof SITE_SETTINGS_CHANGE_GENERAL_FORM_SITE_LOGO, - siteLogo: string, -} - -export const changeSiteSettingsGeneralFormSiteLogo = ( - siteLogo: string -): SiteSettingsChangeGeneralFormSiteLogo => ({ - type: SITE_SETTINGS_CHANGE_GENERAL_FORM_SITE_LOGO, - siteLogo, -}); - -// brandDisplaySetting -export const SITE_SETTINGS_CHANGE_GENERAL_FORM_BRAND_SETTING = 'SITE_SETTINGS_CHANGE_GENERAL_FORM_BRAND_SETTING'; - -interface SiteSettingsChangeGeneralFormBrandSetting { - type: typeof SITE_SETTINGS_CHANGE_GENERAL_FORM_BRAND_SETTING, - brandDisplaySetting: string, -} - -export const changeSiteSettingsGeneralFormBrandSetting = ( - brandDisplaySetting: string -): SiteSettingsChangeGeneralFormBrandSetting => ({ - type: SITE_SETTINGS_CHANGE_GENERAL_FORM_BRAND_SETTING, - brandDisplaySetting, -}); - -// locale -export const SITE_SETTINGS_CHANGE_GENERAL_FORM_LOCALE = 'SITE_SETTINGS_CHANGE_GENERAL_FORM_LOCALE'; - -interface SiteSettingsChangeGeneralFormLocale { - type: typeof SITE_SETTINGS_CHANGE_GENERAL_FORM_LOCALE, - locale: string, -} - -export const changeSiteSettingsGeneralFormLocale = ( - locale: string -): SiteSettingsChangeGeneralFormLocale => ({ - type: SITE_SETTINGS_CHANGE_GENERAL_FORM_LOCALE, - locale, -}); - -export type ChangeSiteSettingsGeneralFormActionTypes = - SiteSettingsChangeGeneralFormSiteName | - SiteSettingsChangeGeneralFormSiteLogo | - SiteSettingsChangeGeneralFormBrandSetting | - SiteSettingsChangeGeneralFormLocale; \ No newline at end of file diff --git a/app/javascript/components/SiteSettings/Boards/BoardForm.tsx b/app/javascript/components/SiteSettings/Boards/BoardForm.tsx index 39087b85..a5c1252b 100644 --- a/app/javascript/components/SiteSettings/Boards/BoardForm.tsx +++ b/app/javascript/components/SiteSettings/Boards/BoardForm.tsx @@ -1,4 +1,5 @@ import * as React from 'react'; +import { useForm, SubmitHandler } from 'react-hook-form'; import I18n from 'i18n-js'; import Button from '../../common/Button'; @@ -10,7 +11,7 @@ interface Props { name?: string; description?: string; - handleSubmit?( + handleCreate?( name: string, description: string, onSuccess: Function, @@ -22,101 +23,75 @@ interface Props { ): void; } -interface State { +interface IBoardForm { name: string; description: string; } -class BoardForm extends React.Component { - initialState: State = { - name: '', - description: '', - }; +const BoardForm = ({ + mode, + id, + name, + description, + handleCreate, + handleUpdate, +}: Props) => { + const { + register, + handleSubmit, + reset, + formState: { isValid }, + } = useForm({ + mode: 'onChange', + defaultValues: { + name: name || '', + description: description || '', + }, + }); - constructor(props: Props) { - super(props); - - this.state = this.props.mode === 'create' ? - this.initialState - : - { - name: this.props.name, - description: this.props.description, - }; - - this.onSubmit = this.onSubmit.bind(this); - } - - isFormValid() { - return this.state.name && this.state.name.length > 0; - } - - onNameChange(nameText: string) { - this.setState({ - name: nameText, - }); - } - - onDescriptionChange(descriptionText: string) { - this.setState({ - description: descriptionText, - }); - } - - onSubmit() { - if (this.props.mode === 'create') { - this.props.handleSubmit( - this.state.name, - this.state.description, - () => this.setState({...this.initialState}), + const onSubmit: SubmitHandler = data => { + if (mode === 'create') { + handleCreate( + data.name, + data.description, + () => reset({ name: '', description: '' }) ); } else { - this.props.handleUpdate(this.props.id, this.state.name, this.state.description); + handleUpdate(id, data.name, data.description); } } - render() { - const {mode} = this.props; - const {name, description} = this.state; - - return ( -
-
- this.onNameChange(e.target.value)} - autoFocus - className="form-control" - /> - - -
- -