diff --git a/ui/src/App.vue b/ui/src/App.vue index d0c0005..2f1c063 100644 --- a/ui/src/App.vue +++ b/ui/src/App.vue @@ -1,8 +1,8 @@ diff --git a/ui/src/api/application.ts b/ui/src/api/application.ts new file mode 100644 index 0000000..fd069fd --- /dev/null +++ b/ui/src/api/application.ts @@ -0,0 +1,55 @@ +export type ApplicationDto = Partial<{ + age: number | string + name: string + playtime: number | string + hobbies: string + military: boolean + communities: string + joinReason: string + milsimAttraction: string + referral: string + steamProfile: string + timezone: string + canAttendSaturday: boolean + interests: string + aknowledgeRules: boolean +}> + +type ApplicationFull = Partial<{ + app: ApplicationDto, + messages: object[] +}> + +const addr = "localhost:3000" + +export async function loadApplication(): Promise { + const res = await fetch(`http://${addr}/me/application`) + if (res.status === 204) return null + if (!res.ok) throw new Error('Failed to load application') + const json = await res.json() + // Accept either the object at root or under `application` + return json; +} + +export async function postApplication(val: any) { + await fetch(`http://${addr}/application`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(val), + }) +} + +export async function postChatMessage(val: any) { + + let output = { + message: val, + sender: 1, + timestamp: Date.now(), + } + + await fetch(`http://${addr}/application/message`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(output), + }) +} \ No newline at end of file diff --git a/ui/src/components/application/ApplicationForm.vue b/ui/src/components/application/ApplicationForm.vue index 7f993fa..7a41553 100644 --- a/ui/src/components/application/ApplicationForm.vue +++ b/ui/src/components/application/ApplicationForm.vue @@ -10,11 +10,10 @@ import { FormMessage, } from '@/components/ui/form' import Input from '@/components/ui/input/Input.vue'; -import Label from '@/components/ui/label/Label.vue'; import Textarea from '@/components/ui/textarea/Textarea.vue'; import { toTypedSchema } from '@vee-validate/zod'; import { Form } from 'vee-validate'; -import { onMounted, readonly, ref } from 'vue'; +import { onMounted, ref } from 'vue'; import * as z from 'zod'; const formSchema = toTypedSchema(z.object({ @@ -36,22 +35,6 @@ const formSchema = toTypedSchema(z.object({ }), })) -type ApplicationDto = Partial<{ - age: number | string - name: string - playtime: number | string - hobbies: string - military: boolean - communities: string - joinReason: string - milsimAttraction: string - referral: string - steamProfile: string - timezone: string - canAttendSaturday: boolean - interests: string - aknowledgeRules: boolean -}> const fallbackInitials = { military: false, @@ -59,44 +42,23 @@ const fallbackInitials = { aknowledgeRules: false, } +const props = defineProps<{ + readOnly: boolean, + data: object, +}>() + +const emit = defineEmits(['submit']); + const initialValues = ref | null>(null); -const readOnly = ref(false); async function onSubmit(val: any) { - if (readOnly.value) return // guard (shouldn't be visible anyway) - await fetch('http://localhost:3000/application', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(val), - }) + emit('submit', val); } -async function loadApplication(): Promise { - const res = await fetch('http://localhost:3000/me/application') - if (res.status === 204) return null - if (!res.ok) throw new Error('Failed to load application') - const json = await res.json() - // Accept either the object at root or under `application` - return (json?.application ?? json) as ApplicationDto -} - -onMounted(async () => { - try { - const data = await loadApplication() - if (data) { - // Optional: coerce/normalize incoming payload to field names/types if needed - initialValues.value = { - ...fallbackInitials, // ensure booleans exist even if API omits them - ...data, - } - readOnly.value = true; - } else { - // 204 or empty → use your preset three fields only - initialValues.value = { ...fallbackInitials } - } - } catch (e) { - console.error(e) - // On error, also fall back +onMounted(() => { + if (props.data) { + initialValues.value = { ...props.data, ...fallbackInitials } + } else { initialValues.value = { ...fallbackInitials } } }) @@ -104,7 +66,7 @@ onMounted(async () => {