Skip to content

Commit 89a9cca

Browse files
authored
Merge pull request #1907 from apinf/feature/autoform-datepicker
Use bootstrap-datepicker for metadata Dates
2 parents d7dbdd4 + d84c1b0 commit 89a9cca

File tree

10 files changed

+82
-14
lines changed

10 files changed

+82
-14
lines changed

.meteor/packages

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,5 @@ shell-server@0.2.1
7474
tmeasday:publish-counts
7575
percolate:migrations
7676
michalvalasek:autoform-bootstrap-colorpicker
77+
rajit:bootstrap3-datepicker
78+
apinf:autoform-bs-datepicker

.meteor/versions

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ allow-deny@1.0.5
1616
amplify@1.0.0
1717
apinf:accounts-admin-ui@0.3.1
1818
apinf:api-umbrella@1.4.1
19+
apinf:autoform-bs-datepicker@1.1.2
1920
apinf:create-role-if-undefined@0.1.1
2021
apinf:first-admin@0.1.2
2122
autoupdate@1.3.12
@@ -119,6 +120,7 @@ promise@0.8.8
119120
raix:eventemitter@0.1.3
120121
raix:handlebar-helpers@0.2.5
121122
raix:ui-dropped-event@0.0.7
123+
rajit:bootstrap3-datepicker@1.5.1
122124
random@1.0.10
123125
rate-limit@1.0.6
124126
reactive-dict@1.1.8
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import moment from 'moment';
2+
/*
3+
Check file name for provided extensions
4+
@param {Date} date - Date object to be formatted
5+
@param {string} dateFormat - Date format, eg. "MMMM DD YYYY" (default)
6+
*/
7+
export function formatDate (date, dateFormat = 'MMMM DD YYYY') {
8+
// Return formatted date
9+
return moment(date).format(dateFormat);
10+
}

