:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(signup): make the invite role optional and focus the failed field (#107886)
posthog[bot]d392ba767960
diff --git a/frontend/src/lib/components/SignupRoleSelect.tsx b/frontend/src/lib/components/SignupRoleSelect.tsxindex fcedd434aa62..a84bd0776d2f 100644--- a/frontend/src/lib/components/SignupRoleSelect.tsx+++ b/frontend/src/lib/components/SignupRoleSelect.tsx@@ -33,7 +33,7 @@ export default function SignupRoleSelect({showOptional,}: {className?: string- // Only the organization confirmation form lets people submit without a role+ // Only the invite signup form lets people submit without a roleshowOptional?: boolean}): JSX.Element {return (@@ -43,8 +43,9 @@ export default function SignupRoleSelect({className={className}showOptional={showOptional}>- {({ value, onChange }) => (+ {({ value, onChange, error, id }) => (<LemonSelect+ id={id}fullWidthdata-attr="signup-role-at-organization"options={roleOptions}@@ -53,6 +54,7 @@ export default function SignupRoleSelect({value={value || undefined}onChange={onChange}placeholder="Select your role"+ status={error ? 'danger' : 'default'}/>)}</LemonField>diff --git a/frontend/src/scenes/authentication/invite-signup/InviteSignup.test.tsx b/frontend/src/scenes/authentication/invite-signup/InviteSignup.test.tsxnew file mode 100644index 000000000000..049cd08af9e2--- /dev/null+++ b/frontend/src/scenes/authentication/invite-signup/InviteSignup.test.tsx@@ -0,0 +1,55 @@+import '@testing-library/jest-dom'++import { cleanup, render, screen } from '@testing-library/react'+import userEvent from '@testing-library/user-event'+import { Provider } from 'kea'+import { router } from 'kea-router'++import { useMocks } from '~/mocks/jest'+import { initKeaTests } from '~/test/init'++import { InviteSignup } from './InviteSignup'+import { inviteSignupLogic } from './inviteSignupLogic'++describe('InviteSignup', () => {+ beforeEach(() => {+ useMocks({+ get: {+ // The signup form only renders for a logged-out person+ '/api/users/@me/': () => [401, { detail: 'Not authenticated' }],+ '/api/signup/1234/': () => [+ 200,+ { id: '1234', target_email: '[email protected]', first_name: 'Jane', organization_name: 'Example' },+ ],+ },+ post: {+ '/api/login/precheck': () => [200, { sso_enforcement: null, saml_available: false }],+ },+ })+ window.POSTHOG_APP_CONTEXT = { current_user: null } as any+ initKeaTests()+ inviteSignupLogic.mount()+ router.actions.push('/signup/1234')+ render(+ <Provider>+ <InviteSignup />+ </Provider>+ )+ })++ afterEach(() => {+ cleanup()+ })++ it('points a submit with no password at the password input', async () => {+ const passwordInput = await screen.findByPlaceholderText('••••••••••')+ await userEvent.click(await screen.findByText(/Join Example/))++ expect(passwordInput).toHaveFocus()+ expect(screen.getByText('Please enter your password to continue')).toBeInTheDocument()+ })++ it('marks the role as optional, because this form submits without one', async () => {+ expect((await screen.findByText('What is your role?')).closest('label')).toHaveTextContent('(optional)')+ })+})diff --git a/frontend/src/scenes/authentication/invite-signup/InviteSignupForm.tsx b/frontend/src/scenes/authentication/invite-signup/InviteSignupForm.tsxindex a5d8958ba34a..ea3d4e7a63c8 100644--- a/frontend/src/scenes/authentication/invite-signup/InviteSignupForm.tsx+++ b/frontend/src/scenes/authentication/invite-signup/InviteSignupForm.tsx@@ -226,7 +226,7 @@ function InviteNewUser({ invite }: { invite: PrevalidatedInvite }): JSX.Element/>)}</LemonField>- <SignupRoleSelect />+ <SignupRoleSelect showOptional /></>)}diff --git a/frontend/src/scenes/authentication/invite-signup/inviteSignupLogic.test.ts b/frontend/src/scenes/authentication/invite-signup/inviteSignupLogic.test.tsnew file mode 100644index 000000000000..261cf1b73943--- /dev/null+++ b/frontend/src/scenes/authentication/invite-signup/inviteSignupLogic.test.ts@@ -0,0 +1,58 @@+import { expectLogic } from 'kea-test-utils'++import { useMocks } from '~/mocks/jest'+import { initKeaTests } from '~/test/init'++import { inviteSignupLogic } from './inviteSignupLogic'++const INVITE_ID = '1234'++describe('inviteSignupLogic', () => {+ let logic: ReturnType<typeof inviteSignupLogic.build>++ beforeEach(async () => {+ useMocks({+ get: {+ [`/api/signup/${INVITE_ID}/`]: () => [+ 200,+ { id: INVITE_ID, target_email: '[email protected]', organization_name: 'Example' },+ ],+ },+ })+ initKeaTests()+ logic = inviteSignupLogic()+ logic.mount()+ await expectLogic(logic, () => {+ logic.actions.prevalidateInvite(INVITE_ID)+ }).toDispatchActions(['prevalidateInviteSuccess'])+ })++ it('shows the name error on touch, before any submit', () => {+ expect(logic.values.signupErrors).toEqual({})++ logic.actions.touchSignupField('first_name')+ expect(logic.values.signupErrors).toEqual({ first_name: 'Please enter your name' })++ logic.actions.setSignupValue('first_name', 'Jane')+ expect(logic.values.signupErrors).toEqual({})+ })++ it('submits without a role, and sends the next attempt after the API rejects one', async () => {+ const signupRequest = jest.fn(() => [400, { code: 'invalid_input', detail: 'Something went wrong.' }])+ useMocks({ post: { [`/api/signup/${INVITE_ID}/`]: signupRequest } })+ logic.actions.setSignupValues({ first_name: 'Jane', password: 'a-long-and-unusual-passphrase-42' })++ await expectLogic(logic, () => {+ logic.actions.submitSignup()+ }).toFinishAllListeners()+ expect(logic.values.signupManualErrors).toEqual({+ generic: { code: 'invalid_input', detail: 'Something went wrong.' },+ })++ // Nothing touches `generic`, so without preSubmit clearing it every retry would fail validation+ await expectLogic(logic, () => {+ logic.actions.submitSignup()+ }).toFinishAllListeners()+ expect(signupRequest).toHaveBeenCalledTimes(2)+ })+})diff --git a/frontend/src/scenes/authentication/invite-signup/inviteSignupLogic.ts b/frontend/src/scenes/authentication/invite-signup/inviteSignupLogic.tsindex 74626ead0155..cdc4c1091538 100644--- a/frontend/src/scenes/authentication/invite-signup/inviteSignupLogic.ts+++ b/frontend/src/scenes/authentication/invite-signup/inviteSignupLogic.ts@@ -32,6 +32,15 @@ export interface AcceptInvitePayloadInterface {role_at_organization?: string}+const LOGIC_PATH = ['scenes', 'authentication', 'invite-signup', 'inviteSignupLogic']+const FORM_KEY = 'signup'++// In the order the form renders them, so a failed submit lands on the first field to fix+const FIELDS_IN_RENDER_ORDER: (keyof AcceptInvitePayloadInterface)[] = ['password', 'first_name']++// kea-forms ids each field input `<logic path>.<form key>.<field>`+const fieldInputId = (field: keyof AcceptInvitePayloadInterface): string => [...LOGIC_PATH, FORM_KEY, field].join('.')+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface inviteSignupLogicValues {acceptedInvite: null@@ -175,7 +184,7 @@ export type inviteSignupLogicType = MakeLogicType<>export const inviteSignupLogic = kea<inviteSignupLogicType>([- path(['scenes', 'authentication', 'invite-signup', 'inviteSignupLogic']),+ path(LOGIC_PATH),actions({setError: (payload: ErrorInterface) => ({ payload }),registerPasskey: true,@@ -286,8 +295,11 @@ export const inviteSignupLogic = kea<inviteSignupLogicType>([})),forms(({ actions, values }) => ({signup: {+ options: {+ showErrorsOnTouch: true,+ },defaults: { role_at_organization: '' } as AcceptInvitePayloadInterface,- errors: ({ password, first_name, role_at_organization }) => ({+ errors: ({ password, first_name }) => ({password:!values.passkeyRegistered && !password? 'Please enter your password to continue'@@ -295,8 +307,12 @@ export const inviteSignupLogic = kea<inviteSignupLogicType>([? values.validatedPassword.feedback || undefined: undefined,first_name: !first_name ? 'Please enter your name' : undefined,- role_at_organization: !role_at_organization ? 'Please select your role to continue' : undefined,}),+ // A manual error from a rejected attempt counts as a validation error, and nothing touches+ // `generic` to clear it. Without this, every later submit fails without a request.+ preSubmit: () => {+ actions.setSignupManualErrors({})+ },submit: async (payload, breakpoint) => {breakpoint()@@ -375,6 +391,19 @@ export const inviteSignupLogic = kea<inviteSignupLogicType>([],}),listeners(({ actions, values }) => ({+ // kea-forms passes its own `errors` payload here, but reads it off a selector that does not+ // exist, so it always arrives undefined. Take the errors from the logic instead.+ submitSignupFailure: () => {+ const errors = values.signupAllErrors+ const firstInvalidField = FIELDS_IN_RENDER_ORDER.find((field) => errors[field])+ if (!firstInvalidField) {+ return+ }+ const input = document.getElementById(fieldInputId(firstInvalidField))+ // preventScroll so the smooth scroll below is the only movement the person sees+ input?.focus({ preventScroll: true })+ input?.scrollIntoView({ behavior: 'smooth', block: 'center' })+ },setTurnstileToken: ({ token }) => {if (token && values.challengeNonce) {actions.submitSignup()diff --git a/frontend/src/scenes/organization/ConfirmOrganization/ConfirmOrganization.test.tsx b/frontend/src/scenes/organization/ConfirmOrganization/ConfirmOrganization.test.tsxindex 768ebe6fb021..4635981f225c 100644--- a/frontend/src/scenes/organization/ConfirmOrganization/ConfirmOrganization.test.tsx+++ b/frontend/src/scenes/organization/ConfirmOrganization/ConfirmOrganization.test.tsx@@ -46,7 +46,13 @@ describe('ConfirmOrganization', () => {expect(screen.getByText('Select your role')).toBeInTheDocument()})- it('marks the role as optional, because this form submits without one', () => {- expect(screen.getByText('What is your role?').closest('label')).toHaveTextContent('(optional)')+ it('points a submit with no role at the role dropdown', async () => {+ await userEvent.type(screen.getByLabelText('Organization name'), 'Spikes Inc')+ await userEvent.click(screen.getByText('Create organization'))++ const roleSelect = screen.getByText('Select your role').closest('button')+ expect(roleSelect).toHaveFocus()+ expect(roleSelect).toHaveClass('LemonButton--status-danger')+ expect(screen.getByText('Please select your role to continue')).toBeInTheDocument()})})diff --git a/frontend/src/scenes/organization/ConfirmOrganization/ConfirmOrganization.tsx b/frontend/src/scenes/organization/ConfirmOrganization/ConfirmOrganization.tsxindex 6e862e09374d..ac02a024264b 100644--- a/frontend/src/scenes/organization/ConfirmOrganization/ConfirmOrganization.tsx+++ b/frontend/src/scenes/organization/ConfirmOrganization/ConfirmOrganization.tsx@@ -82,7 +82,7 @@ export function ConfirmOrganization(): JSX.Element {<LemonInput className="ph-ignore-input" placeholder="Hogflix Movies" /></LemonField>- <SignupRoleSelect showOptional />+ <SignupRoleSelect /><SignupReferralSource disabled={isConfirmOrganizationSubmitting} /><LemonButtondiff --git a/frontend/src/scenes/organization/ConfirmOrganization/confirmOrganizationLogic.test.ts b/frontend/src/scenes/organization/ConfirmOrganization/confirmOrganizationLogic.test.tsindex 9badcbb42984..b914e0b60fed 100644--- a/frontend/src/scenes/organization/ConfirmOrganization/confirmOrganizationLogic.test.ts+++ b/frontend/src/scenes/organization/ConfirmOrganization/confirmOrganizationLogic.test.ts@@ -52,13 +52,14 @@ describe('confirmOrganizationLogic', () => {})describe('form', () => {- it('requires a name and an organization name', async () => {+ it('requires a name, an organization name and a role', async () => {await expectLogic(logic, () => {logic.actions.submitConfirmOrganization()}).toMatchValues({confirmOrganizationValidationErrors: {first_name: 'Please enter your name',organization_name: ORGANIZATION_NAME_ERROR,+ role_at_organization: 'Please select your role to continue',},})})@@ -96,7 +97,11 @@ describe('confirmOrganizationLogic', () => {],},})- logic.actions.setConfirmOrganizationValues({ first_name: 'Spike', organization_name: 'Spikes Inc' })+ logic.actions.setConfirmOrganizationValues({+ first_name: 'Spike',+ organization_name: 'Spikes Inc',+ role_at_organization: 'engineering',+ })await expectLogic(logic, () => {logic.actions.submitConfirmOrganization()diff --git a/frontend/src/scenes/organization/ConfirmOrganization/confirmOrganizationLogic.ts b/frontend/src/scenes/organization/ConfirmOrganization/confirmOrganizationLogic.tsindex 209b9827a626..d92bb2df5a07 100644--- a/frontend/src/scenes/organization/ConfirmOrganization/confirmOrganizationLogic.ts+++ b/frontend/src/scenes/organization/ConfirmOrganization/confirmOrganizationLogic.ts@@ -147,11 +147,12 @@ export const confirmOrganizationLogic = kea<confirmOrganizationLogicType>([referral_source: readPendingOAuthConnection()?.clientName ?? '',referral_source_ai_prompt: '',} as ConfirmOrganizationFormValues,- errors: ({ organization_name, first_name }) => ({+ errors: ({ organization_name, first_name, role_at_organization }) => ({first_name: !first_name?.trim() ? 'Please enter your name' : undefined,organization_name: !organization_name?.trim()? "Please enter an organization name. You can use your own name if you don't belong to one.": undefined,+ role_at_organization: !role_at_organization ? 'Please select your role to continue' : undefined,}),// A manual error from a rejected attempt counts as a validation error, and only its own