|
| 1 | +import { FormInput, messageInstance, PasswordInput } from "lowcoder-design"; |
| 2 | +import { |
| 3 | + AuthBottomView, |
| 4 | + ConfirmButton, |
| 5 | + FormWrapperMobile, |
| 6 | + LoginCardTitle, |
| 7 | + StyledRouteLink, |
| 8 | +} from "pages/userAuth/authComponents"; |
| 9 | +import React, { useContext, useMemo, useState } from "react"; |
| 10 | +import styled from "styled-components"; |
| 11 | +import UserApi from "api/userApi"; |
| 12 | +import { useRedirectUrl } from "util/hooks"; |
| 13 | +import { checkEmailValid, checkPhoneValid } from "util/stringUtils"; |
| 14 | +import { UserConnectionSource } from "@lowcoder-ee/constants/userConstants"; |
| 15 | +import { trans } from "i18n"; |
| 16 | +import { AuthContext, useAuthSubmit } from "pages/userAuth/authUtils"; |
| 17 | +import { ThirdPartyAuth } from "pages/userAuth/thirdParty/thirdPartyAuth"; |
| 18 | +import { AUTH_FORGOT_PASSWORD_URL, AUTH_REGISTER_URL, ORG_AUTH_FORGOT_PASSWORD_URL, ORG_AUTH_REGISTER_URL } from "constants/routesURL"; |
| 19 | +import { Link, useLocation, useParams } from "react-router-dom"; |
| 20 | +import { Divider } from "antd"; |
| 21 | +import Flex from "antd/es/flex"; |
| 22 | +import { validateResponse } from "@lowcoder-ee/api/apiUtils"; |
| 23 | +import OrgApi from "@lowcoder-ee/api/orgApi"; |
| 24 | +import Card from "antd/es/card/Card"; |
| 25 | +import { AccountLoginWrapper } from "./formLogin"; |
| 26 | +import { default as Button } from "antd/es/button"; |
| 27 | +import LeftOutlined from "@ant-design/icons/LeftOutlined"; |
| 28 | + |
| 29 | +const StyledCard = styled.div<{$selected: boolean}>` |
| 30 | + display: flex; |
| 31 | + justify-content: center; |
| 32 | + flex-direction: column; |
| 33 | + min-height: 56px; |
| 34 | + margin-bottom: -1px; |
| 35 | + padding: 0 24px; |
| 36 | + color: rgba(0, 0, 0, 0.88); |
| 37 | + font-size: 16px; |
| 38 | + background: transparent; |
| 39 | + border: 1px solid #f0f0f0; |
| 40 | + border-radius: 8px; |
| 41 | + cursor: pointer; |
| 42 | + margin-bottom: 16px; |
| 43 | + // box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.03), 0 1px 6px -1px rgba(0, 0, 0, 0.02); |
| 44 | + ${props => props.$selected && `background: #e6f4ff;`} |
| 45 | +
|
| 46 | + &:hover { |
| 47 | + box-shadow: 0 1px 2px -2px rgba(0, 0, 0, 0.16), 0 3px 6px 0 rgba(0, 0, 0, 0.12), 0 5px 12px 4px rgba(0, 0, 0, 0.09); |
| 48 | + } |
| 49 | +`; |
| 50 | + |
| 51 | +type OrgItem = { |
| 52 | + orgId: string; |
| 53 | + orgName: string; |
| 54 | +} |
| 55 | + |
| 56 | +enum CurrentStepEnum { |
| 57 | + EMAIL = "EMAIL", |
| 58 | + WORKSPACES = "WORKSPACES", |
| 59 | + AUTH_PROVIDERS = "AUTH_PROVIDERS", |
| 60 | +} |
| 61 | + |
| 62 | +const StepHeader = (props : { |
| 63 | + title: string, |
| 64 | +}) => ( |
| 65 | + <Flex justify="center" style={{marginBottom: '22px'}}> |
| 66 | + <h3 style={{margin: 0}}>{props.title}</h3> |
| 67 | + </Flex> |
| 68 | +) |
| 69 | + |
| 70 | +const StepBackButton = (props : { |
| 71 | + onClick: () => void, |
| 72 | +}) => ( |
| 73 | + <Button |
| 74 | + type="link" |
| 75 | + icon={<LeftOutlined style={{fontSize: '12px'}} />} |
| 76 | + style={{ |
| 77 | + position: 'absolute', |
| 78 | + padding: 0, |
| 79 | + }} |
| 80 | + onClick={props.onClick} |
| 81 | + > |
| 82 | + Back |
| 83 | + </Button> |
| 84 | +) |
| 85 | +export default function FormLoginSteps() { |
| 86 | + const [account, setAccount] = useState(""); |
| 87 | + const [password, setPassword] = useState(""); |
| 88 | + const redirectUrl = useRedirectUrl(); |
| 89 | + const { systemConfig, inviteInfo, fetchUserAfterAuthSuccess } = useContext(AuthContext); |
| 90 | + const invitationId = inviteInfo?.invitationId; |
| 91 | + const authId = systemConfig?.form.id; |
| 92 | + const location = useLocation(); |
| 93 | + const [orgLoading, setOrgLoading] = useState(false); |
| 94 | + const [orgList, setOrgList] = useState<OrgItem[]>([]); |
| 95 | + const [currentStep, setCurrentStep] = useState<CurrentStepEnum>(CurrentStepEnum.EMAIL); |
| 96 | + const [organizationId, setOrganizationId] = useState<string>(); |
| 97 | + |
| 98 | + const { onSubmit, loading } = useAuthSubmit( |
| 99 | + () => |
| 100 | + UserApi.formLogin({ |
| 101 | + register: false, |
| 102 | + loginId: account, |
| 103 | + password: password, |
| 104 | + invitationId: invitationId, |
| 105 | + source: UserConnectionSource.email, |
| 106 | + orgId: organizationId, |
| 107 | + authId, |
| 108 | + }), |
| 109 | + false, |
| 110 | + redirectUrl, |
| 111 | + fetchUserAfterAuthSuccess, |
| 112 | + ); |
| 113 | + |
| 114 | + const fetchOrgsByEmail = () => { |
| 115 | + setOrgLoading(true); |
| 116 | + OrgApi.fetchOrgsByEmail(account) |
| 117 | + .then((resp) => { |
| 118 | + if (validateResponse(resp)) { |
| 119 | + console.log(resp.data.data); |
| 120 | + setOrgList(resp.data.data); |
| 121 | + if (resp.data.data.length === 1) { |
| 122 | + setOrganizationId(resp.data.data[0].orgId); |
| 123 | + setCurrentStep(CurrentStepEnum.AUTH_PROVIDERS); |
| 124 | + return; |
| 125 | + } |
| 126 | + setCurrentStep(CurrentStepEnum.WORKSPACES); |
| 127 | + } else { |
| 128 | + throw new Error('Error while fetching organizations'); |
| 129 | + } |
| 130 | + }) |
| 131 | + .catch((e) => { |
| 132 | + messageInstance.error(e.message); |
| 133 | + }) |
| 134 | + .finally(() => { |
| 135 | + setOrgLoading(false); |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | + if(currentStep === CurrentStepEnum.EMAIL) { |
| 140 | + return ( |
| 141 | + <> |
| 142 | + <AccountLoginWrapper> |
| 143 | + <StepHeader title={trans("userAuth.inputEmail")} /> |
| 144 | + <FormInput |
| 145 | + className="form-input" |
| 146 | + // label={trans("userAuth.email")} |
| 147 | + label={''} |
| 148 | + defaultValue={account} |
| 149 | + onChange={(value, valid) => setAccount(valid ? value : "")} |
| 150 | + placeholder={trans("userAuth.inputEmail")} |
| 151 | + checkRule={{ |
| 152 | + check: (value) => checkPhoneValid(value) || checkEmailValid(value), |
| 153 | + errorMsg: trans("userAuth.inputValidEmail"), |
| 154 | + }} |
| 155 | + /> |
| 156 | + <ConfirmButton loading={orgLoading} disabled={!account} onClick={fetchOrgsByEmail}> |
| 157 | + {/* {trans("userAuth.login")} */} |
| 158 | + Continue |
| 159 | + </ConfirmButton> |
| 160 | + </AccountLoginWrapper> |
| 161 | + <Divider/> |
| 162 | + <AuthBottomView> |
| 163 | + <StyledRouteLink to={{ |
| 164 | + pathname: AUTH_REGISTER_URL, |
| 165 | + state: location.state |
| 166 | + }}> |
| 167 | + {trans("userAuth.register")} |
| 168 | + </StyledRouteLink> |
| 169 | + </AuthBottomView> |
| 170 | + </> |
| 171 | + ) |
| 172 | + } |
| 173 | + |
| 174 | + if (currentStep === CurrentStepEnum.WORKSPACES) { |
| 175 | + return ( |
| 176 | + <> |
| 177 | + <AccountLoginWrapper> |
| 178 | + <StepBackButton onClick={() => setCurrentStep(CurrentStepEnum.EMAIL)} /> |
| 179 | + <StepHeader title={"Select your workspace"} /> |
| 180 | + {orgList.map(org => ( |
| 181 | + <StyledCard |
| 182 | + key={org.orgId} |
| 183 | + $selected={organizationId === org.orgId} |
| 184 | + onClick={() => { |
| 185 | + setOrganizationId(org.orgId); |
| 186 | + setCurrentStep(CurrentStepEnum.AUTH_PROVIDERS); |
| 187 | + }} |
| 188 | + > |
| 189 | + {org.orgName} |
| 190 | + </StyledCard> |
| 191 | + ))} |
| 192 | + </AccountLoginWrapper> |
| 193 | + </> |
| 194 | + ) |
| 195 | + } |
| 196 | + |
| 197 | + return ( |
| 198 | + <> |
| 199 | + <AccountLoginWrapper> |
| 200 | + <StepBackButton onClick={() => setCurrentStep(CurrentStepEnum.WORKSPACES)} /> |
| 201 | + <StepHeader title={"Enter your password"} /> |
| 202 | + <PasswordInput |
| 203 | + className="form-input password-input" |
| 204 | + passInputConf={{ |
| 205 | + label: ' ', |
| 206 | + }} |
| 207 | + onChange={(value) => setPassword(value)} |
| 208 | + valueCheck={() => [true, ""]} |
| 209 | + /> |
| 210 | + <Flex justify="end" style={{margin: '10px 0'}}> |
| 211 | + <Link to={{ |
| 212 | + pathname: AUTH_FORGOT_PASSWORD_URL, |
| 213 | + state: location.state |
| 214 | + }} |
| 215 | + > |
| 216 | + {`${trans("userAuth.forgotPassword")}?`} |
| 217 | + </Link> |
| 218 | + </Flex> |
| 219 | + <ConfirmButton loading={loading} disabled={!account || !password} onClick={onSubmit}> |
| 220 | + {trans("userAuth.login")} |
| 221 | + </ConfirmButton> |
| 222 | + {organizationId && ( |
| 223 | + <ThirdPartyAuth |
| 224 | + invitationId={invitationId} |
| 225 | + invitedOrganizationId={organizationId} |
| 226 | + authGoal="login" |
| 227 | + /> |
| 228 | + )} |
| 229 | + </AccountLoginWrapper> |
| 230 | + <Divider/> |
| 231 | + <AuthBottomView> |
| 232 | + <StyledRouteLink to={{ |
| 233 | + pathname: AUTH_REGISTER_URL, |
| 234 | + state: location.state |
| 235 | + }}> |
| 236 | + {trans("userAuth.register")} |
| 237 | + </StyledRouteLink> |
| 238 | + </AuthBottomView> |
| 239 | + </> |
| 240 | + ); |
| 241 | +} |
0 commit comments