Skip to content

Commit fe2590f

Browse files
committed
Refactor Posts module
1 parent bc022a1 commit fe2590f

8 files changed

Lines changed: 274 additions & 234 deletions

File tree

client/app/modules/events/config/events.routes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,5 @@ angular.module('com.module.events')
8585
}
8686
}
8787
});
88-
});
88+
}
89+
);
Lines changed: 88 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,89 @@
11
'use strict';
2-
3-
var app = angular.module('com.module.posts');
4-
5-
app.config(function($stateProvider) {
6-
$stateProvider.state('app.posts', {
7-
abstract: true,
8-
url: '/posts',
9-
templateUrl: 'modules/posts/views/main.html',
10-
controller: 'PostsCtrl',
11-
controllerAs: 'ctrl'
12-
}).state('app.posts.list', {
13-
url: '',
14-
templateUrl: 'modules/posts/views/list.html',
15-
resolve: {
16-
posts: ['PostsService', function(PostsService) {
17-
return PostsService.getPosts();
18-
}]
19-
},
20-
controller: function($scope, posts) {
21-
$scope.posts = posts;
22-
}
23-
}).state('app.posts.add', {
24-
url: '/add',
25-
templateUrl: 'modules/posts/views/form.html',
26-
controller: 'PostsCtrl'
27-
}).state('app.posts.edit', {
28-
url: '/:id/edit',
29-
templateUrl: 'modules/posts/views/form.html',
30-
controller: 'PostsCtrl'
31-
}).state('app.posts.view', {
32-
url: '/:id',
33-
templateUrl: 'modules/posts/views/view.html',
34-
resolve: {
35-
post: ['$stateParams', 'PostsService', function($stateParams,
36-
PostsService) {
37-
return PostsService.getPost($stateParams.id);
38-
}]
39-
},
40-
controller: function($scope, post) {
41-
$scope.post = post;
42-
}
43-
});
44-
});
2+
angular.module('com.module.posts')
3+
.config(function ($stateProvider) {
4+
$stateProvider.state('app.posts', {
5+
abstract: true,
6+
url: '/posts',
7+
templateUrl: 'modules/posts/views/main.html'
8+
}).state('app.posts.list', {
9+
url: '',
10+
templateUrl: 'modules/posts/views/list.html',
11+
controllerAs: 'ctrl',
12+
controller: function (posts) {
13+
this.posts = posts;
14+
},
15+
resolve: {
16+
posts: ['PostsService', function (PostsService) {
17+
return PostsService.getPosts();
18+
}]
19+
}
20+
}).state('app.posts.add', {
21+
url: '/add',
22+
templateUrl: 'modules/posts/views/form.html',
23+
controllerAs: 'ctrl',
24+
controller: function ($state, PostsService, post) {
25+
this.post = post;
26+
this.formFields = PostsService.getFormFields();
27+
this.formOptions = {};
28+
this.submit = function () {
29+
PostsService.upsertPost(this.post).then(function () {
30+
$state.go('^.list');
31+
});
32+
};
33+
},
34+
resolve: {
35+
post: function () {
36+
return {};
37+
}
38+
}
39+
}).state('app.posts.edit', {
40+
url: '/:id/edit',
41+
templateUrl: 'modules/posts/views/form.html',
42+
controllerAs: 'ctrl',
43+
controller: function ($state, PostsService, post) {
44+
console.log(post);
45+
this.post = post;
46+
this.formFields = PostsService.getFormFields();
47+
this.formOptions = {};
48+
this.submit = function () {
49+
PostsService.upsertPost(this.post).then(function () {
50+
$state.go('^.list');
51+
});
52+
};
53+
},
54+
resolve: {
55+
post: function ($stateParams, PostsService) {
56+
return PostsService.getPost($stateParams.id);
57+
}
58+
}
59+
}).state('app.posts.view', {
60+
url: '/:id',
61+
templateUrl: 'modules/posts/views/view.html',
62+
controllerAs: 'ctrl',
63+
controller: function (post) {
64+
this.post = post;
65+
},
66+
resolve: {
67+
post: function ($stateParams, PostsService) {
68+
return PostsService.getPost($stateParams.id);
69+
}
70+
}
71+
}).state('app.posts.delete', {
72+
url: '/:id/delete',
73+
template: '',
74+
controllerAs: 'ctrl',
75+
controller: function ($state, PostsService, post) {
76+
PostsService.deletePost(post.id, function () {
77+
$state.go('^.list');
78+
}, function () {
79+
$state.go('^.list');
80+
});
81+
},
82+
resolve: {
83+
post: function ($stateParams, PostsService) {
84+
return PostsService.getPost($stateParams.id);
85+
}
86+
}
87+
});
88+
}
89+
);

client/app/modules/posts/controllers/posts.ctrl.js

Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 79 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,86 @@
11
'use strict';
22
var app = angular.module('com.module.posts');
33

