component orders

This commit is contained in:
Sergey Yarkov 2020-01-19 21:25:29 +03:00
parent bb0e513f1a
commit 54f9bc9a61
5 changed files with 79 additions and 5 deletions

View File

@ -1,4 +1,5 @@
const ObjectID = require('mongodb').ObjectID; const ObjectID = require('mongodb').ObjectID;
const nodemailer = require('../nodemailer.config');
module.exports = function (server, db) { module.exports = function (server, db) {
server.get('/', async (req, res) => { server.get('/', async (req, res) => {
@ -121,4 +122,34 @@ module.exports = function (server, db) {
res.status(500).render('main/404'); 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'});
}
});
} }

View File

@ -6,6 +6,7 @@ include ./assets/templates/header.pug
main.main-section main.main-section
include ./assets/templates/modals/cart.pug include ./assets/templates/modals/cart.pug
include ./assets/templates/modals/pay.pug include ./assets/templates/modals/pay.pug
#notification
.container .container
.row .row
.col.s12 .col.s12
@ -22,7 +23,7 @@ main.main-section
form(action='#') form(action='#')
.email-field(style='max-width: 400px;margin-bottom: 10px;') .email-field(style='max-width: 400px;margin-bottom: 10px;')
label(for='email') Почта label(for='email') Почта
input#email(required='' type='text' name='email') input#email(required='' type='email' name='email')
.show-orders .show-orders
button.waves-effect.waves-light.btn(type='submit') Узнать button.waves-effect.waves-light.btn(type='submit') Узнать

View File

@ -5,16 +5,16 @@ export class NotificationComponent extends Component {
super(id); super(id);
} }
notificate(text, color) { notificate(text, color, delay) {
const card = document.createElement('div'); const card = document.createElement('div');
card.classList.add('card-panel'); card.classList.add('card-panel');
card.style.backgroundColor = color; card.style.backgroundColor = color;
card.innerHTML = `<span class="white-text">${text}</span>`; card.innerHTML = `<span class="white-text">${text}</span>`;
this.$el.innerHTML += card.outerHTML; this.$el.innerHTML += card.outerHTML;
this.removeNotificate(); this.removeNotificate(delay);
} }
removeNotificate() { removeNotificate(delay) {
// Полифилл для IE ^9 // Полифилл для IE ^9
(function() { (function() {
var arr = [window.Element, window.CharacterData, window.DocumentType]; var arr = [window.Element, window.CharacterData, window.DocumentType];
@ -45,6 +45,6 @@ export class NotificationComponent extends Component {
setTimeout(() => { setTimeout(() => {
this.$el.querySelector('.card-panel').remove(); this.$el.querySelector('.card-panel').remove();
}, 2000) }, delay || 2000);
} }
} }

View 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);
}
};
}
}
}

View File

@ -9,6 +9,7 @@ import { CartModal } from "./modals/cart.modal.js";
import { PayModal } from "./modals/pay.modal.js"; import { PayModal } from "./modals/pay.modal.js";
import { NotificationComponent } from "./components/notification.component.js"; import { NotificationComponent } from "./components/notification.component.js";
import { MaterializeConfig } from "./config/materialize.config.js"; import { MaterializeConfig } from "./config/materialize.config.js";
import { OrdersComponent } from "./components/orders.component.js";
// Components // Components
const header = new HeaderComponent('header'); const header = new HeaderComponent('header');
@ -19,6 +20,7 @@ const items = new ItemsComponent('items', {loader}, {notification});
const filter = new FilterComponent('filter', {items}); const filter = new FilterComponent('filter', {items});
const category = new CategoryComponent('category', {items}, {filter}); const category = new CategoryComponent('category', {items}, {filter});
const breadcumb = new BreadcrumbComponent('breadcrumb'); const breadcumb = new BreadcrumbComponent('breadcrumb');
const orders = new OrdersComponent('orders', {notification});
// Modals // Modals
const cart = new CartModal('cart', 'cartOpen', 'cartClose', {items}); const cart = new CartModal('cart', 'cartOpen', 'cartClose', {items});