Calendar updates. Responsive CSS updates.

This commit is contained in:
Mike Cao 2020-09-13 11:33:57 -07:00
parent a0cb278463
commit f59594e4cd
18 changed files with 127 additions and 42 deletions

1
assets/calendar-alt.svg Normal file
View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M400 64h-48V12c0-6.6-5.4-12-12-12h-8c-6.6 0-12 5.4-12 12v52H128V12c0-6.6-5.4-12-12-12h-8c-6.6 0-12 5.4-12 12v52H48C21.5 64 0 85.5 0 112v352c0 26.5 21.5 48 48 48h352c26.5 0 48-21.5 48-48V112c0-26.5-21.5-48-48-48zM48 96h352c8.8 0 16 7.2 16 16v48H32v-48c0-8.8 7.2-16 16-16zm352 384H48c-8.8 0-16-7.2-16-16V192h384v272c0 8.8-7.2 16-16 16zM148 320h-40c-6.6 0-12-5.4-12-12v-40c0-6.6 5.4-12 12-12h40c6.6 0 12 5.4 12 12v40c0 6.6-5.4 12-12 12zm96 0h-40c-6.6 0-12-5.4-12-12v-40c0-6.6 5.4-12 12-12h40c6.6 0 12 5.4 12 12v40c0 6.6-5.4 12-12 12zm96 0h-40c-6.6 0-12-5.4-12-12v-40c0-6.6 5.4-12 12-12h40c6.6 0 12 5.4 12 12v40c0 6.6-5.4 12-12 12zm-96 96h-40c-6.6 0-12-5.4-12-12v-40c0-6.6 5.4-12 12-12h40c6.6 0 12 5.4 12 12v40c0 6.6-5.4 12-12 12zm-96 0h-40c-6.6 0-12-5.4-12-12v-40c0-6.6 5.4-12 12-12h40c6.6 0 12 5.4 12 12v40c0 6.6-5.4 12-12 12zm192 0h-40c-6.6 0-12-5.4-12-12v-40c0-6.6 5.4-12 12-12h40c6.6 0 12 5.4 12 12v40c0 6.6-5.4 12-12 12z"/></svg>

After

Width:  |  Height:  |  Size: 1002 B

View File

@ -29,6 +29,7 @@ export default function Button({
[styles.xsmall]: size === 'xsmall', [styles.xsmall]: size === 'xsmall',
[styles.action]: variant === 'action', [styles.action]: variant === 'action',
[styles.danger]: variant === 'danger', [styles.danger]: variant === 'danger',
[styles.light]: variant === 'light',
[styles.disabled]: disabled, [styles.disabled]: disabled,
})} })}
disabled={disabled} disabled={disabled}

View File

@ -55,7 +55,16 @@
background: var(--red400); background: var(--red400);
} }
.light {
background: var(--gray50);
}
.light:hover {
background: var(--gray75);
}
.button:disabled { .button:disabled {
cursor: default;
color: var(--gray500); color: var(--gray500);
background: var(--gray75); background: var(--gray75);
} }
@ -67,3 +76,7 @@
.button:disabled:hover { .button:disabled:hover {
background: var(--gray75); background: var(--gray75);
} }
.button.light:disabled {
background: var(--gray50);
}

View File

