Website components. Update chart component.

This commit is contained in:
Mike Cao 2020-07-28 01:17:45 -07:00
parent d81ee3932d
commit bdcdcd9d13
9 changed files with 165 additions and 43 deletions

View file

@ -1,5 +1,5 @@
import moment from 'moment-timezone';
import { addMinutes } from 'date-fns';
import { addMinutes, endOfDay, subDays, subHours } from 'date-fns';
export function getTimezone() {
const tz = moment.tz.guess();
@ -9,3 +9,29 @@ export function getTimezone() {
export function getLocalTime(t) {
return addMinutes(new Date(t), new Date().getTimezoneOffset());
}
export function getDateRange(value) {
const now = new Date();
const endToday = endOfDay(now);
switch (value) {
case '7d':
return {
startDate: subDays(endToday, 7),
endDate: endToday,
unit: 'day',
};
case '30d':
return {
startDate: subDays(endToday, 30),
endDate: endToday,
unit: 'day',
};
default:
return {
startDate: subHours(now, 24),
endDate: now,
unit: 'hour',
};
}
}

View file

@ -1,13 +1,27 @@
import { PrismaClient } from '@prisma/client';
export const prisma = new PrismaClient({
const options = {
log: [
{
emit: 'event',
level: 'query',
},
],
});
};
let prisma;
if (process.env.NODE_ENV === 'production') {
prisma = new PrismaClient(options);
} else {
if (!global.prisma) {
global.prisma = new PrismaClient(options);
}
prisma = global.prisma;
}
export default prisma;
prisma.on('query', e => {
if (process.env.LOG_QUERY) {

View file

@ -7,17 +7,20 @@ export const apiRequest = (method, url, body) =>
'Content-Type': 'application/json',
},
body,
}).then(res => (res.ok ? res.json() : null));
}).then(res => {
if (res.ok) {
return res.json();
}
return null;
});
function parseQuery(url, params) {
const query =
params &&
Object.keys(params).reduce((values, key) => {
if (params[key] !== undefined) {
return values.concat(`${key}=${encodeURIComponent(params[key])}`);
}
return values;
}, []);
function parseQuery(url, params = {}) {
const query = Object.keys(params).reduce((values, key) => {
if (params[key] !== undefined) {
return values.concat(`${key}=${encodeURIComponent(params[key])}`);
}
return values;
}, []);
return query.length ? `${url}?${query.join('&')}` : url;
}