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,26 +38,95 @@ 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) => {
const result = processResponse(html)
if (result.json) {
return formProcessJson(form, result.data)
}
return replaceForm(form, result.data)
})
})
}
const processResponse = (html) => {
try { try {
let json = JSON.parse(html) let json = JSON.parse(html)
if (typeof json.MainContent !== undefined) { if (json.MainContent) {
try {
json = JSON.parse(json.MainContent) json = JSON.parse(json.MainContent)
return {
json: true,
data: json
} }
console.log(`${NAME}: JSON response`)
const status = json.status === 'good' ? 'success' : 'error'
replaceForm(form, `<div class="alert alert-${status}">${json.message}</div>`)
} catch (e) { } catch (e) {
console.log(`${NAME}: HTML response`) return {
replaceForm(form, html) json: false,
data: json.MainContent
}
}
}
parent.querySelector('form').addEventListener('submit', submitForm) 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) => {
@ -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;
} }

View File

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