|
20 | 20 | * IN THE SOFTWARE.
|
21 | 21 | */
|
22 | 22 |
|
23 |
| -import { Observable, map } from "rxjs" |
| 23 | +import { Observable, from, map, switchMap, tap } from "rxjs" |
24 | 24 |
|
25 |
| -import { getElement, requestJSON } from "~/browser" |
| 25 | +import { getOptionalElement, requestJSON } from "~/browser" |
26 | 26 |
|
27 | 27 | import { renderPrivateSponsor, renderPublicSponsor } from "_/templates"
|
28 | 28 |
|
29 |
| -import { Component, getComponentElement } from "../_" |
| 29 | +import { Component, getComponentElements } from "../_" |
30 | 30 |
|
31 | 31 | /* ----------------------------------------------------------------------------
|
32 | 32 | * Types
|
@@ -113,35 +113,46 @@ export function mountSponsorship(
|
113 | 113 | )
|
114 | 114 |
|
115 | 115 | /* Retrieve adjacent components */
|
116 |
| - const count = getComponentElement("sponsorship-count") |
117 |
| - const total = getComponentElement("sponsorship-total") |
118 |
| - |
119 |
| - /* Render sponsorship */ |
120 |
| - sponsorship$.subscribe(sponsorship => { |
121 |
| - el.removeAttribute("hidden") |
122 |
| - |
123 |
| - /* Render public sponsors with avatar and links */ |
124 |
| - const list = getElement(":scope > :first-child", el) |
125 |
| - for (const sponsor of sponsorship.sponsors) |
126 |
| - if (sponsor.type === "public") |
127 |
| - list.appendChild(renderPublicSponsor(sponsor.user)) |
128 |
| - |
129 |
| - /* Render combined private sponsors */ |
130 |
| - list.appendChild(renderPrivateSponsor( |
131 |
| - sponsorship.sponsors.filter(({ type }) => ( |
132 |
| - type === "private" |
133 |
| - )).length |
134 |
| - )) |
135 |
| - |
136 |
| - /* Render sponsorship count and total */ |
137 |
| - count.innerText = `${sponsorship.sponsors.length}` |
138 |
| - total.innerText = `$ ${sponsorship.total |
139 |
| - .toString() |
140 |
| - .replace(/\B(?=(\d{3})+(?!\d))/g, ",") |
141 |
| - } a month` |
142 |
| - }) |
| 116 | + const count = getComponentElements("sponsorship-count") |
| 117 | + const total = getComponentElements("sponsorship-total") |
143 | 118 |
|
144 |
| - // /* Create and return component */ |
| 119 | + /* Render sponsorship count */ |
| 120 | + sponsorship$.pipe( |
| 121 | + switchMap(sponsorship => from(count).pipe( |
| 122 | + tap(child => child.innerText = `${sponsorship.sponsors.length}`) |
| 123 | + )) |
| 124 | + ) |
| 125 | + .subscribe(() => el.removeAttribute("hidden")) |
| 126 | + |
| 127 | + /* Render sponsorship total */ |
| 128 | + sponsorship$.pipe( |
| 129 | + switchMap(sponsorship => from(total).pipe( |
| 130 | + tap(child => child.innerText = `$ ${sponsorship.total |
| 131 | + .toString() |
| 132 | + .replace(/\B(?=(\d{3})+(?!\d))/g, ",") |
| 133 | + } a month`) |
| 134 | + )) |
| 135 | + ) |
| 136 | + .subscribe() |
| 137 | + |
| 138 | + // Render sponsorship list |
| 139 | + const list = getOptionalElement(":scope > .mdx-sponsorship__list", el) |
| 140 | + if (list && count.length) { |
| 141 | + sponsorship$.subscribe(sponsorship => { |
| 142 | + for (const sponsor of sponsorship.sponsors) |
| 143 | + if (sponsor.type === "public") |
| 144 | + list.appendChild(renderPublicSponsor(sponsor.user)) |
| 145 | + |
| 146 | + /* Render combined private sponsors */ |
| 147 | + list.appendChild(renderPrivateSponsor( |
| 148 | + sponsorship.sponsors.filter(({ type }) => ( |
| 149 | + type === "private" |
| 150 | + )).length |
| 151 | + )) |
| 152 | + }) |
| 153 | + } |
| 154 | + |
| 155 | + /* Create and return component */ |
145 | 156 | return sponsorship$
|
146 | 157 | .pipe(
|
147 | 158 | map(state => ({ ref: el, ...state }))
|
|
0 commit comments