Skip to content

Commit 57924a6

Browse files
authored
Merge pull request #115 from huangqun/spigit-integration
Spigit integration
2 parents fabed47 + 7218704 commit 57924a6

File tree

3 files changed

+197
-54
lines changed

3 files changed

+197
-54
lines changed

src/app/ideas/ideas-detail-dialog.html

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,9 @@ <h2>Creator Information</h2>
3535
</div>
3636
</div>
3737
<div class="modal-footer">
38-
<button type="button" class="btn btn-white" ng-click="cancel()" ng-disabed="form.isLoading">
38+
<button class="btn btn-primary" ng-click="createProject()" ng-disabled="isLoading">Create Project</button>
39+
<button type="button" class="btn btn-white" ng-click="cancel()" ng-disabed="isLoading">
3940
Close
4041
</button>
4142
</div>
42-
<div style="text-align:center;position:absolute;top:65px;left:0;width:100%;height:100%;zIndex:1;"
43-
ng-show="form.isLoading">
44-
<img src="assets/images/loading.gif"/>
45-
</div>
4643
</div>

src/app/ideas/ideas.detail.controller.js

Lines changed: 105 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,128 @@
22

33
var module = angular.module('supportAdminApp');
44

5-
module.controller('IdeasDetailController', ['$scope', '$log','IdeaService', 'Alert', '$timeout', 'OAuth2Service','$uibModalInstance', 'ideaId', 'domain', 'username', 'password', 'clientId', 'clientSecret', '$sce', 'SPIGIT_API_URL',
6-
function ($scope, $log, IdeaService, $alert, $timeout, OAuth2Service, $modalInstance, ideaId, domain, username, password, clientId, clientSecret, $sce, SPIGIT_API_URL) {
5+
module.controller('IdeasDetailController', [
6+
'$scope',
7+
'$log',
8+
'IdeaService',
9+
'Alert',
10+
'$timeout',
11+
'OAuth2Service',
12+
'$uibModalInstance',
13+
'ideaId',
14+
'domain',
15+
'username',
16+
'password',
17+
'clientId',
18+
'clientSecret',
19+
'$sce',
20+
'SPIGIT_API_URL',
21+
'ProjectService',
22+
function ($scope, $log, IdeaService, $alert, $timeout, OAuth2Service, $modalInstance, ideaId, domain, username, password, clientId, clientSecret, $sce, SPIGIT_API_URL, ProjectService) {
723

8-
$scope.ideaId = ideaId;
9-
$scope.domain = domain;
10-
$scope.username = username;
11-
$scope.password = password;
12-
$scope.clientId = clientId;
13-
$scope.clientSecret = clientSecret;
24+
$scope.ideaId = ideaId;
25+
$scope.domain = domain;
26+
$scope.username = username;
27+
$scope.password = password;
28+
$scope.clientId = clientId;
29+
$scope.clientSecret = clientSecret;
1430

15-
$scope.form = {};
16-
$scope.isLoading = false;
31+
$scope.form = {};
32+
$scope.isLoading = false;
1733

18-
$scope.load = function() {
34+
$scope.load = function () {
1935

20-
$scope.isLoading = true;
36+
$scope.isLoading = true;
2137

22-
// If token expired should get new token first
23-
if (OAuth2Service.checkTokenExpireTime()) {
24-
OAuth2Service.refreshToken($scope.username, $scope.password, $scope.domain, $scope.clientId, $scope.clientSecret).then(function (data) {
25-
$scope.loadDetail();
26-
}).catch(function (error) {
27-
$alert.error(error.message, $scope);
28-
$scope.isLoading = false;
29-
});
30-
} else {
31-
$scope.loadDetail();
38+
// If token expired should get new token first
39+
if (OAuth2Service.checkTokenExpireTime()) {
40+
OAuth2Service
41+
.refreshToken($scope.username, $scope.password, $scope.domain, $scope.clientId, $scope.clientSecret)
42+
.then(function (data) {
43+
$scope.loadDetail();
44+
})
45+
.catch(function (error) {
46+
$alert.error(error.message, $scope);
47+
$scope.isLoading = false;
48+
});
49+
} else {
50+
$scope.loadDetail();
51+
}
3252
}
33-
}
3453

35-
// get idea detail
36-
$scope.loadDetail = function() {
37-
IdeaService.getIdeaDetail(domain, $scope.ideaId).then(function (data) {
38-
$scope.form.title = data.title;
39-
$scope.form.content = $sce.trustAsHtml($scope.convertContent(data.content));
40-
// get creator detail
41-
IdeaService.getUserDetail(domain, data.creator_id).then(function(user) {
42-
$scope.form.firstName = user.first_name;
43-
$scope.form.lastName = user.last_name;
44-
$scope.form.email = user.primary_email;
45-
$scope.isLoading = false;
46-
});
47-
}).catch(function (error) {
48-
$alert.error(error.error, $scope);
49-
$scope.isLoading = false;
50-
});
51-
}
54+
// get idea detail
55+
$scope.loadDetail = function () {
56+
IdeaService
57+
.getIdeaDetail(domain, $scope.ideaId)
58+
.then(function (data) {
59+
$scope.idea = data;
60+
$scope.form.title = data.title;
61+
$scope.form.content = $sce.trustAsHtml($scope.convertContent(data.content));
62+
// get creator detail
63+
IdeaService
64+
.getUserDetail(domain, data.creator_id)
65+
.then(function (user) {
66+
$scope.user = user;
67+
$scope.form.firstName = user.first_name;
68+
$scope.form.lastName = user.last_name;
69+
$scope.form.email = user.primary_email;
70+
$scope.isLoading = false;
71+
});
72+
})
73+
.catch(function (error) {
74+
$alert.error(error.error, $scope);
75+
$scope.isLoading = false;
76+
});
77+
}
5278

5379
/**
5480
* Convert detail content without html tags
5581
* @param content
5682
* @returns {string}
5783
*/
58-
$scope.convertContent = function(content) {
59-
var newContent= content.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi,function(match,capture){
60-
var newStr='<img src="http://' + domain + "." + SPIGIT_API_URL +capture+'" />';
61-
return newStr;
62-
});
63-
return newContent;
64-
}
84+
$scope.convertContent = function (content) {
85+
var newContent = content.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, function (match, capture) {
86+
var newStr = '<img src="http://' + domain + "." + SPIGIT_API_URL + capture + '" />';
87+
return newStr;
88+
});
89+
return newContent;
90+
}
6591

66-
$scope.cancel = function() {
92+
$scope.cancel = function () {
6793
$modalInstance.close();
6894
};
6995

96+
/**
97+
* Creates a project in connect using the idea details
98+
* @param content
99+
*/
100+
$scope.createProject = function () {
101+
var project = {
102+
name: $scope.idea.title,
103+
description: $scope.idea.content
104+
};
105+
$scope.isLoading = true;
106+
ProjectService
107+
.createProject(project, $scope.user)
108+
.then(function (project) {
109+
ProjectService
110+
.addUserToProject(project.id, $scope.idea.creator_id)
111+
.then(function (data) {
112+
$alert.info("Project created.", $scope);
113+
})
114+
.catch(function (error) {
115+
$alert.error(error.error, $scope);
116+
}).finally(function(){
117+
$scope.isLoading = false;
118+
});
119+
})
120+
.catch(function (error) {
121+
$alert.error(error.error, $scope);
122+
}).finally(function(){
123+
$scope.isLoading = false;
124+
});
125+
}
70126

71-
$scope.load();
127+
$scope.load();
72128
}
73129
]);

src/app/ideas/projects.service.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
'use strict';
2+
3+
angular
4+
.module('supportAdminApp')
5+
.factory('ProjectService', [
6+
'$log',
7+
'$http',
8+
'API_URL',
9+
'helper',
10+
function ($log, $http, API_URL, helper) {
11+
// of the API including version.
12+
var basePath = API_URL + '/v4';
13+
14+
// The default headers.
15+
var defaultHeaders = {
16+
"Content-Type": "application/json"
17+
};
18+
19+
var ProjectService = {};
20+
21+
/**
22+
* Creates a new project
23+
* @param {Object} project The project
24+
* @param {Object} user The user
25+
* @returns {Object} The created project
26+
*/
27+
ProjectService.createProject = function (project, user) {
28+
project.type = 'app';
29+
project.details = {
30+
products: ["application_development"],
31+
utm: {
32+
code: "spigit"
33+
},
34+
appDefinition: {
35+
primaryTarget: "desktop",
36+
goal: {
37+
value: "TBD"
38+
},
39+
users: {
40+
value: "Description of target users."
41+
},
42+
notes: user.first_name + ' ' + user.last_name + '-' + user.primary_email
43+
},
44+
hideDiscussions: true
45+
};
46+
47+
var payload = JSON.stringify({param: project});
48+
var request = $http({
49+
method: 'POST',
50+
url: basePath + '/projects/',
51+
headers: defaultHeaders,
52+
data: payload
53+
});
54+
55+
return request.then(function (response) {
56+
$log.debug(response);
57+
return response.data.result.content;
58+
}, helper.handleError);
59+
};
60+
61+
/**
62+
* Add user to project.
63+
* @param {Integer} projectId The project id
64+
* @param {Integer} userId The user id
65+
* @returns {Object} The created project user
66+
*/
67+
ProjectService.addUserToProject = function (projectId, userId) {
68+
var payload = JSON.stringify({
69+
param: {
70+
role: 'customer',
71+
userId: userId,
72+
isPrimary: true
73+
}
74+
});
75+
var request = $http({
76+
method: 'POST',
77+
url: basePath + '/projects/' + projectId + '/members',
78+
headers: defaultHeaders,
79+
data: payload
80+
});
81+
82+
return request.then(function (response) {
83+
$log.debug(response);
84+
return response.data;
85+
}, helper.handleError);
86+
};
87+
88+
return ProjectService;
89+
}
90+
]);

0 commit comments

Comments
 (0)