Skip to content

Commit

Permalink
Merge pull request #23 from kadirahq/delete-method
Browse files Browse the repository at this point in the history
Add a deleteComment method on datastore
  • Loading branch information
Muhammed Thanish authored Oct 12, 2016
2 parents 54a2678 + e565c40 commit 34a370d
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 84 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"devDependencies": {
"@kadira/storybook": "^2.24.0",
"@kadira/storybook-addons": "^1.5.0",
"@kadira/storybook-database-cloud": "^2.1.2",
"@kadira/storybook-database-cloud": "^2.3.0",
"@kadira/storybook-deployer": "^1.2.0",
"@kadira/storybook-ui": "^3.4.1",
"babel-cli": "^6.14.0",
Expand Down
69 changes: 69 additions & 0 deletions src/manager/components/CommentItem/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import React, { Component } from 'react';
import moment from 'moment';
import renderHTML from 'react-render-html';
import insertCss from 'insert-css';
import style from './style';
import commentCSS from './style.css';

insertCss(commentCSS);

export default class CommentItem extends Component {
constructor(props, ...args) {
super(props, ...args);
this.state = { hovered: false };
this.mouseEnter = this.mouseEnter.bind(this);
this.mouseLeave = this.mouseLeave.bind(this);
this.deleteComment = this.deleteComment.bind(this);
}

mouseEnter() {
this.setState({ hovered: true });
}

mouseLeave() {
this.setState({ hovered: false });
}

deleteComment() {
this.props.deleteComment();
}

renderDelete() {
return (
<a
href="#"
style={style.commentDelete}
onClick={this.props.deleteComment}>
delete
</a>
);
}

render() {
const comment = this.props.comment;
let commentStyle = style.commentItem;
if (comment.loading) {
commentStyle = style.commentItemloading;
}

const time = moment(new Date(comment.time), "YYYYMMDD").fromNow();
const body = renderHTML(`<span>${comment.text}</span>`);
const showDelete = this.state.hovered && this.props.ownComment;

return (
<div style={commentStyle} onMouseEnter={this.mouseEnter} onMouseLeave={this.mouseLeave}>
<div style={style.commentAside}>
<img style={style.commentAvatar} src={comment.user.avatar} />
</div>
<div className="comment-content" style={style.commentContent}>
<div style={style.commentHead}>
<span style={style.commentUser}>{comment.user.name}</span>
<span style={style.commentTime}>{time}</span>
</div>
<span style={style.commentText}>{body}</span>
{showDelete ? this.renderDelete() : null}
</div>
</div>
);
}
}
58 changes: 58 additions & 0 deletions src/manager/components/CommentItem/style.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const commentItem = {
display: 'flex',
paddingBottom: '5px',
WebkitFontSmoothing: 'antialiased',
transition: 'opacity 0.5s',
}

export default {
commentItem: {
...commentItem,
},
commentItemloading: {
...commentItem,
opacity: 0.25,
},
commentAside: {
margin: '5px 10px 0 0',
},
commentAvatar: {
width: 32,
height: 32,
borderRadius: 5,
},
commentContent: {
position: 'relative',
flex: 1,
},
commentHead: {
//
},
commentUser: {
fontFamily: 'sans-serif',
fontSize: 13,
lineHeight: 1,
fontWeight: 'bold',
marginRight: 5,
},
commentTime: {
fontFamily: 'sans-serif',
fontSize: 11,
lineHeight: 1,
color: 'rgb(150, 150, 150)',
},
commentText: {
fontFamily: 'sans-serif',
fontSize: 13,
lineHeight: 1.7,
maxWidth: 650,
},
commentDelete: {
fontFamily: 'sans-serif',
position: 'absolute',
top: 2,
right: 0,
fontSize: 11,
color: 'rgb(200, 200, 200)',
},
}
44 changes: 9 additions & 35 deletions src/manager/components/CommentList/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import React, { Component } from 'react';
import moment from 'moment';
import renderHTML from 'react-render-html';
import insertCss from 'insert-css';
import style from './style';
import commentContentCSS from './comment_content_styles.css';

insertCss(commentContentCSS);
import CommentItem from '../CommentItem';

export default class CommentList extends Component {
componentDidMount() {
Expand All @@ -20,34 +15,6 @@ export default class CommentList extends Component {
}
}

formatTime(ts) {
return moment(new Date(ts), "YYYYMMDD").fromNow();
}

renderComment(comment, key) {
if (!comment.user) return null;

let commentStyle = style.commentItem;
if (comment.loading) {
commentStyle = style.commentItemloading;
}

return (
<div style={commentStyle} key={key}>
<div style={style.commentAside}>
<img style={style.commentAvatar} src={comment.user.avatar} />
</div>
<div className="comment-content" style={style.commentContent}>
<div style={style.commentHead}>
<span style={style.commentUser}>{comment.user.name}</span>
<span style={style.commentTime}>{this.formatTime(comment.time)}</span>
</div>
<span style={style.commentText}>{ renderHTML(`<span>${comment.text}</span>`) }</span>
</div>
</div>
);
}

render() {
const { comments } = this.props;

Expand All @@ -61,7 +28,14 @@ export default class CommentList extends Component {

return (
<div ref="wrapper" style={style.wrapper}>
{comments.map((c, idx) => this.renderComment(c, idx))}
{comments.map((comment, idx) => (
<CommentItem
key={`comment_${idx}`}
comment={comment}
ownComment={comment.userId === this.props.user.id}
deleteComment={() => this.props.deleteComment(comment.id)}
/>
))}
</div>
);
}
Expand Down
48 changes: 0 additions & 48 deletions src/manager/components/CommentList/style.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,9 @@
const commentItem = {
display: 'flex',
paddingBottom: '5px',
WebkitFontSmoothing: 'antialiased',
transition: 'opacity 0.5s',
maxWidth: 700,
}

export default {
wrapper: {
flex: 1,
overflow: 'auto',
padding: '7px 15px',
},
commentItem: {
...commentItem,
},
commentItemloading: {
...commentItem,
opacity: 0.25,
},
commentAside: {
margin: '5px 10px 0 0',
},
commentAvatar: {
width: 32,
height: 32,
borderRadius: 5,
},
commentContent: {
flex: 1,
},
commentHead: {
//
},
commentUser: {
fontFamily: 'sans-serif',
fontSize: 13,
lineHeight: 1,
fontWeight: 'bold',
marginRight: 5,
},
commentTime: {
fontFamily: 'sans-serif',
fontSize: 11,
lineHeight: 1,
color: 'rgb(150, 150, 150)',
},
commentText: {
fontFamily: 'sans-serif',
fontSize: 13,
lineHeight: 1.7,
maxWidth: 650,
},
noComments: {
fontFamily: 'sans-serif',
fontSize: 13,
Expand Down
24 changes: 24 additions & 0 deletions src/manager/containers/CommentsPanel/dataStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,17 @@ export default class DataStore {
return Promise.resolve(null);
}

_setDeletedComment(commentId) {
const storyKey = this._getStoryKey(this.currentStory);
const comments = this.cache[storyKey];
const deleted = comments.find(c => c.id === commentId);
if (deleted) {
deleted.loading = true;
}
this._fireComments(comments);
return Promise.resolve(null);
}

_addAuthorToTheDatabase() {
if (this.users[this.user.id]) {
// user exists in the DB.
Expand All @@ -150,6 +161,8 @@ export default class DataStore {
return this.db.getCollection('users').set(this.user);
}

// NOTE the "sbProtected" makes sure only the author can modify
// or delete a comment after its saved on the cloud database.
_addCommentToDatabase(comment) {
const doc = {
...comment,
Expand All @@ -159,11 +172,22 @@ export default class DataStore {
return this.db.getCollection('comments').set(doc);
}

_deleteCommentOnDatabase(commentId) {
const query = { id: commentId };
return this.db.getCollection('comments').del(query);
}

addComment(comment) {
this._addPendingComment(comment)
.then(() => this._addAuthorToTheDatabase())
.then(() => this._addCommentToDatabase(comment))
.then(() => this._loadUsers())
.then(() => this._loadComments())
}

deleteComment(commentId) {
this._setDeletedComment(commentId)
.then(() => this._deleteCommentOnDatabase(commentId))
.then(() => this._loadComments())
}
}
5 changes: 5 additions & 0 deletions src/manager/containers/CommentsPanel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,16 @@ export default class Container extends Component {
this.store.addComment(comment);
}

deleteComment(commentId) {
this.store.deleteComment(commentId);
}

render() {
const props = {
user: this.state.user,
comments: this.state.comments,
loading: this.state.loading,
deleteComment: commentId => this.deleteComment(commentId),
addComment: text => this.addComment(text),
};

Expand Down

0 comments on commit 34a370d

Please sign in to comment.