feat: add localstorage saving

This commit is contained in:
Sergey Krylov 2025-02-10 07:52:23 +03:00
parent 47485f941d
commit 7843804682
2 changed files with 67 additions and 51 deletions

View File

@ -1,28 +1,40 @@
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import { computed, ref, watch } from 'vue';
export const useMovieStore = defineStore('movieStore', { export const useMovieStore = defineStore('movieStore', () => {
state: () => ({ const movies = ref([]);
movies: [], const activeTab = ref(1);
activeTab: 1,
}), const moviesOnLocalStorage = localStorage.getItem('movies');
getters: { if (moviesOnLocalStorage) {
watchedMovies: (store) => { movies.value = JSON.parse(moviesOnLocalStorage)
return store.movies.filter((el) => el.isWatched)
},
totalCount: (store) => {
return store.movies.length
} }
},
actions: { const watchedMovies = computed(() => movies.value.filter((el) => el.isWatched))
setActiveTab(tab) { const totalCount = computed(() => movies.value.length);
this.activeTab = tab
}, const setActiveTab = (tab) => activeTab.value = tab;
toggleWatchedMovie(id) {
const movie = this.movies.find((el) => el.id === id); const toggleWatchedMovie = (id) => {
const movie = movies.value.find((el) => el.id === id);
movie.isWatched = !movie.isWatched movie.isWatched = !movie.isWatched
}, };
removeMovie(id) {
this.movies = this.movies.filter((el) => el.id !== id) const removeMovie = (id) => {
movies.value = movies.value.filter((el) => el.id !== id)
} }
watch(movies, () => {
localStorage.setItem('movies', JSON.stringify(movies.value));
}, {deep: true})
return {
movies,
activeTab,
watchedMovies,
totalCount,
setActiveTab,
toggleWatchedMovie,
removeMovie,
} }
}) })

View File

@ -1,37 +1,41 @@
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import { useMovieStore } from './movie-store.js'; import { useMovieStore } from './movie-store.js';
import { ref } from 'vue';
const url = `https:api.themoviedb.org/3/search/movie?api_key=${import.meta.env.VITE_API_KEY}&query=`; const url = `https:api.themoviedb.org/3/search/movie?api_key=${import.meta.env.VITE_API_KEY}&query=`;
export const useSearchStore = defineStore('search-store', { export const useSearchStore = defineStore('search-store', () => {
state: () => ({ const loading = ref(false);
movies: [], const movies = ref([]);
timer: null, const movieStore = useMovieStore();
loading: false,
}), const getMovies = async (search) => {
actions: {
async getMovies(search) {
const request = async () => { const request = async () => {
const res = await fetch(url + search); const res = await fetch(url + search);
const data = await res.json(); const data = await res.json();
this.movies = data.results movies.value = data.results
} }
try { try {
clearTimeout(this.timer); loading.value = true;
this.loading = true;
await request(); await request();
} catch (e) { } catch (e) {
console.log('error', e); console.log('error', e);
} finally { } finally {
this.loading = false; loading.value = false;
} }
}, }
addToUserMovies(obj) {
const movieStore = useMovieStore(); const addToUserMovies = (obj) => {
movieStore.movies.push(obj); movieStore.movies.push(obj);
movieStore.setActiveTab(1); movieStore.setActiveTab(1);
} }
return {
getMovies,
addToUserMovies,
loading,
movies,
} }
}) })