0
votes

I'm trying to get a clicked button id from MovieTable.vue to WatchedForm.vue component. WatchedForm.vue component updates the data in the database based on the given id. So this Movie_id is the id obtained from the database. I've already tried using props, but I didn't get it to work. Please help! I'm losing my mind..

App.vue:

<template>
  <div class="container p-5">
    <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#movieModal">
      Add Movie
    </button>

  <movie-form @add:movie="addMovie" />
  <movie-table
      :movies="movies"
      @delete:movie="deleteMovie"
      @edit:movie="WatchedMovie"
      @edit2:movie="unWatchedMovie"
  />
  </div>
  <watched-form
      @edit:movie="watchedMovie"
      @edit2:movie="unWatchedMovie"
      />
</template>
<script>
//importing bootstrap 5
import "bootstrap/dist/css/bootstrap.min.css";
import "bootstrap/dist/js/bootstrap.min.js";
import MovieTable from '@/components/MovieTable.vue';
import MovieForm from '@/components/MovieForm.vue';
import WatchedForm from '@/components/WatchedForm.vue';

export default {
  components: {
    MovieTable,
    MovieForm,
    WatchedForm
  },
  data() {
    return {
      movies: {},
    }
  },
  mounted() {
    this.getMovies()
  },
  methods: {
    async getMovies() {
      try {
        const response = await fetch('http://localhost:8081/api/movies')
        const data = await response.json()
        this.movies = data
      } catch (error) {
        console.error(error)
      }
    },
    async addMovie(movie) {
      try {
        const response = await fetch('http://localhost:8081/api/addMovie', {
          method: 'POST',
          body: JSON.stringify(movie),
          headers: {"Content-type": "application/json; charset=UTF-8"}
        })
        const data = await response.json()
        this.movies = [...this.movies, data]
      } catch (error) {
        console.error(error)
      }
    },
    async deleteMovie(Movie_id) {
      try {
        await fetch(`http://localhost:8081/api/delete/${Movie_id}`, {
          method: 'DELETE'
        })
        this.movies = this.movies.filter(movie => movie.Movie_id !== Movie_id)
      } catch (error) {
        console.error(error)
      }
    },
    async watchedMovie(Movie_id, updatedMovie) {
      try {
        const response = await fetch(`http://localhost:8081/api/movies/watched/${Movie_id}`, {
          method: 'PUT',
          body: JSON.stringify(updatedMovie),
          headers: { "Content-type": "application/json" }
        })
        const data = await response.json()
        this.movies = this.movies.map(movie => movie.Movie_id === Movie_id ? data : movie)


      } catch (error) {
        console.error(error)
      }
    },
    async unWatchedMovie(Movie_id, updatedMovie) {
      try {
        const response = await fetch(`http://localhost:8081/api/movies/unwatched?id=${Movie_id}`, {
          method: 'PUT',
          body: JSON.stringify(updatedMovie),
          headers: { "Content-type": "application/json" }
        })
        const data = await response.json()
        this.movies = this.movies.map(movie => movie.Movie_id === Movie_id ? data : movie)
        this.movies = this.getMovies();
      } catch (error) {
        console.error(error)
      }
    }
  }
}
</script>

MovieTable.vue:

<template>
  <div id="movie-table">
    <p v-if="movies.length < 1" class="empty-table">No movies</p>
    <table v-else>
      <thead>
      <tr>
        <th>Name</th>
        <th>Genre</th>
        <th>Duration</th>
        <th>Rating</th>
        <th>Watched?</th>
        <th>Actions</th>
      </tr>
      </thead>
      <tbody>
      <tr :key="movie.Movie_id" v-for="movie in movies " :id="'form' + movie.Movie_id">
        <td>{{movie.Name}}</td>
        <td>{{movie.Genre}}</td>
        <td>{{movie.Duration}}</td>
        <td>{{movie.Rating}}</td>
        <td>
          <span v-if="movie.is_watched">Yes</span>
          <span v-else>No</span>
        </td>

        <td>
          <button type="button" class="btn btn-primary" @click="unWatchedMovie(movie)" v-if="movie.is_watched === 1">
            Unwatched
          </button>