4-
app.service('PostsService', ['CoreService', 'gettextCatalog', 'Post', function(
5-
CoreService, gettextCatalog, Post) {
4+
app.service('PostsService', ['CoreService', 'Post', 'gettextCatalog',
5+
function (CoreService, Post, gettextCatalog) {
66

7-
this.getPosts = function() {
8-
return Post.find({
9-
filter: {
10-
order: 'created DESC'
11-
}
12-
}).$promise;
13-
};
7+
this.getPosts = function () {
8+
return Post.find({
9+
filter: {
10+
order: 'created DESC'
11+
}
12+
}).$promise;
13+
};
1414

15-
this.getPost = function(id) {
16-
return Post.findById({
17-
id: id
18-
}).$promise;
19-
};
15+
this.getPost = function (id) {
16+
return Post.findById({
17+
id: id
18+
}).$promise;
19+
};
2020

21-
this.deletePost = function(id, cb) {
22-
CoreService.confirm(gettextCatalog.getString('Are you sure?'),
23-
gettextCatalog.getString('Deleting this cannot be undone'),
24-
function() {
25-
Post.deleteById(id, function() {
26-
CoreService.toastSuccess(gettextCatalog.getString(
27-
'Item deleted'), gettextCatalog.getString(
28-
'Your item has been deleted!'));
29-
cb();
30-
}, function(err) {
31-
CoreService.toastError(gettextCatalog.getString('Oops'),
32-
gettextCatalog.getString('Error deleting item: ') +
33-
err);
34-
cb();
35-
});
36-
},
37-
function() {
38-
return false;
39-
});
40-
};
21+
this.upsertPost = function (post) {
22+
return Post.upsert(post).$promise
23+
.then(function () {
24+
CoreService.toastSuccess(
25+
gettextCatalog.getString('Post saved'),
26+
gettextCatalog.getString('Your post is safe with us!')
27+
);
28+
})
29+
.catch(function (err) {
30+
CoreService.toastSuccess(
31+
gettextCatalog.getString('Error saving post '),
32+
gettextCatalog.getString('This post could no be saved: ') + err
33+
);
34+
}
35+
);
36+
};
4137

42-
}]);
38+
this.deletePost = function (id, successCb, cancelCb) {
39+
CoreService.confirm(
40+
gettextCatalog.getString('Are you sure?'),
41+
gettextCatalog.getString('Deleting this cannot be undone'),
42+
function () {
43+
Post.deleteById({id: id}, function () {
44+
CoreService.toastSuccess(
45+
gettextCatalog.getString('Post deleted'),
46+
gettextCatalog.getString('Your post is deleted!'));
47+
successCb();
48+
}, function (err) {
49+
CoreService.toastError(
50+
gettextCatalog.getString('Error deleting post'),
51+
gettextCatalog.getString('Your post is not deleted! ') + err);
52+
cancelCb();
53+
});
54+
},
55+
function () {
56+
cancelCb();
57+
}
58+
);
59+
};
60+
61+
this.getFormFields = function () {
62+
return [{
63+
key: 'title',
64+
type: 'input',
65+
templateOptions: {
66+
label: gettextCatalog.getString('Title'),
67+
required: true
68+
}
69+
}, {
70+
key: 'content',
71+
type: 'textarea',
72+
templateOptions: {
73+
label: gettextCatalog.getString('Content'),
74+
required: true
75+
}
76+
}, {
77+
key: 'image',
78+
type: 'input',
79+
templateOptions: {
80+
label: gettextCatalog.getString('Image')
81+
}
82+
}];
83+
};
84+
85+
}
86+
]);
Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
<h4>
2-
<a href="" ui-sref="^.list" class="btn btn-default"><i class="fa fa-arrow-left"></i></a>
3-
4-
<span ng-show="!post.id" translate>Add a new post</span>
5-
<span ng-show="post.id" translate>Edit post</span>
6-
7-
</h4>
8-
9-
10-
<div class="row">
11-
<div class="col-md-4">
12-
<div class="alert alert-danger" ng-if="addError" translate>Could not add post!</div>
13-
<formly-form result="post" fields="formFields" options="formOptions" ng-submit="onSubmit()"></formly-form>
1+
<form name="form">
2+
<div class="panel panel-default">
3+
<div class="panel-heading">
4+
<a href="" ui-sref="^.list" class="btn btn-xs btn-default"><i class="fa fa-arrow-left"></i></a>
5+
<span ng-show="!ctrl.post.id" translate>Add a new post</span>
6+
<span ng-show="ctrl.post.id" translate>Edit post</span>
7+
</div>
8+
<div class="panel-body">
9+
<div class="row">
10+
<div class="col-md-4">
11+
<formly-form model="ctrl.post" fields="ctrl.formFields" options="ctrl.formOptions"></formly-form>
12+
</div>
13+
</div>
14+
<div class="controls">
15+
<button type="submit" class="btn btn-primary btn-success" ng-click="ctrl.submit()" ng-disabled="form.$invalid">
16+
Submit
17+
</button>
18+
<button type="button" class="btn btn-default" ng-click="ctrl.formOptions.resetModel()">Reset</button>
19+
</div>
20+
</div>
1421
</div>
15-
</div>
16-
<div class="controls">
17-
<button class="btn btn-primary btn-success" ng-click="onSubmit()" translate>Submit</button>
18-
</div>
22+
</form>

0 commit comments

Comments
 (0)