@@ -16,6 +16,7 @@ import typings.officeUiFabricReact.personaTypesMod.{ IPersonaProps, PersonaSize
16
16
import typings .officeUiFabricReact .stackTypesMod .{ Alignment , IStackProps }
17
17
import typings .react .anon .Children
18
18
19
+ import scala .concurrent .Future
19
20
import scala .scalajs .concurrent .JSExecutionContext .Implicits .queue
20
21
import scala .scalajs .js
21
22
import scala .scalajs .js .JSON
@@ -28,25 +29,48 @@ class ContributorSection extends Component {
28
29
import ContributorSection ._
29
30
30
31
type Props = Unit
31
- case class State (personas : Seq [IPersonaProps ], hrefs : Seq [String ])
32
- override def initialState : State = State (Nil , Nil )
32
+ case class State (
33
+ texts : Seq [String ], imageUrls : Seq [String ], secondaryTexts : Seq [String ], hrefs : Seq [String ], isMain : Seq [Boolean ]
34
+ ) {
35
+ def toPersonaProps : Seq [PersonaProps ] = texts.indices.map { i =>
36
+ PersonaProps (imageUrls(i), texts(i), secondaryTexts(i), isMain(i))
37
+ }
38
+ }
39
+
40
+ override def initialState : State = State (Nil , Nil , Nil , Nil , Nil )
33
41
val css : ContributorCSS .type = ContributorCSS
34
42
35
43
override def componentWillMount (): Unit = {
36
44
Ajax .get(" https://api.github.com/repos/Pixeval/Pixeval/contributors" ).onComplete {
37
45
case Success (value) => {
38
- val contributors = JSON .parse(value.responseText).asInstanceOf [js.Array [GithubUser ]].toSeq
39
- val personas : Seq [IPersonaProps ] = contributors.zipWithIndex.map {
40
- case (user, i) => new PersonaProps (user, i).asJS
41
- }
42
- val order : Seq [Int ] = centralizeContributors(personas.size);
46
+ val contributors = JSON .parse(value.responseText).asInstanceOf [js.Array [Contributor ]].toSeq
47
+ val order : Seq [Int ] = centralizeContributors( contributors.size)
48
+ val texts : Seq [ String ] = order map contributors.map(_.login)
49
+ val imageUrls : Seq [ String ] = order map contributors.map(_.avatar_url)
50
+ val secondaryTexts : Seq [String ] = order map contributors.map(info => s " 贡献: ${ info.contributions } " )
43
51
val hrefs : Seq [String ] = order map contributors.map(_.html_url)
44
- this .setState(State (order map personas, hrefs))
52
+ val isMain : Seq [Boolean ] = order map (true :: List .fill(contributors.size - 1 )(false ))
53
+
54
+ val namesFuture = Future .traverse(texts)(getUserName)
55
+ namesFuture onComplete {
56
+ case Success (newTexts) => this .setState(State (newTexts, imageUrls, secondaryTexts, hrefs, isMain))
57
+ case Failure (_) => throw new Exception ()
58
+ }
45
59
}
46
60
case Failure (_) => throw new Exception ()
47
61
}
48
62
}
49
63
64
+ private def getUserName (login : String ): Future [String ] =
65
+ Ajax .get(s " https://api.github.com/users/ $login" ).map {
66
+ response => {
67
+ val user = JSON .parse(response.responseText).asInstanceOf [GithubUser ]
68
+ val name = user.name
69
+ if (name == null ) login
70
+ else name
71
+ }
72
+ }
73
+
50
74
final val headerDesc : String = " 贡献者"
51
75
52
76
private def renderPersona (): ReactElement = Stack .withProps(IStackProps ()
@@ -56,7 +80,7 @@ class ContributorSection extends Component {
56
80
.setHorizontalAlign(Alignment .center)
57
81
.setClassName(" Contributor-stack" ).asInstanceOf [IStackProps with Children ])(
58
82
59
- this .state.personas .map(Persona .withProps)
83
+ this .state.toPersonaProps.map(_.asJS) .map(Persona .withProps)
60
84
)
61
85
62
86
private def addAForPersonas (): Unit = {
@@ -93,7 +117,7 @@ class ContributorSection extends Component {
93
117
}
94
118
95
119
override def render (): ReactElement = {
96
- if (this .state.personas .nonEmpty)
120
+ if (this .state.isMain .nonEmpty)
97
121
section(className := " Contributor" )(
98
122
99
123
h2(className := " Contributor-title" , style := js.Dynamic .literal(
@@ -113,19 +137,22 @@ object ContributorSection {
113
137
object ContributorCSS extends js.Object
114
138
115
139
@ js.native
116
- trait GithubUser extends js.Object {
140
+ trait Contributor extends js.Object {
117
141
val login : String
118
142
val avatar_url : String
119
143
val contributions : Int
120
144
val html_url : String
121
145
}
122
146
123
- class PersonaProps (info : GithubUser , i : Int ) {
124
- val imageUrl : String = info.avatar_url
125
- val text : String = info.login
126
- val secondaryText : String = s " 贡献: ${ info.contributions }"
147
+ @ js.native
148
+ trait GithubUser extends js.Object {
149
+ val login : String
150
+ val name : String
151
+ }
152
+
153
+ case class PersonaProps (imageUrl : String , text : String , secondaryText : String , isMain : Boolean ) {
127
154
128
- def getSize : PersonaSize with Double = if (i == 0 ) mainSize else otherSize
155
+ def getSize : PersonaSize with Double = if (isMain ) mainSize else otherSize
129
156
130
157
def asJS : IPersonaProps = {
131
158
val obj = IPersonaProps ()
0 commit comments