-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
06306b7
commit e9fa58d
Showing
11 changed files
with
203 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { writable } from 'svelte/store'; | ||
import type { Answer, Session } from '../../models'; | ||
|
||
export const sessionDetails = writable<Session>(); | ||
export const answer = writable<Answer[]>(); | ||
|
||
export async function loadSessionDetailsFromDB( | ||
platform: Readonly<App.Platform> | undefined, | ||
params: number | ||
): Promise<Session & { answers: Answer[] }> { | ||
const session_res = await platform!.env.FORMS_DB.prepare('SELECT * FROM Session WHERE id = ?') | ||
.bind(params) | ||
.run(); | ||
sessionDetails.set(session_res.results[0]); | ||
|
||
const answer_res = await platform!.env.FORMS_DB.prepare( | ||
'SELECT * FROM Answer WHERE session_id = ?' | ||
) | ||
.bind(params) | ||
.run(); | ||
answer.set(answer_res.results); | ||
|
||
const response = { answers: answer_res.results, ...session_res.results[0] }; | ||
|
||
return response; | ||
} | ||
|
||
export function receiveSessionDetails(data: unknown) { | ||
if (typeof data === 'object' && data !== null && 'sessionDetails' in data) { | ||
sessionDetails.set(data.sessionDetails as Session & { answers: Answer[] }); | ||
} else { | ||
console.error('Invalid data format in receiveSession:', data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { writable } from 'svelte/store'; | ||
import type { External, Session } from '../../models'; | ||
|
||
export const sessions = writable<(Session & { external: External })[]>(); | ||
|
||
export async function loadSessionsFromDB( | ||
platform: Readonly<App.Platform> | undefined, | ||
formId: number | ||
): Promise<(Session & { external: External })[]> { | ||
const query = ` | ||
SELECT | ||
s.*, | ||
e.external_id as e_external_id, | ||
e.kind as e_kind, | ||
e.email as e_email, | ||
e.token as e_token | ||
FROM Session s | ||
LEFT JOIN External e ON s.external_id = e.id | ||
WHERE s.form_id = ? | ||
`; | ||
|
||
const sessions_res = await platform!.env.FORMS_DB.prepare(query).bind(formId).all(); | ||
|
||
const sessionsWithExternal = sessions_res.results.map((row) => ({ | ||
id: row.id, | ||
device_id: row.device_id, | ||
form_id: row.form_id, | ||
external_id: row.external_id, | ||
token: row.token, | ||
last_answer: row.last_answer, | ||
steps: row.steps, | ||
created_at: row.created_at, | ||
external: { | ||
id: row.external_id, | ||
external_id: row.e_external_id, | ||
kind: row.e_kind, | ||
email: row.e_email, | ||
token: row.e_token | ||
} | ||
})); | ||
|
||
sessions.set(sessionsWithExternal); | ||
|
||
return sessionsWithExternal; | ||
} | ||
|
||
export function receiveSessions(data: unknown) { | ||
if (typeof data === 'object' && data !== null && 'sessions' in data) { | ||
sessions.set(data.sessions as (Session & { external: External })[]); | ||
} else { | ||
console.error('Invalid data format in receiveSessions:', data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { PageServerLoad } from './$types'; | ||
import { loadSessionsFromDB } from '$lib/forms/service/stores/sessions'; | ||
|
||
export const load: PageServerLoad = async ({ platform, params }) => { | ||
const sessions = await loadSessionsFromDB(platform, parseInt(params.slug)); | ||
|
||
return { sessions }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<script lang="ts"> | ||
import { page } from '$app/stores'; | ||
import { receiveSessions } from '$lib/forms/service/stores/sessions'; | ||
export let data; | ||
receiveSessions(data); | ||
console.log(data); | ||
</script> | ||
|
||
<h1>ANSWERS</h1> | ||
|
||
<ul> | ||
{#each data.sessions as session, i} | ||
<li> | ||
<a href={`${$page.url.pathname}/${session.id}`}> | ||
Session #{i + 1} | ||
</a> | ||
|
||
<p>{session.external.email}</p> | ||
</li> | ||
{:else} | ||
<li>No sessions avilable</li> | ||
{/each} | ||
</ul> |
8 changes: 8 additions & 0 deletions
8
src/routes/(dashboard)/forms/[slug]/answers/[session_id]/+page.server.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { PageServerLoad } from './$types'; | ||
import { loadSessionDetailsFromDB } from '$lib/forms/service/stores/session'; | ||
|
||
export const load: PageServerLoad = async ({ platform, params }) => { | ||
const sessionDetails = await loadSessionDetailsFromDB(platform, parseInt(params.session_id)); | ||
|
||
return { sessionDetails }; | ||
}; |
36 changes: 36 additions & 0 deletions
36
src/routes/(dashboard)/forms/[slug]/answers/[session_id]/+page.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<script lang="ts"> | ||
import { receiveSessionDetails } from '$lib/forms/service/stores/session'; | ||
export let data; | ||
receiveSessionDetails(data); | ||
console.log(data); | ||
/* | ||
- Si existe token | ||
- No ha terminado el form | ||
- No lo ha terminado, el wey | ||
- No existe | ||
- Ya terminó pa | ||
------------------------------------------- | ||
- Mostrar las listas de las respuestas | ||
*/ | ||
</script> | ||
|
||
{#if data.sessionDetails.token} | ||
<p>No ha terminado</p> | ||
{:else} | ||
<p>Ya terminó el questionario</p> | ||
{/if} | ||
|
||
<h2>Answers</h2> | ||
{#each data.sessionDetails.answers as answer} | ||
<h4>{data.questions.find((question) => question.id === answer.question_id).title}</h4> | ||
{#if answer.data} | ||
<li>{answer.data}</li> | ||
{:else} | ||
<li>No answer</li> | ||
{/if} | ||
{/each} |