Skip to content

Commit

Permalink
Merge pull request #974 from eciis/create-json-patch-observer-recorder
Browse files Browse the repository at this point in the history
Create json patch observer recorder
  • Loading branch information
mayzabeel authored Apr 9, 2018
2 parents 5577f38 + ab8e805 commit 38e2c2f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
8 changes: 8 additions & 0 deletions frontend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,4 +461,12 @@
$state.go("new_invite", {key: inviteKey});
});
});

app.run(function jsonPatchUnobserveInterceptor(ObserverRecorderService, $transitions) {
$transitions.onSuccess({
to: () => true
}, function () {
ObserverRecorderService.unobserveAll();
});
});
})();
21 changes: 6 additions & 15 deletions frontend/auth/configProfileController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
var app = angular.module("app");

app.controller("ConfigProfileController", function ConfigProfileController($state, InstitutionService,
CropImageService, AuthService, UserService, ImageService, $rootScope, $mdToast, $q, MessageService, $mdDialog) {
CropImageService, AuthService, UserService, ImageService, $rootScope, $mdToast, $q, MessageService, $mdDialog, ObserverRecorderService) {

var configProfileCtrl = this;

Expand All @@ -18,8 +18,6 @@
configProfileCtrl.photo_url = configProfileCtrl.newUser.photo_url;
configProfileCtrl.loadingSubmission = false;

const CAN_EDIT = ['name', 'cpf', 'photo_url', 'uploaded_images'];

var HAS_ONLY_ONE_INSTITUTION_MSG = "Esta é a única instituição ao qual você é vinculado." +
" Ao remover o vínculo você não poderá mais acessar o sistema," +
" exceto por meio de novo convite. Deseja remover?";
Expand Down Expand Up @@ -74,18 +72,11 @@
}
};

function filter_patch(patch) {
return patch.filter((operation) => {
var atomicPath = operation.path.split('/')[1];
return CAN_EDIT.includes(atomicPath);
});
}

function saveUser() {
var deffered = $q.defer();
if (configProfileCtrl.newUser.isValid()) {
updateUser();
var patch = filter_patch(jsonpatch.generate(observer));
var patch = ObserverRecorderService.generate(observer);
UserService.save(patch).then(function success() {
AuthService.save();
configProfileCtrl.loadingSubmission = false;
Expand Down Expand Up @@ -219,7 +210,7 @@
}

(function main() {
observer = jsonpatch.observe(configProfileCtrl.user);
observer = ObserverRecorderService.register(configProfileCtrl.user);

if (configProfileCtrl.user.name === 'Unknown') {
delete configProfileCtrl.user.name;
Expand All @@ -230,7 +221,7 @@


app.controller("EditProfileController", function EditProfileController(institution, ProfileService,
AuthService, $mdDialog, MessageService) {
AuthService, $mdDialog, MessageService, ObserverRecorderService) {
var editProfileCtrl = this;
editProfileCtrl.phoneRegex = "[0-9]{2}[\\s][0-9]{4,5}[-][0-9]{4,5}";
editProfileCtrl.user = AuthService.getCurrentUser();
Expand All @@ -240,7 +231,7 @@

editProfileCtrl.edit = function edit() {
if (isValidProfile()) {
var patch = jsonpatch.generate(profileObserver);
var patch = ObserverRecorderService.generate(profileObserver);
if (!_.isEmpty(patch)) {
ProfileService.editProfile(patch).then(function success() {
MessageService.showToast('Perfil editado com sucesso');
Expand Down Expand Up @@ -270,7 +261,7 @@
.filter(profile => profile.institution_key === editProfileCtrl.institution.key)
.reduce(profile => profile);
oldProfile = _.clone(editProfileCtrl.profile);
profileObserver = jsonpatch.observe(editProfileCtrl.user);
profileObserver = ObserverRecorderService.register(editProfileCtrl.user);
})();
});
})();
3 changes: 3 additions & 0 deletions frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,8 @@
<!-- Form Listener library -->
<script src="app/utils/submitFormListenerService.js"></script>
<script src="app/utils/preventStateChangeDirective.js"></script>

<!-- Jsonpatch observer register Library -->
<script src="app/utils/observerRecorderService.js"></script>
</body>
</html>
29 changes: 29 additions & 0 deletions frontend/utils/observerRecorderService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

(function() {
var app = angular.module('app');

app.service('ObserverRecorderService', function() {
let service = this;
let observers = [];

service.register = function register(object) {
const observer = jsonpatch.observe(object);
observers.push(observer);
return observer;
};

service.generate = function generate(observer) {
return jsonpatch.generate(observer);
};

service.unobserve = function unobserve(observer) {
jsonpatch.unobserve(observer);
};

service.unobserveAll = function unobserveAll() {
observers.map(observer => observer.unobserve());
observers = [];
};
});
})();

0 comments on commit 38e2c2f

Please sign in to comment.