2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-12-27 11:02:53 +01:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-12-28 18:09:45 +01:00
|
|
|
import { action, computed, observable, transaction } from 'mobx';
|
2016-12-27 11:02:53 +01:00
|
|
|
|
2017-05-09 12:01:44 +02:00
|
|
|
import { validateName } from '@parity/shared/util/validation';
|
2016-12-27 11:02:53 +01:00
|
|
|
|
|
|
|
export default class Store {
|
|
|
|
@observable address = null;
|
|
|
|
@observable isAccount = false;
|
2017-03-03 19:50:54 +01:00
|
|
|
@observable isBusy = false;
|
2016-12-27 11:02:53 +01:00
|
|
|
@observable description = null;
|
2016-12-28 18:09:45 +01:00
|
|
|
@observable meta = null;
|
2016-12-27 11:02:53 +01:00
|
|
|
@observable name = null;
|
|
|
|
@observable nameError = null;
|
|
|
|
@observable passwordHint = null;
|
2016-12-28 18:09:45 +01:00
|
|
|
@observable tags = null;
|
2017-02-24 18:05:04 +01:00
|
|
|
@observable vaultName = null;
|
2016-12-27 11:02:53 +01:00
|
|
|
|
|
|
|
constructor (api, account) {
|
|
|
|
const { address, name, meta, uuid } = account;
|
|
|
|
|
|
|
|
this._api = api;
|
|
|
|
|
2016-12-28 18:09:45 +01:00
|
|
|
transaction(() => {
|
|
|
|
this.address = address;
|
|
|
|
this.meta = meta || {};
|
|
|
|
this.name = name || '';
|
2017-02-24 18:05:04 +01:00
|
|
|
this.isAccount = !!uuid;
|
2016-12-28 18:09:45 +01:00
|
|
|
|
|
|
|
this.description = this.meta.description || '';
|
|
|
|
this.passwordHint = this.meta.passwordHint || '';
|
2017-05-16 12:25:47 +02:00
|
|
|
this.tags = this.meta.tags && this.meta.tags.slice() || [];
|
2017-02-24 18:05:04 +01:00
|
|
|
this.vaultName = this.meta.vault;
|
2016-12-28 18:09:45 +01:00
|
|
|
});
|
2016-12-27 11:02:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@computed get hasError () {
|
|
|
|
return !!(this.nameError);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action setDescription = (description) => {
|
|
|
|
this.description = description;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action setName = (_name) => {
|
|
|
|
const { name, nameError } = validateName(_name);
|
|
|
|
|
|
|
|
transaction(() => {
|
|
|
|
this.name = name;
|
|
|
|
this.setNameError(nameError);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@action setNameError = (nameError) => {
|
|
|
|
this.nameError = nameError;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action setPasswordHint = (passwordHint) => {
|
|
|
|
this.passwordHint = passwordHint;
|
|
|
|
}
|
|
|
|
|
2017-03-03 19:50:54 +01:00
|
|
|
@action setBusy = (isBusy) => {
|
|
|
|
this.isBusy = isBusy;
|
|
|
|
}
|
|
|
|
|
2016-12-27 11:02:53 +01:00
|
|
|
@action setTags = (tags) => {
|
2016-12-28 18:09:45 +01:00
|
|
|
this.tags = tags.slice();
|
2016-12-27 11:02:53 +01:00
|
|
|
}
|
|
|
|
|
2017-02-24 18:05:04 +01:00
|
|
|
@action setVaultName = (vaultName) => {
|
|
|
|
this.vaultName = vaultName;
|
|
|
|
}
|
|
|
|
|
2017-03-03 19:50:54 +01:00
|
|
|
save (vaultStore) {
|
|
|
|
this.setBusy(true);
|
2017-02-24 18:05:04 +01:00
|
|
|
|
2016-12-27 11:02:53 +01:00
|
|
|
const meta = {
|
|
|
|
description: this.description,
|
|
|
|
tags: this.tags.peek()
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.isAccount) {
|
|
|
|
meta.passwordHint = this.passwordHint;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise
|
|
|
|
.all([
|
|
|
|
this._api.parity.setAccountName(this.address, this.name),
|
2016-12-28 18:09:45 +01:00
|
|
|
this._api.parity.setAccountMeta(this.address, Object.assign({}, this.meta, meta))
|
2016-12-27 11:02:53 +01:00
|
|
|
])
|
2017-03-03 19:50:54 +01:00
|
|
|
.then(() => {
|
|
|
|
if (vaultStore && this.isAccount && (this.meta.vault !== this.vaultName)) {
|
|
|
|
return vaultStore.moveAccount(this.vaultName, this.address);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
this.setBusy(false);
|
|
|
|
})
|
2016-12-27 11:02:53 +01:00
|
|
|
.catch((error) => {
|
|
|
|
console.error('onSave', error);
|
2017-03-03 19:50:54 +01:00
|
|
|
this.setBusy(false);
|
2017-01-03 17:41:21 +01:00
|
|
|
throw error;
|
2016-12-27 11:02:53 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|