Skip to content

Commit

Permalink
решение js конфликтоов
Browse files Browse the repository at this point in the history
  • Loading branch information
biz87 committed Apr 22, 2024
1 parent d63edcb commit fe1c021
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 122 deletions.
90 changes: 41 additions & 49 deletions assets/components/minishop3/js/web/modules/order.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,42 @@
ms3.order = {
init () {
const orderForms = document.querySelectorAll('.ms3_order_form')
if (orderForms.length > 0) {
orderForms.forEach(orderForm => {
ms3.order.formListener(orderForm)
})
}
},
formListener (customerForm) {
const inputs = customerForm.querySelectorAll('input, textarea, select')
if (inputs.length > 0) {
inputs.forEach(input => {
ms3.order.changeInputListener(input)
})
}
},
changeInputListener (input) {
input.addEventListener('change', async () => {
const form = input.closest('.ms3_order_form')
const parent = input.closest('div')
parent.classList.remove('was-validated')
input.classList.remove('is-invalid')
parent.querySelector('.invalid-feedback').textContent = ''
const formData = new FormData()
formData.append('key', input.name)
formData.append('value', input.value)
const response = await ms3.order.add(formData)
if (response.success === true) {
parent.classList.add('was-validated')
//TODO не менять radio, checkbox, select
input.value = response.data[input.name]
} else {
parent.classList.add('was-validated')
input.classList.add('is-invalid')
parent.querySelector('.invalid-feedback').textContent = response.message
}
})
},
async add (formData) {
formData.append('ms3_action', 'order/add')
const response = await ms3.request.send(formData)
//TODO callback, event
return response
},
async remove (formData) {
formData.append('ms3_action', 'order/remove')
const response = await ms3.request.send(formData)
},
}
init () {
const orderForms = document.querySelectorAll('.ms3_order_form')
orderForms.forEach(orderForm => ms3.order.formListener(orderForm))
},
formListener (customerForm) {
const inputs = customerForm.querySelectorAll('input, textarea, select')
inputs.forEach(input => ms3.order.changeInputListener(input))
},
changeInputListener (input) {
input.addEventListener('change', async () => {
// const form = input.closest('.ms3_order_form')
const parent = input.closest('div')
parent.classList.remove('was-validated')
input.classList.remove('is-invalid')
parent.querySelector('.invalid-feedback').textContent = ''
const formData = new FormData()
formData.append('key', input.name)
formData.append('value', input.value)
const response = await ms3.order.add(formData)
if (response.success === true) {
parent.classList.add('was-validated')
// TODO не менять radio, checkbox, select
input.value = response.data[input.name]
} else {
parent.classList.add('was-validated')
input.classList.add('is-invalid')
parent.querySelector('.invalid-feedback').textContent = response.message
}
})
},
async add (formData) {
formData.append('ms3_action', 'order/add')
const response = await ms3.request.send(formData)
// TODO callback, event
return response
},
async remove (formData) {
formData.append('ms3_action', 'order/remove')
await ms3.request.send(formData)
}
}
141 changes: 70 additions & 71 deletions assets/components/minishop3/js/web/ms3.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,80 @@
const ms3 = {
config: {},
init () {
this.config = window.ms3Config
this.checkToken()
ms3.form.init()
ms3.cart.init()
ms3.customer.init()
ms3.order.init()
},
checkToken () {
let ms3TokenJson = localStorage.getItem(ms3.config.tokenName)
if (ms3TokenJson === null) {
ms3.setToken()
return false
}
config: {},
init () {
this.config = window.ms3Config
this.checkToken()
ms3.form.init()
ms3.cart.init()
ms3.customer.init()
ms3.order.init()
},
checkToken () {
const ms3Token = localStorage.getItem(ms3.config.tokenName)
if (ms3Token === null) {
ms3.setToken()
return false
}

const isJson = ms3.isJson(ms3TokenJson)
if (!isJson) {
localStorage.removeItem(ms3.config.tokenName)
ms3.setToken()
return false
}
if (!ms3.isJSON(ms3Token)) {
localStorage.removeItem(ms3.config.tokenName)
ms3.setToken()
return false
}

const ms3TokenData = JSON.parse(ms3TokenJson)
const now = new Date()
if (now.getTime() > parseInt(ms3TokenData.expiry)) {
localStorage.removeItem(ms3.config.tokenName)
ms3.setToken()
} else {
ms3.updateToken()
}
},
async setToken () {
this.request.setHeaders()
const formData = new FormData()
formData.append('ms3_action', 'customer/token/get')
const response = await this.request.get(formData)
if (response.success === true) {
const now = new Date()
const tokenData = {
token: response.data.token,
expiry: now.getTime() + parseInt(response.data.lifetime)
}
localStorage.setItem(ms3.config.tokenName, JSON.stringify(tokenData))
}
},
async updateToken () {
this.request.setHeaders()
const formData = new FormData()
formData.append('ms3_action', 'customer/token/update')
const response = await this.request.post(formData)
if (response.success === true) {
const now = new Date()
const tokenData = {
token: response.data.token,
expiry: now.getTime() + parseInt(response.data.lifetime)
}
localStorage.setItem(ms3.config.tokenName, JSON.stringify(tokenData))
}
},
isJson (str) {
try {
JSON.parse(str)
} catch (e) {
return false
}
return true
const ms3TokenData = JSON.parse(ms3Token)
const now = new Date()
if (now.getTime() > parseInt(ms3TokenData.expiry)) {
localStorage.removeItem(ms3.config.tokenName)
ms3.setToken()
} else {
ms3.updateToken()
}
},
async setToken () {
this.request.setHeaders()
const formData = new FormData()
formData.append('ms3_action', 'customer/token/get')
const response = await this.request.get(formData)
if (response.success === true) {
const now = new Date()
const tokenData = {
token: response.data.token,
expiry: now.getTime() + parseInt(response.data.lifetime)
}
localStorage.setItem(ms3.config.tokenName, JSON.stringify(tokenData))
}
},
async updateToken () {
this.request.setHeaders()
const formData = new FormData()
formData.append('ms3_action', 'customer/token/update')
const response = await this.request.post(formData)
if (response.success === true) {
const now = new Date()
const tokenData = {
token: response.data.token,
expiry: now.getTime() + parseInt(response.data.lifetime)
}
localStorage.setItem(ms3.config.tokenName, JSON.stringify(tokenData))
}
},
isJSON (str) {
try {
JSON.parse(str)
} catch (e) {
return false
}
return true
}
}

document.addEventListener('DOMContentLoaded', () => {
ms3.init()
ms3.init()
})

document.addEventListener('ms3_send_success', () => {
//Время на перерисовку DOM
setTimeout(() => {
ms3.cart.init()
}, 300)
})
// Время на перерисовку DOM
setTimeout(() => {
ms3.cart.init()
}, 300)
})
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fe1c021

Please sign in to comment.