Skip to content

Commit

Permalink
feat: handle missing FORMSPREE_URL and some errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bdrtsky committed Jan 6, 2023
1 parent 9fdb7be commit 7d32c65
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
9 changes: 8 additions & 1 deletion components/Button.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ defineProps({
type: {
type: String,
default: 'submit'
},
disabled: {
type: Boolean,
default: false
}
})
</script>

<template>
<button :type="type">
<button :type="type" :disabled="disabled">
<slot />
</button>
</template>
Expand All @@ -33,6 +37,9 @@ css({
'@dark': {
backgroundColor: '{color.gray.100}',
color: '{color.gray.900}',
},
'&:disabled': {
cursor: 'not-allowed',
}
}
})
Expand Down
16 changes: 13 additions & 3 deletions components/content/ContactForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ const alpine = useAppConfig().alpine
const { FORMSPREE_URL } = useRuntimeConfig().public
if (!FORMSPREE_URL) {
console.warn('No FORMSPREE_URL provided')
}
const status = ref()
const props = defineProps({
sendButton: {
submitButtonText: {
type: String,
default: 'Send message'
},
Expand Down Expand Up @@ -56,6 +60,8 @@ const onSend = async (e: any) => {
e.preventDefault()
const data = new FormData(e.target)
status.value = 'Sending...'
fetch(e.target.action, {
method: e.target.method,
body: data,
Expand All @@ -70,7 +76,11 @@ const onSend = async (e: any) => {
// Handle errors from API
response.json().then(data => {
if (Object.hasOwn(data, 'errors')) {
status.value = data["errors"][0].message
console.error(data["errors"].map((error: any) => error["message"]).join(", "))
setTimeout(() => {
status.value = 'Send message'
}, 2000)
} else {
console.error("There was a problem submitting your form")
}
Expand All @@ -90,8 +100,8 @@ const onSend = async (e: any) => {
<Input v-for="(field, index) in form" :key="index" v-model="field.data" :field="field" />
</div>
<div>
<Button type="submit">
{{ status ? status : sendButton }}
<Button type="submit" :disabled="!FORMSPREE_URL">
{{ status ? status : submitButtonText }}
</Button>
</div>
</form>
Expand Down
3 changes: 2 additions & 1 deletion components/data-entry/Input.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ css({
fontSize: '{text.base.fontSize}',
lineHeight: '{text.base.lineHeight}',
fontWeight: '{fontWeight.semibold}',
marginBottom: '{space.2}'
marginBottom: '{space.2}',
cursor: 'pointer',
},
'input, textarea': {
backgroundColor: 'transparent',
Expand Down

1 comment on commit 7d32c65

@vercel
Copy link

@vercel vercel bot commented on 7d32c65 Jan 6, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.