IMPR: AJAX processing

This commit is contained in:
Tony Air 2024-02-02 23:24:41 +02:00
parent d2e0dbb6fd
commit 755457b96a
4 changed files with 102 additions and 24 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@a2nt/ss-bootstrap-ui-webpack-boilerplate-react", "name": "@a2nt/ss-bootstrap-ui-webpack-boilerplate-react",
"version": "5.2.6", "version": "5.2.7",
"description": "This UI Kit allows you to build Bootstrap 5 webapp with some extra UI features. It's easy to extend and easy to convert HTML templates to CMS templates.", "description": "This UI Kit allows you to build Bootstrap 5 webapp with some extra UI features. It's easy to extend and easy to convert HTML templates to CMS templates.",
"author": "Tony Air <tony@twma.pro>", "author": "Tony Air <tony@twma.pro>",
"license": "BSD-2-Clause", "license": "BSD-2-Clause",

View File

@ -21,6 +21,11 @@ const submitForm = (e) => {
data.append('formid', id) data.append('formid', id)
data.append('ajax', '1') data.append('ajax', '1')
form.querySelectorAll('.field__alert').forEach((el) => {
el.remove()
})
parent.classList.remove('loaded') parent.classList.remove('loaded')
parent.classList.add('loading') parent.classList.add('loading')
@ -33,28 +38,97 @@ const submitForm = (e) => {
body: data, body: data,
}) })
.then(async (resp) => { .then(async (resp) => {
if (!resp.ok) {
console.error(`${NAME}: BAD RESPONSE`)
console.log(resp)
return
}
const body = resp.text().then((html) => { const body = resp.text().then((html) => {
try { const result = processResponse(html)
let json = JSON.parse(html) if (result.json) {
return formProcessJson(form, result.data)
if (typeof json.MainContent !== undefined) {
json = JSON.parse(json.MainContent)
}
console.log(`${NAME}: JSON response`)
const status = json.status === 'good' ? 'success' : 'error'
replaceForm(form, `<div class="alert alert-${status}">${json.message}</div>`)
} catch (e) {
console.log(`${NAME}: HTML response`)
replaceForm(form, html)
parent.querySelector('form').addEventListener('submit', submitForm)
} }
return replaceForm(form, result.data)
}) })
}) })
} }
const processResponse = (html) => {
try {
let json = JSON.parse(html)
if (json.MainContent) {
try {
json = JSON.parse(json.MainContent)
return {
json: true,
data: json
}
} catch (e) {
return {
json: false,
data: json.MainContent
}
}
}
return {
json: true,
data: json
}
} catch (e) {
return {
json: false,
data: html
}
}
}
const formProcessJson = (form, json) => {
const status = json.status === 'good' ? 'success' : 'danger'
if (json.msgs) {
json.msgs.forEach((i) => {
const field = form.querySelector('[name="' + i.fieldName + '"],[name^="' + i.fieldName + '["]')
if (field) {
field.classList.add('error')
const fieldContainer = field.closest('.form__field')
if (fieldContainer) {
fieldContainer.classList.add('error')
const msg = document.createElement('div')
msg.classList.add(...['field__alert', 'alert', `alert-${status}`, `alert-${i.messageCast}`, `${i.messageType}`])
msg.innerHTML = i.message
fieldContainer.appendChild(msg)
}
}
})
return setLoaded(form)
}
return replaceForm(form, `<div class="alert alert-${status}">${json.message}</div>`)
}
const setLoaded = (form) => {
const parent = form.parentElement;
parent.classList.remove('loading')
parent.classList.add('loaded')
const btns = form.querySelectorAll('input[type="submit"],button')
btns.forEach((el) => {
el.removeAttribute('disabled', 'disabled')
})
window.dispatchEvent(new Event(`${Events.AJAX}`))
}
const replaceForm = (form, html) => { const replaceForm = (form, html) => {
const parent = form.parentElement; const parent = form.parentElement;
@ -62,8 +136,11 @@ const replaceForm = (form, html) => {
parent.classList.remove('loading') parent.classList.remove('loading')
parent.classList.add('loaded') parent.classList.add('loaded')
window.dispatchEvent(new Event(`${Events.AJAX}`)) const newForm = parent.querySelector('form')
return if (newForm) {
newForm.addEventListener('submit', submitForm)
return setLoaded(newForm)
}
} }
const formInit = (form) => { const formInit = (form) => {

View File

@ -10,7 +10,7 @@
display: inline-flex; display: inline-flex;
padding-right: $form-spacer-x; padding-right: $form-spacer-x;
& + .field__content { &+.field__content {
padding-left: $form-spacer-x; padding-left: $form-spacer-x;
} }
@ -52,5 +52,5 @@
} }
.form__fieldset { .form__fieldset {
margin-bottom: -1rem; margin-bottom: -1rem;
} }

View File

@ -29,6 +29,7 @@
} }
} }
.alert-bad { .alert-bad,
.alert-error {
@extend .alert-danger; @extend .alert-danger;
} }