|
| 1 | +import { Box, Callout } from '@rocket.chat/fuselage'; |
| 2 | +import type { Meta, Story } from '@storybook/react'; |
| 3 | +import { useState } from 'react'; |
| 4 | + |
| 5 | +import type { AdminInfoPayload } from '../../forms/AdminInfoForm/AdminInfoForm'; |
| 6 | +import type { CloudAccountEmailPayload } from '../../forms/CloudAccountEmailForm/CloudAccountEmailForm'; |
| 7 | +import type { OrganizationInfoPayload } from '../../forms/OrganizationInfoForm/OrganizationInfoForm'; |
| 8 | +import type { RegisterServerPayload } from '../../forms/RegisterServerForm/RegisterServerForm'; |
| 9 | +import AdminInfoPage from '../../pages/AdminInfoPage'; |
| 10 | +import AwaitingConfirmationPage from '../../pages/AwaitingConfirmationPage'; |
| 11 | +import CloudAccountEmailPage from '../../pages/CloudAccountEmailPage'; |
| 12 | +import ConfirmationProcessPage from '../../pages/ConfirmationProcessPage'; |
| 13 | +import EmailConfirmedPage from '../../pages/EmailConfirmedPage'; |
| 14 | +import OrganizationInfoPage from '../../pages/OrganizationInfoPage'; |
| 15 | +import RegisterServerPage from '../../pages/RegisterServerPage'; |
| 16 | +import { |
| 17 | + countryOptions, |
| 18 | + logSubmit, |
| 19 | + organizationIndustryOptions, |
| 20 | + organizationSizeOptions, |
| 21 | + organizationTypes, |
| 22 | + validateEmail, |
| 23 | + validatePassword, |
| 24 | + validateUsername, |
| 25 | +} from './mocks'; |
| 26 | + |
| 27 | +export default { |
| 28 | + title: 'flows/Self-Hosted Registration', |
| 29 | + parameters: { |
| 30 | + layout: 'fullscreen', |
| 31 | + actions: { argTypesRegex: '^on.*' }, |
| 32 | + loki: { skip: true }, |
| 33 | + }, |
| 34 | +} as Meta; |
| 35 | + |
| 36 | +export const SelfHostedRegistration: Story = () => { |
| 37 | + const [path, navigateTo] = |
| 38 | + useState<`/${ |
| 39 | + | 'admin-info' |
| 40 | + | 'org-info' |
| 41 | + | 'register-server' |
| 42 | + | 'cloud-email' |
| 43 | + | 'awaiting' |
| 44 | + | 'home' |
| 45 | + | 'email' |
| 46 | + | 'confirmation-progress' |
| 47 | + | 'email-confirmed'}`>('/admin-info'); |
| 48 | + |
| 49 | + const [adminInfo, setAdminInfo] = |
| 50 | + useState<Omit<AdminInfoPayload, 'password'>>(); |
| 51 | + |
| 52 | + const [organizationInfo, setOrganizationInfo] = |
| 53 | + useState<OrganizationInfoPayload>(); |
| 54 | + |
| 55 | + const [serverRegistration, setServerRegistration] = useState<{ |
| 56 | + updates?: boolean; |
| 57 | + agreement?: boolean; |
| 58 | + cloudAccountEmail?: string; |
| 59 | + securityCode?: string; |
| 60 | + }>(); |
| 61 | + |
| 62 | + const handleAdminInfoSubmit = logSubmit((data: AdminInfoPayload) => { |
| 63 | + setAdminInfo(data); |
| 64 | + navigateTo('/org-info'); |
| 65 | + }); |
| 66 | + |
| 67 | + const handleOrganizationInfoSubmit = logSubmit( |
| 68 | + (data: OrganizationInfoPayload) => { |
| 69 | + setOrganizationInfo(data); |
| 70 | + navigateTo('/register-server'); |
| 71 | + } |
| 72 | + ); |
| 73 | + |
| 74 | + const handleRegisterServerSubmit = logSubmit( |
| 75 | + (data: RegisterServerPayload) => { |
| 76 | + switch (data.registerType) { |
| 77 | + case 'standalone': { |
| 78 | + navigateTo('/home'); |
| 79 | + break; |
| 80 | + } |
| 81 | + |
| 82 | + case 'registered': { |
| 83 | + setServerRegistration((serverRegistration) => ({ |
| 84 | + ...serverRegistration, |
| 85 | + updates: data.updates, |
| 86 | + agreement: data.agreement, |
| 87 | + })); |
| 88 | + navigateTo('/cloud-email'); |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + ); |
| 94 | + |
| 95 | + const handleCloudAccountEmailSubmit = logSubmit( |
| 96 | + (data: CloudAccountEmailPayload) => { |
| 97 | + setServerRegistration((serverRegistration) => ({ |
| 98 | + ...serverRegistration, |
| 99 | + cloudAccountEmail: data.email, |
| 100 | + securityCode: 'Funny Tortoise In The Hat', |
| 101 | + })); |
| 102 | + navigateTo('/awaiting'); |
| 103 | + } |
| 104 | + ); |
| 105 | + |
| 106 | + if (path === '/admin-info') { |
| 107 | + return ( |
| 108 | + <AdminInfoPage |
| 109 | + currentStep={1} |
| 110 | + stepCount={4} |
| 111 | + passwordRulesHint='' |
| 112 | + validateUsername={validateUsername} |
| 113 | + validateEmail={validateEmail} |
| 114 | + validatePassword={validatePassword} |
| 115 | + initialValues={adminInfo} |
| 116 | + onSubmit={handleAdminInfoSubmit} |
| 117 | + /> |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + if (path === '/org-info') { |
| 122 | + return ( |
| 123 | + <OrganizationInfoPage |
| 124 | + currentStep={2} |
| 125 | + stepCount={4} |
| 126 | + organizationTypeOptions={organizationTypes} |
| 127 | + organizationIndustryOptions={organizationIndustryOptions} |
| 128 | + organizationSizeOptions={organizationSizeOptions} |
| 129 | + countryOptions={countryOptions} |
| 130 | + initialValues={organizationInfo} |
| 131 | + onBackButtonClick={() => navigateTo('/admin-info')} |
| 132 | + onSubmit={handleOrganizationInfoSubmit} |
| 133 | + /> |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + if (path === '/register-server') { |
| 138 | + return ( |
| 139 | + <RegisterServerPage |
| 140 | + currentStep={3} |
| 141 | + stepCount={4} |
| 142 | + initialValues={{ |
| 143 | + ...(serverRegistration?.updates && { |
| 144 | + updates: serverRegistration?.updates, |
| 145 | + }), |
| 146 | + ...(serverRegistration?.agreement && { |
| 147 | + agreement: serverRegistration?.agreement, |
| 148 | + }), |
| 149 | + }} |
| 150 | + onBackButtonClick={() => navigateTo('/org-info')} |
| 151 | + onSubmit={handleRegisterServerSubmit} |
| 152 | + /> |
| 153 | + ); |
| 154 | + } |
| 155 | + |
| 156 | + if (path === '/cloud-email') { |
| 157 | + return ( |
| 158 | + <CloudAccountEmailPage |
| 159 | + currentStep={4} |
| 160 | + stepCount={4} |
| 161 | + initialValues={{}} |
| 162 | + onBackButtonClick={() => navigateTo('/register-server')} |
| 163 | + onSubmit={handleCloudAccountEmailSubmit} |
| 164 | + /> |
| 165 | + ); |
| 166 | + } |
| 167 | + |
| 168 | + if (path === '/awaiting') { |
| 169 | + if (!serverRegistration?.cloudAccountEmail) { |
| 170 | + throw new Error('missing cloud account email'); |
| 171 | + } |
| 172 | + |
| 173 | + if (!serverRegistration?.securityCode) { |
| 174 | + throw new Error('missing verification code'); |
| 175 | + } |
| 176 | + |
| 177 | + setTimeout(() => { |
| 178 | + navigateTo('/confirmation-progress'); |
| 179 | + }, 5000); |
| 180 | + |
| 181 | + return ( |
| 182 | + <AwaitingConfirmationPage |
| 183 | + emailAddress={serverRegistration.cloudAccountEmail} |
| 184 | + securityCode={serverRegistration.securityCode} |
| 185 | + onChangeEmailRequest={() => navigateTo('/admin-info')} |
| 186 | + onResendEmailRequest={() => undefined} |
| 187 | + /> |
| 188 | + ); |
| 189 | + } |
| 190 | + |
| 191 | + if (path === '/confirmation-progress') { |
| 192 | + setTimeout(() => { |
| 193 | + navigateTo('/email-confirmed'); |
| 194 | + }, 3000); |
| 195 | + |
| 196 | + return <ConfirmationProcessPage />; |
| 197 | + } |
| 198 | + |
| 199 | + if (path === '/email-confirmed') { |
| 200 | + return <EmailConfirmedPage />; |
| 201 | + } |
| 202 | + |
| 203 | + if (path === '/home') { |
| 204 | + return ( |
| 205 | + <Box |
| 206 | + width='100vw' |
| 207 | + height='100vh' |
| 208 | + display='flex' |
| 209 | + justifyContent='center' |
| 210 | + alignItems='center' |
| 211 | + > |
| 212 | + <Callout type='success'>This is the home of the workspace.</Callout> |
| 213 | + </Box> |
| 214 | + ); |
| 215 | + } |
| 216 | + |
| 217 | + throw new Error('invalid path'); |
| 218 | +}; |
| 219 | +SelfHostedRegistration.storyName = 'Self-Hosted Registration'; |
0 commit comments