@ -160,7 +160,7 @@ const MonthSelector = ({ date, minDate, maxDate, locale, onSelect }) => {
const start = startOfYear(date); const start = startOfYear(date);
const months = []; const months = [];
for (let i = 0; i < 12; i++) { for (let i = 0; i < 12; i++) {
months.push(endOfMonth(addMonths(start, i))); months.push(addMonths(start, i));
} }
function handleSelect(value) { function handleSelect(value) {
@ -173,7 +173,8 @@ const MonthSelector = ({ date, minDate, maxDate, locale, onSelect }) => {
{chunk(months, 3).map((row, i) => ( {chunk(months, 3).map((row, i) => (
<tr key={i}> <tr key={i}>
{row.map((month, j) => { {row.map((month, j) => {
const disabled = isBefore(month, minDate) || isAfter(month, maxDate); const disabled =
isBefore(endOfMonth(month), minDate) || isAfter(startOfMonth(month), maxDate);
return ( return (
<td <td
key={j} key={j}
@ -221,10 +222,11 @@ const YearSelector = ({ date, minDate, maxDate, onSelect }) => {
<div className={styles.pager}> <div className={styles.pager}>
<Button <Button
icon={<Chevron />} icon={<Chevron />}
size="xsmall" size="small"
className={styles.left} className={styles.left}
onClick={handlePrevClick} onClick={handlePrevClick}
disabled={years[0] <= minYear} disabled={years[0] <= minYear}
variant="light"
/> />
<table> <table>
<tbody> <tbody>
@ -248,10 +250,11 @@ const YearSelector = ({ date, minDate, maxDate, onSelect }) => {
</table> </table>
<Button <Button
icon={<Chevron />} icon={<Chevron />}
size="xsmall" size="small"
className={styles.right} className={styles.right}
onClick={handleNextClick} onClick={handleNextClick}
disabled={years[years.length - 1] > maxYear} disabled={years[years.length - 1] > maxYear}
variant="light"
/> />
</div> </div>
); );

View File

@ -3,6 +3,7 @@
flex-direction: column; flex-direction: column;
font-size: var(--font-size-small); font-size: var(--font-size-small);
flex: 1; flex: 1;
min-height: 285px;
} }
.calendar table { .calendar table {
@ -37,7 +38,7 @@
} }
.calendar td.disabled { .calendar td.disabled {
color: var(--gray300); color: var(--gray400);
background: var(--gray75); background: var(--gray75);
} }
@ -47,7 +48,7 @@
} }
.calendar td.faded.disabled { .calendar td.faded.disabled {
color: var(--gray200); background: var(--gray100);
} }
.header { .header {

View File

@ -7,20 +7,33 @@ import DatePickerForm from 'components/forms/DatePickerForm';
import useLocale from 'hooks/useLocale'; import useLocale from 'hooks/useLocale';
import { getDateRange } from 'lib/date'; import { getDateRange } from 'lib/date';
import { dateFormat } from 'lib/lang'; import { dateFormat } from 'lib/lang';
import Calendar from 'assets/calendar-alt.svg';
import Icon from './Icon';
const filterOptions = [ const filterOptions = [
{ label: <FormattedMessage id="label.today" defaultMessage="Today" />, value: '1day' },
{ {
label: ( label: (
<FormattedMessage id="label.last-hours" defaultMessage="Last {x} hours" values={{ x: 24 }} /> <FormattedMessage id="label.last-hours" defaultMessage="Last {x} hours" values={{ x: 24 }} />
), ),
value: '24hour', value: '24hour',
}, },
{
label: <FormattedMessage id="label.this-week" defaultMessage="This week" />,
value: '1week',
divider: true,
},
{ {
label: ( label: (
<FormattedMessage id="label.last-days" defaultMessage="Last {x} days" values={{ x: 7 }} /> <FormattedMessage id="label.last-days" defaultMessage="Last {x} days" values={{ x: 7 }} />
), ),
value: '7day', value: '7day',
}, },
{
label: <FormattedMessage id="label.this-month" defaultMessage="This month" />,
value: '1month',
divider: true,
},
{ {
label: ( label: (
<FormattedMessage id="label.last-days" defaultMessage="Last {x} days" values={{ x: 30 }} /> <FormattedMessage id="label.last-days" defaultMessage="Last {x} days" values={{ x: 30 }} />
@ -33,26 +46,22 @@ const filterOptions = [
), ),
value: '90day', value: '90day',
}, },
{ label: <FormattedMessage id="label.today" defaultMessage="Today" />, value: '1day' },
{ label: <FormattedMessage id="label.this-week" defaultMessage="This week" />, value: '1week' },
{
label: <FormattedMessage id="label.this-month" defaultMessage="This month" />,
value: '1month',
},
{ label: <FormattedMessage id="label.this-year" defaultMessage="This year" />, value: '1year' }, { label: <FormattedMessage id="label.this-year" defaultMessage="This year" />, value: '1year' },
{ {
label: <FormattedMessage id="label.custom-range" defaultMessage="Custom range" />, label: <FormattedMessage id="label.custom-range" defaultMessage="Custom range" />,
value: 'custom', value: 'custom',
divider: true,
}, },
]; ];
export default function DateFilter({ value, startDate, endDate, onChange, className }) { export default function DateFilter({ value, startDate, endDate, onChange, className }) {
const [locale] = useLocale();
const [showPicker, setShowPicker] = useState(false); const [showPicker, setShowPicker] = useState(false);
const displayValue = const displayValue =
value === 'custom' value === 'custom' ? (
? `${dateFormat(startDate, 'd LLL y', locale)}${dateFormat(endDate, 'd LLL y', locale)}` <CustomRange startDate={startDate} endDate={endDate} onClick={() => handleChange('custom')} />
: value; ) : (
value
);
function handleChange(value) { function handleChange(value) {
if (value === 'custom') { if (value === 'custom') {
@ -90,3 +99,20 @@ export default function DateFilter({ value, startDate, endDate, onChange, classN
</> </>
); );
} }
const CustomRange = ({ startDate, endDate, onClick }) => {
const [locale] = useLocale();
function handleClick(e) {
e.stopPropagation();
onClick();
}
return (
<>
<Icon icon={<Calendar />} className="mr-2" onClick={handleClick} />
{`${dateFormat(startDate, 'd LLL y', locale)}${dateFormat(endDate, 'd LLL y', locale)}`}
</>
);
};

View File

@ -36,8 +36,8 @@ export default function DropDown({
return ( return (
<div ref={ref} className={classNames(styles.dropdown, className)} onClick={handleShowMenu}> <div ref={ref} className={classNames(styles.dropdown, className)} onClick={handleShowMenu}>
<div className={styles.value}> <div className={styles.value}>
<div>{options.find(e => e.value === value)?.label || value}</div> {options.find(e => e.value === value)?.label || value}
<Icon icon={<Chevron />} size="small" /> <Icon icon={<Chevron />} className={styles.icon} size="small" />
</div> </div>
{showMenu && ( {showMenu && (
<Menu className={menuClassName} options={options} onSelect={handleSelect} float="bottom" /> <Menu className={menuClassName} options={options} onSelect={handleSelect} float="bottom" />

View File

@ -1,15 +1,24 @@
.dropdown { .dropdown {
flex: 1;
position: relative; position: relative;
display: flex;
justify-content: space-between;
align-items: center;
border: 1px solid var(--gray500); border: 1px solid var(--gray500);
border-radius: 4px; border-radius: 4px;
cursor: pointer; cursor: pointer;
} }
.value { .value {
flex: 1;
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
font-size: var(--font-size-small); font-size: var(--font-size-small);
min-width: 140px; flex-wrap: nowrap;
white-space: nowrap;
padding: 4px 16px; padding: 4px 16px;
min-width: 160px;
}
.icon {
padding-left: 10px;
} }

View File

@ -20,6 +20,10 @@ export default function LanguageButton({ menuPosition = 'bottom', menuAlign = 'l
setShowMenu(false); setShowMenu(false);
} }
function toggleMenu() {
setShowMenu(state => !state);
}
useDocumentClick(e => { useDocumentClick(e => {
if (!ref.current.contains(e.target)) { if (!ref.current.contains(e.target)) {
setShowMenu(false); setShowMenu(false);
@ -43,7 +47,7 @@ export default function LanguageButton({ menuPosition = 'bottom', menuAlign = 'l
)} )}
</Head> </Head>
<div ref={ref} className={styles.container}> <div ref={ref} className={styles.container}>
<Button icon={<Globe />} onClick={() => setShowMenu(true)} size="small"> <Button icon={<Globe />} onClick={toggleMenu} size="small">
<div className={locale}>{selectedLocale}</div> <div className={locale}>{selectedLocale}</div>
</Button> </Button>
{showMenu && ( {showMenu && (

View File

@ -25,7 +25,7 @@ export default function Menu({
{options {options
.filter(({ hidden }) => !hidden) .filter(({ hidden }) => !hidden)
.map(option => { .map(option => {
const { label, value, className: customClassName, render } = option; const { label, value, className: customClassName, render, divider } = option;
return render ? ( return render ? (
render(option) render(option)
@ -34,6 +34,7 @@ export default function Menu({
key={value} key={value}
className={classNames(styles.option, optionClassName, customClassName, { className={classNames(styles.option, optionClassName, customClassName, {
[selectedClassName]: selectedOption === value, [selectedClassName]: selectedOption === value,
[styles.divider]: divider,
})} })}
onClick={e => onSelect(value, e)} onClick={e => onSelect(value, e)}
> >

View File

@ -40,3 +40,7 @@
.right { .right {
right: 0; right: 0;
} }
.divider {
border-top: 1px solid var(--gray300);
}

View File

@ -3,7 +3,7 @@
flex-direction: column; flex-direction: column;
justify-content: center; justify-content: center;
min-width: 120px; min-width: 120px;
margin-right: 20px; padding-right: 20px;
} }
.value { .value {
@ -16,4 +16,5 @@
.label { .label {
font-size: var(--font-size-normal); font-size: var(--font-size-normal);
white-space: nowrap;
} }

View File

@ -4,6 +4,9 @@
} }
@media only screen and (max-width: 992px) { @media only screen and (max-width: 992px) {
.bar {
justify-content: space-between;
}
.bar > div:last-child { .bar > div:last-child {
display: none; display: none;
} }

View File

@ -57,14 +57,17 @@ export default function WebsiteChart({
stickyClassName={styles.sticky} stickyClassName={styles.sticky}
enabled={stickyHeader} enabled={stickyHeader}
> >
<MetricsBar className="col-12 col-md-9" websiteId={websiteId} /> <div className="col-12 col-lg-9">
<MetricsBar websiteId={websiteId} />
</div>
<div className={classNames(styles.filter, 'col-12 col-lg-3')}>
<DateFilter <DateFilter
className="col-12 col-md-3"
value={value} value={value}
startDate={startDate} startDate={startDate}
endDate={endDate} endDate={endDate}
onChange={handleDateChange} onChange={handleDateChange}
/> />
</div>
</StickyHeader> </StickyHeader>
</div> </div>
<div className="row"> <div className="row">

View File

@ -29,3 +29,15 @@
border-bottom: 1px solid var(--gray300); border-bottom: 1px solid var(--gray300);
z-index: 3; z-index: 3;
} }
.filter {
display: flex;
justify-content: flex-end;
align-items: center;
}
@media only screen and (max-width: 992px) {
.filter {
display: block;
}
}

View File

@ -88,14 +88,16 @@ export function getDateRange(value) {
} }
export function getDateRangeValues(startDate, endDate) { export function getDateRangeValues(startDate, endDate) {
if (differenceInHours(endDate, startDate) <= 48) { let unit = 'year';
return { startDate: startOfHour(startDate), endDate: endOfHour(endDate), unit: 'hour' }; if (differenceInHours(endDate, startDate) <= 72) {
unit = 'hour';
} else if (differenceInCalendarDays(endDate, startDate) <= 90) { } else if (differenceInCalendarDays(endDate, startDate) <= 90) {
return { startDate: startOfDay(startDate), endDate: endOfDay(endDate), unit: 'day' }; unit = 'day';
} else if (differenceInCalendarMonths(endDate, startDate) <= 24) { } else if (differenceInCalendarMonths(endDate, startDate) <= 24) {
return { startDate: startOfMonth(startDate), endDate: endOfMonth(endDate), unit: 'month' }; unit = 'month';
} }
return { startDate: startOfYear(startDate), endDate: endOfYear(endDate), unit: 'year' };
return { startDate: startOfDay(startDate), endDate: endOfDay(endDate), unit };
} }
const dateFuncs = { const dateFuncs = {
@ -112,11 +114,12 @@ export function getDateArray(data, startDate, endDate, unit) {
function findData(t) { function findData(t) {
const x = data.find(e => { const x = data.find(e => {
if (unit === 'day') { if (unit === 'hour') {
return normalize(new Date(e.t)).getTime() === t.getTime();
}
const [year, month, day] = e.t.split('-'); const [year, month, day] = e.t.split('-');
return normalize(new Date(year, month - 1, day)).getTime() === t.getTime(); return normalize(new Date(year, month - 1, day)).getTime() === t.getTime();
}
return normalize(new Date(e.t)).getTime() === t.getTime();
}); });
return x?.y || 0; return x?.y || 0;

View File

@ -37,9 +37,9 @@ export const menuOptions = [
{ label: 'Nederlands (Dutch)', value: 'nl-NL', display: 'NL' }, { label: 'Nederlands (Dutch)', value: 'nl-NL', display: 'NL' },
{ label: 'Deutsch (German)', value: 'de-DE', display: 'DE' }, { label: 'Deutsch (German)', value: 'de-DE', display: 'DE' },
{ label: '日本語 (Japanese)', value: 'ja-JP', display: '日本語' }, { label: '日本語 (Japanese)', value: 'ja-JP', display: '日本語' },
{ label: 'Español (Mexicano)', value: 'es-MX', display: 'ES' },
{ label: 'Русский (Russian)', value: 'ru-RU', display: 'РУ' }, { label: 'Русский (Russian)', value: 'ru-RU', display: 'РУ' },
{ label: 'Turkish', value: 'tr-TR', display: 'TR' }, { label: 'Turkish', value: 'tr-TR', display: 'TR' },
{ label: 'Español (Mexicano)', value: 'es-MX', display: 'ES' },
]; ];
export function dateFormat(date, str, locale) { export function dateFormat(date, str, locale) {

View File

@ -3,7 +3,7 @@ import { getEvents } from 'lib/queries';
import { ok, badRequest, methodNotAllowed } from 'lib/response'; import { ok, badRequest, methodNotAllowed } from 'lib/response';
import { useAuth } from 'lib/middleware'; import { useAuth } from 'lib/middleware';
const unitTypes = ['month', 'hour', 'day']; const unitTypes = ['year', 'month', 'hour', 'day'];
export default async (req, res) => { export default async (req, res) => {
await useAuth(req, res); await useAuth(req, res);