Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added comment system #4

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions app/components/comments/comment-compose.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import Component from 'ember-component';
import get, { getProperties } from 'ember-metal/get';
import set from 'ember-metal/set';
import computed from 'ember-computed';
import service from 'ember-service/inject';
import { invokeAction } from 'ember-invoke-action';
import { next } from 'ember-runloop';
import { isEmpty } from 'ember-utils';
import $ from 'jquery';

export default Component.extend({
classNames: ['comment-compose'],
expanded: false,
store: service(),

editing: computed('text', function() {
return !isEmpty(get(this, 'text'));
}).readOnly(),

reset() {
set(this, 'text', null);
set(this, 'expanded', false);
},

actions: {
expand() {
set(this, 'expanded', true);
next(() => {
$('.comment-compose-link').focus();
});
},

collapse() {
if (!get(this, 'editing')) {
set(this, 'expanded', false);
}
},

create() {
const store = get(this, 'store');
const { text, post } = getProperties(this, 'text', 'post');
const user = get(this, 'session.account');
const comment = store.createRecord('comment', { text, user, post });
comment.save().then((createdComment) => {
invokeAction(this, 'insertComment', createdComment);
this.reset();
});
}
}
});
19 changes: 19 additions & 0 deletions app/components/comments/comment-edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Component from 'ember-component';
import set from 'ember-metal/set';

export default Component.extend({
classNames: ['comment-edit'],

actions: {
save(comment) {
comment.save().then(() => set(this, 'editing', false));
},

cancel(comment) {
// I don't think ember-change-set is needed here
comment.rollbackAttributes();
set(this, 'editing', false);
}
}

});
20 changes: 20 additions & 0 deletions app/components/comments/comment-view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Component from 'ember-component';
import get from 'ember-metal/get';
import computed from 'ember-computed';

export default Component.extend({
classNames: ['comment-view'],
editing: false,

owner: computed('session', 'comment', function() {
return get(this, 'session.account.id') == get(this, 'comment.user.id');
}).readOnly(),

actions: {
delete_comment(comment) {
// add confirmation popup?
comment.destroyRecord();
}
}

});
38 changes: 38 additions & 0 deletions app/components/comments/comments-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Component from 'ember-component';
import service from 'ember-service/inject';
import get from 'ember-metal/get';
import set from 'ember-metal/set';
import { task } from 'ember-concurrency';

export default Component.extend({
classNames: ['comment-list'],
expanded: false,
store: service(),

init() {
this._super(...arguments);
get(this, 'getComments').perform();
},

actions: {
expand() {
set(this, 'expanded', true);
},

collapse() {
set(this, 'expanded', false);
},
insertComment(comment) {
get(this, 'comments').pushObject(comment._internalModel);
}
},

getComments: task(function* () {
const comments = yield get(this, 'store').query('comment', {
filter: { postId: get(this, 'post.id') },
include: 'user',
sort: 'createdAt'
});
set(this, 'comments', comments);
})
});
19 changes: 19 additions & 0 deletions app/components/friendships/friendship-entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Component from 'ember-component';
import service from 'ember-service/inject';

export default Component.extend({
classNames: ['friendship-entry'],
store: service(),

actions: {
accept(friendship) {
friendship.set('confirmed', true);
friendship.save();
},

// TODO: save decline status instead of deleting record
remove(friendship) {
friendship.destroyRecord();
}
}
});
19 changes: 19 additions & 0 deletions app/components/posts/post-edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Component from 'ember-component';
import set from 'ember-metal/set';

export default Component.extend({
classNames: ['post-edit'],

actions: {
save(post) {
post.save().then(() => set(this, 'editing', false));
},

cancel(post) {
// I don't think ember-change-set is needed here
post.rollbackAttributes();
set(this, 'editing', false);
}
}

});
17 changes: 16 additions & 1 deletion app/components/posts/post-view.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import Component from 'ember-component';
import get from 'ember-metal/get';
import computed from 'ember-computed';

export default Component.extend({
classNames: ['post-view']
classNames: ['post-view'],
editing: false,

owner: computed('session', 'post', function() {
return get(this, 'session.account.id') == get(this, 'post.user.id');
}).readOnly(),

actions: {
delete_post(post) {
// add confirmation popup?
post.destroyRecord();
}
}

});
21 changes: 21 additions & 0 deletions app/components/users/user-entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Component from 'ember-component';
import get from 'ember-metal/get';
import service from 'ember-service/inject';

export default Component.extend({
classNames: ['user-entry'],
store: service(),

actions: {
befriend(friend) {
const user = get(this, 'session.account');
const store = get(this, 'store');
const friendship = store.createRecord('friendship', {
user: user,
friend: friend,
confirmed: false
});
friendship.save();
}
}
});
12 changes: 12 additions & 0 deletions app/models/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo } from 'ember-data/relationships';

export default Model.extend({
text: attr('string'),
rating: attr('integer'),
createdAt: attr('utc'),
updatedAt: attr('utc'),
user: belongsTo('user'),
post: belongsTo('post')
});
10 changes: 10 additions & 0 deletions app/models/friendship.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo } from 'ember-data/relationships';

export default Model.extend({
createdAt: attr('utc'),
confirmed: attr('boolean'),
user: belongsTo('user', { inverse: 'sent_friendships' }),
friend: belongsTo('user', { inverse: 'received_friendships' })
});
13 changes: 12 additions & 1 deletion app/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,16 @@ export default Model.extend(Validations, {
email: attr('string'),
password: attr('string'),
posts: hasMany('post'),
subscriptions: hasMany('subscription')
subscriptions: hasMany('subscription'),
friendships: hasMany('friendship'),
friends: hasMany('user', {
inverse: null}),
confirmed_friends: hasMany('user', {
inverse: null}),
// not in use but don't know how to remove without breaking ember
sent_friendships: hasMany('friendship', {
inverse: 'user'}),
// not in use but don't know how to remove without breaking ember
received_friendships: hasMany('friendship', {
inverse: 'friend'})
});
3 changes: 2 additions & 1 deletion app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ Router.map(function() {
this.route('new');
});

this.route('users', { path: '/users/:name' });
this.route('users', { path: '/users/:id' });

this.route('404', { path: '*' });
this.route('search');
});

export default Router;
45 changes: 45 additions & 0 deletions app/routes/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import Ember from 'ember';
import Route from 'ember-route';
import get from 'ember-metal/get';

export default Route.extend({
model() {
const users = get(this, 'store').query('user', {
filter: {
available_users_to_friend: true
}
});

const pending_friends = get(this, 'store').query('friendship', {
filter: {
confirmed_friends: false
}
});

const sent = get(this, 'store').query('friendship', {
filter: {
sent: true
}
});

const received = get(this, 'store').query('friendship', {
filter: {
received: true
}
});

const friends = get(this, 'store').query('friendship', {
filter: {
confirmed_friends: true
}
});

return Ember.RSVP.hash({
users: users,
sent: sent,
received: received,
friends: friends,
pending_friends: pending_friends
});
}
});
6 changes: 3 additions & 3 deletions app/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import get from 'ember-metal/get';

export default Route.extend({
model(params) {
const { name } = params;
const { id } = params;
return get(this, 'store')
.query('user', {
filter: { name }
filter: { id }
})
.then(records => get(records, 'firstObject'))
.then(records => get(records, 'firstObject'));
}
});
1 change: 1 addition & 0 deletions app/styles/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,6 @@ a {
@import 'channels';
@import 'dashboard';
@import 'posts';
@import 'comments';
@import 'sign-up';
@import 'ui';
Loading