Compare commits
No commits in common. "ai-dev" and "master" have entirely different histories.
5
.gitignore
vendored
5
.gitignore
vendored
@ -1,8 +1,3 @@
|
|||||||
node_modules/
|
node_modules/
|
||||||
*/node_modules/
|
*/node_modules/
|
||||||
*/build/
|
*/build/
|
||||||
|
|
||||||
**/node_modules/
|
|
||||||
**/build/
|
|
||||||
.DS_Store
|
|
||||||
.env
|
|
||||||
File diff suppressed because one or more lines are too long
457
backend/src/db/api/conversions.js
Normal file
457
backend/src/db/api/conversions.js
Normal file
@ -0,0 +1,457 @@
|
|||||||
|
const db = require('../models');
|
||||||
|
const FileDBApi = require('./file');
|
||||||
|
const crypto = require('crypto');
|
||||||
|
const Utils = require('../utils');
|
||||||
|
|
||||||
|
const Sequelize = db.Sequelize;
|
||||||
|
const Op = Sequelize.Op;
|
||||||
|
|
||||||
|
module.exports = class ConversionsDBApi {
|
||||||
|
static async create(data, options) {
|
||||||
|
const currentUser = (options && options.currentUser) || { id: null };
|
||||||
|
const transaction = (options && options.transaction) || undefined;
|
||||||
|
|
||||||
|
const conversions = await db.conversions.create(
|
||||||
|
{
|
||||||
|
id: data.id || undefined,
|
||||||
|
|
||||||
|
input_value: data.input_value || null,
|
||||||
|
input_unit: data.input_unit || null,
|
||||||
|
output_value_btu_h: data.output_value_btu_h || null,
|
||||||
|
output_value_kw: data.output_value_kw || null,
|
||||||
|
output_value_kcal_h: data.output_value_kcal_h || null,
|
||||||
|
output_value_tr: data.output_value_tr || null,
|
||||||
|
output_value_hp: data.output_value_hp || null,
|
||||||
|
output_value_w: data.output_value_w || null,
|
||||||
|
importHash: data.importHash || null,
|
||||||
|
createdById: currentUser.id,
|
||||||
|
updatedById: currentUser.id,
|
||||||
|
},
|
||||||
|
{ transaction },
|
||||||
|
);
|
||||||
|
|
||||||
|
return conversions;
|
||||||
|
}
|
||||||
|
|
||||||
|
static async bulkImport(data, options) {
|
||||||
|
const currentUser = (options && options.currentUser) || { id: null };
|
||||||
|
const transaction = (options && options.transaction) || undefined;
|
||||||
|
|
||||||
|
// Prepare data - wrapping individual data transformations in a map() method
|
||||||
|
const conversionsData = data.map((item, index) => ({
|
||||||
|
id: item.id || undefined,
|
||||||
|
|
||||||
|
input_value: item.input_value || null,
|
||||||
|
input_unit: item.input_unit || null,
|
||||||
|
output_value_btu_h: item.output_value_btu_h || null,
|
||||||
|
output_value_kw: item.output_value_kw || null,
|
||||||
|
output_value_kcal_h: item.output_value_kcal_h || null,
|
||||||
|
output_value_tr: item.output_value_tr || null,
|
||||||
|
output_value_hp: item.output_value_hp || null,
|
||||||
|
output_value_w: item.output_value_w || null,
|
||||||
|
importHash: item.importHash || null,
|
||||||
|
createdById: currentUser.id,
|
||||||
|
updatedById: currentUser.id,
|
||||||
|
createdAt: new Date(Date.now() + index * 1000),
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Bulk create items
|
||||||
|
const conversions = await db.conversions.bulkCreate(conversionsData, {
|
||||||
|
transaction,
|
||||||
|
});
|
||||||
|
|
||||||
|
// For each item created, replace relation files
|
||||||
|
|
||||||
|
return conversions;
|
||||||
|
}
|
||||||
|
|
||||||
|
static async update(id, data, options) {
|
||||||
|
const currentUser = (options && options.currentUser) || { id: null };
|
||||||
|
const transaction = (options && options.transaction) || undefined;
|
||||||
|
|
||||||
|
const conversions = await db.conversions.findByPk(id, {}, { transaction });
|
||||||
|
|
||||||
|
const updatePayload = {};
|
||||||
|
|
||||||
|
if (data.input_value !== undefined)
|
||||||
|
updatePayload.input_value = data.input_value;
|
||||||
|
|
||||||
|
if (data.input_unit !== undefined)
|
||||||
|
updatePayload.input_unit = data.input_unit;
|
||||||
|
|
||||||
|
if (data.output_value_btu_h !== undefined)
|
||||||
|
updatePayload.output_value_btu_h = data.output_value_btu_h;
|
||||||
|
|
||||||
|
if (data.output_value_kw !== undefined)
|
||||||
|
updatePayload.output_value_kw = data.output_value_kw;
|
||||||
|
|
||||||
|
if (data.output_value_kcal_h !== undefined)
|
||||||
|
updatePayload.output_value_kcal_h = data.output_value_kcal_h;
|
||||||
|
|
||||||
|
if (data.output_value_tr !== undefined)
|
||||||
|
updatePayload.output_value_tr = data.output_value_tr;
|
||||||
|
|
||||||
|
if (data.output_value_hp !== undefined)
|
||||||
|
updatePayload.output_value_hp = data.output_value_hp;
|
||||||
|
|
||||||
|
if (data.output_value_w !== undefined)
|
||||||
|
updatePayload.output_value_w = data.output_value_w;
|
||||||
|
|
||||||
|
updatePayload.updatedById = currentUser.id;
|
||||||
|
|
||||||
|
await conversions.update(updatePayload, { transaction });
|
||||||
|
|
||||||
|
return conversions;
|
||||||
|
}
|
||||||
|
|
||||||
|
static async deleteByIds(ids, options) {
|
||||||
|
const currentUser = (options && options.currentUser) || { id: null };
|
||||||
|
const transaction = (options && options.transaction) || undefined;
|
||||||
|
|
||||||
|
const conversions = await db.conversions.findAll({
|
||||||
|
where: {
|
||||||
|
id: {
|
||||||
|
[Op.in]: ids,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
transaction,
|
||||||
|
});
|
||||||
|
|
||||||
|
await db.sequelize.transaction(async (transaction) => {
|
||||||
|
for (const record of conversions) {
|
||||||
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
||||||
|
}
|
||||||
|
for (const record of conversions) {
|
||||||
|
await record.destroy({ transaction });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return conversions;
|
||||||
|
}
|
||||||
|
|
||||||
|
static async remove(id, options) {
|
||||||
|
const currentUser = (options && options.currentUser) || { id: null };
|
||||||
|
const transaction = (options && options.transaction) || undefined;
|
||||||
|
|
||||||
|
const conversions = await db.conversions.findByPk(id, options);
|
||||||
|
|
||||||
|
await conversions.update(
|
||||||
|
{
|
||||||
|
deletedBy: currentUser.id,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
transaction,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
await conversions.destroy({
|
||||||
|
transaction,
|
||||||
|
});
|
||||||
|
|
||||||
|
return conversions;
|
||||||
|
}
|
||||||
|
|
||||||
|
static async findBy(where, options) {
|
||||||
|
const transaction = (options && options.transaction) || undefined;
|
||||||
|
|
||||||
|
const conversions = await db.conversions.findOne(
|
||||||
|
{ where },
|
||||||
|
{ transaction },
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!conversions) {
|
||||||
|
return conversions;
|
||||||
|
}
|
||||||
|
|
||||||
|
const output = conversions.get({ plain: true });
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
static async findAll(filter, options) {
|
||||||
|
const limit = filter.limit || 0;
|
||||||
|
let offset = 0;
|
||||||
|
let where = {};
|
||||||
|
const currentPage = +filter.page;
|
||||||
|
|
||||||
|
offset = currentPage * limit;
|
||||||
|
|
||||||
|
const orderBy = null;
|
||||||
|
|
||||||
|
const transaction = (options && options.transaction) || undefined;
|
||||||
|
|
||||||
|
let include = [];
|
||||||
|
|
||||||
|
if (filter) {
|
||||||
|
if (filter.id) {
|
||||||
|
where = {
|
||||||
|
...where,
|
||||||
|
['id']: Utils.uuid(filter.id),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filter.input_valueRange) {
|
||||||
|
const [start, end] = filter.input_valueRange;
|
||||||
|
|
||||||
|
if (start !== undefined && start !== null && start !== '') {
|
||||||
|
where = {
|
||||||
|
...where,
|
||||||
|
input_value: {
|
||||||
|
...where.input_value,
|
||||||
|
[Op.gte]: start,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (end !== undefined && end !== null && end !== '') {
|
||||||
|
where = {
|
||||||
|
...where,
|
||||||
|
input_value: {
|
||||||
|
...where.input_value,
|
||||||
|
[Op.lte]: end,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filter.output_value_btu_hRange) {
|
||||||
|
const [start, end] = filter.output_value_btu_hRange;
|
||||||
|
|
||||||
|
if (start !== undefined && start !== null && start !== '') {
|
||||||
|
where = {
|
||||||
|
...where,
|
||||||
|
output_value_btu_h: {
|
||||||
|
...where.output_value_btu_h,
|
||||||
|
[Op.gte]: start,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (end !== undefined && end !== null && end !== '') {
|
||||||
|
where = {
|
||||||
|
...where,
|
||||||
|
output_value_btu_h: {
|
||||||
|
...where.output_value_btu_h,
|
||||||
|
[Op.lte]: end,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filter.output_value_kwRange) {
|
||||||
|
const [start, end] = filter.output_value_kwRange;
|
||||||
|
|
||||||
|
if (start !== undefined && start !== null && start !== '') {
|
||||||
|
where = {
|
||||||
|
...where,
|
||||||
|
output_value_kw: {
|
||||||
|
...where.output_value_kw,
|
||||||
|
[Op.gte]: start,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (end !== undefined && end !== null && end !== '') {
|
||||||
|
where = {
|
||||||
|
...where,
|
||||||
|
output_value_kw: {
|
||||||
|
...where.output_value_kw,
|
||||||
|
[Op.lte]: end,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filter.output_value_kcal_hRange) {
|
||||||
|
const [start, end] = filter.output_value_kcal_hRange;
|
||||||
|
|
||||||
|
if (start !== undefined && start !== null && start !== '') {
|
||||||
|
where = {
|
||||||
|
...where,
|
||||||
|
output_value_kcal_h: {
|
||||||
|
...where.output_value_kcal_h,
|
||||||
|
[Op.gte]: start,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (end !== undefined && end !== null && end !== '') {
|
||||||
|
where = {
|
||||||
|
...where,
|
||||||
|
output_value_kcal_h: {
|
||||||
|
...where.output_value_kcal_h,
|
||||||
|
[Op.lte]: end,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filter.output_value_trRange) {
|
||||||
|
const [start, end] = filter.output_value_trRange;
|
||||||
|
|
||||||
|
if (start !== undefined && start !== null && start !== '') {
|
||||||
|
where = {
|
||||||
|
...where,
|
||||||
|
output_value_tr: {
|
||||||
|
...where.output_value_tr,
|
||||||
|
[Op.gte]: start,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (end !== undefined && end !== null && end !== '') {
|
||||||
|
where = {
|
||||||
|
...where,
|
||||||
|
output_value_tr: {
|
||||||
|
...where.output_value_tr,
|
||||||
|
[Op.lte]: end,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filter.output_value_hpRange) {
|
||||||
|
const [start, end] = filter.output_value_hpRange;
|
||||||
|
|
||||||
|
if (start !== undefined && start !== null && start !== '') {
|
||||||
|
where = {
|
||||||
|
...where,
|
||||||
|
output_value_hp: {
|
||||||
|
...where.output_value_hp,
|
||||||
|
[Op.gte]: start,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (end !== undefined && end !== null && end !== '') {
|
||||||
|
where = {
|
||||||
|
...where,
|
||||||
|
output_value_hp: {
|
||||||
|
...where.output_value_hp,
|
||||||
|
[Op.lte]: end,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filter.output_value_wRange) {
|
||||||
|
const [start, end] = filter.output_value_wRange;
|
||||||
|
|
||||||
|
if (start !== undefined && start !== null && start !== '') {
|
||||||
|
where = {
|
||||||
|
...where,
|
||||||
|
output_value_w: {
|
||||||
|
...where.output_value_w,
|
||||||
|
[Op.gte]: start,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (end !== undefined && end !== null && end !== '') {
|
||||||
|
where = {
|
||||||
|
...where,
|
||||||
|
output_value_w: {
|
||||||
|
...where.output_value_w,
|
||||||
|
[Op.lte]: end,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filter.active !== undefined) {
|
||||||
|
where = {
|
||||||
|
...where,
|
||||||
|
active: filter.active === true || filter.active === 'true',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filter.input_unit) {
|
||||||
|
where = {
|
||||||
|
...where,
|
||||||
|
input_unit: filter.input_unit,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filter.createdAtRange) {
|
||||||
|
const [start, end] = filter.createdAtRange;
|
||||||
|
|
||||||
|
if (start !== undefined && start !== null && start !== '') {
|
||||||
|
where = {
|
||||||
|
...where,
|
||||||
|
['createdAt']: {
|
||||||
|
...where.createdAt,
|
||||||
|
[Op.gte]: start,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (end !== undefined && end !== null && end !== '') {
|
||||||
|
where = {
|
||||||
|
...where,
|
||||||
|
['createdAt']: {
|
||||||
|
...where.createdAt,
|
||||||
|
[Op.lte]: end,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const queryOptions = {
|
||||||
|
where,
|
||||||
|
include,
|
||||||
|
distinct: true,
|
||||||
|
order:
|
||||||
|
filter.field && filter.sort
|
||||||
|
? [[filter.field, filter.sort]]
|
||||||
|
: [['createdAt', 'desc']],
|
||||||
|
transaction: options?.transaction,
|
||||||
|
logging: console.log,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!options?.countOnly) {
|
||||||
|
queryOptions.limit = limit ? Number(limit) : undefined;
|
||||||
|
queryOptions.offset = offset ? Number(offset) : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { rows, count } = await db.conversions.findAndCountAll(
|
||||||
|
queryOptions,
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
rows: options?.countOnly ? [] : rows,
|
||||||
|
count: count,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error executing query:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async findAllAutocomplete(query, limit, offset) {
|
||||||
|
let where = {};
|
||||||
|
|
||||||
|
if (query) {
|
||||||
|
where = {
|
||||||
|
[Op.or]: [
|
||||||
|
{ ['id']: Utils.uuid(query) },
|
||||||
|
Utils.ilike('conversions', 'input_value', query),
|
||||||
|
],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const records = await db.conversions.findAll({
|
||||||
|
attributes: ['id', 'input_value'],
|
||||||
|
where,
|
||||||
|
limit: limit ? Number(limit) : undefined,
|
||||||
|
offset: offset ? Number(offset) : undefined,
|
||||||
|
orderBy: [['input_value', 'ASC']],
|
||||||
|
});
|
||||||
|
|
||||||
|
return records.map((record) => ({
|
||||||
|
id: record.id,
|
||||||
|
label: record.input_value,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -1,72 +0,0 @@
|
|||||||
module.exports = {
|
|
||||||
/**
|
|
||||||
* @param {QueryInterface} queryInterface
|
|
||||||
* @param {Sequelize} Sequelize
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
async up(queryInterface, Sequelize) {
|
|
||||||
/**
|
|
||||||
* @type {Transaction}
|
|
||||||
*/
|
|
||||||
const transaction = await queryInterface.sequelize.transaction();
|
|
||||||
try {
|
|
||||||
await queryInterface.dropTable('conversions', { transaction });
|
|
||||||
|
|
||||||
await transaction.commit();
|
|
||||||
} catch (err) {
|
|
||||||
await transaction.rollback();
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
/**
|
|
||||||
* @param {QueryInterface} queryInterface
|
|
||||||
* @param {Sequelize} Sequelize
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
async down(queryInterface, Sequelize) {
|
|
||||||
/**
|
|
||||||
* @type {Transaction}
|
|
||||||
*/
|
|
||||||
const transaction = await queryInterface.sequelize.transaction();
|
|
||||||
try {
|
|
||||||
await queryInterface.createTable(
|
|
||||||
'conversions',
|
|
||||||
{
|
|
||||||
id: {
|
|
||||||
type: Sequelize.DataTypes.UUID,
|
|
||||||
defaultValue: Sequelize.DataTypes.UUIDV4,
|
|
||||||
primaryKey: true,
|
|
||||||
},
|
|
||||||
createdById: {
|
|
||||||
type: Sequelize.DataTypes.UUID,
|
|
||||||
references: {
|
|
||||||
key: 'id',
|
|
||||||
model: 'users',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
updatedById: {
|
|
||||||
type: Sequelize.DataTypes.UUID,
|
|
||||||
references: {
|
|
||||||
key: 'id',
|
|
||||||
model: 'users',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
createdAt: { type: Sequelize.DataTypes.DATE },
|
|
||||||
updatedAt: { type: Sequelize.DataTypes.DATE },
|
|
||||||
deletedAt: { type: Sequelize.DataTypes.DATE },
|
|
||||||
importHash: {
|
|
||||||
type: Sequelize.DataTypes.STRING(255),
|
|
||||||
allowNull: true,
|
|
||||||
unique: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{ transaction },
|
|
||||||
);
|
|
||||||
|
|
||||||
await transaction.commit();
|
|
||||||
} catch (err) {
|
|
||||||
await transaction.rollback();
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
|
||||||
79
backend/src/db/models/conversions.js
Normal file
79
backend/src/db/models/conversions.js
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
const config = require('../../config');
|
||||||
|
const providers = config.providers;
|
||||||
|
const crypto = require('crypto');
|
||||||
|
const bcrypt = require('bcrypt');
|
||||||
|
const moment = require('moment');
|
||||||
|
|
||||||
|
module.exports = function (sequelize, DataTypes) {
|
||||||
|
const conversions = sequelize.define(
|
||||||
|
'conversions',
|
||||||
|
{
|
||||||
|
id: {
|
||||||
|
type: DataTypes.UUID,
|
||||||
|
defaultValue: DataTypes.UUIDV4,
|
||||||
|
primaryKey: true,
|
||||||
|
},
|
||||||
|
|
||||||
|
input_value: {
|
||||||
|
type: DataTypes.DECIMAL,
|
||||||
|
},
|
||||||
|
|
||||||
|
input_unit: {
|
||||||
|
type: DataTypes.ENUM,
|
||||||
|
|
||||||
|
values: ['BTU/h', 'kW', 'kcal/h', 'TR', 'HP', 'W'],
|
||||||
|
},
|
||||||
|
|
||||||
|
output_value_btu_h: {
|
||||||
|
type: DataTypes.DECIMAL,
|
||||||
|
},
|
||||||
|
|
||||||
|
output_value_kw: {
|
||||||
|
type: DataTypes.DECIMAL,
|
||||||
|
},
|
||||||
|
|
||||||
|
output_value_kcal_h: {
|
||||||
|
type: DataTypes.DECIMAL,
|
||||||
|
},
|
||||||
|
|
||||||
|
output_value_tr: {
|
||||||
|
type: DataTypes.DECIMAL,
|
||||||
|
},
|
||||||
|
|
||||||
|
output_value_hp: {
|
||||||
|
type: DataTypes.DECIMAL,
|
||||||
|
},
|
||||||
|
|
||||||
|
output_value_w: {
|
||||||
|
type: DataTypes.DECIMAL,
|
||||||
|
},
|
||||||
|
|
||||||
|
importHash: {
|
||||||
|
type: DataTypes.STRING(255),
|
||||||
|
allowNull: true,
|
||||||
|
unique: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timestamps: true,
|
||||||
|
paranoid: true,
|
||||||
|
freezeTableName: true,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
conversions.associate = (db) => {
|
||||||
|
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
||||||
|
|
||||||
|
//end loop
|
||||||
|
|
||||||
|
db.conversions.belongsTo(db.users, {
|
||||||
|
as: 'createdBy',
|
||||||
|
});
|
||||||
|
|
||||||
|
db.conversions.belongsTo(db.users, {
|
||||||
|
as: 'updatedBy',
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
return conversions;
|
||||||
|
};
|
||||||
@ -93,7 +93,7 @@ module.exports = {
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
const entities = ['users', 'roles', 'permissions', ,];
|
const entities = ['users', 'conversions', 'roles', 'permissions', ,];
|
||||||
await queryInterface.bulkInsert(
|
await queryInterface.bulkInsert(
|
||||||
'permissions',
|
'permissions',
|
||||||
entities.flatMap(createPermissions),
|
entities.flatMap(createPermissions),
|
||||||
@ -154,6 +154,83 @@ primary key ("roles_permissionsId", "permissionId")
|
|||||||
permissionId: getId('CREATE_USERS'),
|
permissionId: getId('CREATE_USERS'),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
createdAt,
|
||||||
|
updatedAt,
|
||||||
|
roles_permissionsId: getId('conversion_manager'),
|
||||||
|
permissionId: getId('CREATE_CONVERSIONS'),
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
createdAt,
|
||||||
|
updatedAt,
|
||||||
|
roles_permissionsId: getId('conversion_manager'),
|
||||||
|
permissionId: getId('READ_CONVERSIONS'),
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
createdAt,
|
||||||
|
updatedAt,
|
||||||
|
roles_permissionsId: getId('conversion_manager'),
|
||||||
|
permissionId: getId('UPDATE_CONVERSIONS'),
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
createdAt,
|
||||||
|
updatedAt,
|
||||||
|
roles_permissionsId: getId('conversion_manager'),
|
||||||
|
permissionId: getId('DELETE_CONVERSIONS'),
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
createdAt,
|
||||||
|
updatedAt,
|
||||||
|
roles_permissionsId: getId('conversion_user'),
|
||||||
|
permissionId: getId('CREATE_CONVERSIONS'),
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
createdAt,
|
||||||
|
updatedAt,
|
||||||
|
roles_permissionsId: getId('conversion_user'),
|
||||||
|
permissionId: getId('READ_CONVERSIONS'),
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
createdAt,
|
||||||
|
updatedAt,
|
||||||
|
roles_permissionsId: getId('guest'),
|
||||||
|
permissionId: getId('CREATE_CONVERSIONS'),
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
createdAt,
|
||||||
|
updatedAt,
|
||||||
|
roles_permissionsId: getId('guest'),
|
||||||
|
permissionId: getId('READ_CONVERSIONS'),
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
createdAt,
|
||||||
|
updatedAt,
|
||||||
|
roles_permissionsId: getId('viewer'),
|
||||||
|
permissionId: getId('CREATE_CONVERSIONS'),
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
createdAt,
|
||||||
|
updatedAt,
|
||||||
|
roles_permissionsId: getId('viewer'),
|
||||||
|
permissionId: getId('READ_CONVERSIONS'),
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
createdAt,
|
||||||
|
updatedAt,
|
||||||
|
roles_permissionsId: getId('report_viewer'),
|
||||||
|
permissionId: getId('READ_CONVERSIONS'),
|
||||||
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
createdAt,
|
createdAt,
|
||||||
updatedAt,
|
updatedAt,
|
||||||
@ -214,6 +291,31 @@ primary key ("roles_permissionsId", "permissionId")
|
|||||||
permissionId: getId('DELETE_USERS'),
|
permissionId: getId('DELETE_USERS'),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
createdAt,
|
||||||
|
updatedAt,
|
||||||
|
roles_permissionsId: getId('Administrator'),
|
||||||
|
permissionId: getId('CREATE_CONVERSIONS'),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
createdAt,
|
||||||
|
updatedAt,
|
||||||
|
roles_permissionsId: getId('Administrator'),
|
||||||
|
permissionId: getId('READ_CONVERSIONS'),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
createdAt,
|
||||||
|
updatedAt,
|
||||||
|
roles_permissionsId: getId('Administrator'),
|
||||||
|
permissionId: getId('UPDATE_CONVERSIONS'),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
createdAt,
|
||||||
|
updatedAt,
|
||||||
|
roles_permissionsId: getId('Administrator'),
|
||||||
|
permissionId: getId('DELETE_CONVERSIONS'),
|
||||||
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
createdAt,
|
createdAt,
|
||||||
updatedAt,
|
updatedAt,
|
||||||
|
|||||||
@ -1,14 +1,76 @@
|
|||||||
const db = require('../models');
|
const db = require('../models');
|
||||||
const Users = db.users;
|
const Users = db.users;
|
||||||
|
|
||||||
|
const Conversions = db.conversions;
|
||||||
|
|
||||||
|
const ConversionsData = [
|
||||||
|
{
|
||||||
|
input_value: 10000,
|
||||||
|
|
||||||
|
input_unit: 'kcal/h',
|
||||||
|
|
||||||
|
output_value_btu_h: 34121,
|
||||||
|
|
||||||
|
output_value_kw: 10,
|
||||||
|
|
||||||
|
output_value_kcal_h: 8600,
|
||||||
|
|
||||||
|
output_value_tr: 2.84,
|
||||||
|
|
||||||
|
output_value_hp: 3.79,
|
||||||
|
|
||||||
|
output_value_w: 10000,
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
input_value: 5000,
|
||||||
|
|
||||||
|
input_unit: 'TR',
|
||||||
|
|
||||||
|
output_value_btu_h: 17060.5,
|
||||||
|
|
||||||
|
output_value_kw: 5,
|
||||||
|
|
||||||
|
output_value_kcal_h: 4300,
|
||||||
|
|
||||||
|
output_value_tr: 1.42,
|
||||||
|
|
||||||
|
output_value_hp: 1.89,
|
||||||
|
|
||||||
|
output_value_w: 5000,
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
input_value: 15000,
|
||||||
|
|
||||||
|
input_unit: 'BTU/h',
|
||||||
|
|
||||||
|
output_value_btu_h: 15000,
|
||||||
|
|
||||||
|
output_value_kw: 4.39,
|
||||||
|
|
||||||
|
output_value_kcal_h: 12900,
|
||||||
|
|
||||||
|
output_value_tr: 1.25,
|
||||||
|
|
||||||
|
output_value_hp: 5.68,
|
||||||
|
|
||||||
|
output_value_w: 15000,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
// Similar logic for "relation_many"
|
// Similar logic for "relation_many"
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
up: async (queryInterface, Sequelize) => {
|
up: async (queryInterface, Sequelize) => {
|
||||||
|
await Conversions.bulkCreate(ConversionsData);
|
||||||
|
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
// Similar logic for "relation_many"
|
// Similar logic for "relation_many"
|
||||||
]);
|
]);
|
||||||
},
|
},
|
||||||
|
|
||||||
down: async (queryInterface, Sequelize) => {},
|
down: async (queryInterface, Sequelize) => {
|
||||||
|
await queryInterface.bulkDelete('conversions', null, {});
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@ -19,6 +19,8 @@ const openaiRoutes = require('./routes/openai');
|
|||||||
|
|
||||||
const usersRoutes = require('./routes/users');
|
const usersRoutes = require('./routes/users');
|
||||||
|
|
||||||
|
const conversionsRoutes = require('./routes/conversions');
|
||||||
|
|
||||||
const rolesRoutes = require('./routes/roles');
|
const rolesRoutes = require('./routes/roles');
|
||||||
|
|
||||||
const permissionsRoutes = require('./routes/permissions');
|
const permissionsRoutes = require('./routes/permissions');
|
||||||
@ -94,6 +96,12 @@ app.use(
|
|||||||
usersRoutes,
|
usersRoutes,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
app.use(
|
||||||
|
'/api/conversions',
|
||||||
|
passport.authenticate('jwt', { session: false }),
|
||||||
|
conversionsRoutes,
|
||||||
|
);
|
||||||
|
|
||||||
app.use(
|
app.use(
|
||||||
'/api/roles',
|
'/api/roles',
|
||||||
passport.authenticate('jwt', { session: false }),
|
passport.authenticate('jwt', { session: false }),
|
||||||
|
|||||||
471
backend/src/routes/conversions.js
Normal file
471
backend/src/routes/conversions.js
Normal file
@ -0,0 +1,471 @@
|
|||||||
|
const express = require('express');
|
||||||
|
|
||||||
|
const ConversionsService = require('../services/conversions');
|
||||||
|
const ConversionsDBApi = require('../db/api/conversions');
|
||||||
|
const wrapAsync = require('../helpers').wrapAsync;
|
||||||
|
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
const { parse } = require('json2csv');
|
||||||
|
|
||||||
|
const { checkCrudPermissions } = require('../middlewares/check-permissions');
|
||||||
|
|
||||||
|
router.use(checkCrudPermissions('conversions'));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @swagger
|
||||||
|
* components:
|
||||||
|
* schemas:
|
||||||
|
* Conversions:
|
||||||
|
* type: object
|
||||||
|
* properties:
|
||||||
|
|
||||||
|
* input_value:
|
||||||
|
* type: integer
|
||||||
|
* format: int64
|
||||||
|
* output_value_btu_h:
|
||||||
|
* type: integer
|
||||||
|
* format: int64
|
||||||
|
* output_value_kw:
|
||||||
|
* type: integer
|
||||||
|
* format: int64
|
||||||
|
* output_value_kcal_h:
|
||||||
|
* type: integer
|
||||||
|
* format: int64
|
||||||
|
* output_value_tr:
|
||||||
|
* type: integer
|
||||||
|
* format: int64
|
||||||
|
* output_value_hp:
|
||||||
|
* type: integer
|
||||||
|
* format: int64
|
||||||
|
* output_value_w:
|
||||||
|
* type: integer
|
||||||
|
* format: int64
|
||||||
|
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @swagger
|
||||||
|
* tags:
|
||||||
|
* name: Conversions
|
||||||
|
* description: The Conversions managing API
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @swagger
|
||||||
|
* /api/conversions:
|
||||||
|
* post:
|
||||||
|
* security:
|
||||||
|
* - bearerAuth: []
|
||||||
|
* tags: [Conversions]
|
||||||
|
* summary: Add new item
|
||||||
|
* description: Add new item
|
||||||
|
* requestBody:
|
||||||
|
* required: true
|
||||||
|
* content:
|
||||||
|
* application/json:
|
||||||
|
* schema:
|
||||||
|
* properties:
|
||||||
|
* data:
|
||||||
|
* description: Data of the updated item
|
||||||
|
* type: object
|
||||||
|
* $ref: "#/components/schemas/Conversions"
|
||||||
|
* responses:
|
||||||
|
* 200:
|
||||||
|
* description: The item was successfully added
|
||||||
|
* content:
|
||||||
|
* application/json:
|
||||||
|
* schema:
|
||||||
|
* $ref: "#/components/schemas/Conversions"
|
||||||
|
* 401:
|
||||||
|
* $ref: "#/components/responses/UnauthorizedError"
|
||||||
|
* 405:
|
||||||
|
* description: Invalid input data
|
||||||
|
* 500:
|
||||||
|
* description: Some server error
|
||||||
|
*/
|
||||||
|
router.post(
|
||||||
|
'/',
|
||||||
|
wrapAsync(async (req, res) => {
|
||||||
|
const referer =
|
||||||
|
req.headers.referer ||
|
||||||
|
`${req.protocol}://${req.hostname}${req.originalUrl}`;
|
||||||
|
const link = new URL(referer);
|
||||||
|
await ConversionsService.create(
|
||||||
|
req.body.data,
|
||||||
|
req.currentUser,
|
||||||
|
true,
|
||||||
|
link.host,
|
||||||
|
);
|
||||||
|
const payload = true;
|
||||||
|
res.status(200).send(payload);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @swagger
|
||||||
|
* /api/budgets/bulk-import:
|
||||||
|
* post:
|
||||||
|
* security:
|
||||||
|
* - bearerAuth: []
|
||||||
|
* tags: [Conversions]
|
||||||
|
* summary: Bulk import items
|
||||||
|
* description: Bulk import items
|
||||||
|
* requestBody:
|
||||||
|
* required: true
|
||||||
|
* content:
|
||||||
|
* application/json:
|
||||||
|
* schema:
|
||||||
|
* properties:
|
||||||
|
* data:
|
||||||
|
* description: Data of the updated items
|
||||||
|
* type: array
|
||||||
|
* items:
|
||||||
|
* $ref: "#/components/schemas/Conversions"
|
||||||
|
* responses:
|
||||||
|
* 200:
|
||||||
|
* description: The items were successfully imported
|
||||||
|
* content:
|
||||||
|
* application/json:
|
||||||
|
* schema:
|
||||||
|
* $ref: "#/components/schemas/Conversions"
|
||||||
|
* 401:
|
||||||
|
* $ref: "#/components/responses/UnauthorizedError"
|
||||||
|
* 405:
|
||||||
|
* description: Invalid input data
|
||||||
|
* 500:
|
||||||
|
* description: Some server error
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
router.post(
|
||||||
|
'/bulk-import',
|
||||||
|
wrapAsync(async (req, res) => {
|
||||||
|
const referer =
|
||||||
|
req.headers.referer ||
|
||||||
|
`${req.protocol}://${req.hostname}${req.originalUrl}`;
|
||||||
|
const link = new URL(referer);
|
||||||
|
await ConversionsService.bulkImport(req, res, true, link.host);
|
||||||
|
const payload = true;
|
||||||
|
res.status(200).send(payload);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @swagger
|
||||||
|
* /api/conversions/{id}:
|
||||||
|
* put:
|
||||||
|
* security:
|
||||||
|
* - bearerAuth: []
|
||||||
|
* tags: [Conversions]
|
||||||
|
* summary: Update the data of the selected item
|
||||||
|
* description: Update the data of the selected item
|
||||||
|
* parameters:
|
||||||
|
* - in: path
|
||||||
|
* name: id
|
||||||
|
* description: Item ID to update
|
||||||
|
* required: true
|
||||||
|
* schema:
|
||||||
|
* type: string
|
||||||
|
* requestBody:
|
||||||
|
* description: Set new item data
|
||||||
|
* required: true
|
||||||
|
* content:
|
||||||
|
* application/json:
|
||||||
|
* schema:
|
||||||
|
* properties:
|
||||||
|
* id:
|
||||||
|
* description: ID of the updated item
|
||||||
|
* type: string
|
||||||
|
* data:
|
||||||
|
* description: Data of the updated item
|
||||||
|
* type: object
|
||||||
|
* $ref: "#/components/schemas/Conversions"
|
||||||
|
* required:
|
||||||
|
* - id
|
||||||
|
* responses:
|
||||||
|
* 200:
|
||||||
|
* description: The item data was successfully updated
|
||||||
|
* content:
|
||||||
|
* application/json:
|
||||||
|
* schema:
|
||||||
|
* $ref: "#/components/schemas/Conversions"
|
||||||
|
* 400:
|
||||||
|
* description: Invalid ID supplied
|
||||||
|
* 401:
|
||||||
|
* $ref: "#/components/responses/UnauthorizedError"
|
||||||
|
* 404:
|
||||||
|
* description: Item not found
|
||||||
|
* 500:
|
||||||
|
* description: Some server error
|
||||||
|
*/
|
||||||
|
router.put(
|
||||||
|
'/:id',
|
||||||
|
wrapAsync(async (req, res) => {
|
||||||
|
await ConversionsService.update(
|
||||||
|
req.body.data,
|
||||||
|
req.body.id,
|
||||||
|
req.currentUser,
|
||||||
|
);
|
||||||
|
const payload = true;
|
||||||
|
res.status(200).send(payload);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @swagger
|
||||||
|
* /api/conversions/{id}:
|
||||||
|
* delete:
|
||||||
|
* security:
|
||||||
|
* - bearerAuth: []
|
||||||
|
* tags: [Conversions]
|
||||||
|
* summary: Delete the selected item
|
||||||
|
* description: Delete the selected item
|
||||||
|
* parameters:
|
||||||
|
* - in: path
|
||||||
|
* name: id
|
||||||
|
* description: Item ID to delete
|
||||||
|
* required: true
|
||||||
|
* schema:
|
||||||
|
* type: string
|
||||||
|
* responses:
|
||||||
|
* 200:
|
||||||
|
* description: The item was successfully deleted
|
||||||
|
* content:
|
||||||
|
* application/json:
|
||||||
|
* schema:
|
||||||
|
* $ref: "#/components/schemas/Conversions"
|
||||||
|
* 400:
|
||||||
|
* description: Invalid ID supplied
|
||||||
|
* 401:
|
||||||
|
* $ref: "#/components/responses/UnauthorizedError"
|
||||||
|
* 404:
|
||||||
|
* description: Item not found
|
||||||
|
* 500:
|
||||||
|
* description: Some server error
|
||||||
|
*/
|
||||||
|
router.delete(
|
||||||
|
'/:id',
|
||||||
|
wrapAsync(async (req, res) => {
|
||||||
|
await ConversionsService.remove(req.params.id, req.currentUser);
|
||||||
|
const payload = true;
|
||||||
|
res.status(200).send(payload);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @swagger
|
||||||
|
* /api/conversions/deleteByIds:
|
||||||
|
* post:
|
||||||
|
* security:
|
||||||
|
* - bearerAuth: []
|
||||||
|
* tags: [Conversions]
|
||||||
|
* summary: Delete the selected item list
|
||||||
|
* description: Delete the selected item list
|
||||||
|
* requestBody:
|
||||||
|
* required: true
|
||||||
|
* content:
|
||||||
|
* application/json:
|
||||||
|
* schema:
|
||||||
|
* properties:
|
||||||
|
* ids:
|
||||||
|
* description: IDs of the updated items
|
||||||
|
* type: array
|
||||||
|
* responses:
|
||||||
|
* 200:
|
||||||
|
* description: The items was successfully deleted
|
||||||
|
* content:
|
||||||
|
* application/json:
|
||||||
|
* schema:
|
||||||
|
* $ref: "#/components/schemas/Conversions"
|
||||||
|
* 401:
|
||||||
|
* $ref: "#/components/responses/UnauthorizedError"
|
||||||
|
* 404:
|
||||||
|
* description: Items not found
|
||||||
|
* 500:
|
||||||
|
* description: Some server error
|
||||||
|
*/
|
||||||
|
router.post(
|
||||||
|
'/deleteByIds',
|
||||||
|
wrapAsync(async (req, res) => {
|
||||||
|
await ConversionsService.deleteByIds(req.body.data, req.currentUser);
|
||||||
|
const payload = true;
|
||||||
|
res.status(200).send(payload);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @swagger
|
||||||
|
* /api/conversions:
|
||||||
|
* get:
|
||||||
|
* security:
|
||||||
|
* - bearerAuth: []
|
||||||
|
* tags: [Conversions]
|
||||||
|
* summary: Get all conversions
|
||||||
|
* description: Get all conversions
|
||||||
|
* responses:
|
||||||
|
* 200:
|
||||||
|
* description: Conversions list successfully received
|
||||||
|
* content:
|
||||||
|
* application/json:
|
||||||
|
* schema:
|
||||||
|
* type: array
|
||||||
|
* items:
|
||||||
|
* $ref: "#/components/schemas/Conversions"
|
||||||
|
* 401:
|
||||||
|
* $ref: "#/components/responses/UnauthorizedError"
|
||||||
|
* 404:
|
||||||
|
* description: Data not found
|
||||||
|
* 500:
|
||||||
|
* description: Some server error
|
||||||
|
*/
|
||||||
|
router.get(
|
||||||
|
'/',
|
||||||
|
wrapAsync(async (req, res) => {
|
||||||
|
const filetype = req.query.filetype;
|
||||||
|
|
||||||
|
const currentUser = req.currentUser;
|
||||||
|
const payload = await ConversionsDBApi.findAll(req.query, { currentUser });
|
||||||
|
if (filetype && filetype === 'csv') {
|
||||||
|
const fields = [
|
||||||
|
'id',
|
||||||
|
|
||||||
|
'input_value',
|
||||||
|
'output_value_btu_h',
|
||||||
|
'output_value_kw',
|
||||||
|
'output_value_kcal_h',
|
||||||
|
'output_value_tr',
|
||||||
|
'output_value_hp',
|
||||||
|
'output_value_w',
|
||||||
|
];
|
||||||
|
const opts = { fields };
|
||||||
|
try {
|
||||||
|
const csv = parse(payload.rows, opts);
|
||||||
|
res.status(200).attachment(csv);
|
||||||
|
res.send(csv);
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
res.status(200).send(payload);
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @swagger
|
||||||
|
* /api/conversions/count:
|
||||||
|
* get:
|
||||||
|
* security:
|
||||||
|
* - bearerAuth: []
|
||||||
|
* tags: [Conversions]
|
||||||
|
* summary: Count all conversions
|
||||||
|
* description: Count all conversions
|
||||||
|
* responses:
|
||||||
|
* 200:
|
||||||
|
* description: Conversions count successfully received
|
||||||
|
* content:
|
||||||
|
* application/json:
|
||||||
|
* schema:
|
||||||
|
* type: array
|
||||||
|
* items:
|
||||||
|
* $ref: "#/components/schemas/Conversions"
|
||||||
|
* 401:
|
||||||
|
* $ref: "#/components/responses/UnauthorizedError"
|
||||||
|
* 404:
|
||||||
|
* description: Data not found
|
||||||
|
* 500:
|
||||||
|
* description: Some server error
|
||||||
|
*/
|
||||||
|
router.get(
|
||||||
|
'/count',
|
||||||
|
wrapAsync(async (req, res) => {
|
||||||
|
const currentUser = req.currentUser;
|
||||||
|
const payload = await ConversionsDBApi.findAll(req.query, null, {
|
||||||
|
countOnly: true,
|
||||||
|
currentUser,
|
||||||
|
});
|
||||||
|
|
||||||
|
res.status(200).send(payload);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @swagger
|
||||||
|
* /api/conversions/autocomplete:
|
||||||
|
* get:
|
||||||
|
* security:
|
||||||
|
* - bearerAuth: []
|
||||||
|
* tags: [Conversions]
|
||||||
|
* summary: Find all conversions that match search criteria
|
||||||
|
* description: Find all conversions that match search criteria
|
||||||
|
* responses:
|
||||||
|
* 200:
|
||||||
|
* description: Conversions list successfully received
|
||||||
|
* content:
|
||||||
|
* application/json:
|
||||||
|
* schema:
|
||||||
|
* type: array
|
||||||
|
* items:
|
||||||
|
* $ref: "#/components/schemas/Conversions"
|
||||||
|
* 401:
|
||||||
|
* $ref: "#/components/responses/UnauthorizedError"
|
||||||
|
* 404:
|
||||||
|
* description: Data not found
|
||||||
|
* 500:
|
||||||
|
* description: Some server error
|
||||||
|
*/
|
||||||
|
router.get('/autocomplete', async (req, res) => {
|
||||||
|
const payload = await ConversionsDBApi.findAllAutocomplete(
|
||||||
|
req.query.query,
|
||||||
|
req.query.limit,
|
||||||
|
req.query.offset,
|
||||||
|
);
|
||||||
|
|
||||||
|
res.status(200).send(payload);
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @swagger
|
||||||
|
* /api/conversions/{id}:
|
||||||
|
* get:
|
||||||
|
* security:
|
||||||
|
* - bearerAuth: []
|
||||||
|
* tags: [Conversions]
|
||||||
|
* summary: Get selected item
|
||||||
|
* description: Get selected item
|
||||||
|
* parameters:
|
||||||
|
* - in: path
|
||||||
|
* name: id
|
||||||
|
* description: ID of item to get
|
||||||
|
* required: true
|
||||||
|
* schema:
|
||||||
|
* type: string
|
||||||
|
* responses:
|
||||||
|
* 200:
|
||||||
|
* description: Selected item successfully received
|
||||||
|
* content:
|
||||||
|
* application/json:
|
||||||
|
* schema:
|
||||||
|
* $ref: "#/components/schemas/Conversions"
|
||||||
|
* 400:
|
||||||
|
* description: Invalid ID supplied
|
||||||
|
* 401:
|
||||||
|
* $ref: "#/components/responses/UnauthorizedError"
|
||||||
|
* 404:
|
||||||
|
* description: Item not found
|
||||||
|
* 500:
|
||||||
|
* description: Some server error
|
||||||
|
*/
|
||||||
|
router.get(
|
||||||
|
'/:id',
|
||||||
|
wrapAsync(async (req, res) => {
|
||||||
|
const payload = await ConversionsDBApi.findBy({ id: req.params.id });
|
||||||
|
|
||||||
|
res.status(200).send(payload);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
router.use('/', require('../helpers').commonErrorHandler);
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
114
backend/src/services/conversions.js
Normal file
114
backend/src/services/conversions.js
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
const db = require('../db/models');
|
||||||
|
const ConversionsDBApi = require('../db/api/conversions');
|
||||||
|
const processFile = require('../middlewares/upload');
|
||||||
|
const ValidationError = require('./notifications/errors/validation');
|
||||||
|
const csv = require('csv-parser');
|
||||||
|
const axios = require('axios');
|
||||||
|
const config = require('../config');
|
||||||
|
const stream = require('stream');
|
||||||
|
|
||||||
|
module.exports = class ConversionsService {
|
||||||
|
static async create(data, currentUser) {
|
||||||
|
const transaction = await db.sequelize.transaction();
|
||||||
|
try {
|
||||||
|
await ConversionsDBApi.create(data, {
|
||||||
|
currentUser,
|
||||||
|
transaction,
|
||||||
|
});
|
||||||
|
|
||||||
|
await transaction.commit();
|
||||||
|
} catch (error) {
|
||||||
|
await transaction.rollback();
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async bulkImport(req, res, sendInvitationEmails = true, host) {
|
||||||
|
const transaction = await db.sequelize.transaction();
|
||||||
|
|
||||||
|
try {
|
||||||
|
await processFile(req, res);
|
||||||
|
const bufferStream = new stream.PassThrough();
|
||||||
|
const results = [];
|
||||||
|
|
||||||
|
await bufferStream.end(Buffer.from(req.file.buffer, 'utf-8')); // convert Buffer to Stream
|
||||||
|
|
||||||
|
await new Promise((resolve, reject) => {
|
||||||
|
bufferStream
|
||||||
|
.pipe(csv())
|
||||||
|
.on('data', (data) => results.push(data))
|
||||||
|
.on('end', async () => {
|
||||||
|
console.log('CSV results', results);
|
||||||
|
resolve();
|
||||||
|
})
|
||||||
|
.on('error', (error) => reject(error));
|
||||||
|
});
|
||||||
|
|
||||||
|
await ConversionsDBApi.bulkImport(results, {
|
||||||
|
transaction,
|
||||||
|
ignoreDuplicates: true,
|
||||||
|
validate: true,
|
||||||
|
currentUser: req.currentUser,
|
||||||
|
});
|
||||||
|
|
||||||
|
await transaction.commit();
|
||||||
|
} catch (error) {
|
||||||
|
await transaction.rollback();
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async update(data, id, currentUser) {
|
||||||
|
const transaction = await db.sequelize.transaction();
|
||||||
|
try {
|
||||||
|
let conversions = await ConversionsDBApi.findBy({ id }, { transaction });
|
||||||
|
|
||||||
|
if (!conversions) {
|
||||||
|
throw new ValidationError('conversionsNotFound');
|
||||||
|
}
|
||||||
|
|
||||||
|
const updatedConversions = await ConversionsDBApi.update(id, data, {
|
||||||
|
currentUser,
|
||||||
|
transaction,
|
||||||
|
});
|
||||||
|
|
||||||
|
await transaction.commit();
|
||||||
|
return updatedConversions;
|
||||||
|
} catch (error) {
|
||||||
|
await transaction.rollback();
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async deleteByIds(ids, currentUser) {
|
||||||
|
const transaction = await db.sequelize.transaction();
|
||||||
|
|
||||||
|
try {
|
||||||
|
await ConversionsDBApi.deleteByIds(ids, {
|
||||||
|
currentUser,
|
||||||
|
transaction,
|
||||||
|
});
|
||||||
|
|
||||||
|
await transaction.commit();
|
||||||
|
} catch (error) {
|
||||||
|
await transaction.rollback();
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async remove(id, currentUser) {
|
||||||
|
const transaction = await db.sequelize.transaction();
|
||||||
|
|
||||||
|
try {
|
||||||
|
await ConversionsDBApi.remove(id, {
|
||||||
|
currentUser,
|
||||||
|
transaction,
|
||||||
|
});
|
||||||
|
|
||||||
|
await transaction.commit();
|
||||||
|
} catch (error) {
|
||||||
|
await transaction.rollback();
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -43,7 +43,23 @@ module.exports = class SearchService {
|
|||||||
const tableColumns = {
|
const tableColumns = {
|
||||||
users: ['firstName', 'lastName', 'phoneNumber', 'email'],
|
users: ['firstName', 'lastName', 'phoneNumber', 'email'],
|
||||||
};
|
};
|
||||||
const columnsInt = {};
|
const columnsInt = {
|
||||||
|
conversions: [
|
||||||
|
'input_value',
|
||||||
|
|
||||||
|
'output_value_btu_h',
|
||||||
|
|
||||||
|
'output_value_kw',
|
||||||
|
|
||||||
|
'output_value_kcal_h',
|
||||||
|
|
||||||
|
'output_value_tr',
|
||||||
|
|
||||||
|
'output_value_hp',
|
||||||
|
|
||||||
|
'output_value_w',
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
let allFoundRecords = [];
|
let allFoundRecords = [];
|
||||||
|
|
||||||
|
|||||||
@ -1 +0,0 @@
|
|||||||
{}
|
|
||||||
186
frontend/src/components/Conversions/CardConversions.tsx
Normal file
186
frontend/src/components/Conversions/CardConversions.tsx
Normal file
@ -0,0 +1,186 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import ImageField from '../ImageField';
|
||||||
|
import ListActionsPopover from '../ListActionsPopover';
|
||||||
|
import { useAppSelector } from '../../stores/hooks';
|
||||||
|
import dataFormatter from '../../helpers/dataFormatter';
|
||||||
|
import { Pagination } from '../Pagination';
|
||||||
|
import { saveFile } from '../../helpers/fileSaver';
|
||||||
|
import LoadingSpinner from '../LoadingSpinner';
|
||||||
|
import Link from 'next/link';
|
||||||
|
|
||||||
|
import { hasPermission } from '../../helpers/userPermissions';
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
conversions: any[];
|
||||||
|
loading: boolean;
|
||||||
|
onDelete: (id: string) => void;
|
||||||
|
currentPage: number;
|
||||||
|
numPages: number;
|
||||||
|
onPageChange: (page: number) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
const CardConversions = ({
|
||||||
|
conversions,
|
||||||
|
loading,
|
||||||
|
onDelete,
|
||||||
|
currentPage,
|
||||||
|
numPages,
|
||||||
|
onPageChange,
|
||||||
|
}: Props) => {
|
||||||
|
const asideScrollbarsStyle = useAppSelector(
|
||||||
|
(state) => state.style.asideScrollbarsStyle,
|
||||||
|
);
|
||||||
|
const bgColor = useAppSelector((state) => state.style.cardsColor);
|
||||||
|
const darkMode = useAppSelector((state) => state.style.darkMode);
|
||||||
|
const corners = useAppSelector((state) => state.style.corners);
|
||||||
|
const focusRing = useAppSelector((state) => state.style.focusRingColor);
|
||||||
|
|
||||||
|
const currentUser = useAppSelector((state) => state.auth.currentUser);
|
||||||
|
const hasUpdatePermission = hasPermission(currentUser, 'UPDATE_CONVERSIONS');
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={'p-4'}>
|
||||||
|
{loading && <LoadingSpinner />}
|
||||||
|
<ul
|
||||||
|
role='list'
|
||||||
|
className='grid grid-cols-1 gap-x-6 gap-y-8 lg:grid-cols-3 2xl:grid-cols-4 xl:gap-x-8'
|
||||||
|
>
|
||||||
|
{!loading &&
|
||||||
|
conversions.map((item, index) => (
|
||||||
|
<li
|
||||||
|
key={item.id}
|
||||||
|
className={`overflow-hidden ${
|
||||||
|
corners !== 'rounded-full' ? corners : 'rounded-3xl'
|
||||||
|
} border ${focusRing} border-gray-200 dark:border-dark-700 ${
|
||||||
|
darkMode ? 'aside-scrollbars-[slate]' : asideScrollbarsStyle
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
className={`flex items-center ${bgColor} p-6 gap-x-4 border-b border-gray-900/5 bg-gray-50 dark:bg-dark-800 relative`}
|
||||||
|
>
|
||||||
|
<Link
|
||||||
|
href={`/conversions/conversions-view/?id=${item.id}`}
|
||||||
|
className='text-lg font-bold leading-6 line-clamp-1'
|
||||||
|
>
|
||||||
|
{item.input_value}
|
||||||
|
</Link>
|
||||||
|
|
||||||
|
<div className='ml-auto '>
|
||||||
|
<ListActionsPopover
|
||||||
|
onDelete={onDelete}
|
||||||
|
itemId={item.id}
|
||||||
|
pathEdit={`/conversions/conversions-edit/?id=${item.id}`}
|
||||||
|
pathView={`/conversions/conversions-view/?id=${item.id}`}
|
||||||
|
hasUpdatePermission={hasUpdatePermission}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<dl className='divide-y divide-stone-300 dark:divide-dark-700 px-6 py-4 text-sm leading-6 h-64 overflow-y-auto'>
|
||||||
|
<div className='flex justify-between gap-x-4 py-3'>
|
||||||
|
<dt className=' text-gray-500 dark:text-dark-600'>
|
||||||
|
InputValue
|
||||||
|
</dt>
|
||||||
|
<dd className='flex items-start gap-x-2'>
|
||||||
|
<div className='font-medium line-clamp-4'>
|
||||||
|
{item.input_value}
|
||||||
|
</div>
|
||||||
|
</dd>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className='flex justify-between gap-x-4 py-3'>
|
||||||
|
<dt className=' text-gray-500 dark:text-dark-600'>
|
||||||
|
InputUnit
|
||||||
|
</dt>
|
||||||
|
<dd className='flex items-start gap-x-2'>
|
||||||
|
<div className='font-medium line-clamp-4'>
|
||||||
|
{item.input_unit}
|
||||||
|
</div>
|
||||||
|
</dd>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className='flex justify-between gap-x-4 py-3'>
|
||||||
|
<dt className=' text-gray-500 dark:text-dark-600'>
|
||||||
|
OutputValueBTU/h
|
||||||
|
</dt>
|
||||||
|
<dd className='flex items-start gap-x-2'>
|
||||||
|
<div className='font-medium line-clamp-4'>
|
||||||
|
{item.output_value_btu_h}
|
||||||
|
</div>
|
||||||
|
</dd>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className='flex justify-between gap-x-4 py-3'>
|
||||||
|
<dt className=' text-gray-500 dark:text-dark-600'>
|
||||||
|
OutputValuekW
|
||||||
|
</dt>
|
||||||
|
<dd className='flex items-start gap-x-2'>
|
||||||
|
<div className='font-medium line-clamp-4'>
|
||||||
|
{item.output_value_kw}
|
||||||
|
</div>
|
||||||
|
</dd>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className='flex justify-between gap-x-4 py-3'>
|
||||||
|
<dt className=' text-gray-500 dark:text-dark-600'>
|
||||||
|
OutputValuekcal/h
|
||||||
|
</dt>
|
||||||
|
<dd className='flex items-start gap-x-2'>
|
||||||
|
<div className='font-medium line-clamp-4'>
|
||||||
|
{item.output_value_kcal_h}
|
||||||
|
</div>
|
||||||
|
</dd>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className='flex justify-between gap-x-4 py-3'>
|
||||||
|
<dt className=' text-gray-500 dark:text-dark-600'>
|
||||||
|
OutputValueTR
|
||||||
|
</dt>
|
||||||
|
<dd className='flex items-start gap-x-2'>
|
||||||
|
<div className='font-medium line-clamp-4'>
|
||||||
|
{item.output_value_tr}
|
||||||
|
</div>
|
||||||
|
</dd>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className='flex justify-between gap-x-4 py-3'>
|
||||||
|
<dt className=' text-gray-500 dark:text-dark-600'>
|
||||||
|
OutputValueHP
|
||||||
|
</dt>
|
||||||
|
<dd className='flex items-start gap-x-2'>
|
||||||
|
<div className='font-medium line-clamp-4'>
|
||||||
|
{item.output_value_hp}
|
||||||
|
</div>
|
||||||
|
</dd>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className='flex justify-between gap-x-4 py-3'>
|
||||||
|
<dt className=' text-gray-500 dark:text-dark-600'>
|
||||||
|
OutputValueW
|
||||||
|
</dt>
|
||||||
|
<dd className='flex items-start gap-x-2'>
|
||||||
|
<div className='font-medium line-clamp-4'>
|
||||||
|
{item.output_value_w}
|
||||||
|
</div>
|
||||||
|
</dd>
|
||||||
|
</div>
|
||||||
|
</dl>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
{!loading && conversions.length === 0 && (
|
||||||
|
<div className='col-span-full flex items-center justify-center h-40'>
|
||||||
|
<p className=''>No data to display</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</ul>
|
||||||
|
<div className={'flex items-center justify-center my-6'}>
|
||||||
|
<Pagination
|
||||||
|
currentPage={currentPage}
|
||||||
|
numPages={numPages}
|
||||||
|
setCurrentPage={onPageChange}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default CardConversions;
|
||||||
136
frontend/src/components/Conversions/ListConversions.tsx
Normal file
136
frontend/src/components/Conversions/ListConversions.tsx
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import CardBox from '../CardBox';
|
||||||
|
import ImageField from '../ImageField';
|
||||||
|
import dataFormatter from '../../helpers/dataFormatter';
|
||||||
|
import { saveFile } from '../../helpers/fileSaver';
|
||||||
|
import ListActionsPopover from '../ListActionsPopover';
|
||||||
|
import { useAppSelector } from '../../stores/hooks';
|
||||||
|
import { Pagination } from '../Pagination';
|
||||||
|
import LoadingSpinner from '../LoadingSpinner';
|
||||||
|
import Link from 'next/link';
|
||||||
|
|
||||||
|
import { hasPermission } from '../../helpers/userPermissions';
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
conversions: any[];
|
||||||
|
loading: boolean;
|
||||||
|
onDelete: (id: string) => void;
|
||||||
|
currentPage: number;
|
||||||
|
numPages: number;
|
||||||
|
onPageChange: (page: number) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
const ListConversions = ({
|
||||||
|
conversions,
|
||||||
|
loading,
|
||||||
|
onDelete,
|
||||||
|
currentPage,
|
||||||
|
numPages,
|
||||||
|
onPageChange,
|
||||||
|
}: Props) => {
|
||||||
|
const currentUser = useAppSelector((state) => state.auth.currentUser);
|
||||||
|
const hasUpdatePermission = hasPermission(currentUser, 'UPDATE_CONVERSIONS');
|
||||||
|
|
||||||
|
const corners = useAppSelector((state) => state.style.corners);
|
||||||
|
const bgColor = useAppSelector((state) => state.style.cardsColor);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<div className='relative overflow-x-auto p-4 space-y-4'>
|
||||||
|
{loading && <LoadingSpinner />}
|
||||||
|
{!loading &&
|
||||||
|
conversions.map((item) => (
|
||||||
|
<div key={item.id}>
|
||||||
|
<CardBox hasTable isList className={'rounded shadow-none'}>
|
||||||
|
<div
|
||||||
|
className={`flex rounded dark:bg-dark-900 border border-stone-300 items-center overflow-hidden`}
|
||||||
|
>
|
||||||
|
<Link
|
||||||
|
href={`/conversions/conversions-view/?id=${item.id}`}
|
||||||
|
className={
|
||||||
|
'flex-1 px-4 py-6 h-24 flex divide-x-2 divide-stone-300 items-center overflow-hidden`}> dark:divide-dark-700 overflow-x-auto'
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<div className={'flex-1 px-3'}>
|
||||||
|
<p className={'text-xs text-gray-500 '}>InputValue</p>
|
||||||
|
<p className={'line-clamp-2'}>{item.input_value}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className={'flex-1 px-3'}>
|
||||||
|
<p className={'text-xs text-gray-500 '}>InputUnit</p>
|
||||||
|
<p className={'line-clamp-2'}>{item.input_unit}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className={'flex-1 px-3'}>
|
||||||
|
<p className={'text-xs text-gray-500 '}>
|
||||||
|
OutputValueBTU/h
|
||||||
|
</p>
|
||||||
|
<p className={'line-clamp-2'}>
|
||||||
|
{item.output_value_btu_h}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className={'flex-1 px-3'}>
|
||||||
|
<p className={'text-xs text-gray-500 '}>
|
||||||
|
OutputValuekW
|
||||||
|
</p>
|
||||||
|
<p className={'line-clamp-2'}>{item.output_value_kw}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className={'flex-1 px-3'}>
|
||||||
|
<p className={'text-xs text-gray-500 '}>
|
||||||
|
OutputValuekcal/h
|
||||||
|
</p>
|
||||||
|
<p className={'line-clamp-2'}>
|
||||||
|
{item.output_value_kcal_h}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className={'flex-1 px-3'}>
|
||||||
|
<p className={'text-xs text-gray-500 '}>
|
||||||
|
OutputValueTR
|
||||||
|
</p>
|
||||||
|
<p className={'line-clamp-2'}>{item.output_value_tr}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className={'flex-1 px-3'}>
|
||||||
|
<p className={'text-xs text-gray-500 '}>
|
||||||
|
OutputValueHP
|
||||||
|
</p>
|
||||||
|
<p className={'line-clamp-2'}>{item.output_value_hp}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className={'flex-1 px-3'}>
|
||||||
|
<p className={'text-xs text-gray-500 '}>OutputValueW</p>
|
||||||
|
<p className={'line-clamp-2'}>{item.output_value_w}</p>
|
||||||
|
</div>
|
||||||
|
</Link>
|
||||||
|
<ListActionsPopover
|
||||||
|
onDelete={onDelete}
|
||||||
|
itemId={item.id}
|
||||||
|
pathEdit={`/conversions/conversions-edit/?id=${item.id}`}
|
||||||
|
pathView={`/conversions/conversions-view/?id=${item.id}`}
|
||||||
|
hasUpdatePermission={hasUpdatePermission}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</CardBox>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
{!loading && conversions.length === 0 && (
|
||||||
|
<div className='col-span-full flex items-center justify-center h-40'>
|
||||||
|
<p className=''>No data to display</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<div className={'flex items-center justify-center my-6'}>
|
||||||
|
<Pagination
|
||||||
|
currentPage={currentPage}
|
||||||
|
numPages={numPages}
|
||||||
|
setCurrentPage={onPageChange}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ListConversions;
|
||||||
484
frontend/src/components/Conversions/TableConversions.tsx
Normal file
484
frontend/src/components/Conversions/TableConversions.tsx
Normal file
@ -0,0 +1,484 @@
|
|||||||
|
import React, { useEffect, useState, useMemo } from 'react';
|
||||||
|
import { createPortal } from 'react-dom';
|
||||||
|
import { ToastContainer, toast } from 'react-toastify';
|
||||||
|
import BaseButton from '../BaseButton';
|
||||||
|
import CardBoxModal from '../CardBoxModal';
|
||||||
|
import CardBox from '../CardBox';
|
||||||
|
import {
|
||||||
|
fetch,
|
||||||
|
update,
|
||||||
|
deleteItem,
|
||||||
|
setRefetch,
|
||||||
|
deleteItemsByIds,
|
||||||
|
} from '../../stores/conversions/conversionsSlice';
|
||||||
|
import { useAppDispatch, useAppSelector } from '../../stores/hooks';
|
||||||
|
import { useRouter } from 'next/router';
|
||||||
|
import { Field, Form, Formik } from 'formik';
|
||||||
|
import { DataGrid, GridColDef } from '@mui/x-data-grid';
|
||||||
|
import { loadColumns } from './configureConversionsCols';
|
||||||
|
import _ from 'lodash';
|
||||||
|
import dataFormatter from '../../helpers/dataFormatter';
|
||||||
|
import { dataGridStyles } from '../../styles';
|
||||||
|
|
||||||
|
const perPage = 10;
|
||||||
|
|
||||||
|
const TableSampleConversions = ({
|
||||||
|
filterItems,
|
||||||
|
setFilterItems,
|
||||||
|
filters,
|
||||||
|
showGrid,
|
||||||
|
}) => {
|
||||||
|
const notify = (type, msg) => toast(msg, { type, position: 'bottom-center' });
|
||||||
|
|
||||||
|
const dispatch = useAppDispatch();
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
const pagesList = [];
|
||||||
|
const [id, setId] = useState(null);
|
||||||
|
const [currentPage, setCurrentPage] = useState(0);
|
||||||
|
const [filterRequest, setFilterRequest] = React.useState('');
|
||||||
|
const [columns, setColumns] = useState<GridColDef[]>([]);
|
||||||
|
const [selectedRows, setSelectedRows] = useState([]);
|
||||||
|
const [sortModel, setSortModel] = useState([
|
||||||
|
{
|
||||||
|
field: '',
|
||||||
|
sort: 'desc',
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
const {
|
||||||
|
conversions,
|
||||||
|
loading,
|
||||||
|
count,
|
||||||
|
notify: conversionsNotify,
|
||||||
|
refetch,
|
||||||
|
} = useAppSelector((state) => state.conversions);
|
||||||
|
const { currentUser } = useAppSelector((state) => state.auth);
|
||||||
|
const focusRing = useAppSelector((state) => state.style.focusRingColor);
|
||||||
|
const bgColor = useAppSelector((state) => state.style.bgLayoutColor);
|
||||||
|
const corners = useAppSelector((state) => state.style.corners);
|
||||||
|
const numPages =
|
||||||
|
Math.floor(count / perPage) === 0 ? 1 : Math.ceil(count / perPage);
|
||||||
|
for (let i = 0; i < numPages; i++) {
|
||||||
|
pagesList.push(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
const loadData = async (page = currentPage, request = filterRequest) => {
|
||||||
|
if (page !== currentPage) setCurrentPage(page);
|
||||||
|
if (request !== filterRequest) setFilterRequest(request);
|
||||||
|
const { sort, field } = sortModel[0];
|
||||||
|
|
||||||
|
const query = `?page=${page}&limit=${perPage}${request}&sort=${sort}&field=${field}`;
|
||||||
|
dispatch(fetch({ limit: perPage, page, query }));
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (conversionsNotify.showNotification) {
|
||||||
|
notify(
|
||||||
|
conversionsNotify.typeNotification,
|
||||||
|
conversionsNotify.textNotification,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}, [conversionsNotify.showNotification]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!currentUser) return;
|
||||||
|
loadData();
|
||||||
|
}, [sortModel, currentUser]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (refetch) {
|
||||||
|
loadData(0);
|
||||||
|
dispatch(setRefetch(false));
|
||||||
|
}
|
||||||
|
}, [refetch, dispatch]);
|
||||||
|
|
||||||
|
const [isModalInfoActive, setIsModalInfoActive] = useState(false);
|
||||||
|
const [isModalTrashActive, setIsModalTrashActive] = useState(false);
|
||||||
|
|
||||||
|
const handleModalAction = () => {
|
||||||
|
setIsModalInfoActive(false);
|
||||||
|
setIsModalTrashActive(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDeleteModalAction = (id: string) => {
|
||||||
|
setId(id);
|
||||||
|
setIsModalTrashActive(true);
|
||||||
|
};
|
||||||
|
const handleDeleteAction = async () => {
|
||||||
|
if (id) {
|
||||||
|
await dispatch(deleteItem(id));
|
||||||
|
await loadData(0);
|
||||||
|
setIsModalTrashActive(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const generateFilterRequests = useMemo(() => {
|
||||||
|
let request = '&';
|
||||||
|
filterItems.forEach((item) => {
|
||||||
|
const isRangeFilter = filters.find(
|
||||||
|
(filter) =>
|
||||||
|
filter.title === item.fields.selectedField &&
|
||||||
|
(filter.number || filter.date),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (isRangeFilter) {
|
||||||
|
const from = item.fields.filterValueFrom;
|
||||||
|
const to = item.fields.filterValueTo;
|
||||||
|
if (from) {
|
||||||
|
request += `${item.fields.selectedField}Range=${from}&`;
|
||||||
|
}
|
||||||
|
if (to) {
|
||||||
|
request += `${item.fields.selectedField}Range=${to}&`;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const value = item.fields.filterValue;
|
||||||
|
if (value) {
|
||||||
|
request += `${item.fields.selectedField}=${value}&`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return request;
|
||||||
|
}, [filterItems, filters]);
|
||||||
|
|
||||||
|
const deleteFilter = (value) => {
|
||||||
|
const newItems = filterItems.filter((item) => item.id !== value);
|
||||||
|
|
||||||
|
if (newItems.length) {
|
||||||
|
setFilterItems(newItems);
|
||||||
|
} else {
|
||||||
|
loadData(0, '');
|
||||||
|
|
||||||
|
setFilterItems(newItems);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSubmit = () => {
|
||||||
|
loadData(0, generateFilterRequests);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleChange = (id) => (e) => {
|
||||||
|
const value = e.target.value;
|
||||||
|
const name = e.target.name;
|
||||||
|
|
||||||
|
setFilterItems(
|
||||||
|
filterItems.map((item) => {
|
||||||
|
if (item.id !== id) return item;
|
||||||
|
if (name === 'selectedField') return { id, fields: { [name]: value } };
|
||||||
|
|
||||||
|
return { id, fields: { ...item.fields, [name]: value } };
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleReset = () => {
|
||||||
|
setFilterItems([]);
|
||||||
|
loadData(0, '');
|
||||||
|
};
|
||||||
|
|
||||||
|
const onPageChange = (page: number) => {
|
||||||
|
loadData(page);
|
||||||
|
setCurrentPage(page);
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!currentUser) return;
|
||||||
|
|
||||||
|
loadColumns(handleDeleteModalAction, `conversions`, currentUser).then(
|
||||||
|
(newCols) => setColumns(newCols),
|
||||||
|
);
|
||||||
|
}, [currentUser]);
|
||||||
|
|
||||||
|
const handleTableSubmit = async (id: string, data) => {
|
||||||
|
if (!_.isEmpty(data)) {
|
||||||
|
await dispatch(update({ id, data }))
|
||||||
|
.unwrap()
|
||||||
|
.then((res) => res)
|
||||||
|
.catch((err) => {
|
||||||
|
throw new Error(err);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const onDeleteRows = async (selectedRows) => {
|
||||||
|
await dispatch(deleteItemsByIds(selectedRows));
|
||||||
|
await loadData(0);
|
||||||
|
};
|
||||||
|
|
||||||
|
const controlClasses =
|
||||||
|
'w-full py-2 px-2 my-2 rounded dark:placeholder-gray-400 ' +
|
||||||
|
` ${bgColor} ${focusRing} ${corners} ` +
|
||||||
|
'dark:bg-slate-800 border';
|
||||||
|
|
||||||
|
const dataGrid = (
|
||||||
|
<div className='relative overflow-x-auto'>
|
||||||
|
<DataGrid
|
||||||
|
autoHeight
|
||||||
|
rowHeight={64}
|
||||||
|
sx={dataGridStyles}
|
||||||
|
className={'datagrid--table'}
|
||||||
|
getRowClassName={() => `datagrid--row`}
|
||||||
|
rows={conversions ?? []}
|
||||||
|
columns={columns}
|
||||||
|
initialState={{
|
||||||
|
pagination: {
|
||||||
|
paginationModel: {
|
||||||
|
pageSize: 10,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
disableRowSelectionOnClick
|
||||||
|
onProcessRowUpdateError={(params) => {
|
||||||
|
console.log('Error', params);
|
||||||
|
}}
|
||||||
|
processRowUpdate={async (newRow, oldRow) => {
|
||||||
|
const data = dataFormatter.dataGridEditFormatter(newRow);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await handleTableSubmit(newRow.id, data);
|
||||||
|
return newRow;
|
||||||
|
} catch {
|
||||||
|
return oldRow;
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
sortingMode={'server'}
|
||||||
|
checkboxSelection
|
||||||
|
onRowSelectionModelChange={(ids) => {
|
||||||
|
setSelectedRows(ids);
|
||||||
|
}}
|
||||||
|
onSortModelChange={(params) => {
|
||||||
|
params.length
|
||||||
|
? setSortModel(params)
|
||||||
|
: setSortModel([{ field: '', sort: 'desc' }]);
|
||||||
|
}}
|
||||||
|
rowCount={count}
|
||||||
|
pageSizeOptions={[10]}
|
||||||
|
paginationMode={'server'}
|
||||||
|
loading={loading}
|
||||||
|
onPaginationModelChange={(params) => {
|
||||||
|
onPageChange(params.page);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{filterItems && Array.isArray(filterItems) && filterItems.length ? (
|
||||||
|
<CardBox>
|
||||||
|
<Formik
|
||||||
|
initialValues={{
|
||||||
|
checkboxes: ['lorem'],
|
||||||
|
switches: ['lorem'],
|
||||||
|
radio: 'lorem',
|
||||||
|
}}
|
||||||
|
onSubmit={() => null}
|
||||||
|
>
|
||||||
|
<Form>
|
||||||
|
<>
|
||||||
|
{filterItems &&
|
||||||
|
filterItems.map((filterItem) => {
|
||||||
|
return (
|
||||||
|
<div key={filterItem.id} className='flex mb-4'>
|
||||||
|
<div className='flex flex-col w-full mr-3'>
|
||||||
|
<div className=' text-gray-500 font-bold'>
|
||||||
|
Filter
|
||||||
|
</div>
|
||||||
|
<Field
|
||||||
|
className={controlClasses}
|
||||||
|
name='selectedField'
|
||||||
|
id='selectedField'
|
||||||
|
component='select'
|
||||||
|
value={filterItem?.fields?.selectedField || ''}
|
||||||
|
onChange={handleChange(filterItem.id)}
|
||||||
|
>
|
||||||
|
{filters.map((selectOption) => (
|
||||||
|
<option
|
||||||
|
key={selectOption.title}
|
||||||
|
value={`${selectOption.title}`}
|
||||||
|
>
|
||||||
|
{selectOption.label}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</Field>
|
||||||
|
</div>
|
||||||
|
{filters.find(
|
||||||
|
(filter) =>
|
||||||
|
filter.title === filterItem?.fields?.selectedField,
|
||||||
|
)?.type === 'enum' ? (
|
||||||
|
<div className='flex flex-col w-full mr-3'>
|
||||||
|
<div className='text-gray-500 font-bold'>Value</div>
|
||||||
|
<Field
|
||||||
|
className={controlClasses}
|
||||||
|
name='filterValue'
|
||||||
|
id='filterValue'
|
||||||
|
component='select'
|
||||||
|
value={filterItem?.fields?.filterValue || ''}
|
||||||
|
onChange={handleChange(filterItem.id)}
|
||||||
|
>
|
||||||
|
<option value=''>Select Value</option>
|
||||||
|
{filters
|
||||||
|
.find(
|
||||||
|
(filter) =>
|
||||||
|
filter.title ===
|
||||||
|
filterItem?.fields?.selectedField,
|
||||||
|
)
|
||||||
|
?.options?.map((option) => (
|
||||||
|
<option key={option} value={option}>
|
||||||
|
{option}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</Field>
|
||||||
|
</div>
|
||||||
|
) : filters.find(
|
||||||
|
(filter) =>
|
||||||
|
filter.title ===
|
||||||
|
filterItem?.fields?.selectedField,
|
||||||
|
)?.number ? (
|
||||||
|
<div className='flex flex-row w-full mr-3'>
|
||||||
|
<div className='flex flex-col w-full mr-3'>
|
||||||
|
<div className=' text-gray-500 font-bold'>
|
||||||
|
From
|
||||||
|
</div>
|
||||||
|
<Field
|
||||||
|
className={controlClasses}
|
||||||
|
name='filterValueFrom'
|
||||||
|
placeholder='From'
|
||||||
|
id='filterValueFrom'
|
||||||
|
value={
|
||||||
|
filterItem?.fields?.filterValueFrom || ''
|
||||||
|
}
|
||||||
|
onChange={handleChange(filterItem.id)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className='flex flex-col w-full'>
|
||||||
|
<div className=' text-gray-500 font-bold'>
|
||||||
|
To
|
||||||
|
</div>
|
||||||
|
<Field
|
||||||
|
className={controlClasses}
|
||||||
|
name='filterValueTo'
|
||||||
|
placeholder='to'
|
||||||
|
id='filterValueTo'
|
||||||
|
value={filterItem?.fields?.filterValueTo || ''}
|
||||||
|
onChange={handleChange(filterItem.id)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
) : filters.find(
|
||||||
|
(filter) =>
|
||||||
|
filter.title ===
|
||||||
|
filterItem?.fields?.selectedField,
|
||||||
|
)?.date ? (
|
||||||
|
<div className='flex flex-row w-full mr-3'>
|
||||||
|
<div className='flex flex-col w-full mr-3'>
|
||||||
|
<div className=' text-gray-500 font-bold'>
|
||||||
|
From
|
||||||
|
</div>
|
||||||
|
<Field
|
||||||
|
className={controlClasses}
|
||||||
|
name='filterValueFrom'
|
||||||
|
placeholder='From'
|
||||||
|
id='filterValueFrom'
|
||||||
|
type='datetime-local'
|
||||||
|
value={
|
||||||
|
filterItem?.fields?.filterValueFrom || ''
|
||||||
|
}
|
||||||
|
onChange={handleChange(filterItem.id)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className='flex flex-col w-full'>
|
||||||
|
<div className=' text-gray-500 font-bold'>
|
||||||
|
To
|
||||||
|
</div>
|
||||||
|
<Field
|
||||||
|
className={controlClasses}
|
||||||
|
name='filterValueTo'
|
||||||
|
placeholder='to'
|
||||||
|
id='filterValueTo'
|
||||||
|
type='datetime-local'
|
||||||
|
value={filterItem?.fields?.filterValueTo || ''}
|
||||||
|
onChange={handleChange(filterItem.id)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className='flex flex-col w-full mr-3'>
|
||||||
|
<div className=' text-gray-500 font-bold'>
|
||||||
|
Contains
|
||||||
|
</div>
|
||||||
|
<Field
|
||||||
|
className={controlClasses}
|
||||||
|
name='filterValue'
|
||||||
|
placeholder='Contained'
|
||||||
|
id='filterValue'
|
||||||
|
value={filterItem?.fields?.filterValue || ''}
|
||||||
|
onChange={handleChange(filterItem.id)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<div className='flex flex-col'>
|
||||||
|
<div className=' text-gray-500 font-bold'>
|
||||||
|
Action
|
||||||
|
</div>
|
||||||
|
<BaseButton
|
||||||
|
className='my-2'
|
||||||
|
type='reset'
|
||||||
|
color='danger'
|
||||||
|
label='Delete'
|
||||||
|
onClick={() => {
|
||||||
|
deleteFilter(filterItem.id);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
<div className='flex'>
|
||||||
|
<BaseButton
|
||||||
|
className='my-2 mr-3'
|
||||||
|
color='success'
|
||||||
|
label='Apply'
|
||||||
|
onClick={handleSubmit}
|
||||||
|
/>
|
||||||
|
<BaseButton
|
||||||
|
className='my-2'
|
||||||
|
color='info'
|
||||||
|
label='Cancel'
|
||||||
|
onClick={handleReset}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
</Form>
|
||||||
|
</Formik>
|
||||||
|
</CardBox>
|
||||||
|
) : null}
|
||||||
|
<CardBoxModal
|
||||||
|
title='Please confirm'
|
||||||
|
buttonColor='info'
|
||||||
|
buttonLabel={loading ? 'Deleting...' : 'Confirm'}
|
||||||
|
isActive={isModalTrashActive}
|
||||||
|
onConfirm={handleDeleteAction}
|
||||||
|
onCancel={handleModalAction}
|
||||||
|
>
|
||||||
|
<p>Are you sure you want to delete this item?</p>
|
||||||
|
</CardBoxModal>
|
||||||
|
|
||||||
|
{dataGrid}
|
||||||
|
|
||||||
|
{selectedRows.length > 0 &&
|
||||||
|
createPortal(
|
||||||
|
<BaseButton
|
||||||
|
className='me-4'
|
||||||
|
color='danger'
|
||||||
|
label={`Delete ${selectedRows.length === 1 ? 'Row' : 'Rows'}`}
|
||||||
|
onClick={() => onDeleteRows(selectedRows)}
|
||||||
|
/>,
|
||||||
|
document.getElementById('delete-rows-button'),
|
||||||
|
)}
|
||||||
|
<ToastContainer />
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default TableSampleConversions;
|
||||||
172
frontend/src/components/Conversions/configureConversionsCols.tsx
Normal file
172
frontend/src/components/Conversions/configureConversionsCols.tsx
Normal file
@ -0,0 +1,172 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import BaseIcon from '../BaseIcon';
|
||||||
|
import { mdiEye, mdiTrashCan, mdiPencilOutline } from '@mdi/js';
|
||||||
|
import axios from 'axios';
|
||||||
|
import {
|
||||||
|
GridActionsCellItem,
|
||||||
|
GridRowParams,
|
||||||
|
GridValueGetterParams,
|
||||||
|
} from '@mui/x-data-grid';
|
||||||
|
import ImageField from '../ImageField';
|
||||||
|
import { saveFile } from '../../helpers/fileSaver';
|
||||||
|
import dataFormatter from '../../helpers/dataFormatter';
|
||||||
|
import DataGridMultiSelect from '../DataGridMultiSelect';
|
||||||
|
import ListActionsPopover from '../ListActionsPopover';
|
||||||
|
|
||||||
|
import { hasPermission } from '../../helpers/userPermissions';
|
||||||
|
|
||||||
|
type Params = (id: string) => void;
|
||||||
|
|
||||||
|
export const loadColumns = async (
|
||||||
|
onDelete: Params,
|
||||||
|
entityName: string,
|
||||||
|
|
||||||
|
user,
|
||||||
|
) => {
|
||||||
|
async function callOptionsApi(entityName: string) {
|
||||||
|
if (!hasPermission(user, 'READ_' + entityName.toUpperCase())) return [];
|
||||||
|
|
||||||
|
try {
|
||||||
|
const data = await axios(`/${entityName}/autocomplete?limit=100`);
|
||||||
|
return data.data;
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const hasUpdatePermission = hasPermission(user, 'UPDATE_CONVERSIONS');
|
||||||
|
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
field: 'input_value',
|
||||||
|
headerName: 'InputValue',
|
||||||
|
flex: 1,
|
||||||
|
minWidth: 120,
|
||||||
|
filterable: false,
|
||||||
|
headerClassName: 'datagrid--header',
|
||||||
|
cellClassName: 'datagrid--cell',
|
||||||
|
|
||||||
|
editable: hasUpdatePermission,
|
||||||
|
|
||||||
|
type: 'number',
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
field: 'input_unit',
|
||||||
|
headerName: 'InputUnit',
|
||||||
|
flex: 1,
|
||||||
|
minWidth: 120,
|
||||||
|
filterable: false,
|
||||||
|
headerClassName: 'datagrid--header',
|
||||||
|
cellClassName: 'datagrid--cell',
|
||||||
|
|
||||||
|
editable: hasUpdatePermission,
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
field: 'output_value_btu_h',
|
||||||
|
headerName: 'OutputValueBTU/h',
|
||||||
|
flex: 1,
|
||||||
|
minWidth: 120,
|
||||||
|
filterable: false,
|
||||||
|
headerClassName: 'datagrid--header',
|
||||||
|
cellClassName: 'datagrid--cell',
|
||||||
|
|
||||||
|
editable: hasUpdatePermission,
|
||||||
|
|
||||||
|
type: 'number',
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
field: 'output_value_kw',
|
||||||
|
headerName: 'OutputValuekW',
|
||||||
|
flex: 1,
|
||||||
|
minWidth: 120,
|
||||||
|
filterable: false,
|
||||||
|
headerClassName: 'datagrid--header',
|
||||||
|
cellClassName: 'datagrid--cell',
|
||||||
|
|
||||||
|
editable: hasUpdatePermission,
|
||||||
|
|
||||||
|
type: 'number',
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
field: 'output_value_kcal_h',
|
||||||
|
headerName: 'OutputValuekcal/h',
|
||||||
|
flex: 1,
|
||||||
|
minWidth: 120,
|
||||||
|
filterable: false,
|
||||||
|
headerClassName: 'datagrid--header',
|
||||||
|
cellClassName: 'datagrid--cell',
|
||||||
|
|
||||||
|
editable: hasUpdatePermission,
|
||||||
|
|
||||||
|
type: 'number',
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
field: 'output_value_tr',
|
||||||
|
headerName: 'OutputValueTR',
|
||||||
|
flex: 1,
|
||||||
|
minWidth: 120,
|
||||||
|
filterable: false,
|
||||||
|
headerClassName: 'datagrid--header',
|
||||||
|
cellClassName: 'datagrid--cell',
|
||||||
|
|
||||||
|
editable: hasUpdatePermission,
|
||||||
|
|
||||||
|
type: 'number',
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
field: 'output_value_hp',
|
||||||
|
headerName: 'OutputValueHP',
|
||||||
|
flex: 1,
|
||||||
|
minWidth: 120,
|
||||||
|
filterable: false,
|
||||||
|
headerClassName: 'datagrid--header',
|
||||||
|
cellClassName: 'datagrid--cell',
|
||||||
|
|
||||||
|
editable: hasUpdatePermission,
|
||||||
|
|
||||||
|
type: 'number',
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
field: 'output_value_w',
|
||||||
|
headerName: 'OutputValueW',
|
||||||
|
flex: 1,
|
||||||
|
minWidth: 120,
|
||||||
|
filterable: false,
|
||||||
|
headerClassName: 'datagrid--header',
|
||||||
|
cellClassName: 'datagrid--cell',
|
||||||
|
|
||||||
|
editable: hasUpdatePermission,
|
||||||
|
|
||||||
|
type: 'number',
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
field: 'actions',
|
||||||
|
type: 'actions',
|
||||||
|
minWidth: 30,
|
||||||
|
headerClassName: 'datagrid--header',
|
||||||
|
cellClassName: 'datagrid--cell',
|
||||||
|
getActions: (params: GridRowParams) => {
|
||||||
|
return [
|
||||||
|
<div key={params?.row?.id}>
|
||||||
|
<ListActionsPopover
|
||||||
|
onDelete={onDelete}
|
||||||
|
itemId={params?.row?.id}
|
||||||
|
pathEdit={`/conversions/conversions-edit/?id=${params?.row?.id}`}
|
||||||
|
pathView={`/conversions/conversions-view/?id=${params?.row?.id}`}
|
||||||
|
hasUpdatePermission={hasUpdatePermission}
|
||||||
|
/>
|
||||||
|
</div>,
|
||||||
|
];
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
};
|
||||||
@ -16,6 +16,17 @@ const menuAside: MenuAsideItem[] = [
|
|||||||
icon: icon.mdiAccountGroup ?? icon.mdiTable,
|
icon: icon.mdiAccountGroup ?? icon.mdiTable,
|
||||||
permissions: 'READ_USERS',
|
permissions: 'READ_USERS',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
href: '/conversions/conversions-list',
|
||||||
|
label: 'Conversions',
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
|
// @ts-ignore
|
||||||
|
icon:
|
||||||
|
'mdiSwapHorizontal' in icon
|
||||||
|
? icon['mdiSwapHorizontal' as keyof typeof icon]
|
||||||
|
: icon.mdiTable ?? icon.mdiTable,
|
||||||
|
permissions: 'READ_CONVERSIONS',
|
||||||
|
},
|
||||||
{
|
{
|
||||||
href: '/roles/roles-list',
|
href: '/roles/roles-list',
|
||||||
label: 'Roles',
|
label: 'Roles',
|
||||||
|
|||||||
208
frontend/src/pages/conversions/[conversionsId].tsx
Normal file
208
frontend/src/pages/conversions/[conversionsId].tsx
Normal file
@ -0,0 +1,208 @@
|
|||||||
|
import { mdiChartTimelineVariant, mdiUpload } from '@mdi/js';
|
||||||
|
import Head from 'next/head';
|
||||||
|
import React, { ReactElement, useEffect, useState } from 'react';
|
||||||
|
import DatePicker from 'react-datepicker';
|
||||||
|
import 'react-datepicker/dist/react-datepicker.css';
|
||||||
|
import dayjs from 'dayjs';
|
||||||
|
|
||||||
|
import CardBox from '../../components/CardBox';
|
||||||
|
import LayoutAuthenticated from '../../layouts/Authenticated';
|
||||||
|
import SectionMain from '../../components/SectionMain';
|
||||||
|
import SectionTitleLineWithButton from '../../components/SectionTitleLineWithButton';
|
||||||
|
import { getPageTitle } from '../../config';
|
||||||
|
|
||||||
|
import { Field, Form, Formik } from 'formik';
|
||||||
|
import FormField from '../../components/FormField';
|
||||||
|
import BaseDivider from '../../components/BaseDivider';
|
||||||
|
import BaseButtons from '../../components/BaseButtons';
|
||||||
|
import BaseButton from '../../components/BaseButton';
|
||||||
|
import FormCheckRadio from '../../components/FormCheckRadio';
|
||||||
|
import FormCheckRadioGroup from '../../components/FormCheckRadioGroup';
|
||||||
|
import FormFilePicker from '../../components/FormFilePicker';
|
||||||
|
import FormImagePicker from '../../components/FormImagePicker';
|
||||||
|
import { SelectField } from '../../components/SelectField';
|
||||||
|
import { SelectFieldMany } from '../../components/SelectFieldMany';
|
||||||
|
import { SwitchField } from '../../components/SwitchField';
|
||||||
|
import { RichTextField } from '../../components/RichTextField';
|
||||||
|
|
||||||
|
import { update, fetch } from '../../stores/conversions/conversionsSlice';
|
||||||
|
import { useAppDispatch, useAppSelector } from '../../stores/hooks';
|
||||||
|
import { useRouter } from 'next/router';
|
||||||
|
import { saveFile } from '../../helpers/fileSaver';
|
||||||
|
import dataFormatter from '../../helpers/dataFormatter';
|
||||||
|
import ImageField from '../../components/ImageField';
|
||||||
|
|
||||||
|
const EditConversions = () => {
|
||||||
|
const router = useRouter();
|
||||||
|
const dispatch = useAppDispatch();
|
||||||
|
const initVals = {
|
||||||
|
input_value: '',
|
||||||
|
|
||||||
|
input_unit: '',
|
||||||
|
|
||||||
|
output_value_btu_h: '',
|
||||||
|
|
||||||
|
output_value_kw: '',
|
||||||
|
|
||||||
|
output_value_kcal_h: '',
|
||||||
|
|
||||||
|
output_value_tr: '',
|
||||||
|
|
||||||
|
output_value_hp: '',
|
||||||
|
|
||||||
|
output_value_w: '',
|
||||||
|
};
|
||||||
|
const [initialValues, setInitialValues] = useState(initVals);
|
||||||
|
|
||||||
|
const { conversions } = useAppSelector((state) => state.conversions);
|
||||||
|
|
||||||
|
const { conversionsId } = router.query;
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
dispatch(fetch({ id: conversionsId }));
|
||||||
|
}, [conversionsId]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (typeof conversions === 'object') {
|
||||||
|
setInitialValues(conversions);
|
||||||
|
}
|
||||||
|
}, [conversions]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (typeof conversions === 'object') {
|
||||||
|
const newInitialVal = { ...initVals };
|
||||||
|
|
||||||
|
Object.keys(initVals).forEach(
|
||||||
|
(el) => (newInitialVal[el] = conversions[el]),
|
||||||
|
);
|
||||||
|
|
||||||
|
setInitialValues(newInitialVal);
|
||||||
|
}
|
||||||
|
}, [conversions]);
|
||||||
|
|
||||||
|
const handleSubmit = async (data) => {
|
||||||
|
await dispatch(update({ id: conversionsId, data }));
|
||||||
|
await router.push('/conversions/conversions-list');
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Head>
|
||||||
|
<title>{getPageTitle('Edit conversions')}</title>
|
||||||
|
</Head>
|
||||||
|
<SectionMain>
|
||||||
|
<SectionTitleLineWithButton
|
||||||
|
icon={mdiChartTimelineVariant}
|
||||||
|
title={'Edit conversions'}
|
||||||
|
main
|
||||||
|
>
|
||||||
|
{''}
|
||||||
|
</SectionTitleLineWithButton>
|
||||||
|
<CardBox>
|
||||||
|
<Formik
|
||||||
|
enableReinitialize
|
||||||
|
initialValues={initialValues}
|
||||||
|
onSubmit={(values) => handleSubmit(values)}
|
||||||
|
>
|
||||||
|
<Form>
|
||||||
|
<FormField label='InputValue'>
|
||||||
|
<Field
|
||||||
|
type='number'
|
||||||
|
name='input_value'
|
||||||
|
placeholder='InputValue'
|
||||||
|
/>
|
||||||
|
</FormField>
|
||||||
|
|
||||||
|
<FormField label='InputUnit' labelFor='input_unit'>
|
||||||
|
<Field name='input_unit' id='input_unit' component='select'>
|
||||||
|
<option value='BTU/h'>BTU/h</option>
|
||||||
|
|
||||||
|
<option value='kW'>kW</option>
|
||||||
|
|
||||||
|
<option value='kcal/h'>kcal/h</option>
|
||||||
|
|
||||||
|
<option value='TR'>TR</option>
|
||||||
|
|
||||||
|
<option value='HP'>HP</option>
|
||||||
|
|
||||||
|
<option value='W'>W</option>
|
||||||
|
</Field>
|
||||||
|
</FormField>
|
||||||
|
|
||||||
|
<FormField label='OutputValueBTU/h'>
|
||||||
|
<Field
|
||||||
|
type='number'
|
||||||
|
name='output_value_btu_h'
|
||||||
|
placeholder='OutputValueBTU/h'
|
||||||
|
/>
|
||||||
|
</FormField>
|
||||||
|
|
||||||
|
<FormField label='OutputValuekW'>
|
||||||
|
<Field
|
||||||
|
type='number'
|
||||||
|
name='output_value_kw'
|
||||||
|
placeholder='OutputValuekW'
|
||||||
|
/>
|
||||||
|
</FormField>
|
||||||
|
|
||||||
|
<FormField label='OutputValuekcal/h'>
|
||||||
|
<Field
|
||||||
|
type='number'
|
||||||
|
name='output_value_kcal_h'
|
||||||
|
placeholder='OutputValuekcal/h'
|
||||||
|
/>
|
||||||
|
</FormField>
|
||||||
|
|
||||||
|
<FormField label='OutputValueTR'>
|
||||||
|
<Field
|
||||||
|
type='number'
|
||||||
|
name='output_value_tr'
|
||||||
|
placeholder='OutputValueTR'
|
||||||
|
/>
|
||||||
|
</FormField>
|
||||||
|
|
||||||
|
<FormField label='OutputValueHP'>
|
||||||
|
<Field
|
||||||
|
type='number'
|
||||||
|
name='output_value_hp'
|
||||||
|
placeholder='OutputValueHP'
|
||||||
|
/>
|
||||||
|
</FormField>
|
||||||
|
|
||||||
|
<FormField label='OutputValueW'>
|
||||||
|
<Field
|
||||||
|
type='number'
|
||||||
|
name='output_value_w'
|
||||||
|
placeholder='OutputValueW'
|
||||||
|
/>
|
||||||
|
</FormField>
|
||||||
|
|
||||||
|
<BaseDivider />
|
||||||
|
<BaseButtons>
|
||||||
|
<BaseButton type='submit' color='info' label='Submit' />
|
||||||
|
<BaseButton type='reset' color='info' outline label='Reset' />
|
||||||
|
<BaseButton
|
||||||
|
type='reset'
|
||||||
|
color='danger'
|
||||||
|
outline
|
||||||
|
label='Cancel'
|
||||||
|
onClick={() => router.push('/conversions/conversions-list')}
|
||||||
|
/>
|
||||||
|
</BaseButtons>
|
||||||
|
</Form>
|
||||||
|
</Formik>
|
||||||
|
</CardBox>
|
||||||
|
</SectionMain>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
EditConversions.getLayout = function getLayout(page: ReactElement) {
|
||||||
|
return (
|
||||||
|
<LayoutAuthenticated permission={'UPDATE_CONVERSIONS'}>
|
||||||
|
{page}
|
||||||
|
</LayoutAuthenticated>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default EditConversions;
|
||||||
206
frontend/src/pages/conversions/conversions-edit.tsx
Normal file
206
frontend/src/pages/conversions/conversions-edit.tsx
Normal file
@ -0,0 +1,206 @@
|
|||||||
|
import { mdiChartTimelineVariant, mdiUpload } from '@mdi/js';
|
||||||
|
import Head from 'next/head';
|
||||||
|
import React, { ReactElement, useEffect, useState } from 'react';
|
||||||
|
import DatePicker from 'react-datepicker';
|
||||||
|
import 'react-datepicker/dist/react-datepicker.css';
|
||||||
|
import dayjs from 'dayjs';
|
||||||
|
|
||||||
|
import CardBox from '../../components/CardBox';
|
||||||
|
import LayoutAuthenticated from '../../layouts/Authenticated';
|
||||||
|
import SectionMain from '../../components/SectionMain';
|
||||||
|
import SectionTitleLineWithButton from '../../components/SectionTitleLineWithButton';
|
||||||
|
import { getPageTitle } from '../../config';
|
||||||
|
|
||||||
|
import { Field, Form, Formik } from 'formik';
|
||||||
|
import FormField from '../../components/FormField';
|
||||||
|
import BaseDivider from '../../components/BaseDivider';
|
||||||
|
import BaseButtons from '../../components/BaseButtons';
|
||||||
|
import BaseButton from '../../components/BaseButton';
|
||||||
|
import FormCheckRadio from '../../components/FormCheckRadio';
|
||||||
|
import FormCheckRadioGroup from '../../components/FormCheckRadioGroup';
|
||||||
|
import FormFilePicker from '../../components/FormFilePicker';
|
||||||
|
import FormImagePicker from '../../components/FormImagePicker';
|
||||||
|
import { SelectField } from '../../components/SelectField';
|
||||||
|
import { SelectFieldMany } from '../../components/SelectFieldMany';
|
||||||
|
import { SwitchField } from '../../components/SwitchField';
|
||||||
|
import { RichTextField } from '../../components/RichTextField';
|
||||||
|
|
||||||
|
import { update, fetch } from '../../stores/conversions/conversionsSlice';
|
||||||
|
import { useAppDispatch, useAppSelector } from '../../stores/hooks';
|
||||||
|
import { useRouter } from 'next/router';
|
||||||
|
import { saveFile } from '../../helpers/fileSaver';
|
||||||
|
import dataFormatter from '../../helpers/dataFormatter';
|
||||||
|
import ImageField from '../../components/ImageField';
|
||||||
|
|
||||||
|
const EditConversionsPage = () => {
|
||||||
|
const router = useRouter();
|
||||||
|
const dispatch = useAppDispatch();
|
||||||
|
const initVals = {
|
||||||
|
input_value: '',
|
||||||
|
|
||||||
|
input_unit: '',
|
||||||
|
|
||||||
|
output_value_btu_h: '',
|
||||||
|
|
||||||
|
output_value_kw: '',
|
||||||
|
|
||||||
|
output_value_kcal_h: '',
|
||||||
|
|
||||||
|
output_value_tr: '',
|
||||||
|
|
||||||
|
output_value_hp: '',
|
||||||
|
|
||||||
|
output_value_w: '',
|
||||||
|
};
|
||||||
|
const [initialValues, setInitialValues] = useState(initVals);
|
||||||
|
|
||||||
|
const { conversions } = useAppSelector((state) => state.conversions);
|
||||||
|
|
||||||
|
const { id } = router.query;
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
dispatch(fetch({ id: id }));
|
||||||
|
}, [id]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (typeof conversions === 'object') {
|
||||||
|
setInitialValues(conversions);
|
||||||
|
}
|
||||||
|
}, [conversions]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (typeof conversions === 'object') {
|
||||||
|
const newInitialVal = { ...initVals };
|
||||||
|
Object.keys(initVals).forEach(
|
||||||
|
(el) => (newInitialVal[el] = conversions[el]),
|
||||||
|
);
|
||||||
|
setInitialValues(newInitialVal);
|
||||||
|
}
|
||||||
|
}, [conversions]);
|
||||||
|
|
||||||
|
const handleSubmit = async (data) => {
|
||||||
|
await dispatch(update({ id: id, data }));
|
||||||
|
await router.push('/conversions/conversions-list');
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Head>
|
||||||
|
<title>{getPageTitle('Edit conversions')}</title>
|
||||||
|
</Head>
|
||||||
|
<SectionMain>
|
||||||
|
<SectionTitleLineWithButton
|
||||||
|
icon={mdiChartTimelineVariant}
|
||||||
|
title={'Edit conversions'}
|
||||||
|
main
|
||||||
|
>
|
||||||
|
{''}
|
||||||
|
</SectionTitleLineWithButton>
|
||||||
|
<CardBox>
|
||||||
|
<Formik
|
||||||
|
enableReinitialize
|
||||||
|
initialValues={initialValues}
|
||||||
|
onSubmit={(values) => handleSubmit(values)}
|
||||||
|
>
|
||||||
|
<Form>
|
||||||
|
<FormField label='InputValue'>
|
||||||
|
<Field
|
||||||
|
type='number'
|
||||||
|
name='input_value'
|
||||||
|
placeholder='InputValue'
|
||||||
|
/>
|
||||||
|
</FormField>
|
||||||
|
|
||||||
|
<FormField label='InputUnit' labelFor='input_unit'>
|
||||||
|
<Field name='input_unit' id='input_unit' component='select'>
|
||||||
|
<option value='BTU/h'>BTU/h</option>
|
||||||
|
|
||||||
|
<option value='kW'>kW</option>
|
||||||
|
|
||||||
|
<option value='kcal/h'>kcal/h</option>
|
||||||
|
|
||||||
|
<option value='TR'>TR</option>
|
||||||
|
|
||||||
|
<option value='HP'>HP</option>
|
||||||
|
|
||||||
|
<option value='W'>W</option>
|
||||||
|
</Field>
|
||||||
|
</FormField>
|
||||||
|
|
||||||
|
<FormField label='OutputValueBTU/h'>
|
||||||
|
<Field
|
||||||
|
type='number'
|
||||||
|
name='output_value_btu_h'
|
||||||
|
placeholder='OutputValueBTU/h'
|
||||||
|
/>
|
||||||
|
</FormField>
|
||||||
|
|
||||||
|
<FormField label='OutputValuekW'>
|
||||||
|
<Field
|
||||||
|
type='number'
|
||||||
|
name='output_value_kw'
|
||||||
|
placeholder='OutputValuekW'
|
||||||
|
/>
|
||||||
|
</FormField>
|
||||||
|
|
||||||
|
<FormField label='OutputValuekcal/h'>
|
||||||
|
<Field
|
||||||
|
type='number'
|
||||||
|
name='output_value_kcal_h'
|
||||||
|
placeholder='OutputValuekcal/h'
|
||||||
|
/>
|
||||||
|
</FormField>
|
||||||
|
|
||||||
|
<FormField label='OutputValueTR'>
|
||||||
|
<Field
|
||||||
|
type='number'
|
||||||
|
name='output_value_tr'
|
||||||
|
placeholder='OutputValueTR'
|
||||||
|
/>
|
||||||
|
</FormField>
|
||||||
|
|
||||||
|
<FormField label='OutputValueHP'>
|
||||||
|
<Field
|
||||||
|
type='number'
|
||||||
|
name='output_value_hp'
|
||||||
|
placeholder='OutputValueHP'
|
||||||
|
/>
|
||||||
|
</FormField>
|
||||||
|
|
||||||
|
<FormField label='OutputValueW'>
|
||||||
|
<Field
|
||||||
|
type='number'
|
||||||
|
name='output_value_w'
|
||||||
|
placeholder='OutputValueW'
|
||||||
|
/>
|
||||||
|
</FormField>
|
||||||
|
|
||||||
|
<BaseDivider />
|
||||||
|
<BaseButtons>
|
||||||
|
<BaseButton type='submit' color='info' label='Submit' />
|
||||||
|
<BaseButton type='reset' color='info' outline label='Reset' />
|
||||||
|
<BaseButton
|
||||||
|
type='reset'
|
||||||
|
color='danger'
|
||||||
|
outline
|
||||||
|
label='Cancel'
|
||||||
|
onClick={() => router.push('/conversions/conversions-list')}
|
||||||
|
/>
|
||||||
|
</BaseButtons>
|
||||||
|
</Form>
|
||||||
|
</Formik>
|
||||||
|
</CardBox>
|
||||||
|
</SectionMain>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
EditConversionsPage.getLayout = function getLayout(page: ReactElement) {
|
||||||
|
return (
|
||||||
|
<LayoutAuthenticated permission={'UPDATE_CONVERSIONS'}>
|
||||||
|
{page}
|
||||||
|
</LayoutAuthenticated>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default EditConversionsPage;
|
||||||
184
frontend/src/pages/conversions/conversions-list.tsx
Normal file
184
frontend/src/pages/conversions/conversions-list.tsx
Normal file
@ -0,0 +1,184 @@
|
|||||||
|
import { mdiChartTimelineVariant } from '@mdi/js';
|
||||||
|
import Head from 'next/head';
|
||||||
|
import { uniqueId } from 'lodash';
|
||||||
|
import React, { ReactElement, useState } from 'react';
|
||||||
|
import CardBox from '../../components/CardBox';
|
||||||
|
import LayoutAuthenticated from '../../layouts/Authenticated';
|
||||||
|
import SectionMain from '../../components/SectionMain';
|
||||||
|
import SectionTitleLineWithButton from '../../components/SectionTitleLineWithButton';
|
||||||
|
import { getPageTitle } from '../../config';
|
||||||
|
import TableConversions from '../../components/Conversions/TableConversions';
|
||||||
|
import BaseButton from '../../components/BaseButton';
|
||||||
|
import axios from 'axios';
|
||||||
|
import Link from 'next/link';
|
||||||
|
import { useAppDispatch, useAppSelector } from '../../stores/hooks';
|
||||||
|
import CardBoxModal from '../../components/CardBoxModal';
|
||||||
|
import DragDropFilePicker from '../../components/DragDropFilePicker';
|
||||||
|
import {
|
||||||
|
setRefetch,
|
||||||
|
uploadCsv,
|
||||||
|
} from '../../stores/conversions/conversionsSlice';
|
||||||
|
|
||||||
|
import { hasPermission } from '../../helpers/userPermissions';
|
||||||
|
|
||||||
|
const ConversionsTablesPage = () => {
|
||||||
|
const [filterItems, setFilterItems] = useState([]);
|
||||||
|
const [csvFile, setCsvFile] = useState<File | null>(null);
|
||||||
|
const [isModalActive, setIsModalActive] = useState(false);
|
||||||
|
const [showTableView, setShowTableView] = useState(false);
|
||||||
|
|
||||||
|
const { currentUser } = useAppSelector((state) => state.auth);
|
||||||
|
|
||||||
|
const dispatch = useAppDispatch();
|
||||||
|
|
||||||
|
const [filters] = useState([
|
||||||
|
{ label: 'InputValue', title: 'input_value', number: 'true' },
|
||||||
|
{ label: 'OutputValueBTU/h', title: 'output_value_btu_h', number: 'true' },
|
||||||
|
{ label: 'OutputValuekW', title: 'output_value_kw', number: 'true' },
|
||||||
|
{
|
||||||
|
label: 'OutputValuekcal/h',
|
||||||
|
title: 'output_value_kcal_h',
|
||||||
|
number: 'true',
|
||||||
|
},
|
||||||
|
{ label: 'OutputValueTR', title: 'output_value_tr', number: 'true' },
|
||||||
|
{ label: 'OutputValueHP', title: 'output_value_hp', number: 'true' },
|
||||||
|
{ label: 'OutputValueW', title: 'output_value_w', number: 'true' },
|
||||||
|
|
||||||
|
{
|
||||||
|
label: 'InputUnit',
|
||||||
|
title: 'input_unit',
|
||||||
|
type: 'enum',
|
||||||
|
options: ['BTU/h', 'kW', 'kcal/h', 'TR', 'HP', 'W'],
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
const hasCreatePermission =
|
||||||
|
currentUser && hasPermission(currentUser, 'CREATE_CONVERSIONS');
|
||||||
|
|
||||||
|
const addFilter = () => {
|
||||||
|
const newItem = {
|
||||||
|
id: uniqueId(),
|
||||||
|
fields: {
|
||||||
|
filterValue: '',
|
||||||
|
filterValueFrom: '',
|
||||||
|
filterValueTo: '',
|
||||||
|
selectedField: '',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
newItem.fields.selectedField = filters[0].title;
|
||||||
|
setFilterItems([...filterItems, newItem]);
|
||||||
|
};
|
||||||
|
|
||||||
|
const getConversionsCSV = async () => {
|
||||||
|
const response = await axios({
|
||||||
|
url: '/conversions?filetype=csv',
|
||||||
|
method: 'GET',
|
||||||
|
responseType: 'blob',
|
||||||
|
});
|
||||||
|
const type = response.headers['content-type'];
|
||||||
|
const blob = new Blob([response.data], { type: type });
|
||||||
|
const link = document.createElement('a');
|
||||||
|
link.href = window.URL.createObjectURL(blob);
|
||||||
|
link.download = 'conversionsCSV.csv';
|
||||||
|
link.click();
|
||||||
|
};
|
||||||
|
|
||||||
|
const onModalConfirm = async () => {
|
||||||
|
if (!csvFile) return;
|
||||||
|
await dispatch(uploadCsv(csvFile));
|
||||||
|
dispatch(setRefetch(true));
|
||||||
|
setCsvFile(null);
|
||||||
|
setIsModalActive(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
const onModalCancel = () => {
|
||||||
|
setCsvFile(null);
|
||||||
|
setIsModalActive(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Head>
|
||||||
|
<title>{getPageTitle('Conversions')}</title>
|
||||||
|
</Head>
|
||||||
|
<SectionMain>
|
||||||
|
<SectionTitleLineWithButton
|
||||||
|
icon={mdiChartTimelineVariant}
|
||||||
|
title='Conversions'
|
||||||
|
main
|
||||||
|
>
|
||||||
|
{''}
|
||||||
|
</SectionTitleLineWithButton>
|
||||||
|
<CardBox className='mb-6' cardBoxClassName='flex flex-wrap'>
|
||||||
|
{hasCreatePermission && (
|
||||||
|
<BaseButton
|
||||||
|
className={'mr-3'}
|
||||||
|
href={'/conversions/conversions-new'}
|
||||||
|
color='info'
|
||||||
|
label='New Item'
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<BaseButton
|
||||||
|
className={'mr-3'}
|
||||||
|
color='info'
|
||||||
|
label='Filter'
|
||||||
|
onClick={addFilter}
|
||||||
|
/>
|
||||||
|
<BaseButton
|
||||||
|
className={'mr-3'}
|
||||||
|
color='info'
|
||||||
|
label='Download CSV'
|
||||||
|
onClick={getConversionsCSV}
|
||||||
|
/>
|
||||||
|
|
||||||
|
{hasCreatePermission && (
|
||||||
|
<BaseButton
|
||||||
|
color='info'
|
||||||
|
label='Upload CSV'
|
||||||
|
onClick={() => setIsModalActive(true)}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className='md:inline-flex items-center ms-auto'>
|
||||||
|
<div id='delete-rows-button'></div>
|
||||||
|
</div>
|
||||||
|
</CardBox>
|
||||||
|
|
||||||
|
<CardBox className='mb-6' hasTable>
|
||||||
|
<TableConversions
|
||||||
|
filterItems={filterItems}
|
||||||
|
setFilterItems={setFilterItems}
|
||||||
|
filters={filters}
|
||||||
|
showGrid={false}
|
||||||
|
/>
|
||||||
|
</CardBox>
|
||||||
|
</SectionMain>
|
||||||
|
<CardBoxModal
|
||||||
|
title='Upload CSV'
|
||||||
|
buttonColor='info'
|
||||||
|
buttonLabel={'Confirm'}
|
||||||
|
// buttonLabel={false ? 'Deleting...' : 'Confirm'}
|
||||||
|
isActive={isModalActive}
|
||||||
|
onConfirm={onModalConfirm}
|
||||||
|
onCancel={onModalCancel}
|
||||||
|
>
|
||||||
|
<DragDropFilePicker
|
||||||
|
file={csvFile}
|
||||||
|
setFile={setCsvFile}
|
||||||
|
formats={'.csv'}
|
||||||
|
/>
|
||||||
|
</CardBoxModal>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
ConversionsTablesPage.getLayout = function getLayout(page: ReactElement) {
|
||||||
|
return (
|
||||||
|
<LayoutAuthenticated permission={'READ_CONVERSIONS'}>
|
||||||
|
{page}
|
||||||
|
</LayoutAuthenticated>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ConversionsTablesPage;
|
||||||
180
frontend/src/pages/conversions/conversions-new.tsx
Normal file
180
frontend/src/pages/conversions/conversions-new.tsx
Normal file
@ -0,0 +1,180 @@
|
|||||||
|
import {
|
||||||
|
mdiAccount,
|
||||||
|
mdiChartTimelineVariant,
|
||||||
|
mdiMail,
|
||||||
|
mdiUpload,
|
||||||
|
} from '@mdi/js';
|
||||||
|
import Head from 'next/head';
|
||||||
|
import React, { ReactElement } from 'react';
|
||||||
|
import CardBox from '../../components/CardBox';
|
||||||
|
import LayoutAuthenticated from '../../layouts/Authenticated';
|
||||||
|
import SectionMain from '../../components/SectionMain';
|
||||||
|
import SectionTitleLineWithButton from '../../components/SectionTitleLineWithButton';
|
||||||
|
import { getPageTitle } from '../../config';
|
||||||
|
|
||||||
|
import { Field, Form, Formik } from 'formik';
|
||||||
|
import FormField from '../../components/FormField';
|
||||||
|
import BaseDivider from '../../components/BaseDivider';
|
||||||
|
import BaseButtons from '../../components/BaseButtons';
|
||||||
|
import BaseButton from '../../components/BaseButton';
|
||||||
|
import FormCheckRadio from '../../components/FormCheckRadio';
|
||||||
|
import FormCheckRadioGroup from '../../components/FormCheckRadioGroup';
|
||||||
|
import FormFilePicker from '../../components/FormFilePicker';
|
||||||
|
import FormImagePicker from '../../components/FormImagePicker';
|
||||||
|
import { SwitchField } from '../../components/SwitchField';
|
||||||
|
|
||||||
|
import { SelectField } from '../../components/SelectField';
|
||||||
|
import { SelectFieldMany } from '../../components/SelectFieldMany';
|
||||||
|
import { RichTextField } from '../../components/RichTextField';
|
||||||
|
|
||||||
|
import { create } from '../../stores/conversions/conversionsSlice';
|
||||||
|
import { useAppDispatch } from '../../stores/hooks';
|
||||||
|
import { useRouter } from 'next/router';
|
||||||
|
import moment from 'moment';
|
||||||
|
|
||||||
|
const initialValues = {
|
||||||
|
input_value: '',
|
||||||
|
|
||||||
|
input_unit: 'BTU/h',
|
||||||
|
|
||||||
|
output_value_btu_h: '',
|
||||||
|
|
||||||
|
output_value_kw: '',
|
||||||
|
|
||||||
|
output_value_kcal_h: '',
|
||||||
|
|
||||||
|
output_value_tr: '',
|
||||||
|
|
||||||
|
output_value_hp: '',
|
||||||
|
|
||||||
|
output_value_w: '',
|
||||||
|
};
|
||||||
|
|
||||||
|
const ConversionsNew = () => {
|
||||||
|
const router = useRouter();
|
||||||
|
const dispatch = useAppDispatch();
|
||||||
|
|
||||||
|
const handleSubmit = async (data) => {
|
||||||
|
await dispatch(create(data));
|
||||||
|
await router.push('/conversions/conversions-list');
|
||||||
|
};
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Head>
|
||||||
|
<title>{getPageTitle('New Item')}</title>
|
||||||
|
</Head>
|
||||||
|
<SectionMain>
|
||||||
|
<SectionTitleLineWithButton
|
||||||
|
icon={mdiChartTimelineVariant}
|
||||||
|
title='New Item'
|
||||||
|
main
|
||||||
|
>
|
||||||
|
{''}
|
||||||
|
</SectionTitleLineWithButton>
|
||||||
|
<CardBox>
|
||||||
|
<Formik
|
||||||
|
initialValues={initialValues}
|
||||||
|
onSubmit={(values) => handleSubmit(values)}
|
||||||
|
>
|
||||||
|
<Form>
|
||||||
|
<FormField label='InputValue'>
|
||||||
|
<Field
|
||||||
|
type='number'
|
||||||
|
name='input_value'
|
||||||
|
placeholder='InputValue'
|
||||||
|
/>
|
||||||
|
</FormField>
|
||||||
|
|
||||||
|
<FormField label='InputUnit' labelFor='input_unit'>
|
||||||
|
<Field name='input_unit' id='input_unit' component='select'>
|
||||||
|
<option value='BTU/h'>BTU/h</option>
|
||||||
|
|
||||||
|
<option value='kW'>kW</option>
|
||||||
|
|
||||||
|
<option value='kcal/h'>kcal/h</option>
|
||||||
|
|
||||||
|
<option value='TR'>TR</option>
|
||||||
|
|
||||||
|
<option value='HP'>HP</option>
|
||||||
|
|
||||||
|
<option value='W'>W</option>
|
||||||
|
</Field>
|
||||||
|
</FormField>
|
||||||
|
|
||||||
|
<FormField label='OutputValueBTU/h'>
|
||||||
|
<Field
|
||||||
|
type='number'
|
||||||
|
name='output_value_btu_h'
|
||||||
|
placeholder='OutputValueBTU/h'
|
||||||
|
/>
|
||||||
|
</FormField>
|
||||||
|
|
||||||
|
<FormField label='OutputValuekW'>
|
||||||
|
<Field
|
||||||
|
type='number'
|
||||||
|
name='output_value_kw'
|
||||||
|
placeholder='OutputValuekW'
|
||||||
|
/>
|
||||||
|
</FormField>
|
||||||
|
|
||||||
|
<FormField label='OutputValuekcal/h'>
|
||||||
|
<Field
|
||||||
|
type='number'
|
||||||
|
name='output_value_kcal_h'
|
||||||
|
placeholder='OutputValuekcal/h'
|
||||||
|
/>
|
||||||
|
</FormField>
|
||||||
|
|
||||||
|
<FormField label='OutputValueTR'>
|
||||||
|
<Field
|
||||||
|
type='number'
|
||||||
|
name='output_value_tr'
|
||||||
|
placeholder='OutputValueTR'
|
||||||
|
/>
|
||||||
|
</FormField>
|
||||||
|
|
||||||
|
<FormField label='OutputValueHP'>
|
||||||
|
<Field
|
||||||
|
type='number'
|
||||||
|
name='output_value_hp'
|
||||||
|
placeholder='OutputValueHP'
|
||||||
|
/>
|
||||||
|
</FormField>
|
||||||
|
|
||||||
|
<FormField label='OutputValueW'>
|
||||||
|
<Field
|
||||||
|
type='number'
|
||||||
|
name='output_value_w'
|
||||||
|
placeholder='OutputValueW'
|
||||||
|
/>
|
||||||
|
</FormField>
|
||||||
|
|
||||||
|
<BaseDivider />
|
||||||
|
<BaseButtons>
|
||||||
|
<BaseButton type='submit' color='info' label='Submit' />
|
||||||
|
<BaseButton type='reset' color='info' outline label='Reset' />
|
||||||
|
<BaseButton
|
||||||
|
type='reset'
|
||||||
|
color='danger'
|
||||||
|
outline
|
||||||
|
label='Cancel'
|
||||||
|
onClick={() => router.push('/conversions/conversions-list')}
|
||||||
|
/>
|
||||||
|
</BaseButtons>
|
||||||
|
</Form>
|
||||||
|
</Formik>
|
||||||
|
</CardBox>
|
||||||
|
</SectionMain>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
ConversionsNew.getLayout = function getLayout(page: ReactElement) {
|
||||||
|
return (
|
||||||
|
<LayoutAuthenticated permission={'CREATE_CONVERSIONS'}>
|
||||||
|
{page}
|
||||||
|
</LayoutAuthenticated>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ConversionsNew;
|
||||||
187
frontend/src/pages/conversions/conversions-table.tsx
Normal file
187
frontend/src/pages/conversions/conversions-table.tsx
Normal file
@ -0,0 +1,187 @@
|
|||||||
|
import { mdiChartTimelineVariant } from '@mdi/js';
|
||||||
|
import Head from 'next/head';
|
||||||
|
import { uniqueId } from 'lodash';
|
||||||
|
import React, { ReactElement, useState } from 'react';
|
||||||
|
import CardBox from '../../components/CardBox';
|
||||||
|
import LayoutAuthenticated from '../../layouts/Authenticated';
|
||||||
|
import SectionMain from '../../components/SectionMain';
|
||||||
|
import SectionTitleLineWithButton from '../../components/SectionTitleLineWithButton';
|
||||||
|
import { getPageTitle } from '../../config';
|
||||||
|
import TableConversions from '../../components/Conversions/TableConversions';
|
||||||
|
import BaseButton from '../../components/BaseButton';
|
||||||
|
import axios from 'axios';
|
||||||
|
import Link from 'next/link';
|
||||||
|
import { useAppDispatch, useAppSelector } from '../../stores/hooks';
|
||||||
|
import CardBoxModal from '../../components/CardBoxModal';
|
||||||
|
import DragDropFilePicker from '../../components/DragDropFilePicker';
|
||||||
|
import {
|
||||||
|
setRefetch,
|
||||||
|
uploadCsv,
|
||||||
|
} from '../../stores/conversions/conversionsSlice';
|
||||||
|
|
||||||
|
import { hasPermission } from '../../helpers/userPermissions';
|
||||||
|
|
||||||
|
const ConversionsTablesPage = () => {
|
||||||
|
const [filterItems, setFilterItems] = useState([]);
|
||||||
|
const [csvFile, setCsvFile] = useState<File | null>(null);
|
||||||
|
const [isModalActive, setIsModalActive] = useState(false);
|
||||||
|
const [showTableView, setShowTableView] = useState(false);
|
||||||
|
|
||||||
|
const { currentUser } = useAppSelector((state) => state.auth);
|
||||||
|
|
||||||
|
const dispatch = useAppDispatch();
|
||||||
|
|
||||||
|
const [filters] = useState([
|
||||||
|
{ label: 'InputValue', title: 'input_value', number: 'true' },
|
||||||
|
{ label: 'OutputValueBTU/h', title: 'output_value_btu_h', number: 'true' },
|
||||||
|
{ label: 'OutputValuekW', title: 'output_value_kw', number: 'true' },
|
||||||
|
{
|
||||||
|
label: 'OutputValuekcal/h',
|
||||||
|
title: 'output_value_kcal_h',
|
||||||
|
number: 'true',
|
||||||
|
},
|
||||||
|
{ label: 'OutputValueTR', title: 'output_value_tr', number: 'true' },
|
||||||
|
{ label: 'OutputValueHP', title: 'output_value_hp', number: 'true' },
|
||||||
|
{ label: 'OutputValueW', title: 'output_value_w', number: 'true' },
|
||||||
|
|
||||||
|
{
|
||||||
|
label: 'InputUnit',
|
||||||
|
title: 'input_unit',
|
||||||
|
type: 'enum',
|
||||||
|
options: ['BTU/h', 'kW', 'kcal/h', 'TR', 'HP', 'W'],
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
const hasCreatePermission =
|
||||||
|
currentUser && hasPermission(currentUser, 'CREATE_CONVERSIONS');
|
||||||
|
|
||||||
|
const addFilter = () => {
|
||||||
|
const newItem = {
|
||||||
|
id: uniqueId(),
|
||||||
|
fields: {
|
||||||
|
filterValue: '',
|
||||||
|
filterValueFrom: '',
|
||||||
|
filterValueTo: '',
|
||||||
|
selectedField: '',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
newItem.fields.selectedField = filters[0].title;
|
||||||
|
setFilterItems([...filterItems, newItem]);
|
||||||
|
};
|
||||||
|
|
||||||
|
const getConversionsCSV = async () => {
|
||||||
|
const response = await axios({
|
||||||
|
url: '/conversions?filetype=csv',
|
||||||
|
method: 'GET',
|
||||||
|
responseType: 'blob',
|
||||||
|
});
|
||||||
|
const type = response.headers['content-type'];
|
||||||
|
const blob = new Blob([response.data], { type: type });
|
||||||
|
const link = document.createElement('a');
|
||||||
|
link.href = window.URL.createObjectURL(blob);
|
||||||
|
link.download = 'conversionsCSV.csv';
|
||||||
|
link.click();
|
||||||
|
};
|
||||||
|
|
||||||
|
const onModalConfirm = async () => {
|
||||||
|
if (!csvFile) return;
|
||||||
|
await dispatch(uploadCsv(csvFile));
|
||||||
|
dispatch(setRefetch(true));
|
||||||
|
setCsvFile(null);
|
||||||
|
setIsModalActive(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
const onModalCancel = () => {
|
||||||
|
setCsvFile(null);
|
||||||
|
setIsModalActive(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Head>
|
||||||
|
<title>{getPageTitle('Conversions')}</title>
|
||||||
|
</Head>
|
||||||
|
<SectionMain>
|
||||||
|
<SectionTitleLineWithButton
|
||||||
|
icon={mdiChartTimelineVariant}
|
||||||
|
title='Conversions'
|
||||||
|
main
|
||||||
|
>
|
||||||
|
{''}
|
||||||
|
</SectionTitleLineWithButton>
|
||||||
|
<CardBox className='mb-6' cardBoxClassName='flex flex-wrap'>
|
||||||
|
{hasCreatePermission && (
|
||||||
|
<BaseButton
|
||||||
|
className={'mr-3'}
|
||||||
|
href={'/conversions/conversions-new'}
|
||||||
|
color='info'
|
||||||
|
label='New Item'
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<BaseButton
|
||||||
|
className={'mr-3'}
|
||||||
|
color='info'
|
||||||
|
label='Filter'
|
||||||
|
onClick={addFilter}
|
||||||
|
/>
|
||||||
|
<BaseButton
|
||||||
|
className={'mr-3'}
|
||||||
|
color='info'
|
||||||
|
label='Download CSV'
|
||||||
|
onClick={getConversionsCSV}
|
||||||
|
/>
|
||||||
|
|
||||||
|
{hasCreatePermission && (
|
||||||
|
<BaseButton
|
||||||
|
color='info'
|
||||||
|
label='Upload CSV'
|
||||||
|
onClick={() => setIsModalActive(true)}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className='md:inline-flex items-center ms-auto'>
|
||||||
|
<div id='delete-rows-button'></div>
|
||||||
|
|
||||||
|
<Link href={'/conversions/conversions-list'}>
|
||||||
|
Back to <span className='capitalize'>table</span>
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
</CardBox>
|
||||||
|
<CardBox className='mb-6' hasTable>
|
||||||
|
<TableConversions
|
||||||
|
filterItems={filterItems}
|
||||||
|
setFilterItems={setFilterItems}
|
||||||
|
filters={filters}
|
||||||
|
showGrid={true}
|
||||||
|
/>
|
||||||
|
</CardBox>
|
||||||
|
</SectionMain>
|
||||||
|
<CardBoxModal
|
||||||
|
title='Upload CSV'
|
||||||
|
buttonColor='info'
|
||||||
|
buttonLabel={'Confirm'}
|
||||||
|
// buttonLabel={false ? 'Deleting...' : 'Confirm'}
|
||||||
|
isActive={isModalActive}
|
||||||
|
onConfirm={onModalConfirm}
|
||||||
|
onCancel={onModalCancel}
|
||||||
|
>
|
||||||
|
<DragDropFilePicker
|
||||||
|
file={csvFile}
|
||||||
|
setFile={setCsvFile}
|
||||||
|
formats={'.csv'}
|
||||||
|
/>
|
||||||
|
</CardBoxModal>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
ConversionsTablesPage.getLayout = function getLayout(page: ReactElement) {
|
||||||
|
return (
|
||||||
|
<LayoutAuthenticated permission={'READ_CONVERSIONS'}>
|
||||||
|
{page}
|
||||||
|
</LayoutAuthenticated>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ConversionsTablesPage;
|
||||||
118
frontend/src/pages/conversions/conversions-view.tsx
Normal file
118
frontend/src/pages/conversions/conversions-view.tsx
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
import React, { ReactElement, useEffect } from 'react';
|
||||||
|
import Head from 'next/head';
|
||||||
|
import DatePicker from 'react-datepicker';
|
||||||
|
import 'react-datepicker/dist/react-datepicker.css';
|
||||||
|
import dayjs from 'dayjs';
|
||||||
|
import { useAppDispatch, useAppSelector } from '../../stores/hooks';
|
||||||
|
import { useRouter } from 'next/router';
|
||||||
|
import { fetch } from '../../stores/conversions/conversionsSlice';
|
||||||
|
import { saveFile } from '../../helpers/fileSaver';
|
||||||
|
import dataFormatter from '../../helpers/dataFormatter';
|
||||||
|
import ImageField from '../../components/ImageField';
|
||||||
|
import LayoutAuthenticated from '../../layouts/Authenticated';
|
||||||
|
import { getPageTitle } from '../../config';
|
||||||
|
import SectionTitleLineWithButton from '../../components/SectionTitleLineWithButton';
|
||||||
|
import SectionMain from '../../components/SectionMain';
|
||||||
|
import CardBox from '../../components/CardBox';
|
||||||
|
import BaseButton from '../../components/BaseButton';
|
||||||
|
import BaseDivider from '../../components/BaseDivider';
|
||||||
|
import { mdiChartTimelineVariant } from '@mdi/js';
|
||||||
|
import { SwitchField } from '../../components/SwitchField';
|
||||||
|
import FormField from '../../components/FormField';
|
||||||
|
|
||||||
|
const ConversionsView = () => {
|
||||||
|
const router = useRouter();
|
||||||
|
const dispatch = useAppDispatch();
|
||||||
|
const { conversions } = useAppSelector((state) => state.conversions);
|
||||||
|
|
||||||
|
const { id } = router.query;
|
||||||
|
|
||||||
|
function removeLastCharacter(str) {
|
||||||
|
console.log(str, `str`);
|
||||||
|
return str.slice(0, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
dispatch(fetch({ id }));
|
||||||
|
}, [dispatch, id]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Head>
|
||||||
|
<title>{getPageTitle('View conversions')}</title>
|
||||||
|
</Head>
|
||||||
|
<SectionMain>
|
||||||
|
<SectionTitleLineWithButton
|
||||||
|
icon={mdiChartTimelineVariant}
|
||||||
|
title={removeLastCharacter('View conversions')}
|
||||||
|
main
|
||||||
|
>
|
||||||
|
<BaseButton
|
||||||
|
color='info'
|
||||||
|
label='Edit'
|
||||||
|
href={`/conversions/conversions-edit/?id=${id}`}
|
||||||
|
/>
|
||||||
|
</SectionTitleLineWithButton>
|
||||||
|
<CardBox>
|
||||||
|
<div className={'mb-4'}>
|
||||||
|
<p className={'block font-bold mb-2'}>InputValue</p>
|
||||||
|
<p>{conversions?.input_value || 'No data'}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className={'mb-4'}>
|
||||||
|
<p className={'block font-bold mb-2'}>InputUnit</p>
|
||||||
|
<p>{conversions?.input_unit ?? 'No data'}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className={'mb-4'}>
|
||||||
|
<p className={'block font-bold mb-2'}>OutputValueBTU/h</p>
|
||||||
|
<p>{conversions?.output_value_btu_h || 'No data'}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className={'mb-4'}>
|
||||||
|
<p className={'block font-bold mb-2'}>OutputValuekW</p>
|
||||||
|
<p>{conversions?.output_value_kw || 'No data'}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className={'mb-4'}>
|
||||||
|
<p className={'block font-bold mb-2'}>OutputValuekcal/h</p>
|
||||||
|
<p>{conversions?.output_value_kcal_h || 'No data'}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className={'mb-4'}>
|
||||||
|
<p className={'block font-bold mb-2'}>OutputValueTR</p>
|
||||||
|
<p>{conversions?.output_value_tr || 'No data'}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className={'mb-4'}>
|
||||||
|
<p className={'block font-bold mb-2'}>OutputValueHP</p>
|
||||||
|
<p>{conversions?.output_value_hp || 'No data'}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className={'mb-4'}>
|
||||||
|
<p className={'block font-bold mb-2'}>OutputValueW</p>
|
||||||
|
<p>{conversions?.output_value_w || 'No data'}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<BaseDivider />
|
||||||
|
|
||||||
|
<BaseButton
|
||||||
|
color='info'
|
||||||
|
label='Back'
|
||||||
|
onClick={() => router.push('/conversions/conversions-list')}
|
||||||
|
/>
|
||||||
|
</CardBox>
|
||||||
|
</SectionMain>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
ConversionsView.getLayout = function getLayout(page: ReactElement) {
|
||||||
|
return (
|
||||||
|
<LayoutAuthenticated permission={'READ_CONVERSIONS'}>
|
||||||
|
{page}
|
||||||
|
</LayoutAuthenticated>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ConversionsView;
|
||||||
@ -29,6 +29,7 @@ const Dashboard = () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const [users, setUsers] = React.useState(loadingMessage);
|
const [users, setUsers] = React.useState(loadingMessage);
|
||||||
|
const [conversions, setConversions] = React.useState(loadingMessage);
|
||||||
const [roles, setRoles] = React.useState(loadingMessage);
|
const [roles, setRoles] = React.useState(loadingMessage);
|
||||||
const [permissions, setPermissions] = React.useState(loadingMessage);
|
const [permissions, setPermissions] = React.useState(loadingMessage);
|
||||||
|
|
||||||
@ -41,8 +42,8 @@ const Dashboard = () => {
|
|||||||
const { rolesWidgets, loading } = useAppSelector((state) => state.roles);
|
const { rolesWidgets, loading } = useAppSelector((state) => state.roles);
|
||||||
|
|
||||||
async function loadData() {
|
async function loadData() {
|
||||||
const entities = ['users', 'roles', 'permissions'];
|
const entities = ['users', 'conversions', 'roles', 'permissions'];
|
||||||
const fns = [setUsers, setRoles, setPermissions];
|
const fns = [setUsers, setConversions, setRoles, setPermissions];
|
||||||
|
|
||||||
const requests = entities.map((entity, index) => {
|
const requests = entities.map((entity, index) => {
|
||||||
if (hasPermission(currentUser, `READ_${entity.toUpperCase()}`)) {
|
if (hasPermission(currentUser, `READ_${entity.toUpperCase()}`)) {
|
||||||
@ -186,6 +187,42 @@ const Dashboard = () => {
|
|||||||
</Link>
|
</Link>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{hasPermission(currentUser, 'READ_CONVERSIONS') && (
|
||||||
|
<Link href={'/conversions/conversions-list'}>
|
||||||
|
<div
|
||||||
|
className={`${
|
||||||
|
corners !== 'rounded-full' ? corners : 'rounded-3xl'
|
||||||
|
} dark:bg-dark-900 ${cardsStyle} dark:border-dark-700 p-6`}
|
||||||
|
>
|
||||||
|
<div className='flex justify-between align-center'>
|
||||||
|
<div>
|
||||||
|
<div className='text-lg leading-tight text-gray-500 dark:text-gray-400'>
|
||||||
|
Conversions
|
||||||
|
</div>
|
||||||
|
<div className='text-3xl leading-tight font-semibold'>
|
||||||
|
{conversions}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<BaseIcon
|
||||||
|
className={`${iconsColor}`}
|
||||||
|
w='w-16'
|
||||||
|
h='h-16'
|
||||||
|
size={48}
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
|
// @ts-ignore
|
||||||
|
path={
|
||||||
|
'mdiSwapHorizontal' in icon
|
||||||
|
? icon['mdiSwapHorizontal' as keyof typeof icon]
|
||||||
|
: icon.mdiTable || icon.mdiTable
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</Link>
|
||||||
|
)}
|
||||||
|
|
||||||
{hasPermission(currentUser, 'READ_ROLES') && (
|
{hasPermission(currentUser, 'READ_ROLES') && (
|
||||||
<Link href={'/roles/roles-list'}>
|
<Link href={'/roles/roles-list'}>
|
||||||
<div
|
<div
|
||||||
|
|||||||
241
frontend/src/stores/conversions/conversionsSlice.ts
Normal file
241
frontend/src/stores/conversions/conversionsSlice.ts
Normal file
@ -0,0 +1,241 @@
|
|||||||
|
import { createSlice, createAsyncThunk, PayloadAction } from '@reduxjs/toolkit';
|
||||||
|
import axios from 'axios';
|
||||||
|
import {
|
||||||
|
fulfilledNotify,
|
||||||
|
rejectNotify,
|
||||||
|
resetNotify,
|
||||||
|
} from '../../helpers/notifyStateHandler';
|
||||||
|
|
||||||
|
interface MainState {
|
||||||
|
conversions: any;
|
||||||
|
loading: boolean;
|
||||||
|
count: number;
|
||||||
|
refetch: boolean;
|
||||||
|
rolesWidgets: any[];
|
||||||
|
notify: {
|
||||||
|
showNotification: boolean;
|
||||||
|
textNotification: string;
|
||||||
|
typeNotification: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const initialState: MainState = {
|
||||||
|
conversions: [],
|
||||||
|
loading: false,
|
||||||
|
count: 0,
|
||||||
|
refetch: false,
|
||||||
|
rolesWidgets: [],
|
||||||
|
notify: {
|
||||||
|
showNotification: false,
|
||||||
|
textNotification: '',
|
||||||
|
typeNotification: 'warn',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export const fetch = createAsyncThunk(
|
||||||
|
'conversions/fetch',
|
||||||
|
async (data: any) => {
|
||||||
|
const { id, query } = data;
|
||||||
|
const result = await axios.get(
|
||||||
|
`conversions${query || (id ? `/${id}` : '')}`,
|
||||||
|
);
|
||||||
|
return id
|
||||||
|
? result.data
|
||||||
|
: { rows: result.data.rows, count: result.data.count };
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
export const deleteItemsByIds = createAsyncThunk(
|
||||||
|
'conversions/deleteByIds',
|
||||||
|
async (data: any, { rejectWithValue }) => {
|
||||||
|
try {
|
||||||
|
await axios.post('conversions/deleteByIds', { data });
|
||||||
|
} catch (error) {
|
||||||
|
if (!error.response) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rejectWithValue(error.response.data);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
export const deleteItem = createAsyncThunk(
|
||||||
|
'conversions/deleteConversions',
|
||||||
|
async (id: string, { rejectWithValue }) => {
|
||||||
|
try {
|
||||||
|
await axios.delete(`conversions/${id}`);
|
||||||
|
} catch (error) {
|
||||||
|
if (!error.response) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rejectWithValue(error.response.data);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
export const create = createAsyncThunk(
|
||||||
|
'conversions/createConversions',
|
||||||
|
async (data: any, { rejectWithValue }) => {
|
||||||
|
try {
|
||||||
|
const result = await axios.post('conversions', { data });
|
||||||
|
return result.data;
|
||||||
|
} catch (error) {
|
||||||
|
if (!error.response) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rejectWithValue(error.response.data);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
export const uploadCsv = createAsyncThunk(
|
||||||
|
'conversions/uploadCsv',
|
||||||
|
async (file: File, { rejectWithValue }) => {
|
||||||
|
try {
|
||||||
|
const data = new FormData();
|
||||||
|
data.append('file', file);
|
||||||
|
data.append('filename', file.name);
|
||||||
|
|
||||||
|
const result = await axios.post('conversions/bulk-import', data, {
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'multipart/form-data',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return result.data;
|
||||||
|
} catch (error) {
|
||||||
|
if (!error.response) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rejectWithValue(error.response.data);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
export const update = createAsyncThunk(
|
||||||
|
'conversions/updateConversions',
|
||||||
|
async (payload: any, { rejectWithValue }) => {
|
||||||
|
try {
|
||||||
|
const result = await axios.put(`conversions/${payload.id}`, {
|
||||||
|
id: payload.id,
|
||||||
|
data: payload.data,
|
||||||
|
});
|
||||||
|
return result.data;
|
||||||
|
} catch (error) {
|
||||||
|
if (!error.response) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rejectWithValue(error.response.data);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
export const conversionsSlice = createSlice({
|
||||||
|
name: 'conversions',
|
||||||
|
initialState,
|
||||||
|
reducers: {
|
||||||
|
setRefetch: (state, action: PayloadAction<boolean>) => {
|
||||||
|
state.refetch = action.payload;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
extraReducers: (builder) => {
|
||||||
|
builder.addCase(fetch.pending, (state) => {
|
||||||
|
state.loading = true;
|
||||||
|
resetNotify(state);
|
||||||
|
});
|
||||||
|
builder.addCase(fetch.rejected, (state, action) => {
|
||||||
|
state.loading = false;
|
||||||
|
rejectNotify(state, action);
|
||||||
|
});
|
||||||
|
|
||||||
|
builder.addCase(fetch.fulfilled, (state, action) => {
|
||||||
|
if (action.payload.rows && action.payload.count >= 0) {
|
||||||
|
state.conversions = action.payload.rows;
|
||||||
|
state.count = action.payload.count;
|
||||||
|
} else {
|
||||||
|
state.conversions = action.payload;
|
||||||
|
}
|
||||||
|
state.loading = false;
|
||||||
|
});
|
||||||
|
|
||||||
|
builder.addCase(deleteItemsByIds.pending, (state) => {
|
||||||
|
state.loading = true;
|
||||||
|
resetNotify(state);
|
||||||
|
});
|
||||||
|
|
||||||
|
builder.addCase(deleteItemsByIds.fulfilled, (state) => {
|
||||||
|
state.loading = false;
|
||||||
|
fulfilledNotify(state, 'Conversions has been deleted');
|
||||||
|
});
|
||||||
|
|
||||||
|
builder.addCase(deleteItemsByIds.rejected, (state, action) => {
|
||||||
|
state.loading = false;
|
||||||
|
rejectNotify(state, action);
|
||||||
|
});
|
||||||
|
|
||||||
|
builder.addCase(deleteItem.pending, (state) => {
|
||||||
|
state.loading = true;
|
||||||
|
resetNotify(state);
|
||||||
|
});
|
||||||
|
|
||||||
|
builder.addCase(deleteItem.fulfilled, (state) => {
|
||||||
|
state.loading = false;
|
||||||
|
fulfilledNotify(state, `${'Conversions'.slice(0, -1)} has been deleted`);
|
||||||
|
});
|
||||||
|
|
||||||
|
builder.addCase(deleteItem.rejected, (state, action) => {
|
||||||
|
state.loading = false;
|
||||||
|
rejectNotify(state, action);
|
||||||
|
});
|
||||||
|
|
||||||
|
builder.addCase(create.pending, (state) => {
|
||||||
|
state.loading = true;
|
||||||
|
resetNotify(state);
|
||||||
|
});
|
||||||
|
builder.addCase(create.rejected, (state, action) => {
|
||||||
|
state.loading = false;
|
||||||
|
rejectNotify(state, action);
|
||||||
|
});
|
||||||
|
|
||||||
|
builder.addCase(create.fulfilled, (state) => {
|
||||||
|
state.loading = false;
|
||||||
|
fulfilledNotify(state, `${'Conversions'.slice(0, -1)} has been created`);
|
||||||
|
});
|
||||||
|
|
||||||
|
builder.addCase(update.pending, (state) => {
|
||||||
|
state.loading = true;
|
||||||
|
resetNotify(state);
|
||||||
|
});
|
||||||
|
builder.addCase(update.fulfilled, (state) => {
|
||||||
|
state.loading = false;
|
||||||
|
fulfilledNotify(state, `${'Conversions'.slice(0, -1)} has been updated`);
|
||||||
|
});
|
||||||
|
builder.addCase(update.rejected, (state, action) => {
|
||||||
|
state.loading = false;
|
||||||
|
rejectNotify(state, action);
|
||||||
|
});
|
||||||
|
|
||||||
|
builder.addCase(uploadCsv.pending, (state) => {
|
||||||
|
state.loading = true;
|
||||||
|
resetNotify(state);
|
||||||
|
});
|
||||||
|
builder.addCase(uploadCsv.fulfilled, (state) => {
|
||||||
|
state.loading = false;
|
||||||
|
fulfilledNotify(state, 'Conversions has been uploaded');
|
||||||
|
});
|
||||||
|
builder.addCase(uploadCsv.rejected, (state, action) => {
|
||||||
|
state.loading = false;
|
||||||
|
rejectNotify(state, action);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Action creators are generated for each case reducer function
|
||||||
|
export const { setRefetch } = conversionsSlice.actions;
|
||||||
|
|
||||||
|
export default conversionsSlice.reducer;
|
||||||
@ -5,6 +5,7 @@ import authSlice from './authSlice';
|
|||||||
import openAiSlice from './openAiSlice';
|
import openAiSlice from './openAiSlice';
|
||||||
|
|
||||||
import usersSlice from './users/usersSlice';
|
import usersSlice from './users/usersSlice';
|
||||||
|
import conversionsSlice from './conversions/conversionsSlice';
|
||||||
import rolesSlice from './roles/rolesSlice';
|
import rolesSlice from './roles/rolesSlice';
|
||||||
import permissionsSlice from './permissions/permissionsSlice';
|
import permissionsSlice from './permissions/permissionsSlice';
|
||||||
|
|
||||||
@ -16,6 +17,7 @@ export const store = configureStore({
|
|||||||
openAi: openAiSlice,
|
openAi: openAiSlice,
|
||||||
|
|
||||||
users: usersSlice,
|
users: usersSlice,
|
||||||
|
conversions: conversionsSlice,
|
||||||
roles: rolesSlice,
|
roles: rolesSlice,
|
||||||
permissions: permissionsSlice,
|
permissions: permissionsSlice,
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user