Skip to content

Commit f404c9d

Browse files
committed
really fix new post subscription
1 parent 2235e6e commit f404c9d

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed

src/actions/profilesActions.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,33 @@ async function addressForDid(did: string): Promise<Address> {
114114
return doc.publicKey[2].ethereumAddress;
115115
}
116116

117+
118+
/**
119+
* get thread without joining
120+
* @param threadName
121+
*/
122+
export async function getThread(threadName?: string): Promise<I3BoxThread> {
123+
try {
124+
await assert3Box();
125+
console.info("getting thread");
126+
return unauthorizedBox.openThread(spaceName, threadName, {firstModerator: THREEBOXTHREADSMODERATOR, members: false});
127+
}
128+
catch (ex) {
129+
// eslint-disable-next-line no-console
130+
console.error(`error getting 3box thread: ${ex}`);
131+
return null;
132+
}
133+
}
134+
117135
/**
118-
* @param thread Optional, if supplied then gets the posts from therre
119-
* @param threadName Optional, but if no thread, then you must supply threadname
136+
* Get the thread posts
137+
* @param thread
120138
*/
121-
export async function getPosts(thread?: I3BoxThread, threadName?: string): Promise<Array<I3BoxThreadPost>> {
139+
export async function getPosts(thread?: I3BoxThread): Promise<Array<I3BoxThreadPost>> {
122140
try {
123141
await assert3Box();
124142
console.info("getting posts");
125-
let posts: Array<I3BoxThreadPost>;
126-
if (thread) {
127-
posts = (await thread.getPosts()) as Array<I3BoxThreadPost>;
128-
} else {
129-
posts = (await Box.getThread(spaceName, threadName, THREEBOXTHREADSMODERATOR, false)) as Array<I3BoxThreadPost>;
130-
}
143+
const posts = (await thread.getPosts()) as Array<I3BoxThreadPost>;
131144

132145
for (const post of posts) {
133146
post.createDate = moment.unix(post.timestamp);

src/components/Dao/DaoLandingPage.tsx

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { DiscussionEmbed } from "disqus-react";
99
import { formatFriendlyDateForLocalTimezone, showSimpleMessage, targetedNetwork, waitUntilTrue } from "lib/util";
1010
import customDaoInfo from "../../customDaoInfo";
1111
import i18next from "i18next";
12-
import { addPost, getPosts, joinThread, subcribeToThread, threeboxLogin } from "actions/profilesActions";
12+
import { addPost, getPosts, getThread, joinThread, subcribeToThread, threeboxLogin } from "actions/profilesActions";
1313
import { connect } from "react-redux";
1414
import { I3BoxThread, I3BoxThreadPost, IProfilesState } from "reducers/profilesReducer";
1515
import { IRootState } from "reducers";
@@ -34,6 +34,7 @@ interface IDispatchProps {
3434

3535
interface IExternalStateProps {
3636
profiles: IProfilesState;
37+
threeBox: any;
3738
}
3839

3940
interface IStateProps {
@@ -52,6 +53,7 @@ const mapStateToProps = (state: IRootState, ownProps: IExternalProps): IExternal
5253
return {
5354
...ownProps,
5455
profiles: state.profiles,
56+
threeBox: state.profiles.threeBox,
5557
};
5658
};
5759

@@ -74,12 +76,16 @@ class DaoLandingPage extends React.Component<IProps, IStateProps> {
7476
}
7577

7678

77-
private get3BoxThread(): Promise<I3BoxThread | null> {
78-
return joinThread(this.props.daoState.id, this.props.currentAccountAddress, this.props.profiles);
79+
private get3BoxThread(join = false): Promise<I3BoxThread | null> {
80+
if (join) {
81+
return joinThread(this.props.daoState.id, this.props.currentAccountAddress, this.props.profiles);
82+
} else {
83+
return getThread(this.props.daoState.id);
84+
}
7985
}
8086

81-
private getPosts = async (thread?: I3BoxThread) => {
82-
return (await getPosts(thread, this.props.daoState.id)).sort((a, b) =>
87+
private getPosts = async (thread: I3BoxThread) => {
88+
return (await getPosts(thread)).sort((a, b) =>
8389
{ return SortService.evaluateDateTime(a.createDate, b.createDate, SortOrder.DESC); });
8490
}
8591

@@ -90,18 +96,19 @@ class DaoLandingPage extends React.Component<IProps, IStateProps> {
9096
"DAO Name": this.props.daoState.name,
9197
});
9298

93-
const state: Partial<IStateProps> = {};
9499
const thread = await this.get3BoxThread();
95100
if (thread) {
96-
subcribeToThread(thread, this.handleNewPosts);
97-
state.joinedThread = thread;
101+
// subcribeToThread(thread, this.handleNewPosts);
102+
const posts = await this.getPosts(thread);
103+
this.setState({ threeboxPosts: posts });
98104
}
99-
const posts = await this.getPosts(thread);
100-
state.threeboxPosts = posts;
105+
}
101106

102-
this.setState(state);
107+
componentWillUnmount() {
108+
this.setState({ threeboxPosts: null, joinedThread: null, hasCommentInput: false, joiningThread: false });
103109
}
104110

111+
105112
private handleNewPosts = async () => {
106113
const posts = await this.getPosts(this.state.joinedThread);
107114
this.setState({ threeboxPosts: posts });
@@ -133,10 +140,10 @@ class DaoLandingPage extends React.Component<IProps, IStateProps> {
133140
await waitUntilTrue(() => !!this.props.currentAccountAddress && getAccountIsEnabled(), 15000);
134141
await this.props.threeboxLogin(this.props.currentAccountAddress);
135142
await waitUntilTrue(() => !!this.props.profiles.threeBoxSpace, 30000);
136-
const thread = await this.get3BoxThread();
143+
const thread = await this.get3BoxThread(true);
137144
if (thread) {
138145
subcribeToThread(thread, this.handleNewPosts);
139-
this.setState({ joinedThread: thread });
146+
this.setState({ joinedThread: thread }); // so we can see our new posts when we post them
140147
} else {
141148
showNotification(NotificationStatus.Failure, "Unable to join the conversation");
142149
}

0 commit comments

Comments
 (0)