Skip to content

Commit a51d79c

Browse files
authored
Merge pull request #23 from imdhruvgupta/TagsMechanism
[FEATURE] introduced tags
2 parents 6eaab5a + 8788217 commit a51d79c

File tree

10 files changed

+101
-4
lines changed

10 files changed

+101
-4
lines changed

app/models/question.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ export default DS.Model.extend({
55
description: DS.attr(),
66
difficulty: DS.attr(),
77
user: DS.belongsTo('user'),
8-
choices: DS.hasMany('choice')
8+
choices: DS.hasMany('choice'),
9+
tags: DS.hasMany('tag')
910
})

app/models/tag.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import DS from "ember-data";
2+
3+
export default DS.Model.extend({
4+
title: DS.attr(),
5+
user: DS.belongsTo('user'),
6+
questions: DS.hasMany('question')
7+
})

app/pods/components/question-editor/component.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
import Component from '@ember/component';
22
import { inject as service } from '@ember/service';
3+
import { task, timeout } from 'ember-concurrency';
34

45
export default Component.extend({
6+
currentUser: service(),
57
store: service(),
68
api: service(),
79
notify: service(),
810
isEditing: false,
11+
tagsFilterTask: task(function * (str) {
12+
yield timeout(250)
13+
const tags = yield this.get('store').query('tag', {
14+
filter: {
15+
title: {
16+
$iLike: `%${str}%`
17+
}
18+
}
19+
})
20+
return tags.toArray()
21+
}),
922
actions: {
1023
toggleEditing () {
1124
this.toggleProperty('isEditing')
@@ -48,8 +61,18 @@ export default Component.extend({
4861
}).then(() => {
4962
this.set('correctChoices', [...correctChoices])
5063
})
51-
64+
},
65+
createNewTag() {
66+
const onSuccess = () => this.get('notify').success('New Tag Created')
67+
68+
let newTag = this.store.createRecord('tag', {
69+
title: this.get('tagTitle'),
70+
})
5271

72+
newTag.set('user', this.get('currentUser.user'))
73+
console.log(newTag.user)
74+
newTag.save()
75+
.then(onSuccess)
5376
}
5477
}
5578
});

app/pods/components/question-editor/template.hbs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,35 @@
5050
{{/each}}
5151
</ul>
5252

53+
<div class="row">
54+
<div class="col-2">
55+
<label>Tags</label>
56+
</div>
57+
<div class="col-6">
58+
{{#power-select-multiple
59+
search=(perform tagsFilterTask)
60+
selected=question.tags
61+
placeholder="Add tags for this question"
62+
onchange=(action (mut question.tags ))
63+
as |tag|}}
64+
{{tag.title}}
65+
{{/power-select-multiple}}
66+
</div>
67+
</div>
68+
<div class="row"></div>
69+
<div class="row justify-content-center align-items-center">
70+
<div class="col-2">
71+
<label>Create New Tag: </label>
72+
</div>
73+
<div class="col-5 input-group">
74+
{{input type="text" class="input-text" placeholder="Enter Tag's Title" value=tagTitle}}
75+
</div>
76+
<div class="col-5">
77+
<span {{action 'createNewTag'}}>
78+
Save
79+
<i class="far fa-save"></i>
80+
</span></div>
81+
</div>
5382
Created By: <mark>{{question.user.firstname}}</mark>
5483

5584
{{/unless}}

app/pods/questions/id/route.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default Route.extend({
77
model (params) {
88
return RSVP.hash({
99
question: this.store.findRecord('question', params.id, {
10-
include: 'user,choices'
10+
include: 'user,choices,tags'
1111
}),
1212
answers: this.api.request(`/questions/${params.id}/answers`)
1313
})

app/pods/questions/index/controller.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import Controller from '@ember/controller';
2+
import { inject as service } from '@ember/service';
23
import { task, timeout } from 'ember-concurrency';
34

45
export default Controller.extend({
6+
store: service(),
57
queryParams: ['page', 'limit'],
68
page: 1,
79
limit: 10,
@@ -10,18 +12,36 @@ export default Controller.extend({
1012
yield timeout(250)
1113

1214
let searchStr = this.get('searchString').trim()
15+
let selectedTags = []
16+
17+
if(this.get('filterTags')) {
18+
selectedTags = this.get('filterTags')
19+
selectedTags = selectedTags.map(t => +t.id)
20+
}
1321

1422
const questions = yield this.get('store').query('question', {
1523
include: 'user',
1624
filter: {
1725
title: {
1826
$iLike: `%${this.get('searchString')}%`
19-
}
27+
},
28+
tags: selectedTags
2029
}
2130
})
2231
this.set('page', 1)
2332
this.set('questions', questions)
2433
}).restartable(),
34+
tagsFilterTask: task(function * (str) {
35+
yield timeout(250)
36+
const tags = yield this.get('store').query('tag', {
37+
filter: {
38+
title: {
39+
$iLike: `%${str}%`
40+
}
41+
}
42+
})
43+
return tags.toArray()
44+
}),
2545
actions : {
2646
deleteQuestion(question) {
2747
question.destroyRecord()

app/pods/questions/index/route.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ export default Route.extend({
2121
},
2222
setupController (controller, model) {
2323
controller.set("questions", model)
24+
controller.set("tags", model.filterTags)
2425
}
2526
});

app/pods/questions/index/template.hbs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111
<img src="http://online.codingblocks.com/images/searchicon-06d8faf96de7c883b5440b6594e39eda.png" alt="search">
1212
</div>
1313
</div>
14+
<div class="row justify-content-center">
15+
<div class="col-8">
16+
{{#power-select-multiple
17+
search=(perform tagsFilterTask)
18+
selected=filterTags
19+
onchange=(action (pipe (action (mut filterTags)) (perform searchTask)))
20+
placeholder="Filter by tags"
21+
as |tag|}}
22+
{{tag.title}}
23+
{{/power-select-multiple}}
24+
</div>
25+
</div>
1426
<div class="row">
1527
<div class="col-5"></div>
1628
<div class="col-6">

ember-cli-build.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ const EmberApp = require('ember-cli/lib/broccoli/ember-app');
44

55
module.exports = function(defaults) {
66
let app = new EmberApp(defaults, {
7+
'ember-composable-helpers': {
8+
// Add conditions
9+
}
710
// Add options here
811
});
912

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"ember-cli-showdown": "^4.4.4",
3737
"ember-cli-sri": "^2.1.0",
3838
"ember-cli-uglify": "^2.0.0",
39+
"ember-composable-helpers": "^2.3.1",
3940
"ember-data": "~3.3.0",
4041
"ember-datetime-picker": "^0.2.1",
4142
"ember-decorators": "^2.2.0",

0 commit comments

Comments
 (0)