Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_watcher_when_change_tracker #145

Open
ebergu78 opened this issue Aug 19, 2024 · 16 comments
Open

add_watcher_when_change_tracker #145

ebergu78 opened this issue Aug 19, 2024 · 16 comments

Comments

@ebergu78
Copy link

Hi @onozaty :)

I need add a group as a watcher when editing a issue and when the tracker is changed
$('#issue_watcher_user_ids_15 input').prop('checked', true); -> doesn't work when editing a issue :(

the HTML structure is different ...

Can you help me?
thank you

I need ->
imagen

@ebergu78 ebergu78 changed the title change_watcher_when_change_tracker [] add_watcher_when_change_tracker Aug 19, 2024
@onozaty
Copy link
Owner

onozaty commented Aug 19, 2024

Hi @ebergu78

It does not appear to be possible to add a Watcher by editing the issue.
Adding a Watcher is a separate interface.

The REST API may be used to add the Watcher itself.

However, it is going to be complicated.

Thanks.

@ebergu78
Copy link
Author

Hi @onozaty

I have modified my code ..
shows the fail message and adds the watcher (?)
any idea to fix it?
thank you for your support

// Path pattern: /issues/
// Head of all pages

$(function() {

$('#issue-form input[type="submit"]')
.on('click', function(event) {

var trackerId = $('#issue_tracker_id').val();
var issueId = ViewCustomize.context.issue.id;

switch(trackerId) {
case '26':
case '28':

     $.ajax({
      type: 'POST',
      url: '/issues/' + issueId + '/watchers.json',
      headers: {
        'X-Redmine-API-Key': ViewCustomize.context.user.apiKey
      },
      dataType: 'text',
      contentType: 'application/json',
      data: JSON.stringify(
                 {'user_id': 27}
      )
     })
     .done(function(data) {
        location.reload();
     })
     .fail(function(data) {
        alert('Verificar Seguidores');
     });

  break;

}
});
});

@ebergu78 ebergu78 reopened this Aug 20, 2024
@onozaty
Copy link
Owner

onozaty commented Aug 20, 2024

Hi @ebergu78

Is REST web service enabled?

@ebergu78
Copy link
Author

hi @onozaty :)

The REST web service is enabled
In fact, the code adds the watcher to the issue after displaying the fail alert.

1- I edit the issue
2- I click on “submit”.
3- This code runs and shows the fail alert.
4- Successful update
5- The watcher was added to the issue

Thanks for your time

@onozaty
Copy link
Owner

onozaty commented Aug 22, 2024

I am concerned about the content of the dataType specification.
Try changing dataType: 'text', to dataType: 'json',.

Thanks.

@ebergu78
Copy link
Author

ebergu78 commented Aug 26, 2024

