-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.vue
128 lines (111 loc) · 2.96 KB
/
app.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<script setup lang="ts">
import type { FormError } from '#ui/types'
const state = reactive<SchemaOptions>({
url: undefined,
headers: [],
})
const rawSchema = ref('')
const errorSchema = ref<Error | null>(null)
const loading = ref(false)
function validate(state: any): FormError[] {
const errors = []
if (!state.url)
errors.push({ path: 'url', message: 'Required' })
// if (hasHeader.value) {
// if (!state.headerName)
// errors.push({ path: 'headerName', message: 'Required' })
// if (!state.headerValue)
// errors.push({ path: 'headerValue', message: 'Required' })
// }
return errors
}
async function onSubmit() {
rawSchema.value = ''
loading.value = true
const { data, error } = await useAsyncData(() => getSchema(state))
errorSchema.value = error.value
if (!data.value)
return loading.value = false
rawSchema.value = data.value
loading.value = false
}
useHead({
htmlAttrs: {
lang: 'en',
},
})
</script>
<template>
<AppHead />
<main class="bg-gray-900">
<div class="max-w-5xl mx-auto min-h-screen px-4 py-8 flex flex-col">
<Header />
<UForm
class="max-w-lg w-full mx-auto"
:validate="validate"
:state="state"
:validate-on="['submit']"
@submit="onSubmit"
>
<UFormGroup name="url" class="mb-4">
<UInput
v-model="state.url"
aria-label="GraphQL url"
type="url"
placeholder="https://my-api.com/graphql"
/>
</UFormGroup>
<div v-for="item, index in state.headers" :key="index" class="flex gap-3 mb-2">
<UFormGroup name="headerName" class="grow">
<UInput
v-model="item[0]"
aria-label="Header name"
placeholder="Key"
/>
</UFormGroup>
<UFormGroup name="headerValue" class="grow">
<UInput
v-model="item[1]"
aria-label="Header value"
placeholder="Value"
/>
</UFormGroup>
<UButton
icon="i-heroicons-minus"
color="white"
square
variant="solid"
aria-label="Remove"
@click="state.headers.splice(index, 1)"
/>
</div>
<UButton
icon="i-heroicons-plus"
color="white"
variant="solid"
@click="state.headers.push(['', ''])"
>
Add Header
</UButton>
<UButton :loading="loading" block type="submit" class="mt-5">
Get my schema
</UButton>
</UForm>
<UAlert
v-if="errorSchema"
class="mt-12 mb-8"
color="primary"
variant="solid"
title="Error"
:description="errorSchema.message"
/>
<LazyResult
v-else-if="rawSchema || loading"
:loading="loading"
:raw-schema="rawSchema"
/>
<Footer v-once />
</div>
</main>
<UNotifications />
</template>