completed reducer logic; fixed cross-posting

This commit is contained in:
Isaac Visintainer 2019-06-13 17:11:12 -07:00
parent 3c4cf7d267
commit 2dd0302210
12 changed files with 255 additions and 58 deletions

View File

@ -14,8 +14,6 @@ export class Blog extends Component {
let ship = this.props.ship;
let blog = this.retrieveColl(blogId, ship);
console.log("buildPosts", blog);
let pinProps = blog.order.pin.map((post) => {
return this.buildPostPreviewProps(post, blogId, ship, true);
});
@ -39,7 +37,8 @@ export class Blog extends Component {
numComments: com.length,
collectionTitle: col.title,
collectionName: coll,
author: who,
author: pos.info.creator.slice(1),
blogOwner: who,
date: pos.info["date-created"],
pinned: pinned,
}

View File

@ -22,7 +22,6 @@ export class NewBlog extends Component {
constructor(props){
super(props);
console.log("new blog", this.props);
this.state = {
title: '',
collaborators: [],
@ -85,16 +84,12 @@ export class NewBlog extends Component {
awaiting: blogId
});
console.log("blogSubmit", data);
this.props.api.action("write", "write-action", data);
}
componentDidUpdate(prevProps, prevState) {
console.log("componentDidUpdate", this.state.awaiting, this.props.pubs);
if (this.state.awaiting) {
console.log("awaiting", this.state.awaiting);
if (this.props.pubs[this.state.awaiting]) {
console.log("transitioning");
this.props.history.push(`/~publish/~${window.ship}/${this.state.awaiting}`);
}
}
@ -113,12 +108,10 @@ export class NewBlog extends Component {
}
firstPost() {
console.log("action first post");
this.blogSubmit();
}
addCollaborators() {
console.log("action add collaborators");
this.setState({page: 'addCollab'});
}

View File

@ -1,6 +1,7 @@
import React, { Component } from 'react';
import classnames from 'classnames';
import { Link } from 'react-router-dom';
import _ from 'lodash';
class SideTab extends Component {
constructor(props) {
@ -29,12 +30,12 @@ export class NewPost extends Component {
constructor(props){
super(props);
console.log("new post", this.props);
this.state = {
title: "",
body: "",
awaiting: false,
};
this.titleChange = this.titleChange.bind(this);
this.bodyChange = this.bodyChange.bind(this);
this.postSubmit = this.postSubmit.bind(this);
@ -92,6 +93,7 @@ export class NewPost extends Component {
let data = {
"new-post" : {
who: ship,
coll: blogId,
name: postId,
title: postTitle,
@ -101,10 +103,43 @@ export class NewPost extends Component {
},
};
console.log("postSubmit", data);
this.props.api.action("write", "write-action", data).then((foo) => {
console.log("foo", foo);
this.setState({
awaiting: {
ship: ship,
blogId: blogId,
postId: postId,
}
});
this.props.api.action("write", "write-action", data);
}
componentDidUpdate(prevProps, prevState) {
if (this.state.awaiting) {
let ship = this.state.awaiting.ship;
let blogId = this.state.awaiting.blogId;
let postId = this.state.awaiting.postId;
if (this.state.awaiting.ship == window.ship) {
let post = _.get(this.props, `pubs[${blogId}].posts[${postId}].post`, false);
let comments = _.get(this.props, `pubs[${blogId}].posts[${postId}].comments`, false);
if (post && comments) {
let redirect = `/~publish/~${ship}/${blogId}/${postId}`;
this.props.history.push(redirect);
}
} else {
let post = _.get(this.props, `subs[${ship}][${blogId}].posts[${postId}].post`, false);
let comments = _.get(this.props, `subs[${ship}][${blogId}].posts[${postId}].comments`, false);
if (post && comments) {
let redirect = `/~publish/~${ship}/${blogId}/${postId}`;
this.props.history.push(redirect);
}
}
}
}
render() {

View File

@ -40,7 +40,7 @@ export class PostPreview extends Component {
let date = moment(this.props.post.date).fromNow();
let authorDate = `~${this.props.post.author}${date}`
let collLink = "/~publish/~" +
this.props.post.author + "/" +
this.props.post.blogOwner + "/" +
this.props.post.collectionName;
let postLink = collLink + "/" + this.props.post.postName;

View File

@ -38,8 +38,6 @@ export class Post extends Component {
let ship = this.props.ship;
let blog = this.retrieveColl(blogId, ship);
console.log("buildposts", blog);
let pinProps = blog.order.pin.map((post) => {
return this.buildPostPreviewProps(post, blogId, ship, true);
});

View File

@ -40,7 +40,7 @@ export class RecentPreview extends Component {
let date = moment(this.props.post.date).fromNow();
let authorDate = `~${this.props.post.author}${date}`
let collLink = "/~publish/~" +
this.props.post.author + "/" +
this.props.post.blogOwner + "/" +
this.props.post.collectionName;
let postLink = collLink + "/" + this.props.post.postName;

View File

@ -26,25 +26,18 @@ export class Recent extends Component {
date: this.roundDay(postDate),
posts: [postProps],
}
if (i == (this.props.latest.length - 1)) {
recent.push(Object.assign({}, group));
}
} else if ( this.sameDay(group.date, postDate) ) {
group.posts.push(postProps) ;
} else {
recent.push(Object.assign({}, group));
group = {
date: this.roundDay(postDate),
posts: [postProps],
}
}
if (i == (this.props.latest.length - 1)) {
recent.push(Object.assign({}, group));
}
if (i == (this.props.latest.length - 1)) {
recent.push(Object.assign({}, group));
}
}
return recent;
@ -62,7 +55,8 @@ export class Recent extends Component {
numComments: com.length,
collectionTitle: col.title,
collectionName: coll,
author: who,
author: pos.info.creator.slice(1),
blogOwner: who,
date: pos.info["date-created"]
}

View File

@ -28,8 +28,6 @@ export class Subs extends Component {
render() {
let blogData = this.buildBlogData();
console.log("blogData", blogData);
let blogs = this.buildBlogData().map( (data, i) => {
let bg = (i % 2 == 0)
? "bg-v-light-gray"

View File

@ -1,17 +1,191 @@
export class UpdateReducer {
reduce(json, state){
console.log("update-reducer", json, state);
if (json.collection){
if (json.collection.who === window.ship) {
state.pubs[json.collection.coll] = {
info: json.collection.data,
order: { pin: [], unpin: [] },
posts: {},
}
} else {
if (json.collection) {
this.reduceCollection(json, state);
}
if (json.post) {
this.reducePost(json, state);
}
if (json.comments) {
this.reduceComments(json, state);
}
if (json.total) {
this.reduceTotal(json, state);
}
}
reduceCollection(json, state) {
if (json.collection.who === window.ship) {
state.pubs[json.collection.coll] = {
info: json.collection.data,
order: { pin: [], unpin: [] },
posts: {},
}
} else {
state.subs[json.collection.who][json.collection.coll] = {
info: json.collection.data,
order: { pin: [], unpin: [] },
posts: {},
}
}
}
reducePost(json, state) {
let who = json.post.who;
let coll = json.post.coll;
let post = json.post.post;
let data = json.post.data;
if (who === window.ship) {
if (state.pubs[coll].posts[post]) {
state.pubs[coll].posts[post].post = data;
} else {
state.pubs[coll].posts[post] = {
post: data,
comments: [],
};
}
} else {
if (state.subs[who][coll].posts[post]) {
state.subs[who][coll].posts[post].post = data;
} else {
state.subs[who][coll].posts[post] = {
post: data,
comments: [],
};
}
}
this.insertPost(json, state);
}
insertPost(json, state) {
this.insertLatest(json, state);
this.insertUnread(json, state);
this.insertOrder(json, state);
}
insertLatest(json, state) {
let newIndex = {
post: json.post.post,
coll: json.post.coll,
who: json.post.who,
}
let newDate = json.post.data.info["date-created"];
for (var i=0; i<state.latest.length; i++) {
let postId = state.latest[i].post;
let blogId = state.latest[i].coll;
let ship = state.latest[i].who;
if (newIndex.post == postId && newIndex.coll == blogId && newIndex.who == ship) {
break;
}
let idate = this.retrievePost(state, blogId, postId, ship).info["date-created"];
if (newDate >= idate) {
state.latest.splice(i, 0, newIndex);
break;
} else if (i == (state.latest.length - 1)) {
state.latest.push(newIndex);
break;
}
}
}
insertUnread(json, state) {
if (json.post.who != window.ship) {
state.unread.push({
post: json.post.post,
coll: json.post.coll,
who: json.post.who,
});
}
}
insertOrder(json, state) {
let blogId = json.post.coll;
let ship = json.post.who;
let blog = this.retrieveColl(state, blogId, ship);
let list = json.post.data.info.pinned
? blog.order.pin
: blog.order.unpin;
let newDate = json.post.data.info["date-created"];
for (var i=0; i<list.length; i++) {
let postId = list[i];
if (json.post.post === postId) {
break;
}
let idate = this.retrievePost(state, blogId, postId, ship).info["date-created"];
if (newDate >= idate) {
list.splice(i, 0, json.post.post);
break;
} else if (i == (state.latest.length - 1)) {
list.push(json.post.post);
break;
}
}
if (window.ship == ship) {
state.pubs[blogId].order = json.post.data.info.pinned
? {pin: list, unpin: blog.order.unpin}
: {pin: blog.order.pin, unpin: list};
} else {
state.subs[ship][blogId].order = json.post.data.info.pinned
? {pin: list, unpin: blog.order.unpin}
: {pin: blog.order.pin, unpin: list};
}
}
retrieveColl(state, coll, who) {
if (who === window.ship) {
return state.pubs[coll];
} else {
return state.subs[who][coll];
}
}
retrievePost(state, coll, post, who) {
if (who === window.ship) {
return state.pubs[coll].posts[post].post;
} else {
return state.subs[who][coll].posts[post].post;
}
}
reduceComments(json, state) {
let who = json.comments.who;
let coll = json.comments.coll;
let post = json.comments.post;
let data = json.comments.data;
if (who === window.ship) {
if (state.pubs[coll].posts[post]) {
state.pubs[coll].posts[post].comments = data;
} else {
state.pubs[coll].posts[post] = {
post: null,
comments: data,
};
}
} else {
if (state.subs[who][coll].posts[post]) {
state.subs[who][coll].posts[post].comments = data;
} else {
state.subs[who][coll].posts[post] = {
post: null,
comments: data,
};
}
}
}
reduceTotal(json, state) {
console.log("reduce total", json); // XX TODO
}
}

View File

@ -270,10 +270,11 @@
(da-emil (affection del))
::
%total
~& da-change+del
=? pubs.sat =(our.bol who.del)
(~(put by pubs.sat) col.del dat.del)
=? subs.sat !=(our.bol who.del)
(~(put by subs.sat) [who.del col.del] dat.del)
(~(put by subs.sat) [who.del col.del] dat.del(order [~ ~]))
::
=/ posts=(list @tas) ~(tap in ~(key by pos.dat.del))
=. da-this
@ -290,6 +291,7 @@
::
++ da-insert
|= [who=@p coll=@tas post=@tas]
~& da-insert+[coll post]
^+ da-this
:: assume we've read our own posts
::
@ -305,6 +307,8 @@
|-
?~ suf
(weld pre [who coll post]~)
?: =([who coll post] i.suf)
latest.sat
=/ i-date=@da date-created:(need (get-post-by-index i.suf))
?: (gte new-date i-date)
(weld pre [[who coll post] suf])
@ -327,6 +331,8 @@
|-
?~ suf
(snoc pre post)
?: =(post i.suf)
(weld pre suf)
=/ i-date=@da date-created:(need (get-post-by-index who coll i.suf))
?: (gte date-created.new-post i-date)
(weld pre [post suf])
@ -340,6 +346,8 @@
[new-list unpin.order.col]
[pin.order.col new-list]
::
~& order+order.col
::
=? pubs.sat =(our.bol who)
(~(put by pubs.sat) coll col)
=? subs.sat !=(our.bol who)
@ -402,7 +410,7 @@
^- (unit collection)
?: =(our.bol who)
(~(get by pubs.sat) coll)
(~(get by subs.sat) coll)
(~(get by subs.sat) who coll)
::
++ made
|= [wir=wire wen=@da mad=made-result:ford]
@ -623,6 +631,9 @@
==
::
%new-post
?. =(who.act our.bol)
:_ this
[ost.bol %poke /forward [who.act %write] %write-action act]~
:: XX check permissions of src.bol
:: XX check if file already exists
:: XX check if coll doesn't exist
@ -665,6 +676,9 @@
==
::
%new-comment
?. =(who.act our.bol)
:_ this
[ost.bol %poke /forward [who.act %write] %write-action act]~
:: XX check permissions of src.bol
:: XX check if file already exists
=. content.act (cat 3 content.act '\0a') :: XX fix udon parser
@ -803,6 +817,7 @@
:: %subscribe:
::
%subscribe
~& write-action+act
=/ wir=wire /collection/[coll.act]
:_ this
[ost.bol %peer wir [who.act %write] wir]~

View File

@ -53,7 +53,8 @@
::
++ new-post
%- ot:dejs
:~ coll+(su:dejs sym)
:~ who+(su:dejs fed:ag)
coll+(su:dejs sym)
name+(su:dejs sym)
title+so:dejs
comments+comment-config
@ -63,7 +64,8 @@
::
++ new-comment
%- ot:dejs
:~ coll+(su:dejs sym)
:~ who+(su:dejs fed:ag)
coll+(su:dejs sym)
name+(su:dejs sym)
content+so:dejs
==
@ -173,15 +175,3 @@
--
--
--

View File

@ -15,6 +15,7 @@
==
::
$: %new-post
who=@p
coll=@tas
name=@tas
title=@t
@ -23,7 +24,7 @@
content=@t
==
::
[%new-comment coll=@tas post=@tas content=@t]
[%new-comment who=@p coll=@tas post=@tas content=@t]
::
[%delete item-id]
::