Skip to content

Commit

Permalink
basic data loading works
Browse files Browse the repository at this point in the history
  • Loading branch information
refact0r committed Nov 11, 2022
1 parent 392610e commit d437ed7
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 61 deletions.
2 changes: 1 addition & 1 deletion jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"checkJs": false,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
Expand Down
13 changes: 3 additions & 10 deletions src/hooks.server.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import cookie from 'cookie'

export const handle = async ({ event, resolve }) => {
const cookies = cookie.parse(event.request.headers.get('cookie') || '')
const authCookie = event.cookies.get('auth')

if (cookies.auth) {
const auth = cookies.auth.split(':')
if (authCookie) {
const auth = authCookie.split(':')
event.locals.user = {
username: auth[0],
password: auth[1],
Expand All @@ -13,12 +13,5 @@ export const handle = async ({ event, resolve }) => {
}

const response = await resolve(event)

return response
}

// Sets session on client-side
// try console logging session in routes' load({ session }) functions
export const getSession = async (event) => {
return event.locals
}
7 changes: 7 additions & 0 deletions src/routes/+layout.server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @type {import('./$types').PageServerLoad} */
export async function load({ params, locals }) {
console.log('layout server load')
return {
user: locals
}
}
91 changes: 52 additions & 39 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,64 +1,77 @@
<script context="module">
<!-- <script context="module">
export async function load({ url }) {
return {
props: {
key: url.href
}
}
}
</script>

</script> -->
<script>
import '../app.scss'
import { onMount } from 'svelte'
import { fly } from 'svelte/transition'
import { fade } from 'svelte/transition'
import { session, page } from '$app/stores'
import { browser } from '$app/environment'
import { parseData } from '$lib/js/parseData.js'
import { settings } from '$lib/js/settings.js'
import { oldAssignments } from '$lib/js/oldAssignments.js'
import Spinner from '$lib/components/Spinner.svelte'
export let key
let spinning = false
// import { fly } from 'svelte/transition'
// import { fade } from 'svelte/transition'
// import { page } from '$app/stores'
// import { browser } from '$app/environment'
// import { parseData } from '$lib/js/parseData.js'
// import { settings } from '$lib/js/settings.js'
// import { oldAssignments } from '$lib/js/oldAssignments.js'
// import Spinner from '$lib/components/Spinner.svelte'
export let data
// export let key
// let spinning = false
onMount(async () => {
if ($session.user) {
console.log('fetch')
if (data.user) {
await load()
}
})
$: if ($session.periods) {
$session.selected = $session.periods[$session.selectedPeriod]
}
async function load() {
const res = await fetch('/data')
const json = await res.json()
let { student, periods, currentPeriod } = json
$session = {
...$session,
student,
periods,
currentPeriod,
selectedPeriod: currentPeriod,
selected: periods[currentPeriod],
gradebook: periods[currentPeriod]
}
parseData($session, $oldAssignments)
$oldAssignments = $oldAssignments
console.log(json)
}
async function refresh() {
spinning = true
await load()
spinning = false
}
// onMount(async () => {
// if ($session.user) {
// console.log('fetch')
// await load()
// }
// })
// $: if ($session.periods) {
// $session.selected = $session.periods[$session.selectedPeriod]
// }
// async function load() {
// const res = await fetch('/data')
// const json = await res.json()
// let { student, periods, currentPeriod } = json
// $session = {
// ...$session,
// student,
// periods,
// currentPeriod,
// selectedPeriod: currentPeriod,
// selected: periods[currentPeriod],
// gradebook: periods[currentPeriod]
// }
// parseData($session, $oldAssignments)
// $oldAssignments = $oldAssignments
// }
// async function refresh() {
// spinning = true
// await load()
// spinning = false
// }
</script>

<svelte:head>
<!-- <svelte:head>
<meta name="color-scheme" content={$settings.theme} />
<link rel="stylesheet" href={`/themes/${$settings.theme}.css`} />
</svelte:head>
Expand Down Expand Up @@ -250,4 +263,4 @@
height: calc(100% - 2 * $spacing - 50px);
}
}
</style>
</style> -->
11 changes: 11 additions & 0 deletions src/routes/+page.server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { redirect } from '@sveltejs/kit'

/** @type {import('./$types').PageLoad} */
export async function load({ params, locals }) {
console.log('page server load')
if (!locals.user) {
console.log('redirect')
throw redirect(307, '/login')
}
return {}
}
4 changes: 2 additions & 2 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script context="module">
<!-- <script context="module">
export async function load({ session }) {
if (!session.user) {
return {
Expand Down Expand Up @@ -233,4 +233,4 @@
display: none;
}
}
</style>
</style> -->
File renamed without changes.
28 changes: 19 additions & 9 deletions src/routes/data/index.js → src/routes/data/+server.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { login } from 'studentvue.js'
import * as cookie from 'cookie'

export async function get(event) {
console.log('get all')
export async function GET({ locals }) {
console.log('get data')
console.log(locals)

let client = await login(
Buffer.from(event.locals.user.districtUrl, 'base64').toString('ascii'),
Buffer.from(event.locals.user.username, 'base64').toString('ascii'),
Buffer.from(event.locals.user.password, 'base64').toString('ascii')
Buffer.from(locals.user.districtUrl, 'base64').toString('ascii'),
Buffer.from(locals.user.username, 'base64').toString('ascii'),
Buffer.from(locals.user.password, 'base64').toString('ascii')
)
// let student = JSON.parse(await client.getStudentInfo()).StudentInfo
// let gradebook = JSON.parse(await client.getGradebook()).Gradebook
Expand All @@ -31,16 +33,24 @@ export async function get(event) {
}
}

console.log('logged in')

const currentPeriod = result[1].ReportingPeriods.ReportPeriod.findIndex((period) => {
const date = new Date()
return date > new Date(period.StartDate) && date < new Date(period.EndDate)
})

return {
body: {
console.log({
student: result.shift(),
periods: result,
currentPeriod
})

return new Response(
JSON.stringify({
student: result.shift(),
periods: result,
currentPeriod
}
}
})
)
}
File renamed without changes.

0 comments on commit d437ed7

Please sign in to comment.