Upgrade to vue 3 (#1)
* updated dependencies * updated readme * upgrade to vue 3 * updated buttons * duplicate id removed * updated contact
This commit is contained in:
@@ -1,22 +1,106 @@
|
||||
<template>
|
||||
<div class="contact_container">
|
||||
<div class="message"></div>
|
||||
<header class="container_head">Formularz kontaktowy:</header>
|
||||
<form id="form-point" @submit="formSubmit">
|
||||
<input v-model="emailValue" class="contact_input" type="text" name="email" placeholder="Twój adres e-mail." />
|
||||
<textarea v-model="messageValue" class="contact_input" name="message" placeholder="Twoja wiadomość."></textarea>
|
||||
<input v-model="senderValue" class="contact_input" type="text" name="sender" placeholder="Twój podpis." />
|
||||
<base-btn is-reverse :disabled="buttonDisabled">Wyślij</base-btn>
|
||||
<div
|
||||
v-if="hasMessageError"
|
||||
class="message message_error"
|
||||
>
|
||||
{{ messageError }}
|
||||
</div>
|
||||
<div
|
||||
v-if="hasMessageOkStatus"
|
||||
class="message message_ok"
|
||||
>
|
||||
{{ messageOk }}
|
||||
</div>
|
||||
<header class="container_head">
|
||||
Formularz kontaktowy:
|
||||
</header>
|
||||
<form
|
||||
id="form-point"
|
||||
@submit="formSubmit"
|
||||
>
|
||||
<label
|
||||
class="label-info"
|
||||
:class="{ 'label-error': isEmailError }"
|
||||
for="email"
|
||||
>
|
||||
E-mail:
|
||||
</label>
|
||||
<input
|
||||
id="email"
|
||||
v-model="emailValue"
|
||||
class="contact_input"
|
||||
:class="{ 'contact_input-error': isEmailError }"
|
||||
type="text"
|
||||
name="email"
|
||||
placeholder="przemek.kowalski@gmail.com"
|
||||
>
|
||||
<span
|
||||
v-if="isEmailError"
|
||||
class="error-message"
|
||||
>
|
||||
E-mail musi być poprawny, np. przemek.kowalski@gmail.com
|
||||
</span>
|
||||
<label
|
||||
class="label-info"
|
||||
:class="{ 'label-error': isMessageError }"
|
||||
for="message"
|
||||
>
|
||||
Wiadomość:
|
||||
</label>
|
||||
<textarea
|
||||
id="message"
|
||||
v-model="messageValue"
|
||||
class="contact_input"
|
||||
:class="{ 'contact_input-error': isMessageError }"
|
||||
name="message"
|
||||
placeholder="Chciałbym zlecić wykonanie strony..."
|
||||
/>
|
||||
<span
|
||||
v-if="isMessageError"
|
||||
class="error-message"
|
||||
>
|
||||
Wiadomość musi zawierać przynajmniej 3 znaki!
|
||||
</span>
|
||||
<label
|
||||
class="label-info"
|
||||
:class="{ 'label-error': isSenderError }"
|
||||
for="sender"
|
||||
>
|
||||
Podpis nadawcy:
|
||||
</label>
|
||||
<input
|
||||
id="sender"
|
||||
v-model="senderValue"
|
||||
class="contact_input"
|
||||
:class="{ 'contact_input-error': isSenderError }"
|
||||
type="text"
|
||||
name="sender"
|
||||
placeholder="np. Przemek Kowalski"
|
||||
>
|
||||
<span
|
||||
v-if="isSenderError"
|
||||
class="error-message"
|
||||
>
|
||||
Podpis musi zawierać przynajmniej 3 znaki!
|
||||
</span>
|
||||
<BaseButton
|
||||
is-reverse
|
||||
:disabled="isButtonDisabled"
|
||||
>
|
||||
Wyślij
|
||||
</BaseButton>
|
||||
</form>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import BaseButton from '../../BaseButton'
|
||||
<script setup>
|
||||
import BaseButton from '../../buttons/BaseButton'
|
||||
import { ref, reactive, watch, computed } from 'vue'
|
||||
|
||||
function emailValidate (mailObj) {
|
||||
const mailFormat = /^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/
|
||||
return !!mailObj.value.match(mailFormat)
|
||||
return mailObj.match(mailFormat)
|
||||
}
|
||||
|
||||
async function postData (url = '', data = {}) {
|
||||
@@ -31,99 +115,89 @@ async function postData (url = '', data = {}) {
|
||||
return response.json()
|
||||
}
|
||||
|
||||
export default {
|
||||
name: 'MailContact',
|
||||
data () {
|
||||
return {
|
||||
buttonDisabled: false,
|
||||
statusError: 0,
|
||||
email: {},
|
||||
emailValue: '',
|
||||
message: {},
|
||||
messageValue: '',
|
||||
sender: {},
|
||||
senderValue: ''
|
||||
}
|
||||
},
|
||||
components: {
|
||||
'base-btn': BaseButton
|
||||
},
|
||||
watch: {
|
||||
emailValue (value) {
|
||||
if (this.statusError > 0 && emailValidate(this.email)) {
|
||||
this.email.classList.remove('contact_input-error')
|
||||
}
|
||||
},
|
||||
messageValue (value) {
|
||||
if (this.statusError > 0 && value !== '') {
|
||||
this.message.classList.remove('contact_input-error')
|
||||
}
|
||||
},
|
||||
senderValue (value) {
|
||||
if (this.statusError > 0 && value !== '') {
|
||||
this.sender.classList.remove('contact_input-error')
|
||||
}
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
clearErrors () {
|
||||
this.statusError = 0
|
||||
this.email.classList.remove('contact_input-error')
|
||||
this.message.classList.remove('contact_input-error')
|
||||
this.sender.classList.remove('contact_input-error')
|
||||
},
|
||||
checkForm () {
|
||||
if (!emailValidate(this.email)) {
|
||||
this.email.classList.add('contact_input-error')
|
||||
this.statusError++
|
||||
}
|
||||
if (this.message.value === '') {
|
||||
this.message.classList.add('contact_input-error')
|
||||
this.statusError++
|
||||
}
|
||||
if (this.sender.value === '') {
|
||||
this.sender.classList.add('contact_input-error')
|
||||
this.statusError++
|
||||
}
|
||||
},
|
||||
formSubmit (event) {
|
||||
event.preventDefault()
|
||||
const buttonDisabled = ref(false)
|
||||
const statusError = ref(0)
|
||||
const emailValue = ref('')
|
||||
const messageValue = ref('')
|
||||
const senderValue = ref('')
|
||||
|
||||
this.email = event.target[0]
|
||||
this.message = event.target[1]
|
||||
this.sender = event.target[2]
|
||||
const messageError = ref('')
|
||||
const messageOk = ref('')
|
||||
|
||||
this.clearErrors()
|
||||
this.checkForm()
|
||||
const errors = reactive({
|
||||
email: false,
|
||||
message: false,
|
||||
sender: false
|
||||
})
|
||||
|
||||
const messageElement = document.querySelector('.message')
|
||||
messageElement.classList.remove('message_ok', 'message_error')
|
||||
const isButtonDisabled = computed(() => buttonDisabled.value || errors.email || errors.message || errors.sender)
|
||||
const isEmailError = computed(() => errors.email)
|
||||
const isMessageError = computed(() => errors.message)
|
||||
const isSenderError = computed(() => errors.sender)
|
||||
const hasMessageError = computed(() => messageError.value)
|
||||
const hasMessageOkStatus = computed(() => messageOk.value && !hasMessageError.value)
|
||||
|
||||
if (this.statusError === 0) {
|
||||
this.buttonDisabled = true
|
||||
postData('/send', {
|
||||
email: this.emailValue,
|
||||
message: this.messageValue,
|
||||
sender: this.senderValue
|
||||
}).then(result => {
|
||||
messageElement.classList.add(
|
||||
result.error ? 'message_error' : 'message_ok'
|
||||
)
|
||||
watch([statusError, emailValue], ([errorCount, value]) => {
|
||||
errors.email = errorCount > 0 && !emailValidate(value)
|
||||
})
|
||||
|
||||
messageElement.textContent = result.message
|
||||
if (!result.error) {
|
||||
this.messageValue = ''
|
||||
this.emailValue = ''
|
||||
this.senderValue = ''
|
||||
}
|
||||
this.buttonDisabled = false
|
||||
}).catch(() => {
|
||||
messageElement.classList.add('message_error')
|
||||
messageElement.textContent = 'Wystąpił błąd podczas wysyłania wiadomości. Proszę spróbować później.'
|
||||
this.buttonDisabled = false
|
||||
})
|
||||
watch([statusError, messageValue], ([errorCount, value]) => {
|
||||
errors.message = errorCount > 0 && (value === '' || value.length < 3)
|
||||
})
|
||||
|
||||
watch([statusError, senderValue], ([errorCount, value]) => {
|
||||
errors.sender = errorCount > 0 && (value === '' || value.length < 3)
|
||||
})
|
||||
|
||||
function clearErrors() {
|
||||
statusError.value = 0
|
||||
errors.email = false
|
||||
errors.message = false
|
||||
errors.sender = false
|
||||
}
|
||||
|
||||
function checkForm() {
|
||||
clearErrors()
|
||||
if (!emailValidate(emailValue.value)) {
|
||||
errors.email = true
|
||||
statusError.value++
|
||||
}
|
||||
if (messageValue.value === '') {
|
||||
errors.message = true
|
||||
statusError.value++
|
||||
}
|
||||
if (senderValue.value === '') {
|
||||
errors.sender = true
|
||||
statusError.value++
|
||||
}
|
||||
}
|
||||
|
||||
function formSubmit(event) {
|
||||
event.preventDefault()
|
||||
|
||||
checkForm()
|
||||
|
||||
if (statusError.value === 0) {
|
||||
console.log('Send!')
|
||||
buttonDisabled.value = true
|
||||
postData('https://kamilcraft.com/send', {
|
||||
email: emailValue.value,
|
||||
message: messageValue.value,
|
||||
sender: senderValue.value
|
||||
}).then(result => {
|
||||
if (result.error) {
|
||||
messageError.value = result.message
|
||||
} else {
|
||||
messageOk.value = result.message
|
||||
messageValue.value = ''
|
||||
emailValue.value = ''
|
||||
senderValue.value = ''
|
||||
}
|
||||
}
|
||||
buttonDisabled.value = false
|
||||
}).catch(() => {
|
||||
messageError.value = 'Wystąpił błąd podczas wysyłania wiadomości. Proszę spróbować później.'
|
||||
buttonDisabled.value = false
|
||||
})
|
||||
}
|
||||
}
|
||||
</script>
|
||||
@@ -153,17 +227,35 @@ export default {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.contact_container input, .contact_container textarea {
|
||||
width: 97%;
|
||||
max-width: 97%;
|
||||
border: 0;
|
||||
border-bottom: 2px solid #c9c9c9;
|
||||
padding: 10px 10px 8px;
|
||||
font-size: 1em;
|
||||
font-family: var(--font-family);
|
||||
line-height: 1.3em;
|
||||
margin-bottom: 10px;
|
||||
border-radius: 5px;
|
||||
.contact_container {
|
||||
.label-info {
|
||||
width: 97%;
|
||||
padding-bottom: 5px;
|
||||
color: #7a7a7a;
|
||||
}
|
||||
|
||||
.error-message {
|
||||
width: 97%;
|
||||
padding: 5px 0 10px;
|
||||
color: #d44950;
|
||||
}
|
||||
|
||||
input, textarea {
|
||||
width: 97%;
|
||||
max-width: 97%;
|
||||
border: 0;
|
||||
border-bottom: 2px solid #c9c9c9;
|
||||
padding: 10px 10px 8px;
|
||||
font-size: 1em;
|
||||
font-family: var(--font-family);
|
||||
line-height: 1.3em;
|
||||
margin-bottom: 15px;
|
||||
border-radius: 5px;
|
||||
}
|
||||
}
|
||||
|
||||
.contact_input::placeholder {
|
||||
color: #bdbdbd;
|
||||
}
|
||||
|
||||
.contact_input:focus, .contact_input:focus {
|
||||
@@ -177,14 +269,14 @@ textarea.contact_input {
|
||||
min-height: 150px;
|
||||
}
|
||||
|
||||
.contact_input-error, .contact_input-error {
|
||||
background-color: #ffc3b0;
|
||||
border-color: #ff865f;
|
||||
color: #c90000;
|
||||
input.contact_input-error, textarea.contact_input-error {
|
||||
border-color: #d44950;
|
||||
color: #d44950;
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.contact_input-error::placeholder, .contact_input-error::placeholder {
|
||||
color: #c9000094;
|
||||
color: #d7626a;
|
||||
}
|
||||
|
||||
input[disabled].contact_input {
|
||||
@@ -211,8 +303,8 @@ input[disabled].contact_input {
|
||||
}
|
||||
|
||||
.message_error {
|
||||
background-color: #ffc3b0;
|
||||
border: 1px solid #ff865f;
|
||||
color: #c90000;
|
||||
background-color: #f8d7da;
|
||||
border: 1px solid #f5c6cb;
|
||||
color: #721c24;
|
||||
}
|
||||
</style>
|
||||
|
Reference in New Issue
Block a user