Skip to content

Commit 4fcddb8

Browse files
author
James Ayres
committed
following....
2 parents 53f35be + 9b1818e commit 4fcddb8

File tree

3 files changed

+64
-23
lines changed

3 files changed

+64
-23
lines changed

src/containers/App/App.js

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { BrowserRouter as Router, Route } from 'react-router-dom';
33
import sortBy from 'lodash.sortby';
44
import urlEnv from '../../utils/urlEnv';
55
import profileContents from '../../utils/profileContents';
6-
import { queryFollowers } from '../../utils/following';
6+
import { queryFollowers, followURLCheck } from '../../utils/following';
77

88
import ContentViewContainer from '../ContentViewContainer';
99
import Settings from '../Settings';
@@ -15,7 +15,7 @@ class App extends Component {
1515
this.state = {
1616
loading: true,
1717
contentSelectionOpen: false,
18-
correctBrowser: false,
18+
correctBrowser: true,
1919
isOwner: false,
2020
posts: [],
2121
theirPosts: [],
@@ -26,17 +26,16 @@ class App extends Component {
2626
}
2727

2828
async componentDidMount() {
29-
console.log('hmmm');
3029
try {
3130
const archive = await new global.DatArchive(urlEnv());
3231
const archiveInfo = await archive.getInfo();
33-
const results = await this.refreshPosts(archive);
32+
const allPosts = await this.getAllPosts(archive);
3433
const theirresults = await this.refreshTheirPosts(archive);
3534
const userData = await this.getUserInfo(archive);
3635

3736
this.setState({
3837
correctBrowser: true,
39-
posts: results,
38+
posts: allPosts,
4039
theirPosts: theirresults,
4140
loading: false,
4241
...(archiveInfo.isOwner && { isOwner: true }),
@@ -83,7 +82,32 @@ class App extends Component {
8382
}
8483
};
8584

86-
refreshPosts = async archive => {
85+
/*
86+
* This method takes an archive and
87+
* returns all of their posts.
88+
* It does not setState.
89+
*/
90+
getAllPosts = async archive => {
91+
const posts = await archive.readdir('/posts');
92+
if (posts.length === 0) {
93+
this.setState({
94+
posts: []
95+
});
96+
} else {
97+
const promises = posts.map(async post => {
98+
const postResponse = await archive.readFile(`/posts/${post}`);
99+
return JSON.parse(postResponse);
100+
});
101+
const results = await Promise.all(promises);
102+
return results;
103+
}
104+
};
105+
106+
/*
107+
* Exact same as the above function, but
108+
* also sets state.
109+
*/
110+
getAllPostsSaved = async archive => {
87111
const posts = await archive.readdir('/posts');
88112
if (posts.length === 0) {
89113
this.setState({
@@ -120,7 +144,7 @@ class App extends Component {
120144
deletePost = async postId => {
121145
const archive = await new global.DatArchive(urlEnv());
122146
await archive.unlink(`/posts/${postId}.json`);
123-
this.refreshPosts(archive);
147+
this.getAllPostsSaved(archive);
124148
};
125149

126150
deletePostSingle = async postId => {
@@ -140,9 +164,12 @@ class App extends Component {
140164
3a. return true or false message if valid dat URL
141165
---*/
142166
const followerData = {
143-
name: 'frogs',
167+
name: '',
144168
url: followerFieldVal
145169
};
170+
if (followerData.url === '') {
171+
return null;
172+
}
146173
// 4. write to following array in profile.json
147174
this.setState(
148175
{
@@ -154,18 +181,16 @@ class App extends Component {
154181
}
155182
},
156183
() => this.changeUserData(this.state.userData)
157-
// () => console.log('update', this.state.userData)
158184
);
185+
document.querySelector('#add-follower-input').value = '';
159186
};
160187

161188
updateUserData = e => {
162189
e.preventDefault();
163-
console.log('userdata', this.state.userData);
164190
this.changeUserData(this.state.userData);
165191
};
166192

167193
userDataChange = (e, str) => {
168-
console.log('frogs');
169194
this.setState({
170195
userData: {
171196
...this.state.userData,
@@ -206,7 +231,7 @@ class App extends Component {
206231
: this.state.posts
207232
)}
208233
isOwner={this.state.isOwner}
209-
getPosts={this.refreshPosts}
234+
getPosts={this.getAllPostsSaved}
210235
togglePostDisplayFn={this.togglePostDisplay}
211236
correctBrowser={this.state.correctBrowser}
212237
deletePost={this.deletePost}
@@ -224,7 +249,7 @@ class App extends Component {
224249
contentSelectionOpen={this.state.contentSelectionOpen}
225250
toggleContentSelection={this.toggleContentSelection}
226251
togglePostDisplayFn={this.togglePostDisplay}
227-
getPosts={this.refreshPosts}
252+
getPosts={this.getAllPostsSaved}
228253
isOwner={this.state.isOwner}
229254
correctBrowser={this.state.correctBrowser}
230255
deletePost={this.deletePostSingle}

src/utils/fileContents.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ const fileContents = (
55
postIdStr,
66
postType,
77
postAuthor
8-
) => `{
9-
"postId": "${postIdStr}",
10-
"titleContent": "${postTitle}",
11-
"textContent": ${postText},
12-
"imageSource": "${postImage}",
13-
"createdAt": ${Date.now()},
14-
"postType": "${postType}",
15-
"postAuthor": "${postAuthor}"
16-
}`;
8+
) =>
9+
JSON.stringify({
10+
postId: postIdStr,
11+
titleContent: postTitle,
12+
textContent: postText,
13+
imageSource: postImage,
14+
createdAt: Date.now(),
15+
postType: postType,
16+
postAuthor: postAuthor
17+
});
1718

1819
export default fileContents;

src/utils/following.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export const queryFollowers = async () => {
77
const userData = await archive.readFile(`profile.json`);
88
const followingArray = JSON.parse(userData).follows;
99
// 2. map following users
10-
console.log('followingArray', followingArray);
1110
if (followingArray.length > 0) {
1211
followingArray.map(async follow => {
1312
await queryFollowerPosts(follow);
@@ -35,3 +34,19 @@ const queryFollowerPosts = async follow => {
3534
const writePost = async (post, postId) => {
3635
await archive.writeFile(`/theirposts/${postId}.json`, post);
3736
};
37+
38+
export const followURLCheck = async value => {
39+
if (
40+
value.startsWith('dat://') &&
41+
(value.length === 70 || value.length === 71)
42+
) {
43+
return value;
44+
} else if (
45+
!value.startsWith('dat://') &&
46+
(value.length === 65 || value.length === 66)
47+
) {
48+
return 'dat://' + value;
49+
} else {
50+
return '';
51+
}
52+
};

0 commit comments

Comments
 (0)