<!-- THIS BUTTON ID I WANT TO GET -->
          <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#watchedModal" @click="watchedMovie( movie)" v-else>
            Watched
          </button> 

          <button class="btn btn-primary" @click="$emit('delete:movie', movie.Movie_id)">Delete</button>
        </td>
      </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  name: 'movie-table',
  props: {
    movies: Array
  },
  data() {
    return {
      editing: null,
    }
  },
  methods: {
    watchedMovie(movie) {
      if (movie.Name === '' || movie.Genre === '' || movie.Duration === '' || movie.is_watched === '') return
      this.$emit('edit:movie', movie.Movie_id, movie)
      this.editing = null
    },
    unWatchedMovie(movie) {
      if (movie.Name === '' || movie.Genre === '' || movie.Duration === '' || movie.is_watched === '') return
      this.$emit('edit2:movie', movie.Movie_id, movie)
      this.editing = null
    },
    handler(id) {
      console.log(id);
    }
  }
}

And WatchedForm.vue:

<template>
  <div id="watched-form">
    <div class="modal fade" id="watchedModal" tabindex="-1" aria-labelledby="watchedModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title text-danger" id="watchedModalLabel">Add new view</h5>
            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
          </div>
          <div class="modal-body">
            <form @submit.prevent="handleSubmit">
            <!-- TRYING THERE TO SHOW ID FROM MOVIETABLE -->
              <div id="watched-form1">{{movie.movie_id}}</div>
              <div class="mb-3">
                <label class="form-label">Place</label>
                <input ref="first"
                       type="text"
                       v-model="movie.Place"
                       @focus="clearStatus"
                       @keypress="clearStatus"
                       class="form-control"

                />
              </div>
              <div class="mb-3">
                <label class="form-label">Date</label>
                <input
                    type="Date"
                    v-model="movie.Date"
                    @focus="clearStatus"
                    class="form-control"
                />
              </div>
              <div class="mb-3">
                <label class="form-label">Rating 1-5</label>
                <input
                    type="number"
                    min="1"
                    max="5"
                    :class="{ 'has-error': submitting && invalidRating}" class="form-control"
                    v-model="movie.Rating"
                    @focus="clearStatus"
                />
              </div>
              <div class="mb-3">
                <label class="form-label">Comments</label>
                <textarea class="form-control" v-model="movie.Comments" @focus="clearStatus"></textarea>
              </div>
              <div class="mb-3">
                <label class="form-label">Watched?</label>
                <input
                    type="checkbox"
                    v-model="movie.is_watched.checked"
                    @focus="clearStatus"
                    checked
                    disabled
                />
              </div>

              <p v-if="error && submitting" class="error-message">❗Please fill out rating required field</p>
              <p v-if="success" class="success-message">✅ Movie successfully added</p>
              <button type="submit" class="btn btn-primary">Save</button>
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-warning" data-bs-dismiss="modal">Close</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'watched-form',
  data() {
    return {
      submitting: false,
      error: false,
      success: false,
      movie: {
        Name: '',
        Genre: '',
        Duration: '',
        Rating: '',
        is_watched: '',

      }
    }
  },
  methods: {
    handleSubmit() {
      this.clearStatus()
      this.submitting = true

      if (this.invalidRating ) {
        this.error = true
        return
      }

      this.$emit('edit:movie', this.movie)
      this.$refs.first.focus()
      this.movie = {
        Name: '',
        Genre: '',
        Duration: '',
        Rating: '',
        is_watched: ''
      }
      this.error = false
      this.success = true
      this.submitting = false
    },

    clearStatus() {
      this.success = false
      this.error = false
    },
  },
  computed: {
    invalidRating() {
      return this.movie.Rating === ''
    }
  },
}
</script>