mirror of
https://github.com/getredash/redash.git
synced 2026-05-13 06:00:53 -04:00
* Migrate router and <app-view> to React: skeleton * Update layout on route change * Start moving page routes from angular to react * Move page routes to react except of public dashboard and visualization embed) * Move public dashboard and visualization embed routes to React * Replace $route/$routeParams usages * Some cleanup * Replace AngularJS $location service with implementation based on history library * Minor fix to how ApplicationView handles route change * Explicitly use global layout for each page instead of handling related stuff in ApplicationArea component * Error handling * Remove AngularJS and related dependencies * Move Parameter factory method to a separate file * Fix CSS (replace custom components with classes) * Fix: keep other url parts when updating location partially; refine code * Fix tests * Make router work in multi-org mode (respect <base> tag) * Optimzation: don't resolve route if path didn't change * Fix search input in header; error handling improvement (handle more errors in pages; global error handler for unhandled errors; dialog dismiss 'unhandled rejection' errors) * Fix page keys; fix navigateTo calls (third parameter not available) * Use relative links * Router: ignore location REPLACE events, resolve only on PUSH/POP * Fix tests * Remove unused jQuery reference * Show error from backend when creating Destination * Remove route.resolve where not necessary (used constant values) * New Query page: keep state on saving, reload when creating another new query * Use currentRoute.key instead of hard-coded keys for page components * Tidy up Router * Tidy up location service * Fix tests * Don't add parameters changes to browser's history * Fix test (improved fix) Co-authored-by: Gabriel Dutra <nesk.frz@gmail.com>
96 lines
2.4 KiB
JavaScript
96 lines
2.4 KiB
JavaScript
import { findKey, startsWith, has, includes, isNull, values } from "lodash";
|
|
import moment from "moment";
|
|
import PropTypes from "prop-types";
|
|
import Parameter from "./Parameter";
|
|
|
|
const DATETIME_FORMATS = {
|
|
// eslint-disable-next-line quote-props
|
|
date: "YYYY-MM-DD",
|
|
"datetime-local": "YYYY-MM-DD HH:mm",
|
|
"datetime-with-seconds": "YYYY-MM-DD HH:mm:ss",
|
|
};
|
|
|
|
const DYNAMIC_PREFIX = "d_";
|
|
|
|
const DYNAMIC_DATES = {
|
|
now: {
|
|
name: "Today/Now",
|
|
value: () => moment(),
|
|
},
|
|
yesterday: {
|
|
name: "Yesterday",
|
|
value: () => moment().subtract(1, "day"),
|
|
},
|
|
};
|
|
|
|
export const DynamicDateType = PropTypes.oneOf(values(DYNAMIC_DATES));
|
|
|
|
function isDynamicDateString(value) {
|
|
return startsWith(value, DYNAMIC_PREFIX) && has(DYNAMIC_DATES, value.substring(DYNAMIC_PREFIX.length));
|
|
}
|
|
|
|
export function isDynamicDate(value) {
|
|
return includes(DYNAMIC_DATES, value);
|
|
}
|
|
|
|
export function getDynamicDateFromString(value) {
|
|
if (!isDynamicDateString(value)) {
|
|
return null;
|
|
}
|
|
return DYNAMIC_DATES[value.substring(DYNAMIC_PREFIX.length)];
|
|
}
|
|
|
|
class DateParameter extends Parameter {
|
|
constructor(parameter, parentQueryId) {
|
|
super(parameter, parentQueryId);
|
|
this.useCurrentDateTime = parameter.useCurrentDateTime;
|
|
this.setValue(parameter.value);
|
|
}
|
|
|
|
get hasDynamicValue() {
|
|
return isDynamicDate(this.normalizedValue);
|
|
}
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
normalizeValue(value) {
|
|
if (isDynamicDateString(value)) {
|
|
return getDynamicDateFromString(value);
|
|
}
|
|
|
|
if (isDynamicDate(value)) {
|
|
return value;
|
|
}
|
|
|
|
const normalizedValue = moment(value);
|
|
return normalizedValue.isValid() ? normalizedValue : null;
|
|
}
|
|
|
|
setValue(value) {
|
|
const normalizedValue = this.normalizeValue(value);
|
|
if (isDynamicDate(normalizedValue)) {
|
|
this.value = DYNAMIC_PREFIX + findKey(DYNAMIC_DATES, normalizedValue);
|
|
} else if (moment.isMoment(normalizedValue)) {
|
|
this.value = normalizedValue.format(DATETIME_FORMATS[this.type]);
|
|
} else {
|
|
this.value = normalizedValue;
|
|
}
|
|
this.$$value = normalizedValue;
|
|
|
|
this.updateLocals();
|
|
this.clearPendingValue();
|
|
return this;
|
|
}
|
|
|
|
getExecutionValue() {
|
|
if (this.hasDynamicValue) {
|
|
return this.normalizedValue.value().format(DATETIME_FORMATS[this.type]);
|
|
}
|
|
if (isNull(this.value) && this.useCurrentDateTime) {
|
|
return moment().format(DATETIME_FORMATS[this.type]);
|
|
}
|
|
return this.value;
|
|
}
|
|
}
|
|
|
|
export default DateParameter;
|