DEV/DEV4.1/tp4/ex1/js/controller.js
2024-03-01 15:39:57 +01:00

46 lines
1.1 KiB
JavaScript

class Controller {
constructor(view,model){
this.view = view
this.model = model
this.loading = false
this.lastSearch = null
this.error = null
this.results = []
this.view.setLoading(false)
this.view.bindSearch(this.search.bind(this))
}
reset() {
this.loading = false
this.error = null
this.results = []
}
async search(searchMovie) {
try {
this.reset();
this.loading = true;
this.view.setLoading(true);
const movies = await this.model.getMovies(searchMovie);
if (movies && movies.length > 0) {
this.results = movies;
} else {
this.error = "No movies found.";
}
} catch (error) {
console.error('Error occurred during search:', error);
this.error = "An error occurred during search.";
} finally {
this.loading = false;
this.view.setLoading(false);
this.view.renderResults(this.results, this.error);
}
}
}
export default Controller