-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathVotes.tsx
More file actions
191 lines (171 loc) · 4.72 KB
/
Votes.tsx
File metadata and controls
191 lines (171 loc) · 4.72 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import * as React from "react";
import { Observable } from "rxjs";
import { IVoteQueryOptions as FilterOptions } from "@daostack/arc.js";
import {
Arc as Protocol,
ArcConfig as ProtocolConfig,
InferredVote as Component,
VoteEntity as Entity,
VoteData as Data,
DAO,
DAOEntity,
Member,
MemberEntity,
Proposal,
ProposalEntity,
CProps,
ComponentList,
ComponentListLogs,
ComponentListProps,
createFilterFromScope,
} from "../";
import { CreateContextFeed } from "../runtime/ContextFeed";
type Scopes = "DAO" | "Member as voter" | "Proposal";
const scopeProps: Record<Scopes, string> = {
DAO: "dao",
"Member as voter": "voter",
Proposal: "proposal",
};
interface RequiredProps extends ComponentListProps<Entity, FilterOptions> {
from?: Scopes;
}
interface InferredProps extends RequiredProps {
config: ProtocolConfig;
dao?: string;
voter?: string;
proposal?: string;
}
class InferredVotes extends ComponentList<InferredProps, Component> {
createObservableEntities(): Observable<Entity[]> {
const { config, from, filter } = this.props;
if (!config) {
throw Error(
"Arc Config Missing: Please provide this field as a prop, or use the inference component."
);
}
const f = createFilterFromScope(filter, from, scopeProps, this.props);
return Entity.search(config.connection, f);
}
renderComponent(
entity: Entity,
children: any,
index: number
): React.ComponentElement<CProps<Component>, any> {
const { config } = this.props;
if (!entity.id) {
throw Error("Vote Entity ID undefined. This should never happen.");
}
return (
<Component
key={`${entity.id}_${index}`}
id={entity.id}
config={config}
entity={entity}
>
{children}
</Component>
);
}
public static get Entities() {
return CreateContextFeed(
this.EntitiesContext.Consumer,
this.LogsContext.Consumer,
"Votes"
);
}
public static get Logs() {
return CreateContextFeed(
this.LogsContext.Consumer,
this.LogsContext.Consumer,
"Votes"
);
}
protected static EntitiesContext = React.createContext<Entity[] | undefined>(
undefined
);
protected static LogsContext = React.createContext<
ComponentListLogs | undefined
>(undefined);
}
class Votes extends React.Component<RequiredProps> {
render() {
const { children, from, sort, filter } = this.props;
return (
<Protocol.Config>
{(config: ProtocolConfig) => {
switch (from) {
case "DAO":
return (
<DAO.Entity>
{(dao: DAOEntity) => (
<InferredVotes
dao={dao.id}
config={config}
sort={sort}
filter={filter}
>
{children}
</InferredVotes>
)}
</DAO.Entity>
);
case "Member as voter":
return (
<Member.Entity>
{(voter: MemberEntity) => {
if (!voter.id) {
throw Error(
"Member Entity ID undefined. This should never happen."
);
}
return (
<InferredVotes
voter={voter.id}
config={config}
sort={sort}
filter={filter}
>
{children}
</InferredVotes>
);
}}
</Member.Entity>
);
case "Proposal":
return (
<Proposal.Entity>
{(proposal: ProposalEntity) => (
<InferredVotes
proposal={proposal.id}
config={config}
sort={sort}
filter={filter}
>
{children}
</InferredVotes>
)}
</Proposal.Entity>
);
default:
if (from) {
throw Error(`Unsupported scope: ${from}`);
}
return (
<InferredVotes config={config} sort={sort} filter={filter}>
{children}
</InferredVotes>
);
}
}}
</Protocol.Config>
);
}
public static get Entities() {
return InferredVotes.Entities;
}
public static get Logs() {
return InferredVotes.Logs;
}
}
export default Votes;
export { Votes, InferredVotes };