Skip to content

Commit

Permalink
fix: empty countryCode of current account causes crash (casdoor#1603)
Browse files Browse the repository at this point in the history
* fix: empty countryCode of current account cause crush

* Update UserEditPage.js

---------

Co-authored-by: hsluoyz <hsluoyz@qq.com>
  • Loading branch information
leo220yuyaodog and hsluoyz authored Mar 1, 2023
1 parent d21ae8a commit 8696b08
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
4 changes: 2 additions & 2 deletions web/src/ResetModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const ResetModal = (props) => {
const [confirmLoading, setConfirmLoading] = React.useState(false);
const [dest, setDest] = React.useState("");
const [code, setCode] = React.useState("");
const {buttonText, destType, application, account} = props;
const {buttonText, destType, application, countryCode} = props;

const showModal = () => {
setVisible(true);
Expand Down Expand Up @@ -87,7 +87,7 @@ export const ResetModal = (props) => {
<Row style={{width: "100%", marginBottom: "20px"}}>
<Input
addonBefore={destType === "email" ? i18next.t("user:New Email") : i18next.t("user:New phone")}
prefix={destType === "email" ? <React.Fragment><MailOutlined />&nbsp;&nbsp;</React.Fragment> : (<React.Fragment><PhoneOutlined />&nbsp;&nbsp;{`+${Setting.getCountryCode(account.countryCode)}`}&nbsp;</React.Fragment>)}
prefix={destType === "email" ? <React.Fragment><MailOutlined />&nbsp;&nbsp;</React.Fragment> : (<React.Fragment><PhoneOutlined />&nbsp;&nbsp;{countryCode !== "" ? "+" : null}{Setting.getCountryCode(countryCode)}&nbsp;</React.Fragment>)}
placeholder={placeholder}
onChange={e => setDest(e.target.value)}
/>
Expand Down
5 changes: 4 additions & 1 deletion web/src/Setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,10 @@ export function initCountries() {
}

export function getCountryCode(country) {
return phoneNumber.getCountryCallingCode(country);
if (phoneNumber.isSupportedCountry(country)) {
return phoneNumber.getCountryCallingCode(country);
}
return "";
}

export function getCountryCodeData(countryCodes = phoneNumber.getCountries()) {
Expand Down
8 changes: 6 additions & 2 deletions web/src/UserEditPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ class UserEditPage extends React.Component {
return this.isSelf() || Setting.isAdminUser(this.props.account);
}

getCountryCode() {
return this.props.account.countryCode;
}

renderAccountItem(accountItem) {
if (!accountItem.visible) {
return null;
Expand Down Expand Up @@ -296,7 +300,7 @@ class UserEditPage extends React.Component {
</Col>
<Col span={Setting.isMobile() ? 22 : 11} >
{/* backend auto get the current user, so admin can not edit. Just self can reset*/}
{this.isSelf() ? <ResetModal application={this.state.application} account={this.props.account} disabled={disabled} buttonText={i18next.t("user:Reset Email...")} destType={"email"} /> : null}
{this.isSelf() ? <ResetModal application={this.state.application} disabled={disabled} buttonText={i18next.t("user:Reset Email...")} destType={"email"} /> : null}
</Col>
</Row>
);
Expand Down Expand Up @@ -326,7 +330,7 @@ class UserEditPage extends React.Component {
</Input.Group>
</Col>
<Col span={Setting.isMobile() ? 24 : 11} >
{this.isSelf() ? (<ResetModal application={this.state.application} account={this.props.account} disabled={disabled} buttonText={i18next.t("user:Reset Phone...")} destType={"phone"} />) : null}
{this.isSelf() ? (<ResetModal application={this.state.application} countryCode={this.getCountryCode()} disabled={disabled} buttonText={i18next.t("user:Reset Phone...")} destType={"phone"} />) : null}
</Col>
</Row>
);
Expand Down

0 comments on commit 8696b08

Please sign in to comment.