Inject index properly into ReferendumInfo (with unwrap) (#718)

* Inject inject properly into ReferendumInfo (with unwrap)

* Use unwrapOr

* Fix type import
This commit is contained in:
Jaco Greeff
2019-02-19 15:20:45 +01:00
committed by GitHub
parent f57a6b1533
commit f820dfcffa
2 changed files with 16 additions and 13 deletions
@@ -16,10 +16,10 @@ import { drr } from '../util/drr';
* A [[ReferendumInfo]] with an additional `index` field
*/
export class ReferendumInfoExtended extends ReferendumInfo {
constructor (info: any) {
super(info);
constructor (value: ReferendumInfo) {
super(value);
this.set('index', new ReferendumIndex(info.index));
this.set('index', value.get('index') as ReferendumIndex);
}
/**
@@ -34,14 +34,15 @@ export function referendumInfo (api: ApiInterface$Rx) {
return (index: BN | number): Observable<Option<ReferendumInfoExtended>> => {
return (api.query.democracy.referendumInfoOf(index) as Observable<Option<ReferendumInfo>>)
.pipe(
map((info) =>
new Option<ReferendumInfoExtended>(
ReferendumInfoExtended,
info.isSome
? { ...info.toJSON(), index }
: null
)
),
map((optionInfo) => {
const info = optionInfo.unwrapOr(null);
if (info) {
info.set('index', new ReferendumIndex(index));
}
return new Option<ReferendumInfoExtended>(ReferendumInfoExtended, info);
}),
drr()
);
};