core/lib/i18n/en.i18n.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
"apiIntro_steps_welcome_intro": "Welcome",
9393
"apiIntro_skipLabel": "Skip",
9494
"apiIntro_quickTour_button_text": "Quick tour",
95+
"apiMetadata_dateInvalid": "End date must be after begin date",
9596
"apiRemoveAuthorizedUser_cancelButton_text": "No, cancel",
9697
"apiRemoveAuthorizedUser_confirmation_text": "Are you sure you want to remove this authorized user?",
9798
"apiRemoveAuthorizedUser_modalTitle": "Remove authorized user",
@@ -392,10 +393,12 @@
392393
"label": "Description"
393394
},
394395
"validSince": {
395-
"label": "Valid since"
396+
"label": "Valid since",
397+
"placeholder": "Click to select date"
396398
},
397399
"validUntil": {
398-
"label": "Valid until"
400+
"label": "Valid until",
401+
"placeholder": "Click to select date"
399402
},
400403
"serviceLevelAgreement": {
401404
"label": "Service Level Agreement"

metadata/client/edit/autoform.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import { AutoForm } from 'meteor/aldeed:autoform';
2+
import { Router } from 'meteor/iron:router';
3+
4+
import $ from 'jquery';
5+
16
AutoForm.addHooks('editApiMetadataForm', {
27
before: {
38
insert (metadata) {
@@ -18,10 +23,3 @@ AutoForm.addHooks('editApiMetadataForm', {
1823
$('#apiMetadataModal').modal('hide');
1924
},
2025
});
21-
22-
AutoForm.addHooks('updateApiMetadataForm', {
23-
onSuccess () {
24-
// Close modal dialogue
25-
$('#apiMetadataModal').modal('hide');
26-
},
27-
});

metadata/client/edit/edit.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ <h4 class="modal-title" id="editMetadataFormLabel">
2727
{{# if metadata }}
2828
{{> quickForm
2929
collection=apiMetadataCollection
30-
id="updateApiMetadataForm"
30+
id="editApiMetadataForm"
3131
type="update"
3232
doc=metadata
3333
omitFields="apiBackendId"

metadata/client/edit/edit.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Template } from 'meteor/templating';
2+
import { TAPi18n } from 'meteor/tap:i18n';
13
import { ApiMetadata } from '../../collection';
24

35
Template.editApiMetadata.helpers({

metadata/client/view/metadata.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Template } from 'meteor/templating';
2+
import { formatDate } from '/core/helper_functions/format_date';
13
import { ApiMetadata } from '../../collection';
24

35
Template.viewApiMetadata.onCreated(function () {
@@ -20,6 +22,24 @@ Template.viewApiMetadata.helpers({
2022
// TODO: migrate ApiMetadata schema to use 'apiId' instead of 'apiBackendId'
2123
const apiMetadata = ApiMetadata.findOne({ apiBackendId: apiId });
2224

25+
// Check apiMetadata is defined
26+
if (apiMetadata) {
27+
// Check service is defined
28+
if (apiMetadata.service) {
29+
const service = apiMetadata.service;
30+
// Format validSince if defined
31+
if (service.validSince) {
32+
service.validSince = formatDate(service.validSince);
33+
}
34+
// Format validUntil if defined
35+
if (service.validUntil) {
36+
service.validUntil = formatDate(service.validUntil);
37+
}
38+
// Attach formatted dates to metadata service object
39+
apiMetadata.service = service;
40+
}
41+
}
42+
2343
return apiMetadata;
2444
},
2545
});

metadata/collection/schema.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import { SimpleSchema } from 'meteor/aldeed:simple-schema';
2+
import { TAPi18n } from 'meteor/tap:i18n';
13
import { ApiMetadata } from '/metadata/collection';
24

35
ApiMetadata.schema = new SimpleSchema({
46
// TODO: migrate to use 'apiId' instead of 'apiBackendId'
5-
'apiBackendId': {
7+
apiBackendId: {
68
type: String,
79
regEx: SimpleSchema.RegEx.Id,
810
},
9-
'organization': {
11+
organization: {
1012
type: Object,
1113
optional: true,
1214
},
@@ -18,7 +20,7 @@ ApiMetadata.schema = new SimpleSchema({
1820
type: String,
1921
optional: true,
2022
},
21-
'contact': {
23+
contact: {
2224
type: Object,
2325
optional: true,
2426
},
@@ -34,7 +36,7 @@ ApiMetadata.schema = new SimpleSchema({
3436
type: String,
3537
optional: true,
3638
},
37-
'service': {
39+
service: {
3840
type: Object,
3941
optional: true,
4042
},
@@ -49,17 +51,46 @@ ApiMetadata.schema = new SimpleSchema({
4951
'service.validSince': {
5052
type: Date,
5153
optional: true,
54+
autoform: {
55+
type: 'bootstrap-datepicker',
56+
},
5257
},
5358
'service.validUntil': {
5459
type: Date,
5560
optional: true,
61+
autoform: {
62+
type: 'bootstrap-datepicker',
63+
},
64+
custom () {
65+
let validation;
66+
const validSince = this.field('service.validSince').value;
67+
const validUntil = this.value;
68+
69+
// validUntil must be after validSince
70+
if (
71+
(validSince instanceof Date) &&
72+
(validUntil instanceof Date) &&
73+
validUntil < validSince
74+
) {
75+
validation = 'dateError';
76+
}
77+
return validation;
78+
},
5679
},
5780
'service.serviceLevelAgreement': {
5881
type: String,
5982
optional: true,
6083
},
6184
});
6285

86+
// Fetch dateInvalid message
87+
const dateInvalid = TAPi18n.__('apiMetadata_dateInvalid');
88+
89+
// Define custom validation error messages
90+
ApiMetadata.schema.messages({
91+
dateError: dateInvalid,
92+
});
93+
6394
// Enable translations (i18n)
6495
ApiMetadata.schema.i18n('schemas.apiMetadata');
6596

0 commit comments

Comments
 (0)