ContactPanelShort: Use REST API to update account name and preferred email

Change-Id: I57dd5f6d527653c79e001076f3fad5d76874d49e
This commit is contained in:
David Pursehouse
2016-02-19 11:10:42 +09:00
committed by Edwin Kempin
parent dd884f9e3b
commit 576df448d7
2 changed files with 66 additions and 23 deletions

View File

@@ -83,6 +83,14 @@ public class AccountApi {
new RestApi("/accounts/").id(account).view("name").get(cb);
}
/** Set the account name */
public static void setName(String account, String name,
AsyncCallback<NativeString> cb) {
AccountNameInput input = AccountNameInput.create();
input.name(name);
new RestApi("/accounts/").id(account).view("name").put(input, cb);
}
/** Retrieve email addresses */
public static void getEmails(String account,
AsyncCallback<JsArray<EmailInfo>> cb) {
@@ -97,6 +105,13 @@ public class AccountApi {
.ifNoneMatch().put(in, cb);
}
/** Set preferred email address */
public static void setPreferredEmail(String account, String email,
AsyncCallback<NativeString> cb) {
new RestApi("/accounts/").id(account).view("emails")
.id(email).view("preferred").put(cb);
}
/** Retrieve SSH keys */
public static void getSshKeys(String account,
AsyncCallback<JsArray<SshKeyInfo>> cb) {
@@ -227,6 +242,17 @@ public class AccountApi {
}
}
private static class AccountNameInput extends JavaScriptObject {
final native void name(String n) /*-{ if(n)this.name=n; }-*/;
static AccountNameInput create() {
return createObject().cast();
}
protected AccountNameInput() {
}
}
public static void addGpgKey(String account, String armored,
AsyncCallback<NativeMap<GpgKeyInfo>> cb) {
new RestApi("/accounts/")

View File

@@ -15,7 +15,6 @@
package com.google.gerrit.client.account;
import com.google.gerrit.client.ErrorDialog;
import com.google.gerrit.client.FormatUtil;
import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.info.AccountInfo;
import com.google.gerrit.client.rpc.CallbackGroup;
@@ -46,7 +45,6 @@ import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwtexpui.globalkey.client.NpTextBox;
import com.google.gwtexpui.user.client.AutoCenterDialogBox;
import com.google.gwtjsonrpc.common.AsyncCallback;
class ContactPanelShort extends Composite {
protected final FlowPanel body;
@@ -146,7 +144,7 @@ class ContactPanelShort extends Composite {
save.addClickHandler(new ClickHandler() {
@Override
public void onClick(final ClickEvent event) {
doSave(null);
doSave();
}
});
@@ -347,10 +345,13 @@ class ContactPanelShort extends Composite {
inEmail.setFocus(true);
}
void doSave(final AsyncCallback<Account> onSave) {
String newName = canEditFullName() ? nameTxt.getText() : null;
if (newName != null && newName.trim().isEmpty()) {
void doSave() {
final String newName;
String name = canEditFullName() ? nameTxt.getText() : null;
if (name != null && name.trim().isEmpty()) {
newName = null;
} else {
newName = name;
}
final String newEmail;
@@ -368,24 +369,40 @@ class ContactPanelShort extends Composite {
save.setEnabled(false);
registerNewEmail.setEnabled(false);
Util.ACCOUNT_SEC.updateContact(newName, newEmail,
new GerritCallback<Account>() {
@Override
public void onSuccess(Account result) {
registerNewEmail.setEnabled(true);
onSaveSuccess(FormatUtil.asInfo(result));
if (onSave != null) {
onSave.onSuccess(result);
}
}
CallbackGroup group = new CallbackGroup();
if (!newEmail.equals(currentEmail)) {
AccountApi.setPreferredEmail("self", newEmail,
group.add(new GerritCallback<NativeString>() {
@Override
public void onSuccess(NativeString result) {
}
}));
}
AccountApi.setName("self", newName,
group.add(new GerritCallback<NativeString>() {
@Override
public void onSuccess(NativeString result) {
}
@Override
public void onFailure(final Throwable caught) {
save.setEnabled(true);
registerNewEmail.setEnabled(true);
super.onFailure(caught);
}
});
@Override
public void onFailure(Throwable caught) {
save.setEnabled(true);
registerNewEmail.setEnabled(true);
super.onFailure(caught);
}
}));
group.done();
group.addListener(new GerritCallback<Void>() {
@Override
public void onSuccess(Void result) {
currentEmail = newEmail;
AccountInfo me = Gerrit.getUserAccount();
me.email(currentEmail);
me.name(newName);
onSaveSuccess(me);
registerNewEmail.setEnabled(true);
}
});
}
void onSaveSuccess(AccountInfo result) {