Skip to content

Commit

Permalink
fixes on messages creation and email notification through internal ap…
Browse files Browse the repository at this point in the history
…i and handle input errors on client side (#253)
  • Loading branch information
davidbeig committed Oct 18, 2021
1 parent eee597b commit 8f7a5d1
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 57 deletions.
121 changes: 68 additions & 53 deletions public/assets/js/dashboard/ajax-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,33 +85,39 @@ $(function(){
recipients.push($(this).data('user'));
});
}
// console.log('send comment, recipients:', recipients, 'checkbox', $checkbox.prop('checked'), $checkbox);
var data = {
message: $textarea.val(),
recipients: recipients,
thread: $parent.data('thread'),
shared: $parent.data('shared'),
project: $parent.data('project'),
admin: $parent.data('admin'),
view: 'dashboard'
}
// console.log('sending comment', data);
$error.addClass('hidden').html('');
$.post(url, data, function(data) {
// console.log('ok!', data);
$list.append(data.html);
$textarea.val('');
$recipients.find('.text').html($recipients.data('public'));
}).fail(function(xhr) {
// console.log('error', xhr);
var error;
try {
error = JSON.parse(xhr.responseText).error;
} catch(e) {
error = xhr.statusText;

const messageIsValid = $textarea[0].checkValidity();
if(!messageIsValid) {
$textarea[0].reportValidity();
} else {
// console.log('send comment, recipients:', recipients, 'checkbox', $checkbox.prop('checked'), $checkbox);
var data = {
message: $textarea.val(),
recipients: recipients,
thread: $parent.data('thread'),
shared: $parent.data('shared'),
project: $parent.data('project'),
admin: $parent.data('admin'),
view: 'dashboard'
}
$error.removeClass('hidden').html(error);
});
// console.log('sending comment', data);
$error.addClass('hidden').html('');
$.post(url, data, function(data) {
// console.log('ok!', data);
$list.append(data.html);
$textarea.val('');
$recipients.find('.text').html($recipients.data('public'));
}).fail(function(xhr) {
// console.log('error', xhr);
var error;
try {
error = JSON.parse(xhr.responseText).error;
} catch(e) {
error = xhr.statusText;
}
$error.removeClass('hidden').html(error);
});
}
});

// Delete comments
Expand Down Expand Up @@ -192,33 +198,42 @@ $(function(){
var $query = $parent.find('[name="query"]');
var $users = $parent.find('[name="users"]');

var data = {
subject: $subject.val(),
body: $body.val(),
thread: $parent.data('thread'),
filter: {
others: $others.val(),
reward: $reward.val(),
query: $query.val()
},
users: $users.val().split(','),
project: $parent.data('project'),
// view: 'dashboard'
}
$error.addClass('hidden').html('');
$.post(url, data, function(response) {
// console.log('ok!', response);
// $list.append(response.html);
$(document).trigger('message-sent', [data, response]);
}).fail(function(xhr) {
// console.log('error', xhr);
var error;
try {
error = JSON.parse(xhr.responseText).error;
} catch(e) {
error = xhr.statusText;
var formIsValid = ( $body[0].checkValidity() && $subject[0].checkValidity())

if (!formIsValid) {
$body[0].reportValidity()
$subject[0].reportValidity()
} else {

var data = {
subject: $subject.val(),
body: $body.val(),
thread: $parent.data('thread'),
filter: {
others: $others.val(),
reward: $reward.val(),
query: $query.val()
},
users: $users.val().split(','),
project: $parent.data('project'),
// view: 'dashboard'
}
$error.removeClass('hidden').html(error);
});
$error.addClass('hidden').html('');
$.post(url, data, function(response) {
// console.log('ok!', response);
// $list.append(response.html);
$(document).trigger('message-sent', [data, response]);
}).fail(function(xhr) {
// console.log('error', xhr);
var error;
try {
error = JSON.parse(xhr.responseText).error;
} catch(e) {
error = xhr.statusText;
}
$error.removeClass('hidden').html(error);
}
);
}
});
});
2 changes: 1 addition & 1 deletion src/Goteo/Controller/Api/MessagesApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public function addMessageAction(Request $request) {
throw new ControllerAccessDeniedException();
}

if(!$body && !$subject) {
if(!$body || !$subject) {
throw new ModelException(Text::get('validate-donor-mandatory'));
}
if($subject) {
Expand Down
7 changes: 5 additions & 2 deletions src/Goteo/Controller/Dashboard/ProjectDashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public function indexAction(Request $request) {
]);
}

public function summaryAction($pid = null, Request $request) {
public function summaryAction($pid = null) {
$project = $this->validateProject($pid, 'summary');
if($project instanceOf Response) return $project;

Expand Down Expand Up @@ -789,15 +789,18 @@ public function supportsAction($pid = null, Request $request) {

$data = $editForm->getData();

// print_r($data);die;
if($data['delete']) {
$support = Support::get($data['delete']);
if($support->totalThreadResponses($this->user)) {
Message::error(Text::get('support-remove-error-messages'));
return $this->redirect();
}
$msg = Comment::get($support->thread);
$msg->dbDelete();

$support->dbDelete();
Message::info(Text::get('support-removed'));

return $this->redirect();
}
if($editForm->isValid()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Goteo/Model/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ public function getParticipants($with_author = true) {

$query = self::query($sql, [':id' => $this->id]);
$this->participants = $this->getRecipients();
if($resp = $query->fetchAll(\PDO::FETCH_CLASS, '\Goteo\Model\User')) {
if($resp = $query->fetchAll(\PDO::FETCH_CLASS, User::class)) {
foreach($resp as $user) {
$this->participants[$user->id] = $user;
}
Expand Down

0 comments on commit 8f7a5d1

Please sign in to comment.