mirror of
https://github.com/sergeyyarkov/digital-store.git
synced 2026-10-11 02:09:05 +03:00
dashboard component & fixes
This commit is contained in:
parent
fed81f95f6
commit
8ba91f1c9d
@ -61,6 +61,17 @@ module.exports = function(server, db) {
|
||||
res.render('main/404');
|
||||
}
|
||||
});
|
||||
|
||||
server.get('/api/buyers/page/:page', checkAuthenticated, async (req, res) => {
|
||||
try {
|
||||
const page = parseInt(req.params.page),
|
||||
limit = 5,
|
||||
items = await db.collection('buyers').find({}, {skip: limit * (page - 1), limit}).toArray();
|
||||
items.length > 0 && !isNaN(page) ? res.json(items) : res.json({error: 'Такой страницы не существует.'});
|
||||
} catch (error) {
|
||||
res.render('main/404');
|
||||
}
|
||||
});
|
||||
|
||||
server.get('/api/items/:category', async (req, res) => {
|
||||
const items = await db.collection('items').find({category: req.params.category}).toArray(),
|
||||
|
||||
@ -14,8 +14,24 @@ module.exports = function (server, db) {
|
||||
buyers = await db.collection('buyers').find({}).toArray(),
|
||||
items = await db.collection('items').find({}).toArray();
|
||||
|
||||
|
||||
// считаем сумму проданных товаров
|
||||
const sum = () => {
|
||||
let result = 0;
|
||||
for (let i = 0; i < buyers.length; i++) {
|
||||
result += buyers[i].amount;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// считаем кол-во проданных товаров
|
||||
const quantitySold = () => {
|
||||
let result = 0;
|
||||
for (let i = 0; i < buyers.length; i++) {
|
||||
result += buyers[i].data.length;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
res.render('admin/control-panel', {
|
||||
id: req.user.id,
|
||||
name: req.user.name,
|
||||
@ -23,11 +39,12 @@ module.exports = function (server, db) {
|
||||
title: `${store.title} | Панель управления сайтом`,
|
||||
host: req.headers.host,
|
||||
pageName: ['Главная', 'main'],
|
||||
sum: buyers.reduce((sum, current) => sum.amount + current.amount),
|
||||
quantitySold: buyers.reduce((summ, current) => summ.data.length + current.data.length),
|
||||
sum: sum(),
|
||||
quantitySold: quantitySold(),
|
||||
count: items.length
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.render('main/404');
|
||||
}
|
||||
});
|
||||
|
||||
@ -26,7 +26,7 @@ module.exports = function(server, db, qiwiApi) {
|
||||
const item = await db.collection('info').findOne({"_id": ObjectID(ids[i])}),
|
||||
data = item.data;
|
||||
|
||||
result.push(data.splice(-1, 1));
|
||||
result.push(data.splice(-1, 1).join(''));
|
||||
db.collection('info').updateOne({"_id": ObjectID(ids[i])}, {$set: {data: data}});
|
||||
}
|
||||
|
||||
|
||||
2
dist/views/admin/assets/templates/header.pug
vendored
2
dist/views/admin/assets/templates/header.pug
vendored
@ -28,7 +28,7 @@ header.admin-header
|
||||
li#currentPage(data-current=pageName[1])
|
||||
a(href="#" class="subheader") Управление сайтом
|
||||
li.no-padding
|
||||
li#dashboard.active
|
||||
li#home.active
|
||||
a(href="/control-panel" class="collapsible-header waves-effect active")
|
||||
i.material-icons dashboard
|
||||
| Статистика
|
||||
|
||||
2
dist/views/admin/control-panel.pug
vendored
2
dist/views/admin/control-panel.pug
vendored
@ -25,7 +25,7 @@ body.has-fixed-sidenav
|
||||
.divider
|
||||
h3 #{count}
|
||||
.col.s12
|
||||
.card
|
||||
#dashboard.card
|
||||
.card-content
|
||||
span.card-title Список покупателей
|
||||
.search-field
|
||||
|
||||
21
src/admin/components/dashboard.component.js
Normal file
21
src/admin/components/dashboard.component.js
Normal file
@ -0,0 +1,21 @@
|
||||
import { Component } from "../core/component";
|
||||
import { apiService } from "../services/api.service";
|
||||
|
||||
export class DashboardComponent extends Component {
|
||||
constructor(id, {pagination}, {search}) {
|
||||
super(id);
|
||||
this.pagination = pagination;
|
||||
this.search = search;
|
||||
}
|
||||
|
||||
init() {
|
||||
this.$table = this.$el.querySelector('table tbody');
|
||||
this.getBuyersByPage(1);
|
||||
}
|
||||
|
||||
async getBuyersByPage(page) {
|
||||
const buyers = await apiService.getBuyersOffset(page);
|
||||
console.log(buyers);
|
||||
// рендерим дальше
|
||||
}
|
||||
}
|
||||
@ -39,7 +39,7 @@ export class MaterializeConfig {
|
||||
pages.forEach(function (page) {
|
||||
if (page.dataset.page) {
|
||||
if (currentPage === page.dataset.page) {
|
||||
document.querySelector('#dashboard').classList.remove('active');
|
||||
document.querySelector('#home').classList.remove('active');
|
||||
page.classList.add('active');
|
||||
const collapse = M.Collapsible.getInstance(page.closest('.collapsible'));
|
||||
collapse.open();
|
||||
|
||||
@ -10,6 +10,7 @@ import { ItemDeleteModal } from "./modals/item-delete.modal.js";
|
||||
import { CategoryEditModal } from "./modals/category-edit.modal";
|
||||
import { ContentComponent } from "./components/content.component.js";
|
||||
import { AdministratorComponent } from "./components/administrator.component.js";
|
||||
import { DashboardComponent } from "./components/dashboard.component.js";
|
||||
|
||||
// Components
|
||||
const loader = new LoaderComponent('loader');
|
||||
@ -18,6 +19,7 @@ const search = new SearchComponent('search', {loader});
|
||||
const categories = new CategoriesComponent('categories');
|
||||
const icons = new IconsComponent('icons');
|
||||
const items = new ItemsComponent('items', {pagination}, {search});
|
||||
const dashboards = new DashboardComponent('dashboard', {pagination}, {search});
|
||||
const content = new ContentComponent('content');
|
||||
const administrator = new AdministratorComponent('administrator');
|
||||
|
||||
@ -27,4 +29,4 @@ const itemDelete = new ItemDeleteModal('deleteItemModal', 'edit', 'deleteItemClo
|
||||
const categoryEdit = new CategoryEditModal('editCategoryModal', 'editCategory_select', 'editCategoryClose');
|
||||
|
||||
const materialize = new MaterializeConfig();
|
||||
console.log('Control-Panel v1.0.01');
|
||||
console.log('Control-Panel v1.0.0');
|
||||
@ -79,6 +79,15 @@ class ApiService {
|
||||
}
|
||||
}
|
||||
|
||||
async getBuyersOffset(page) {
|
||||
try {
|
||||
const request = `${this.url}/api/buyers/page/${page}`;
|
||||
return useRequest(request);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
async getIcons() {
|
||||
try {
|
||||
const request = `${this.url}/api/icons`;
|
||||
@ -94,4 +103,4 @@ async function useRequest(request) {
|
||||
return await JSON.parse(response.request.response);
|
||||
}
|
||||
|
||||
export const apiService = new ApiService('https://polar-peak-95205.herokuapp.com');
|
||||
export const apiService = new ApiService('http://localhost:3000');
|
||||
Loading…
x
Reference in New Issue
Block a user