yukina210 / repti_track

0 stars 0 forks source link

Pet 登録_残りのタスク #24

Closed yukina210 closed 4 months ago

yukina210 commented 4 months ago
yukina210 commented 4 months ago
document.addEventListener("turbo:load", function() {
  // Avatar upload for both pet registration and edit
  var avatarPreview = document.getElementById('avatar-preview');
  var fileInput = document.getElementById('pet_avatar') || document.getElementById('profile_avatar');

  if (avatarPreview && fileInput) {
    // When the avatar preview is clicked, trigger the file input
    if (!avatarPreview.dataset.clickListenerAdded) {
      avatarPreview.addEventListener('click', function() {
        console.log("Avatar preview clicked"); // デバッグ用ログ
        fileInput.click();
      });
      avatarPreview.dataset.clickListenerAdded = true;
    }

    // グローバルスコープに関数を定義する
    window.updateAvatarPreview = function(input) {
      if (input.files && input.files[0]) {
        var reader = new FileReader();
        reader.onload = function(e) {
          console.log("Updating avatar preview"); // デバッグ用ログ
          avatarPreview.src = e.target.result;
        };
        reader.readAsDataURL(input.files[0]);
      }
    }

    // Set the onchange event to update the preview
    if (!fileInput.dataset.changeListenerAdded) {
      fileInput.addEventListener('change', function() {
        console.log("File input changed"); // デバッグ用ログ
        updateAvatarPreview(this);
      });
      fileInput.dataset.changeListenerAdded = true;
    }
  }

 // この下に Category change event コード