You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

178 lines
4.6 KiB

import { IMovie } from "@autoplex-api/interface/dist/typings";
import jwtDecode from "jwt-decode";
import { MutationTree } from "vuex";
import { IState } from "./state";
/**
* All available mutation types
*/
export enum Mutation {
LockScroll = "LOCK_SCROLL",
UserForget = "USER_FORGET",
UserLoad = "USER_LOAD",
UserStore = "USER_STORE",
FreeMovies = "FREE_MOVIES",
StoreMovies = "STORE_MOVIES",
RemoveMovieTicket = "REMOVE_MOVIE_TICKET",
StoreMovieTicket = "STORE_MOVIE_TICKET",
UpdateMovieTickets = "UPDATE_MOVIE_TICKETS"
}
/**
* Type declarations for each mutation
*/
export type MutationsTypes<S = IState> = {
[Mutation.UserForget]: (state: S) => void,
[Mutation.UserLoad] : (state: S, token: string) => boolean,
[Mutation.UserStore] : (state: S, remember: boolean) => void,
[Mutation.LockScroll]: (state: S, lock: boolean) => void,
// Movie Store
[Mutation.FreeMovies] : (state: S, movies: IMovie[]) => void,
[Mutation.StoreMovies] : (state: S, movies: IMovie[]) => void,
[Mutation.RemoveMovieTicket] : (state: S, ticketId: number) => void,
[Mutation.StoreMovieTicket] : (state: S, payload: { tmdbId: string|number, ticketId: number}) => void,
[Mutation.UpdateMovieTickets]: (state: S) => void
}
/**
* The auth mutations
*/
export const mutations: MutationsTypes & MutationTree<IState> = {
[Mutation.LockScroll](state: IState, lock: boolean) {
if (lock) {
// document.body.style.position = "fixed";
// document.body.style.top = `-${window.scrollY}px`;
document.body.style.paddingRight = `${window.innerWidth - document.body.clientWidth}px`;
document.body.style.overflow = "hidden";
} else {
// const scrollY = document.body.style.top;
// document.body.style.position = "";
// document.body.style.top = "";
document.body.style.paddingRight = "";
document.body.style.overflow = "";
// window.scrollTo(0, parseInt(scrollY || '0') * -1);
}
},
/**
* Log the user out and remove the token from storage
*/
[Mutation.UserForget](state: IState) {
state.user = null;
sessionStorage.removeItem("jwt");
localStorage.removeItem("jwt");
},
/**
* Try loading a user from the provided token
*/
[Mutation.UserLoad](state: IState, token: string) {
try {
let user: any = jwtDecode(token);
state.user = {
id : user.id,
name : user.name,
isAdmin: user.isAdmin,
token : token
};
} catch(e) {
return false;
}
return true;
},
/**
* Store the user to remember them
*/
[Mutation.UserStore](state: IState, remember: boolean) {
if (state.user == null) {
sessionStorage.removeItem("jwt");
localStorage.removeItem("jwt");
return;
}
if (remember) {
localStorage.setItem("jwt", state.user.token);
} else {
sessionStorage.setItem("jwt", state.user.token);
}
},
// Movie Store ---------------------------------------------------------------------------------
/**
* Free the cached movies from the store
*/
[Mutation.FreeMovies](state, movies: IMovie[]) {
for (let movie of movies) {
if (!(movie.tmdbId in state.movies)) {
console.warn("Attempted to free a movie not stored in the state!");
continue;
}
state.movies[movie.tmdbId].refCount--;
if (state.movies[movie.tmdbId].refCount <= 0) {
delete state.movies[movie.tmdbId];
}
}
},
/**
* Insert movie references in the store for updates
*/
[Mutation.StoreMovies](state, movies: IMovie[]) {
for (let movie of movies) {
if (!(movie.tmdbId in state.movies)) {
state.movies[movie.tmdbId] = {
refCount: 0,
movie
};
}
state.movies[movie.tmdbId].refCount++;
}
},
/**
* Remove the given movie ticket
*/
[Mutation.RemoveMovieTicket](state, ticketId) {
let tmdbId = state.movieTickets[ticketId];
if (tmdbId === undefined) {
return;
}
delete state.movieTickets[ticketId];
state.movies[tmdbId].movie.ticketId = null;
state.movies[tmdbId].movie.progress = null;
},
/**
* Store a new ticket for the given movie
*/
[Mutation.StoreMovieTicket](state, {tmdbId, ticketId}) {
let entry = state.movies[tmdbId];
if (entry === undefined) {
return;
}
entry.movie.ticketId = ticketId;
entry.movie.progress = 0;
state.movieTickets[ticketId] = <string>tmdbId;
},
/**
* Update the cached movie tickets in the store
*/
[Mutation.UpdateMovieTickets](state) {
let tickets: { [ticketId: number]: string } = {};
for (let tmdbId of Object.keys(state.movies)) {
let ticketId = state.movies[tmdbId].movie.ticketId;
let plexLink = state.movies[tmdbId].movie.plexLink;
if (ticketId !== null && plexLink === null) {
tickets[ticketId] = tmdbId;
}
}
state.movieTickets = tickets;
}
}