Skip to content

Commit

Permalink
feat(archive): previous episodes
Browse files Browse the repository at this point in the history
  • Loading branch information
rudywaltz committed Dec 22, 2019
1 parent abc61e2 commit 32ccc29
Showing 1 changed file with 64 additions and 13 deletions.
77 changes: 64 additions & 13 deletions src/components/Archivelist.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script>
import { onMount } from 'svelte';
import { getQuarter, setQuarter, endOfQuarter, startOfQuarter, isAfter, endOfDay, getTime } from 'date-fns';
import { getQuarter, setQuarter, endOfQuarter, startOfQuarter, isAfter, endOfDay, getTime, format } from 'date-fns';
export let year;
export let quarter;
Expand All @@ -10,37 +10,88 @@
export let id;
let archiveShows = [];
let archiveShowsTemp = [];
year = year || new Date().getFullYear();
quarter = quarter || getQuarter(new Date());
let firstDayOfQuarter = setQuarter(new Date(year, 0, 1), quarter);
if (isAfter(firstShowDate, firstDayOfQuarter)) {
firstDayOfQuarter = firstShowDate;
}
const calculateQuarter = (localQuarter, localYear) => {
let firstDayOfQuarter = setQuarter(new Date(localYear, 0, 1), localQuarter);
if (isAfter(firstShowDate, firstDayOfQuarter)) {
firstDayOfQuarter = firstShowDate;
}
let lastDayOfQuarter = endOfQuarter(firstDayOfQuarter);
let lastDayOfQuarter = endOfQuarter(firstDayOfQuarter);
if (isAfter(lastDayOfQuarter, lastShowDate)) {
lastDayOfQuarter = endOfDay(lastShowDate);
firstDayOfQuarter = startOfQuarter(lastDayOfQuarter);
}
if (isAfter(lastDayOfQuarter, lastShowDate)) {
lastDayOfQuarter = endOfDay(lastShowDate);
firstDayOfQuarter = startOfQuarter(lastDayOfQuarter);
}
const load = async () => {
return { firstDayOfQuarter, lastDayOfQuarter };
};
const quarterDecrase = (localQuarter, localYear) => {
if (localQuarter === 1) {
localQuarter = 4;
localYear--;
} else {
localQuarter--;
}
return { localQuarter, localYear };
};
const onPrevPrefetch = async () => {
const { localQuarter, localYear } = quarterDecrase(quarter, year);
archiveShowsTemp = await load(localQuarter, localYear);
};
const prev = async (event) => {
event.preventDefault();
const { localQuarter, localYear } = quarterDecrase(quarter, year);
console.log(quarter, localQuarter);
console.log(year, localYear);
quarter = localQuarter;
year = localYear;
archiveShows = archiveShowsTemp.length ? archiveShowsTemp : await load();
archiveShowsTemp = [];
};
const next = async (event) => {
event.preventDefault();
if (quarter === 4) {
quarter = 1;
year = year + 1;
} else {
quarter++;
}
archiveShows = await load();
};
const load = async (localQuarter = quarter, localYear = year) => {
const { firstDayOfQuarter, lastDayOfQuarter } = calculateQuarter(localQuarter, localYear);
const response = await fetch(`/api/v1/show/${id}/episodes?start=${getTime(firstDayOfQuarter)}&end=${getTime(lastDayOfQuarter)}`);
const res = await response.json();
return res;
};
onMount(async () => {
archiveShows = await load();
console.log(archiveShows);
});
</script>
<h2>Archívum:</h2>
<h2>Archívum:</h2>
<button type="button" href="" on:click={prev} on:mouseenter={onPrevPrefetch}>Prev</button>
<button type="button" href="" on:click={next}>next</button>
{#each archiveShows as archive}
<hr>
<h4>{archive.realFrom} - {archive.realTo}</h4>
<h4>{format(archive.realFrom, 'yyyy-MM-dd HH:mm')} - {format(archive.realTo, 'yyyy-MM-dd HH:mm')}</h4>
<h4>{archive.text ? archive.text.title : '--' }</h4>
<h4>{archive.m3uUrl}</h4>
<a href="{archive.url}">URL</a>
Expand Down

0 comments on commit 32ccc29

Please sign in to comment.