瀏覽代碼

Helper methods for generating deck tiles

crobi 9 年之前
父節點
當前提交
7b636450f1
共有 2 個文件被更改,包括 59 次插入17 次删除
  1. 10
    14
      client/src/views/components/deck.ts
  2. 49
    3
      client/src/views/decks.ts

+ 10
- 14
client/src/views/components/deck.ts 查看文件

@@ -17,23 +17,23 @@ module rpgcards {
17 17
     interface DeckTileState {
18 18
     }
19 19
 
20
-    export function tileHeader(id: string, name: string) {
20
+    export function tileHeader(props: DeckTileProps) {
21 21
         return React.DOM.div
22 22
             ( { className: 'deck-tile-header', onClick: null }
23
-            , React.DOM.div({ className: 'deck-tile-header-name' }, name)
24
-            , React.DOM.div({ className: 'deck-tile-header-id' }, id.substring(0, 5))
23
+            , React.DOM.div({ className: 'deck-tile-header-name' }, props.name)
24
+            , React.DOM.div({ className: 'deck-tile-header-id' }, props.id)
25 25
             );
26 26
     }
27 27
 
28
-    export function tileBody(description: string) {
28
+    export function tileBody(props: DeckTileProps) {
29 29
         return React.DOM.div
30 30
             ( { className: 'deck-tile-body', onClick: null }
31
-            , description
31
+            , props.desc
32 32
             );
33 33
     }
34 34
 
35
-    export function tileFooter(cards: number) {
36
-        let cardsText = "This deck contains " + cards + " cards.";
35
+    export function tileFooter(props: DeckTileProps) {
36
+        let cardsText = props.cards + " cards";
37 37
         return React.DOM.div
38 38
             ( { className: 'deck-tile-footer', onClick: null }
39 39
             , cardsText
@@ -61,16 +61,12 @@ module rpgcards {
61 61
         }
62 62
 
63 63
         render() {
64
-            let deckId = this.props.id;
65
-            let deckName = this.props.name;
66
-            let deckDesc = this.props.desc;
67
-            let deckCards = this.props.cards;
68 64
 
69 65
             return React.DOM.div
70 66
                 ( { className: 'deck-tile' }
71
-                , tileHeader(deckId, deckName)
72
-                , tileBody(deckDesc)
73
-                , tileFooter(deckCards)
67
+                , tileHeader(this.props)
68
+                , tileBody(this.props)
69
+                , tileFooter(this.props)
74 70
                 );
75 71
         }
76 72
     }

+ 49
- 3
client/src/views/decks.ts 查看文件

@@ -2,6 +2,51 @@
2 2
 
3 3
 module rpgcards {
4 4
 
5
+        const deckColor = "#F44336";
6
+
7
+        function deckTile(deck: Deck, cards: string[]) {
8
+            return <React.ReactElement<any>> DeckTile({
9
+                key   : deck.id,
10
+                id    : deck.id,
11
+                name  : deck.name,
12
+                desc  : deck.description,
13
+                cards : cards.length,
14
+                icon  : null,
15
+                color : deckColor });
16
+        }
17
+
18
+        function loadingTile(id: string) {
19
+            return <React.ReactElement<any>> DeckTile({
20
+                key   : id,
21
+                id    : id,
22
+                name  : "Loading...",
23
+                desc  : "",
24
+                cards : null,
25
+                icon  : null,
26
+                color : deckColor });
27
+        }
28
+
29
+        function errorTile(id: string, e: Error) {
30
+            return <React.ReactElement<any>> DeckTile({
31
+                key   : id,
32
+                id    : id,
33
+                name  : "Error",
34
+                desc  : e.message,
35
+                cards : null,
36
+                icon  : null,
37
+                color : deckColor });
38
+        }
39
+
40
+        function newDeckTile() {
41
+            return <React.ReactElement<any>> DeckTile({
42
+                key   : "#new",
43
+                id    : null,
44
+                name  : "New deck",
45
+                desc  : null,
46
+                cards : null,
47
+                icon  : "add circle",
48
+                color : deckColor });
49
+        }
5 50
 
6 51
     export function renderDecks(store: Store): React.ReactElement<any> {
7 52
         var decks = store.getDeckList().fmap(deckIds => deckIds.map(id => {
@@ -17,8 +62,8 @@ module rpgcards {
17 62
                     cards: cards.length });
18 63
             }).caseOf({
19 64
                 just: (t) => t,
20
-                nothing: (e) => React.DOM.div({}, "error: " + e.message),
21
-                pending: () => React.DOM.div({}, "loading...")
65
+                nothing: (e) => errorTile(id, e),
66
+                pending: () => loadingTile(id)
22 67
             });
23 68
         })).caseOf({
24 69
             just: (t) => t,
@@ -27,7 +72,8 @@ module rpgcards {
27 72
         });
28 73
 
29 74
         return React.DOM.div({ className: 'decks' },
30
-            decks
75
+            decks,
76
+            newDeckTile()
31 77
         );
32 78
     }
33 79
 }

Loading…
取消
儲存