If I change the type, the code still shows the alert :(
(on Redmine version 5.0.2.stable)

this code works:

$(function() {
$('#issue-form').on('submit', function(event) {
var trackerId = $('#issue_tracker_id').val();
var issueId = ViewCustomize.context.issue.id;

// Ejecutar AJAX después del envío del formulario
setTimeout(function() {
  switch(trackerId) {
    case '26':
    case '28':

      $.ajax({
        type: 'POST',
        url: '/issues/' + issueId + '/watchers.json',
        headers: {
          'X-Redmine-API-Key': ViewCustomize.context.user.apiKey
        },
        dataType: 'json',
        contentType: 'application/json',
        data: JSON.stringify({
          'user_id': 27
        })
      })
      .done(function(data) {
        // Watcher agregado correctamente, recargar la página
        location.reload();  // Recargar la página para reflejar los cambios
      })
      .fail(function(jqXHR, textStatus, errorThrown) {
        // Si hay error en agregar el watcher
        alert('Agregar a "xxxx" como Seguidor: ' + textStatus + ' - ' + errorThrown);
        console.log(jqXHR.responseText);
      });

      break;
  }
}, 1);  // Tiempo de espera para asegurarse de que el formulario se haya enviado completamente

});
});

@onozaty
Copy link
Owner

onozaty commented Aug 26, 2024

What is the nature of the error?
I would like to know the information in the alert and console.log.

@ebergu78
Copy link
Author

jqXHR.responseText -> undefined

imagen

@onozaty
Copy link
Owner

onozaty commented Aug 27, 2024

What does the alert dialog show?

@ebergu78
Copy link
Author

alert('Agregar a xxxxx como Seguidor: ' + textStatus + ' -errorThrown: [' + errorThrown + '] -jqXHR: [' + jqXHR.responseText + ']');

the alert dialog shows:

imagen

@onozaty
Copy link
Owner

onozaty commented Aug 27, 2024

Thank you very much.
I tried a similar code and did not encounter any errors.

I am a little concerned about the error at the end of the console log.
Since it seems to be occurring on line 366, could you please confirm what the process is in the relevant section?

Thanks.

@ebergu78
Copy link
Author

I disabled another js code that was running
and I set the default "theme"
It still shows the alert but does not show errors or warnings in the console :(
Thank you for your concern and help

@onozaty
Copy link
Owner

onozaty commented Aug 28, 2024

Can you tell me the entire code again?
Also, can you tell me the Insert Position?

@ebergu78
Copy link
Author

ebergu78 commented Aug 28, 2024

Path pattern /issues/
Project pattern
Insertion position Head of all pages
Type JavaScript
Code


$(function() {
  $('#issue-form').on('submit', function(event) {
    var trackerId = $('#issue_tracker_id').val();
    var issueId = ViewCustomize.context.issue.id;

//    setTimeout(function() {
      switch(trackerId) {
        case '26':
        case '28':

          $.ajax({
            type: 'POST',
            url: '/issues/' + issueId + '/watchers.json',
            headers: {
              'X-Redmine-API-Key': ViewCustomize.context.user.apiKey
            },
            dataType: 'json',
            contentType: 'application/json',
            data: JSON.stringify({
              'user_id': 27
            })
          })
          .done(function(data) {
            // Watcher agregado correctamente, recargar la página
            location.reload();  // Recargar la página para reflejar los cambios
          })
          .fail(function(jqXHR, textStatus, errorThrown) {
            // Si hay error en agregar el watcher
            alert('Agregar a xxxxx como Seguidor: ' + textStatus + ' -errorThrown: [' + errorThrown + '] -jqXHR: [' + jqXHR.responseText + ']');
            console.log('LOG: ' + jqXHR.responseText);
          });
          break;
      }
//    }, 1);  //con setTimeout, funciona!!!
  });
});

@onozaty
Copy link
Owner

onozaty commented Aug 29, 2024

No errors occurred, but I was concerned that it was reloading at the same time it was submitting.
What would happen if I do the following?

$(function() {
  $('#issue-form').on('submit', async function(event) {
    var trackerId = $('#issue_tracker_id').val();
    var issueId = ViewCustomize.context.issue.id;

//    setTimeout(function() {
      switch(trackerId) {
        case '26':
        case '28':

        await new Promise((resolve, reject) => {

          $.ajax({
            type: 'POST',
            url: '/issues/' + issueId + '/watchers.json',
            headers: {
              'X-Redmine-API-Key': ViewCustomize.context.user.apiKey
            },
            dataType: 'json',
            contentType: 'application/json',
            data: JSON.stringify({
              'user_id': 15
            })
          })
          .done(function(data) {
            // Watcher agregado correctamente, recargar la página
            resolve();
          })
          .fail(function(jqXHR, textStatus, errorThrown) {
            // Si hay error en agregar el watcher
            reject('Agregar a xxxxx como Seguidor: ' + textStatus + ' -errorThrown: [' + errorThrown + '] -jqXHR: [' + jqXHR.responseText + ']');
          });
        }).catch(err => alert(err));
          break;
      }
//    }, 1);  //con setTimeout, funciona!!!
  });
});

@ebergu78
Copy link
Author

ebergu78 commented Sep 2, 2024

  1. after updating the code ...
    imagen

  2. watcher added - ok!
    ...
    I'm forced to show the alert or use the timeout
    thanks for your help
    If there is nothing else to try, you can close the issue, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants