Skip to content

Commit

Permalink
Creates one session
Browse files Browse the repository at this point in the history
Creates and stores session id in local storage allowing for swifter access to data.

Also added the objects to another file as was taking a while on the network to fetch the same data each time.

Where to watch link has superficial fix but needs more work

Added some error handling, again probs needs more work too.

Closes #55 Relates #43 Closes #52
  • Loading branch information
Glombort committed Feb 22, 2022
1 parent 38921da commit c7b2546
Show file tree
Hide file tree
Showing 3 changed files with 257 additions and 31 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ <h1>Film Picker</h1>
<overlay class="overlay grid"></overlay>
</main>
<footer class=""><img src="https://www.themoviedb.org/assets/2/v4/logos/v2/blue_square_1-5bdc75aaebeb75dc7ae79426ddd9be3b2be1e342510f8202baf6bffa71d7f5c4.svg" alt="TMDB Logo for attribution"><p>"This product uses the <a href="https://developers.themoviedb.org/3/getting-started/introduction">TMDB API</a> but is not endorsed or certified by <a href="https://www.themoviedb.org/">TMDB</a>."</p></footer>
<script src=".\js\objects.js"></script>
<script src=".\js\script.js"></script>
</body>
</html>
211 changes: 211 additions & 0 deletions js/objects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
const genreObj = {
12: "Adventure",
14: "Fantasy",
16: "Animation",
18: "Drama",
27: "Horror",
28: "Action",
35: "Comedy",
36: "History",
37: "Western",
53: "Thriller",
80: "Crime",
99: "Documentary",
878: "Science Fiction",
9648: "Mystery",
10402: "Music",
10749: "Romance",
10751: "Family",
10752: "War",
10770: "TV Movie",
}

const languageObj = {
aa: "Afar",
ab: "Abkhazian",
ae: "Avestan",
af: "Afrikaans",
ak: "Akan",
am: "Amharic",
an: "Aragonese",
ar: "Arabic",
as: "Assamese",
av: "Avaric",
ay: "Aymara",
az: "Azerbaijani",
ba: "Bashkir",
be: "Belarusian",
bg: "Bulgarian",
bi: "Bislama",
bm: "Bambara",
bn: "Bengali",
bo: "Tibetan",
br: "Breton",
bs: "Bosnian",
ca: "Catalan",
ce: "Chechen",
ch: "Chamorro",
cn: "Cantonese",
co: "Corsican",
cr: "Cree",
cs: "Czech",
cu: "Slavic",
cv: "Chuvash",
cy: "Welsh",
da: "Danish",
de: "German",
dv: "Divehi",
dz: "Dzongkha",
ee: "Ewe",
el: "Greek",
en: "English",
eo: "Esperanto",
es: "Spanish",
et: "Estonian",
eu: "Basque",
fa: "Persian",
ff: "Fulah",
fi: "Finnish",
fj: "Fijian",
fo: "Faroese",
fr: "French",
fy: "Frisian",
ga: "Irish",
gd: "Gaelic",
gl: "Galician",
gn: "Guarani",
gu: "Gujarati",
gv: "Manx",
ha: "Hausa",
he: "Hebrew",
hi: "Hindi",
ho: "Hiri Motu",
hr: "Croatian",
ht: "Haitian; Haitian Creole",
hu: "Hungarian",
hy: "Armenian",
hz: "Herero",
ia: "Interlingua",
id: "Indonesian",
ie: "Interlingue",
ig: "Igbo",
ii: "Yi",
ik: "Inupiaq",
io: "Ido",
is: "Icelandic",
it: "Italian",
iu: "Inuktitut",
ja: "Japanese",
jv: "Javanese",
ka: "Georgian",
kg: "Kongo",
ki: "Kikuyu",
kj: "Kuanyama",
kk: "Kazakh",
kl: "Kalaallisut",
km: "Khmer",
kn: "Kannada",
ko: "Korean",
kr: "Kanuri",
ks: "Kashmiri",
ku: "Kurdish",
kv: "Komi",
kw: "Cornish",
ky: "Kirghiz",
la: "Latin",
lb: "Letzeburgesch",
lg: "Ganda",
li: "Limburgish",
ln: "Lingala",
lo: "Lao",
lt: "Lithuanian",
lu: "Luba-Katanga",
lv: "Latvian",
mg: "Malagasy",
mh: "Marshall",
mi: "Maori",
mk: "Macedonian",
ml: "Malayalam",
mn: "Mongolian",
mo: "Moldavian",
mr: "Marathi",
ms: "Malay",
mt: "Maltese",
my: "Burmese",
na: "Nauru",
nb: "Norwegian Bokmål",
nd: "Ndebele",
ne: "Nepali",
ng: "Ndonga",
nl: "Dutch",
nn: "Norwegian Nynorsk",
no: "Norwegian",
nr: "Ndebele",
nv: "Navajo",
ny: "Chichewa; Nyanja",
oc: "Occitan",
oj: "Ojibwa",
om: "Oromo",
or: "Oriya",
os: "Ossetian; Ossetic",
pa: "Punjabi",
pi: "Pali",
pl: "Polish",
ps: "Pushto",
pt: "Portuguese",
qu: "Quechua",
rm: "Raeto-Romance",
rn: "Rundi",
ro: "Romanian",
ru: "Russian",
rw: "Kinyarwanda",
sa: "Sanskrit",
sc: "Sardinian",
sd: "Sindhi",
se: "Northern Sami",
sg: "Sango",
sh: "Serbo-Croatian",
si: "Sinhalese",
sk: "Slovak",
sl: "Slovenian",
sm: "Samoan",
sn: "Shona",
so: "Somali",
sq: "Albanian",
sr: "Serbian",
ss: "Swati",
st: "Sotho",
su: "Sundanese",
sv: "Swedish",
sw: "Swahili",
ta: "Tamil",
te: "Telugu",
tg: "Tajik",
th: "Thai",
ti: "Tigrinya",
tk: "Turkmen",
tl: "Tagalog",
tn: "Tswana",
to: "Tonga",
tr: "Turkish",
ts: "Tsonga",
tt: "Tatar",
tw: "Twi",
ty: "Tahitian",
ug: "Uighur",
uk: "Ukrainian",
ur: "Urdu",
uz: "Uzbek",
ve: "Venda",
vi: "Vietnamese",
vo: "Volapük",
wa: "Walloon",
wo: "Wolof",
xh: "Xhosa",
xx: "No Language",
yi: "Yiddish",
yo: "Yoruba",
za: "Zhuang",
zh: "Mandarin",
zu: "Zulu"
}
76 changes: 45 additions & 31 deletions js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ document.addEventListener('DOMContentLoaded', configTMDB);

//Setting variables for getting items
let request;
let sessionID;
let sessionID = localStorage.getItem('sessionID')
let accountID;
let usersList;

Expand All @@ -33,7 +33,11 @@ const userForm = forms[0]
const uploadList =forms[1]
const submitChoices = forms[2]


if (sessionID !== null) {
sessionID = localStorage.getItem("sessionID");
formSections[0].classList.add("hide");
formSections[1].classList.remove("hide")
}
//Get 3rd party approval through TMDB
userForm.addEventListener("submit", function(event){userAuth(event)})
//Create Session ID then gets users watchlist
Expand Down Expand Up @@ -61,10 +65,14 @@ function upload(event) {
event.preventDefault();

formSections[1].classList.add("hide")

generateSession()
.then(response => getWatchlist(response))
.then(formSections[2].classList.remove("hide"))
if (sessionID === null) {
generateSession()
.then(response => getWatchlist(response))
.then(formSections[2].classList.remove("hide"))
} else {
getWatchlist(sessionID)
.then(formSections[2].classList.remove("hide"))
}
}

