-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMember.tsx
More file actions
143 lines (122 loc) · 3.27 KB
/
Member.tsx
File metadata and controls
143 lines (122 loc) · 3.27 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
import * as React from "react";
import { Member as Entity, IMemberState as Data } from "@daostack/arc.js";
import {
Arc as Protocol,
ArcConfig as ProtocolConfig,
DAO,
DAOEntity,
Component,
ComponentLogs,
ComponentProps,
} from "../";
import { CreateContextFeed } from "../runtime/ContextFeed";
interface RequiredProps extends ComponentProps<Entity, Data> {
// Address of the member
address: string;
dao?: string | DAOEntity;
}
interface InferredProps extends RequiredProps {
config: ProtocolConfig;
}
class InferredMember extends Component<InferredProps, Entity, Data> {
protected async createEntity(): Promise<Entity> {
const { address, dao, config } = this.props;
if (!dao) {
throw Error(
"DAO Missing: Please provide this field as a prop, or use the inference component."
);
}
const daoEntity: DAOEntity =
typeof dao === "string" ? new DAOEntity(config.connection, dao) : dao;
const daoState = await daoEntity.fetchState();
const memberId: string = Entity.calculateId({
contract: daoState.reputation.id,
address,
});
return new Entity(config.connection, memberId);
}
public static get Entity() {
return CreateContextFeed(
this.EntityContext.Consumer,
this.LogsContext.Consumer,
"Member"
);
}
public static get Data() {
return CreateContextFeed(
this.DataContext.Consumer,
this.LogsContext.Consumer,
"Member"
);
}
public static get Logs() {
return CreateContextFeed(
this.LogsContext.Consumer,
this.LogsContext.Consumer,
"Member"
);
}
public static EntityContext = React.createContext<Entity | undefined>(
undefined
);
public static DataContext = React.createContext<Data | undefined>(undefined);
public static LogsContext = React.createContext<ComponentLogs | undefined>(
undefined
);
}
function useMember(): [Data | undefined, Entity | undefined] {
const data = React.useContext<Data | undefined>(InferredMember.DataContext);
const entity = React.useContext<Entity | undefined>(
InferredMember.EntityContext
);
return [data, entity];
}
class Member extends React.Component<RequiredProps> {
public render() {
const { address, dao, children } = this.props;
return (
<Protocol.Config>
{(config: ProtocolConfig) => {
if (dao) {
return (
<InferredMember address={address} dao={dao} config={config}>
{children}
</InferredMember>
);
} else {
return (
<DAO.Entity>
{(entity: DAOEntity) => (
<InferredMember
address={address}
dao={entity}
config={config}
>
{children}
</InferredMember>
)}
</DAO.Entity>
);
}
}}
</Protocol.Config>
);
}
public static get Entity() {
return InferredMember.Entity;
}
public static get Data() {
return InferredMember.Data;
}
public static get Logs() {
return InferredMember.Logs;
}
}
export default Member;
export {
Member,
InferredMember,
Entity as MemberEntity,
Data as MemberData,
useMember,
};