121 lines
2.3 KiB
JavaScript

import { configureAxios, request } from "../requests"
import { addAlertOk } from "./alerts"
import { sendNotification } from "./notifications"
export function updateLastMovieFetchUrl(url) {
return {
type: "UPDATE_LAST_MOVIE_FETCH_URL",
payload: {
url: url,
},
}
}
export function selectMovie(imdbId) {
return {
type: "SELECT_MOVIE",
payload: {
imdbId,
},
}
}
export function updateFilter(filter) {
return {
type: "MOVIE_UPDATE_FILTER",
payload: {
filter,
},
}
}
export function getMovieExploreOptions() {
return request(
"MOVIE_GET_EXPLORE_OPTIONS",
configureAxios().get("/movies/explore/options")
)
}
export function getMovieDetails(imdbId) {
return request(
"MOVIE_GET_DETAILS",
configureAxios().post(`/movies/${imdbId}/refresh`),
null,
{
imdbId,
}
)
}
export function deleteMovie(imdbId, lastFetchUrl) {
return request(
"MOVIE_DELETE",
configureAxios().delete(`/movies/${imdbId}`),
[
fetchMovies(lastFetchUrl),
addAlertOk("Movie deleted"),
],
)
}
export function movieWishlistToggle(imdbId, currentState) {
if (currentState == true) {
return deleteMovieFromWishlist(imdbId)
} else {
return addMovieToWishlist(imdbId)
}
}
export function addMovieToWishlist(imdbId) {
return request(
"MOVIE_ADD_TO_WISHLIST",
configureAxios().post(`/wishlist/movies/${imdbId}`),
[
updateMovieWishlistStore(imdbId, true),
],
)
}
export function deleteMovieFromWishlist(imdbId) {
return request(
"MOVIE_DELETE_FROM_WISHLIST",
configureAxios().delete(`/wishlist/movies/${imdbId}`),
[
updateMovieWishlistStore(imdbId, false),
],
)
}
export function updateMovieWishlistStore(imdbId, wishlisted) {
return {
type: "MOVIE_UPDATE_STORE_WISHLIST",
payload: {
imdbId,
wishlisted,
}
}
}
export function fetchMovies(url) {
return request(
"MOVIE_LIST_FETCH",
configureAxios().get(url),
[
updateLastMovieFetchUrl(url),
]
)
}
export const newMovieEvent = (data) => {
return (dispatch) => {
dispatch(sendNotification({
icon: "film",
autohide: true,
delay: 10000,
title: `${data.title} added to the library`,
imageUrl: data.thumb,
}));
}
}