onozaty / redmine-view-customize-scripts

Code examples for "Redmine View Customize Plugin"
https://github.com/onozaty/redmine-view-customize
106 stars 25 forks source link

add_watcher_when_change_tracker #145

Open ebergu78 opened 4 weeks ago

ebergu78 commented 4 weeks ago

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

onozaty commented 3 weeks ago

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 commented 3 weeks ago

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;

} }); });

onozaty commented 3 weeks ago

Hi @ebergu78

Is REST web service enabled?

ebergu78 commented 3 weeks ago

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 commented 3 weeks ago

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

Thanks.

ebergu78 commented 3 weeks ago

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 commented 2 weeks ago

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

ebergu78 commented 2 weeks ago

jqXHR.responseText -> undefined

imagen

onozaty commented 2 weeks ago

What does the alert dialog show?

ebergu78 commented 2 weeks ago

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

the alert dialog shows:

imagen

onozaty commented 2 weeks ago

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 commented 2 weeks ago

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 commented 2 weeks ago

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

ebergu78 commented 2 weeks ago

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 commented 2 weeks ago

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 commented 2 weeks ago

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