Skip to content

Commit 41ec4e6

Browse files
author
illya
committed
Merge pull request #1085 from apinf/feature/flag-api
Feature/flag api
2 parents 1673647 + 3fa8879 commit 41ec4e6

File tree

12 files changed

+245
-3
lines changed

12 files changed

+245
-3
lines changed
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
<template name="viewApiBackendPageHeading">
22
<div class="page-heading">
3-
{{> apiBackendsUsageInstructionsButton }}
3+
<div class="btn-group pull-right" role="group" aria-label="...">
4+
{{ # if currentUser }}
5+
{{ # if isInRole 'admin' }}
6+
{{> flagApiButton apiBackend=apiBackend }}
7+
{{ / if }}
8+
{{ / if }}
9+
{{> apiBackendsUsageInstructionsButton }}
10+
</div>
411
<h1 class="page-title">
512
{{> viewApiBackendStatus apiBackend=apiBackend width="0.4" }}
613
{{ apiBackend.name }}
714
</h1>
8-
{{> apiBackendUsageInstructions apiBackend=apiBackend}}
915
</div>
1016
</template>

apis/client/api_backends/view_api_backend/usageInstructions/usageInstructionsButton.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template name="apiBackendsUsageInstructionsButton">
22
<button
3-
class="btn btn-info btn-sm pull-right"
3+
class="btn btn-info btn-sm"
44
data-toggle="collapse"
55
data-target="#usage-instructions">
66
<i class="fa fa-eye"></i>&nbsp;

apis/flags/client/autoform.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
AutoForm.addHooks('insertApiFlag', {
2+
onSuccess (formType, result) {
3+
4+
// Hide modal
5+
Modal.hide('flagApiModal');
6+
7+
// Checks formtype
8+
if (formType === 'insert') {
9+
10+
// Show message to a user
11+
sAlert.success(TAPi18n.__('flagApiModal_removeApiFlag_insertMessage'));
12+
13+
} else if (formType === 'update') {
14+
15+
// Show message to a user
16+
sAlert.success(TAPi18n.__('flagApiModal_removeApiFlag_updateMessage'));
17+
}
18+
},
19+
onError (formType, error) {
20+
21+
// Throw an error if one has been chatched
22+
return new Meteor.Error(error);
23+
}
24+
});

apis/flags/client/flag.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<template name="flagApiModal">
2+
<div class="modal fade">
3+
<div class="modal-dialog">
4+
<div class="modal-content">
5+
{{ # autoForm collection="ApiFlags" id="insertApiFlag" type=formType doc=apiFlag }}
6+
<div class="modal-header">
7+
<h4 class="modal-title">
8+
{{ _ "flagApiModal_titleText" }}
9+
</h4>
10+
</div>
11+
<div class="modal-body">
12+
{{> afQuickField name='comments'}}
13+
{{> afQuickField name='reason' options='allowed'}}
14+
</div>
15+
<div class="modal-footer">
16+
{{ # if apiIsFlagged }}
17+
<button type="button" class="btn btn-warning pull-left" id="remove-apiFlag">
18+
{{ _ "flagApiModal_removeButton" }}
19+
</button>
20+
{{ / if }}
21+
<button type="submit" class="btn btn-success">
22+
{{ _ "flagApiModal_saveButton" }}
23+
</button>
24+
<button type="button" class="btn btn-default" data-dismiss="modal">
25+
{{ _ "flagApiModal_cancelButton" }}
26+
</button>
27+
</div>
28+
{{ / autoForm }}
29+
</div>
30+
</div>
31+
</div>
32+
</template>

apis/flags/client/flag.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Template.flagApiModal.onCreated(function () {
2+
3+
// Create reference to instance
4+
const instance = this;
5+
6+
// Get apiFlag doc passed to a template
7+
instance.apiFlag = instance.data.apiFlag;
8+
});
9+
10+
Template.flagApiModal.helpers({
11+
formType () {
12+
13+
// Create reference to instance
14+
const instance = Template.instance();
15+
16+
// Check if apiFlag exists are return related form action
17+
return (instance.apiFlag) ? 'update' : 'insert';
18+
},
19+
apiFlag () {
20+
21+
// Create reference to instance
22+
const instance = Template.instance();
23+
24+
return instance.apiFlag;
25+
},
26+
apiIsFlagged () {
27+
28+
// Create reference to instance
29+
const instance = Template.instance();
30+
31+
// Check if api exists and return boolean
32+
return (instance.apiFlag) ? true : false;
33+
}
34+
});
35+
36+
Template.flagApiModal.events({
37+
'click #remove-apiFlag': function () {
38+
39+
// Create reference to instance
40+
const instance = Template.instance();
41+
42+
// Remove ApiFlag and keep response
43+
const removeApiFlag = ApiFlags.remove({ _id: instance.apiFlag._id });
44+
45+
// Check if document has been removed
46+
if (removeApiFlag > 0) {
47+
48+
// Show message to a user
49+
sAlert.success(TAPi18n.__('flagApiModal_removeApiFlag_successMessage'));
50+
51+
// Hide modal
52+
Modal.hide('flagApiModal');
53+
}
54+
}
55+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<template name="flagApiButton">
2+
<button id="openFlagApiModal" class="btn btn-warning btn-sm">
3+
{{ # if apiIsFlagged }}
4+
{{ _ "flagApiButton_buttonText_active"}}
5+
{{ else }}
6+
{{ _ "flagApiButton_buttonText" }}
7+
{{ / if }}
8+
</button>
9+
</template>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
Template.flagApiButton.onCreated(function () {
2+
3+
// Create reference to instance
4+
const instance = this;
5+
6+
// Get apibackend
7+
const apiBackendId = instance.data.apiBackend._id;
8+
9+
// Init reactive variable
10+
instance.apiFlag = new ReactiveVar();
11+
12+
// Subscription
13+
instance.subscribe('singleApiFlag', apiBackendId);
14+
15+
instance.autorun(() => {
16+
if (instance.subscriptionsReady()) {
17+
instance.apiFlag.set(ApiFlags.findOne({ apiBackendId }));
18+
}
19+
});
20+
});
21+
22+
Template.flagApiButton.events({
23+
'click #openFlagApiModal': function () {
24+
25+
// Create reference to instance
26+
const instance = Template.instance();
27+
28+
// Show modal
29+
Modal.show('flagApiModal', { apiFlag: instance.apiFlag.get() });
30+
}
31+
});
32+
33+
Template.flagApiButton.helpers({
34+
apiIsFlagged () {
35+
36+
// Create reference to instance
37+
const instance = Template.instance();
38+
39+
// Get value from reactive variable
40+
const apiFlag = instance.apiFlag.get();
41+
42+
// Check if api flag exists
43+
return (apiFlag) ? true : false;
44+
}
45+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ApiFlags = new Mongo.Collection('apiFlags');
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
ApiFlags.allow({
2+
insert: function (userId) {
3+
return Roles.userIsInRole(userId, ['admin']);
4+
},
5+
update: function (userId) {
6+
return Roles.userIsInRole(userId, ['admin']);
7+
},
8+
remove: function (userId) {
9+
return Roles.userIsInRole(userId, ['admin']);
10+
}
11+
});

apis/flags/collection/schema.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
ApiFlags.schema = new SimpleSchema({
2+
reason: {
3+
type: String,
4+
allowedValues: [
5+
TAPi18n.__('flagApiSchema_inappropriateText'),
6+
TAPi18n.__('flagApiSchema_DefunctText')
7+
]
8+
},
9+
comments: {
10+
type: String,
11+
autoform: {
12+
rows: 5
13+
}
14+
},
15+
createdBy: {
16+
type: String,
17+
autoValue: function () {
18+
19+
// Check if the field is not already set
20+
if (!this.isSet) {
21+
22+
// Autofill current user id
23+
return Meteor.userId();
24+
}
25+
}
26+
},
27+
apiBackendId: {
28+
type: String,
29+
autoValue: function () {
30+
31+
// Check if the field is not already set
32+
if (!this.isSet) {
33+
34+
// Autofill current api backend id
35+
return Router.current().params._id;
36+
}
37+
}
38+
}
39+
});
40+
41+
ApiFlags.attachSchema(ApiFlags.schema);

0 commit comments

Comments
 (0)