Make OAuthWidget work for prompt page.

This commit is contained in:
Yang Luo 2021-06-20 02:02:08 +08:00
parent 0f69f4f07c
commit e0b6270f50
3 changed files with 88 additions and 15 deletions

View File

@ -249,7 +249,9 @@ class UserEditPage extends React.Component {
<Col span={22} > <Col span={22} >
<div style={{marginBottom: 20}}> <div style={{marginBottom: 20}}>
{ {
this.state.application?.providers.filter(providerItem => Setting.isProviderVisible(providerItem)).map((providerItem, index) => <OAuthWidget user={this.state.user} application={this.state.application} providerItem={providerItem} onUnlinked={() => { return this.unlinked()}} />) (this.state.application === null || this.state.user === null) ? null : (
this.state.application?.providers.filter(providerItem => Setting.isProviderVisible(providerItem)).map((providerItem, index) => <OAuthWidget key={providerItem.name} labelSpan={3} user={this.state.user} application={this.state.application} providerItem={providerItem} onUnlinked={() => { return this.unlinked()}} />)
)
} }
</div> </div>
</Col> </Col>

View File

@ -19,6 +19,7 @@ import * as Setting from "../Setting";
import i18next from "i18next"; import i18next from "i18next";
import AffiliationSelect from "../common/AffiliationSelect"; import AffiliationSelect from "../common/AffiliationSelect";
import * as UserBackend from "../backend/UserBackend"; import * as UserBackend from "../backend/UserBackend";
import OAuthWidget from "../common/OAuthWidget";
class PromptPage extends React.Component { class PromptPage extends React.Component {
constructor(props) { constructor(props) {
@ -28,14 +29,26 @@ class PromptPage extends React.Component {
type: props.type, type: props.type,
applicationName: props.applicationName !== undefined ? props.applicationName : (props.match === undefined ? null : props.match.params.applicationName), applicationName: props.applicationName !== undefined ? props.applicationName : (props.match === undefined ? null : props.match.params.applicationName),
application: null, application: null,
user: Setting.deepCopy(this.props.account), user: null,
}; };
} }
UNSAFE_componentWillMount() { UNSAFE_componentWillMount() {
this.getUser();
this.getApplication(); this.getApplication();
} }
getUser() {
const organizationName = this.props.account.owner;
const userName = this.props.account.name;
UserBackend.getUser(organizationName, userName)
.then((user) => {
this.setState({
user: user,
});
});
}
getApplication() { getApplication() {
if (this.state.applicationName === null) { if (this.state.applicationName === null) {
return; return;
@ -72,15 +85,29 @@ class PromptPage extends React.Component {
this.setState({ this.setState({
user: user, user: user,
}); });
this.submitUserEdit(false);
}
getAllPromptedProviderItems(application) {
return application.providers.filter(providerItem => Setting.isProviderPrompted(providerItem));
}
isAffiliationPrompted(application) {
const signupItems = application.signupItems.filter(signupItem => signupItem.name === "Affiliation");
if (signupItems.length === 0) {
return false;
}
return signupItems[0].prompted;
} }
renderAffiliation(application) { renderAffiliation(application) {
const signupItems = application.signupItems.filter(signupItem => signupItem.name === "Affiliation"); if (!this.isAffiliationPrompted(application)) {
if (signupItems.length === 0) {
return null; return null;
} }
if (!signupItems[0].prompted) { if (application === null || this.state.user === null) {
return null; return null;
} }
@ -89,38 +116,82 @@ class PromptPage extends React.Component {
) )
} }
unlinked() {
this.getUser();
}
renderContent(application) { renderContent(application) {
return ( return (
<div style={{width: '400px'}}> <div style={{width: '400px'}}>
{ {
this.renderAffiliation(application) this.renderAffiliation(application)
} }
<div>
{
(application === null || this.state.user === null) ? null : (
application?.providers.filter(providerItem => Setting.isProviderPrompted(providerItem)).map((providerItem, index) => <OAuthWidget key={providerItem.name} labelSpan={6} user={this.state.user} application={application} providerItem={providerItem} onUnlinked={() => { return this.unlinked()}} />)
)
}
</div>
</div> </div>
) )
} }
isAnswered() { isProviderItemAnswered(application, providerItem) {
if (this.state.user === null) { if (this.state.user === null) {
return false; return false;
} }
const provider = providerItem.provider;
const linkedValue = this.state.user[provider.type.toLowerCase()];
return linkedValue !== undefined && linkedValue !== "";
}
isAffiliationAnswered(application) {
if (!this.isAffiliationPrompted(application)) {
return true;
}
if (this.state.user === null) {
return false;
}
return this.state.user.affiliation !== ""; return this.state.user.affiliation !== "";
} }
submitUserEdit() { isAnswered(application) {
if (!this.isAffiliationAnswered(application)) {
return false;
}
const providerItems = this.getAllPromptedProviderItems(application);
for (let i = 0; i < providerItems.length; i ++) {
if (!this.isProviderItemAnswered(application, providerItems[i])) {
return false;
}
}
return true;
}
submitUserEdit(isFinal) {
let user = Setting.deepCopy(this.state.user); let user = Setting.deepCopy(this.state.user);
UserBackend.updateUser(this.state.user.owner, this.state.user.name, user) UserBackend.updateUser(this.state.user.owner, this.state.user.name, user)
.then((res) => { .then((res) => {
if (res.msg === "") { if (res.msg === "") {
Setting.showMessage("success", `Successfully saved`); if (isFinal) {
Setting.showMessage("success", `Successfully saved`);
Setting.goToLogin(this, this.getApplicationObj()); Setting.goToLogin(this, this.getApplicationObj());
}
} else { } else {
Setting.showMessage("error", res.msg); if (isFinal) {
Setting.showMessage("error", res.msg);
}
} }
}) })
.catch(error => { .catch(error => {
Setting.showMessage("error", `Failed to connect to server: ${error}`); if (isFinal) {
Setting.showMessage("error", `Failed to connect to server: ${error}`);
}
}); });
} }
@ -148,7 +219,7 @@ class PromptPage extends React.Component {
</Col> </Col>
</Row> </Row>
<div style={{marginTop: "50px"}}> <div style={{marginTop: "50px"}}>
<Button disabled={!this.isAnswered()} type="primary" size="large" onClick={this.submitUserEdit.bind(this)}>{i18next.t("signup:Submit and complete")}</Button> <Button disabled={!this.isAnswered(application)} type="primary" size="large" onClick={() => {this.submitUserEdit(true)}}>{i18next.t("signup:Submit and complete")}</Button>
</div> </div>
</div> </div>
</Col> </Col>

View File

@ -130,7 +130,7 @@ class OAuthWidget extends React.Component {
return ( return (
<Row key={provider.name} style={{marginTop: '20px'}} > <Row key={provider.name} style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={3}> <Col style={{marginTop: '5px'}} span={this.props.labelSpan}>
{ {
Setting.getProviderLogo(provider) Setting.getProviderLogo(provider)
} }
@ -140,9 +140,9 @@ class OAuthWidget extends React.Component {
} }
</span> </span>
</Col> </Col>
<Col span={21} > <Col span={24 - this.props.labelSpan} >
<img style={{marginRight: '10px'}} width={30} height={30} src={avatarUrl} alt={name} /> <img style={{marginRight: '10px'}} width={30} height={30} src={avatarUrl} alt={name} />
<span style={{width: '300px', display: "inline-block"}}> <span style={{width: this.props.labelSpan === 3 ? '300px' : '130px', display: "inline-block"}}>
{ {
linkedValue === "" ? ( linkedValue === "" ? (
"(empty)" "(empty)"