mirror of
https://github.com/sergeyyarkov/digital-store.git
synced 2026-10-11 10:19:15 +03:00
component orders
This commit is contained in:
parent
bb0e513f1a
commit
54f9bc9a61
@ -1,4 +1,5 @@
|
||||
const ObjectID = require('mongodb').ObjectID;
|
||||
const nodemailer = require('../nodemailer.config');
|
||||
|
||||
module.exports = function (server, db) {
|
||||
server.get('/', async (req, res) => {
|
||||
@ -121,4 +122,34 @@ module.exports = function (server, db) {
|
||||
res.status(500).render('main/404');
|
||||
});
|
||||
});
|
||||
server.post('/my-orders', async (req, res) => {
|
||||
const buyers = await db.collection('buyers').find({email: req.body.email}).toArray();
|
||||
if (buyers.length > 0) {
|
||||
// рендерим html
|
||||
const html = buyers.map((buyer) => {
|
||||
return `
|
||||
<b>Номер заказа: ${buyer.bill_id}</b><br>
|
||||
<b>Дата оплаты: ${new Date(buyer.date).toLocaleDateString()}</b><br>
|
||||
<b>Данные товара:</b><br>
|
||||
<p>${buyer.data.join('<br>')}</p>
|
||||
<p>======================================================================</p>
|
||||
`;
|
||||
});
|
||||
|
||||
// высылаем данные покупателю
|
||||
try {
|
||||
await nodemailer.transporter.sendMail({
|
||||
from: `"Ваши покупки." <${process.env.EMAIL_LOGIN}>`,
|
||||
to: req.body.email,
|
||||
subject: "Данные купленных товаров находятся в этом письме.",
|
||||
html: html.join('')
|
||||
});
|
||||
res.json({status: 'OK'});
|
||||
} catch (error) {
|
||||
res.json({status: 'ERR'});
|
||||
}
|
||||
} else {
|
||||
res.json({status: 'NF'});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
3
dist/views/main/my-orders.pug
vendored
3
dist/views/main/my-orders.pug
vendored
@ -6,6 +6,7 @@ include ./assets/templates/header.pug
|
||||
main.main-section
|
||||
include ./assets/templates/modals/cart.pug
|
||||
include ./assets/templates/modals/pay.pug
|
||||
#notification
|
||||
.container
|
||||
.row
|
||||
.col.s12
|
||||
@ -22,7 +23,7 @@ main.main-section
|
||||
form(action='#')
|
||||
.email-field(style='max-width: 400px;margin-bottom: 10px;')
|
||||
label(for='email') Почта
|
||||
input#email(required='' type='text' name='email')
|
||||
input#email(required='' type='email' name='email')
|
||||
.show-orders
|
||||
button.waves-effect.waves-light.btn(type='submit') Узнать
|
||||
|
||||
|
||||
@ -5,16 +5,16 @@ export class NotificationComponent extends Component {
|
||||
super(id);
|
||||
}
|
||||
|
||||
notificate(text, color) {
|
||||
notificate(text, color, delay) {
|
||||
const card = document.createElement('div');
|
||||
card.classList.add('card-panel');
|
||||
card.style.backgroundColor = color;
|
||||
card.innerHTML = `<span class="white-text">${text}</span>`;
|
||||
this.$el.innerHTML += card.outerHTML;
|
||||
this.removeNotificate();
|
||||
this.removeNotificate(delay);
|
||||
}
|
||||
|
||||
removeNotificate() {
|
||||
removeNotificate(delay) {
|
||||
// Полифилл для IE ^9
|
||||
(function() {
|
||||
var arr = [window.Element, window.CharacterData, window.DocumentType];
|
||||
@ -45,6 +45,6 @@ export class NotificationComponent extends Component {
|
||||
|
||||
setTimeout(() => {
|
||||
this.$el.querySelector('.card-panel').remove();
|
||||
}, 2000)
|
||||
}, delay || 2000);
|
||||
}
|
||||
}
|
||||
40
src/main/components/orders.component.js
Normal file
40
src/main/components/orders.component.js
Normal file
@ -0,0 +1,40 @@
|
||||
import { Component } from "../core/component";
|
||||
import axios from "axios";
|
||||
|
||||
export class OrdersComponent extends Component {
|
||||
constructor(id, {notification}) {
|
||||
super(id);
|
||||
this.notification = notification;
|
||||
}
|
||||
|
||||
init() {
|
||||
if (this.$el) {
|
||||
let valid = true;
|
||||
this.$el.querySelector('form').onsubmit = async (e) => {
|
||||
e.preventDefault();
|
||||
if (valid) {
|
||||
valid = false;
|
||||
const email = e.target.elements.email.value,
|
||||
request = await axios.post('/my-orders', {email});
|
||||
|
||||
switch (request.data.status) {
|
||||
case 'OK':
|
||||
this.notification.notificate(`Данные были успешно высланы на почту "${email}".`, '#26A69A', 3500);
|
||||
break;
|
||||
case 'NF':
|
||||
this.notification.notificate(`Покупатель с почтой ${email} не был найден.`, '#f44336', 3500);
|
||||
break;
|
||||
case 'ERR':
|
||||
this.notification.notificate(`Произошла ошибка, попробуйте повторить запрос позже.`, '#f44336', 3500);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
setTimeout(() => valid = true, 10000);
|
||||
} else {
|
||||
this.notification.notificate(`Подождите еще нескольно секунд прежде чем сделать запрос...`, '#f44336', 3500);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -9,6 +9,7 @@ import { CartModal } from "./modals/cart.modal.js";
|
||||
import { PayModal } from "./modals/pay.modal.js";
|
||||
import { NotificationComponent } from "./components/notification.component.js";
|
||||
import { MaterializeConfig } from "./config/materialize.config.js";
|
||||
import { OrdersComponent } from "./components/orders.component.js";
|
||||
|
||||
// Components
|
||||
const header = new HeaderComponent('header');
|
||||
@ -19,6 +20,7 @@ const items = new ItemsComponent('items', {loader}, {notification});
|
||||
const filter = new FilterComponent('filter', {items});
|
||||
const category = new CategoryComponent('category', {items}, {filter});
|
||||
const breadcumb = new BreadcrumbComponent('breadcrumb');
|
||||
const orders = new OrdersComponent('orders', {notification});
|
||||
|
||||
// Modals
|
||||
const cart = new CartModal('cart', 'cartOpen', 'cartClose', {items});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user