Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/lib/airtable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ const GLOBAL_BASE_ID =
import.meta.env.VITE_AIRTABLE_GLOBAL_BASE_ID || `appSswal9DNdJKRB8`
const ERROR_LOG_TABLE = `Anmeldefehler`

// Airtable table names for signups
const STUDENTS_TABLE = `Nachhilfelehrkräfte`
const PUPILS_TABLE = `Lernende`

// Log signup errors to Airtable for monitoring
async function log_error_to_airtable(
error: Error,
Expand Down Expand Up @@ -81,12 +77,12 @@ async function log_error_to_airtable(
}
}

// Prepares the form data for Airtable submission
export async function prepare_signup_data_for_airtable(
data: SignupStore,
chapter_base_id: string,
tableName: string,
): Promise<{ status: number; data: unknown }> {
const table = data.type.value === `student` ? STUDENTS_TABLE : PUPILS_TABLE
const table = tableName

// Common fields for both students and pupils
let fields = {
Expand Down Expand Up @@ -184,6 +180,7 @@ export async function signup_form_submit_handler(
fields_to_validate: (keyof SignupStore)[],
chapters: Chapter[],
err_msg: Record<string, string>,
tableName: string,
): Promise<{ error?: Error; success?: boolean }> {
const signup_data = get(signup_store)

Expand Down Expand Up @@ -217,7 +214,11 @@ export async function signup_form_submit_handler(
}

try {
const response = await prepare_signup_data_for_airtable(signup_data, baseId)
const response = await prepare_signup_data_for_airtable(
signup_data,
baseId,
tableName,
)

if (response.status < 200 || response.status >= 300) {
// Include Airtable's error response for debugging
Expand Down
77 changes: 21 additions & 56 deletions src/routes/signup-pupil/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,35 +43,25 @@ export const load = async ({ fetch: customFetch }: { fetch: typeof fetch }) => {
console.debug(`Loading pupil signup form for locale: ${locale}`)

// Load locale-specific YAML data
const messagesData = getLocaleModule(localeModules.messages, locale) || {
submitSuccess: { title: `🎉 ⭐ 🎉`, note: `Success!` },
submitError: { title: `😢`, note: `Error occurred.` },
errMsg: { required: `This field is required` },
const messagesData = getLocaleModule(localeModules.messages, locale) as {
submitSuccess: { title: string; note: string }
submitError: { title: string; note: string }
errMsg: { required: string }
}

const optionsData = (getLocaleModule(localeModules.options, locale) ||
{}) as Record<string, string[]>
const rawFormData = getLocaleModule(localeModules.pupil, locale) || {
header: {
title: `Anmeldung Schüler:innen`,
note: `Formular für Schüler:innen`,
},
fields: [
{
id: `chapter`,
title: `Standort`,
note: `Wähle einen unserer Nachhilfestandorte.`,
required: true,
type: `select`,
maxSelect: 1,
},
],
submit: {
title: `Anmeldung abschicken`,
note: `Du bekommst innerhalb einer Minute eine Bestätigungs-Email von uns.`,
},
const optionsData = getLocaleModule(
localeModules.options,
locale,
) as Record<string, string[]>

const rawFormData = getLocaleModule(localeModules.pupil, locale) as {
airtableTable: string
[key: string]: unknown
}

// Extract airtableTable from form data (defined per form type per locale)
const airtableTable = rawFormData.airtableTable

console.debug(`YAML data loaded for locale ${locale}:`, {
messages: !!messagesData,
options: !!optionsData,
Expand All @@ -91,7 +81,7 @@ export const load = async ({ fetch: customFetch }: { fetch: typeof fetch }) => {
const form = parse_form_data({
...rawFormData,
...messagesData,
} as Parameters<typeof parse_form_data>[0])
} as unknown as Parameters<typeof parse_form_data>[0])
console.debug(`form parsed:`, form)

// In dev mode, add a test chapter at the beginning for testing purposes if defined
Expand All @@ -111,8 +101,9 @@ export const load = async ({ fetch: customFetch }: { fetch: typeof fetch }) => {
}

for (const field of form.fields || []) {
if (field.id in optionsData) {
field.options = optionsData[field.id]
const optionValue = optionsData[field.id]
if (field.id in optionsData && Array.isArray(optionValue)) {
field.options = optionValue
} else if (field.id === `chapter`) {
field.options = chapters.map((chap) => chap.title)
}
Expand All @@ -124,36 +115,10 @@ export const load = async ({ fetch: customFetch }: { fetch: typeof fetch }) => {
return {
chapters: JSON.parse(JSON.stringify(chapters)),
form: JSON.parse(JSON.stringify(form)),
airtableTable,
}
} catch (error) {
console.error(`Error loading pupil signup form:`, error)
console.error(
`Error stack:`,
error instanceof Error ? error.stack : `Unknown error`,
)

// Return fallback form structure
const basicForm = {
header: { title: `Anmeldung Schüler:innen`, note: `Form loading...` },
fields: [
{
id: `chapter`,
title: `Standort`,
required: true,
type: `select`,
maxSelect: 1,
},
],
submit: { title: `Anmeldung abschicken`, note: `` },
submitSuccess: { title: `Success`, note: `Success!` },
submitError: { title: `Error`, note: `Error occurred` },
errMsg: { required: `This field is required` },
}

console.debug(`Returning fallback form:`, basicForm)
return {
chapters: [],
form: JSON.parse(JSON.stringify(basicForm)),
}
throw error
}
}
4 changes: 3 additions & 1 deletion src/routes/signup-pupil/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
const { data } = $props()
const chapters = $derived(data.chapters)
const form = $derived(data.form)
const airtableTable = $derived(data.airtableTable as string)

// Add debugging and fallback
$effect(() => {
Expand Down Expand Up @@ -39,7 +40,8 @@
const response = await signup_form_submit_handler(
field_ids_to_validate,
chapters,
form.errMsg || {}
form.errMsg || {},
airtableTable
)
if (response.success) success = true
error = response.error
Expand Down
74 changes: 21 additions & 53 deletions src/routes/signup-student/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,35 +43,25 @@ export const load = async ({ fetch: customFetch }: { fetch: typeof fetch }) => {
console.debug(`Loading student signup form for locale: ${locale}`)

// Load locale-specific YAML data
const messagesData = getLocaleModule(localeModules.messages, locale) || {
submitSuccess: { title: `🎉 ⭐ 🎉`, note: `Success!` },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arent those supposed to be default values if the locale somehow fails?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, but I think if it fails it should really fail and not silently disappear, or what do you think?
There are a lot of fall back options inside and I'm not entirely sure if this a good thing.
Maybe I could add a test, making sure all locales are available.

submitError: { title: `😢`, note: `Error occurred.` },
errMsg: { required: `This field is required` },
const messagesData = getLocaleModule(localeModules.messages, locale) as {
submitSuccess: { title: string; note: string }
submitError: { title: string; note: string }
errMsg: { required: string }
}

const optionsData = (getLocaleModule(localeModules.options, locale) ||
{}) as Record<string, string[]>
const rawFormData = getLocaleModule(localeModules.student, locale) || {
header: {
title: `Anmeldung Studierende`,
note: `Formular für Studierende`,
},
fields: [
{
id: `chapter`,
title: `Standort`,
note: `An welchem Standort möchtest du Nachhilfe geben?`,
required: true,
type: `select`,
maxSelect: 1,
},
],
submit: {
title: `Anmeldung abschicken`,
note: `Du bekommst innerhalb einer Minute eine Bestätigungs-Email von uns.`,
},
const optionsData = getLocaleModule(
localeModules.options,
locale,
) as Record<string, string[]>

const rawFormData = getLocaleModule(localeModules.student, locale) as {
airtableTable: string
[key: string]: unknown
}

// Extract airtableTable from form data (defined per form type per locale)
const airtableTable = rawFormData.airtableTable

console.debug(`YAML data loaded for locale ${locale}:`, {
messages: !!messagesData,
options: !!optionsData,
Expand All @@ -91,7 +81,7 @@ export const load = async ({ fetch: customFetch }: { fetch: typeof fetch }) => {
const form = parse_form_data({
...rawFormData,
...messagesData,
} as Parameters<typeof parse_form_data>[0])
} as unknown as Parameters<typeof parse_form_data>[0])
console.debug(`form parsed:`, form)

// In dev mode, add a test chapter at the beginning for testing purposes if defined
Expand All @@ -111,8 +101,9 @@ export const load = async ({ fetch: customFetch }: { fetch: typeof fetch }) => {
}

for (const field of form.fields || []) {
if (field.id in optionsData) {
field.options = optionsData[field.id]
const optionValue = optionsData[field.id]
if (field.id in optionsData && Array.isArray(optionValue)) {
field.options = optionValue
} else if (field.id === `chapter`) {
field.options = chapters.map((chap) => chap.title)
}
Expand All @@ -124,33 +115,10 @@ export const load = async ({ fetch: customFetch }: { fetch: typeof fetch }) => {
return {
chapters: JSON.parse(JSON.stringify(chapters)),
form: JSON.parse(JSON.stringify(form)),
airtableTable,
}
} catch (error) {
console.error(`Error loading student signup form:`, error)
console.error(`Error stack:`, (error as Error).stack)

// Return fallback form structure
const basicForm = {
header: { title: `Anmeldung Studierende`, note: `Form loading...` },
fields: [
{
id: `chapter`,
title: `Standort`,
required: true,
type: `select`,
maxSelect: 1,
},
],
submit: { title: `Anmeldung abschicken`, note: `` },
submitSuccess: { title: `Success`, note: `Success!` },
submitError: { title: `Error`, note: `Error occurred` },
errMsg: { required: `This field is required` },
}

console.debug(`Returning fallback form:`, basicForm)
return {
chapters: [],
form: JSON.parse(JSON.stringify(basicForm)),
}
throw error
}
}
4 changes: 3 additions & 1 deletion src/routes/signup-student/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
const { data } = $props()
const chapters = $derived(data.chapters)
const form = $derived(data.form)
const airtableTable = $derived(data.airtableTable as string)

// Add debugging and fallback
$effect(() => {
Expand Down Expand Up @@ -40,7 +41,8 @@
const response = await signup_form_submit_handler(
field_ids_to_validate,
chapters,
form.errMsg || {}
form.errMsg || {},
airtableTable
)
if (response.success) success = true
error = response.error
Expand Down
3 changes: 3 additions & 0 deletions src/signup-form/at/pupil.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Airtable table name for this form type
airtableTable: Lernende

header:
title: Anmeldung Schüler:innen
note: |
Expand Down
3 changes: 3 additions & 0 deletions src/signup-form/at/student.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Airtable table name for this form type
airtableTable: Nachhilfelehrkräfte

header:
title: Anmeldung Studierende
note: |
Expand Down
3 changes: 3 additions & 0 deletions src/signup-form/de/pupil.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Airtable table name for this form type
airtableTable: Lernende

header:
title: Anmeldung Schüler:innen
note: |
Expand Down
3 changes: 3 additions & 0 deletions src/signup-form/de/student.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Airtable table name for this form type
airtableTable: Nachhilfelehrkräfte

header:
title: Anmeldung Studierende
note: |
Expand Down
Loading