-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
113 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.