This repository has been archived by the owner on Feb 18, 2022. It is now read-only.
/
CharacterDetail.tsx
131 lines (123 loc) · 3.34 KB
/
CharacterDetail.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import React from "react";
import "./CharacterDetail.css";
import {
CharacterDetailComponent,
CharacterDetailFieldsFragment
} from "./gen-types";
interface Props {
selectedCharacter?: number;
setSelectedCharacter: (characterId: number) => void;
}
const CharacterDetail: React.FC<Props> = ({
selectedCharacter,
setSelectedCharacter
}) => {
return (
<div className="CharacterDetail">
{selectedCharacter ? (
<CharacterDetailComponent variables={{ id: String(selectedCharacter) }}>
{({ loading, error, data }) => {
if (loading) return "Loading...";
if (error || !data || !data.getCharacter) return `Error!`;
return (
<Detail
character={data.getCharacter}
select={(id: number) => {
setSelectedCharacter(id);
window.scrollTo(0, 0);
}}
/>
);
}}
</CharacterDetailComponent>
) : (
<>
<h2>Character Detail</h2>
<div>Please select a character</div>
</>
)}
</div>
);
};
const Detail: React.FC<{
character: CharacterDetailFieldsFragment;
select: (characterId: number) => void;
}> = ({ character, select }) => {
return (
<>
<h2>{character.name}</h2>
{character.allegiances && character.allegiances.length > 0 && (
<div>
<strong>Loyal to</strong>:{" "}
{character.allegiances.map(allegiance => allegiance.name).join(", ")}
</div>
)}
{renderItem("Culture", character.culture)}
{renderItem("Played by", character.playedBy)}
{renderListItem("Titles", character.titles)}
{renderListItem("Aliases", character.aliases)}
{renderItem("Born", character.born)}
{renderItem("Died", character.died)}
{renderItem("Culture", character.culture)}
{renderCharacter(select, "Father", character.father)}
{renderCharacter(select, "Mother", character.mother)}
{renderCharacter(select, "Spouse", character.spouse)}
{character.children && character.children.length > 0 && (
<div>
<strong>Children</strong>:{" "}
{character.children.map(child => (
<>
<a href="#" onClick={() => select(parseInt(child.id))}>
{child.name}
</a>{" "}
</>
))}
</div>
)}
{renderListItem(
"TV Seasons",
character.appearedIn ? character.appearedIn.map(x => x.name) : []
)}
{renderListItem(
"Books",
character.books ? character.books.map(x => x.name) : []
)}
</>
);
};
export default CharacterDetail;
const renderItem = (label: string, item?: string | null) => {
return (
item && (
<div>
<strong>{label}</strong>: {item}
</div>
)
);
};
const renderListItem = (label: string, items?: string[] | null) => {
return (
items &&
items.length > 0 && (
<div>
<strong>{label}</strong>: {items.join(", ")}
</div>
)
);
};
const renderCharacter = (
select: any,
label: string,
item: { name: string; id: string } | null
) => {
return (
item && (
<div>
<strong>{label}</strong>:{" "}
<a href="#" onClick={() => select(parseInt(item.id))}>
{item.name}
</a>
</div>
)
);
};