//Generates a session id
Expand All @@ -81,6 +89,7 @@ async function generateSession() {
});
const sessionJSON = await sessionPromise.json();
sessionID = sessionJSON.session_id;
localStorage.setItem("sessionID", sessionID);
return sessionID;

}
Expand All @@ -101,9 +110,11 @@ async function getWatchlist(session) {
async function fetchFunc(url) {
try {
const response = await fetch(url);
return await response.json();
} catch (data) {
return console.error(data);
if (!response.ok) throw new Error(response.status);
return response.json();
} catch (error) {
console.log(error);
poster.innerHTML = `Oops having trouble here ${error}`
}
}

Expand Down Expand Up @@ -132,8 +143,8 @@ function selectors(filmList) {
const decadeDropdown = document.querySelector("#decade");

//Adding availables to dropdowns
genreNames.then(response => addDropdown(response, genreDropdown))
languageNames.then(response => addDropdown(response, langDropdown))
addDropdown(genreNames, genreDropdown)
addDropdown(languageNames, langDropdown)
//Decade Dropdown
for (let decade in decades) {
const option = document.createElement("option")
Expand All @@ -143,15 +154,6 @@ function selectors(filmList) {
}
}

//Genre id to genre name object
const genreObj = fetchFunc(`${baseURL}genre/movie/list?api_key=${APIKEY}&language=en-US`)
.then(response => response['genres'])
.then(response => objectCodes(response, 'id', 'name'))

//Creating the langugage id to english name object
const languageObj = fetchFunc(`${baseURL}configuration/languages?api_key=${APIKEY}`)
.then(response => objectCodes(response, 'iso_639_1', 'english_name'))

//Decade Functions
function decadeOptions(filmYear, available) {
let decade = Math.floor(filmYear/10) * 10;
Expand Down Expand Up @@ -179,15 +181,21 @@ function options(current, available) {
return available
}

async function idToName(objPromise, ids) {
function idToName(obj, ids) {
const names = [];
const obj = await objPromise
console.log(obj)
ids.forEach((id) => {
names.push([obj[id],id])
});
return names.sort()
}

function nameToId(names) {
names.forEach((name, index) => {
names[index] = Object.keys(genreObj).find(key => genreObj[key] === name)
})
}

function addDropdown(idNames, dropdown) {
for (let element in idNames) {
const option = document.createElement("option");
Expand Down Expand Up @@ -344,9 +352,9 @@ async function rightContent(film) {
director.textContent = `Directed By - ${directedBy}`

const genre = document.createElement("h3");
const genres = await pickedGenre(film.genre_ids);
console.log(genres)
const genres = pickedGenre(film.genre_ids);
genre.textContent = `Genre - ${genres.join(", ")}`
nameToId(film.genre_ids)

const year = document.createElement("h3");
year.textContent = `Released - ${pickedYear(film.release_date)}`;
Expand Down Expand Up @@ -377,22 +385,28 @@ async function pickedDirector(film) {

}

async function pickedGenre(genres) {
const genre = await genreObj;
function pickedGenre(genres) {
const names = genres
genres.forEach((e,index) => {
genres[index] = genre[e]
names[index] = genreObj[e]
})
return genres
return names
}

function pickedYear(date) {
return date.substring(0,4)
}

async function services(film) {
const providers = await fetchFunc(`${baseURL}movie/${film.id}/watch/providers?api_key=${APIKEY}`);
const ukProvider = providers["results"]["GB"]["link"]
return ukProvider

try {
const providers = await fetchFunc(`${baseURL}movie/${film.id}/watch/providers?api_key=${APIKEY}`);
const ukProvider = providers["results"]["GB"]["link"]
return ukProvider
} catch (error) {
console.log(error);
return "" //Change to something different?
}
}

function overlayEvents() {
Expand Down

0 comments on commit c7b2546

Please sign in to comment.