Skip to content

Commit

Permalink
added export for range labels
Browse files Browse the repository at this point in the history
  • Loading branch information
SemenStruchev committed Oct 30, 2024
1 parent f8525f8 commit 0afc1bf
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 66 deletions.
167 changes: 101 additions & 66 deletions src/components/DatePicker/RelativeDateRange.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from "react";
import { Button, Select } from "../..";
import { now, toDate } from "../../helpers/date";

const options = {
export const rangeOptions = {
// Day
YESTERDAY: "P1D,yesterday",
TODAY: "P1D,today",
Expand Down Expand Up @@ -38,246 +38,281 @@ const options = {
LEADING_YEAR: "P1Y,+1 year",
};

export const rangeLabels = {
// Day
[rangeOptions.YESTERDAY]: "Yesterday",
[rangeOptions.TODAY]: "Today",
[rangeOptions.NEXT_DAY]: "Next Day",

// Week
[rangeOptions.LAST_WEEK]: "Last Week",
[rangeOptions.TRAILING_WEEK]: "Trailing Week",
[rangeOptions.THIS_WEEK]: "This Week",
[rangeOptions.NEXT_WEEK]: "Next Week",
[rangeOptions.LEADING_WEEK]: "Leading Week",

// Month
[rangeOptions.LAST_MONTH]: "Last Month",
[rangeOptions.TRAILING_MONTH]: "Trailing Month",
[rangeOptions.THIS_MONTH]: "This Month",
[rangeOptions.NEXT_MONTH]: "Next Month",
[rangeOptions.LEADING_MONTH]: "Leading Month",

// Quarter
[rangeOptions.LAST_QUARTER]: "Last Quarter",
[rangeOptions.TRAILING_QUARTER]: "Trailing Quarter",
[rangeOptions.THIS_QUARTER]: "This Quarter",
[rangeOptions.NEXT_QUARTER]: "Next Quarter",
[rangeOptions.LEADING_QUARTER]: "Leading Quarter",

// Year
[rangeOptions.LAST_YEAR]: "Last Year",
[rangeOptions.TRAILING_YEAR]: "Trailing Year",
[rangeOptions.THIS_YEAR]: "This Year",
[rangeOptions.NEXT_YEAR]: "Next Year",
[rangeOptions.LEADING_YEAR]: "Leading Year",
};

export const dateRanges = {
day: {
label: "Day",
options: [
{ value: options.YESTERDAY, label: "Yesterday" },
{ value: options.TODAY, label: "Today" },
{ value: options.NEXT_DAY, label: "Tomorrow" },
rangeOptions: [
{ value: rangeOptions.YESTERDAY, label: "Yesterday" },
{ value: rangeOptions.TODAY, label: "Today" },
{ value: rangeOptions.NEXT_DAY, label: "Tomorrow" },
],
},

week: {
label: "Week",
options: [
{ value: options.LAST_WEEK, label: "Last Week" },
{ value: options.TRAILING_WEEK, label: "Trailing Week" },
{ value: options.THIS_WEEK, label: "This Week" },
{ value: options.NEXT_WEEK, label: "Next Week" },
{ value: options.LEADING_WEEK, label: "Leading Week" },
rangeOptions: [
{ value: rangeOptions.LAST_WEEK, label: "Last Week" },
{ value: rangeOptions.TRAILING_WEEK, label: "Trailing Week" },
{ value: rangeOptions.THIS_WEEK, label: "This Week" },
{ value: rangeOptions.NEXT_WEEK, label: "Next Week" },
{ value: rangeOptions.LEADING_WEEK, label: "Leading Week" },
],
},

month: {
label: "Month",
options: [
{ value: options.LAST_MONTH, label: "Last Month" },
{ value: options.TRAILING_MONTH, label: "Trailing Month" },
{ value: options.THIS_MONTH, label: "This Month" },
{ value: options.NEXT_MONTH, label: "Next Month" },
{ value: options.LEADING_MONTH, label: "Leading Month" },
rangeOptions: [
{ value: rangeOptions.LAST_MONTH, label: "Last Month" },
{ value: rangeOptions.TRAILING_MONTH, label: "Trailing Month" },
{ value: rangeOptions.THIS_MONTH, label: "This Month" },
{ value: rangeOptions.NEXT_MONTH, label: "Next Month" },
{ value: rangeOptions.LEADING_MONTH, label: "Leading Month" },
],
},

quarter: {
label: "Quarter",
options: [
{ value: options.LAST_QUARTER, label: "Last Quarter" },
{ value: options.TRAILING_QUARTER, label: "Trailing Quarter" },
{ value: options.THIS_QUARTER, label: "This Quarter" },
{ value: options.NEXT_QUARTER, label: "Next Quarter" },
{ value: options.LEADING_QUARTER, label: "Leading Quarter" },
rangeOptions: [
{ value: rangeOptions.LAST_QUARTER, label: "Last Quarter" },
{ value: rangeOptions.TRAILING_QUARTER, label: "Trailing Quarter" },
{ value: rangeOptions.THIS_QUARTER, label: "This Quarter" },
{ value: rangeOptions.NEXT_QUARTER, label: "Next Quarter" },
{ value: rangeOptions.LEADING_QUARTER, label: "Leading Quarter" },
],
},

year: {
label: "Year",
options: [
{ value: options.LAST_YEAR, label: "Last Year" },
{ value: options.TRAILING_YEAR, label: "Trailing Year" },
{ value: options.THIS_YEAR, label: "This Year" },
{ value: options.NEXT_YEAR, label: "Next Year" },
{ value: options.LEADING_YEAR, label: "Leading Year" },
rangeOptions: [
{ value: rangeOptions.LAST_YEAR, label: "Last Year" },
{ value: rangeOptions.TRAILING_YEAR, label: "Trailing Year" },
{ value: rangeOptions.THIS_YEAR, label: "This Year" },
{ value: rangeOptions.NEXT_YEAR, label: "Next Year" },
{ value: rangeOptions.LEADING_YEAR, label: "Leading Year" },
],
},
};

const filterFutureDates = (options, isFutureDatesAllowed, futureDates) => {
const filterFutureDates = (rangeOptions, isFutureDatesAllowed, futureDates) => {
if (!isFutureDatesAllowed) {
return options.filter((option) => !futureDates.has(option.value));
return rangeOptions.filter((option) => !futureDates.has(option.value));
}

return options;
return rangeOptions;
};

// List of future date options
// List of future date rangeOptions
const futureDates = new Set([
options.NEXT_DAY,
options.NEXT_WEEK,
options.NEXT_MONTH,
options.NEXT_QUARTER,
options.NEXT_YEAR,
options.LEADING_WEEK,
options.LEADING_MONTH,
options.LEADING_QUARTER,
options.LEADING_YEAR,
rangeOptions.NEXT_DAY,
rangeOptions.NEXT_WEEK,
rangeOptions.NEXT_MONTH,
rangeOptions.NEXT_QUARTER,
rangeOptions.NEXT_YEAR,
rangeOptions.LEADING_WEEK,
rangeOptions.LEADING_MONTH,
rangeOptions.LEADING_QUARTER,
rangeOptions.LEADING_YEAR,
]);

const handlers = {
[options.YESTERDAY]: (timezone) => {
[rangeOptions.YESTERDAY]: (timezone) => {
const yesterday = now(null, timezone).subtract(1, "day");
return {
from: toDate(yesterday.startOf("day")),
to: toDate(yesterday.endOf("day"), false),
};
},

[options.TODAY]: (timezone) => {
[rangeOptions.TODAY]: (timezone) => {
return {
from: toDate(now(null, timezone).startOf("day")),
to: toDate(now(null, timezone).endOf("day"), false),
};
},

[options.NEXT_DAY]: (timezone) => {
[rangeOptions.NEXT_DAY]: (timezone) => {
const nextDay = now(null, timezone).add(1, "day");
return {
from: toDate(nextDay.startOf("day")),
to: toDate(nextDay.endOf("day"), false),
};
},

[options.LAST_WEEK]: (timezone) => {
[rangeOptions.LAST_WEEK]: (timezone) => {
const lastWeek = now(null, timezone).subtract(7, "day");
return {
from: toDate(lastWeek.startOf("week")),
to: toDate(lastWeek.endOf("week"), false),
};
},

[options.TRAILING_WEEK]: (timezone) => {
[rangeOptions.TRAILING_WEEK]: (timezone) => {
return {
from: toDate(now(null, timezone).subtract(7, "day").startOf("day")),
to: toDate(now(null, timezone).subtract(1, "day").endOf("day"), false),
};
},

[options.THIS_WEEK]: (timezone) => {
[rangeOptions.THIS_WEEK]: (timezone) => {
return {
from: toDate(now(null, timezone).startOf("week")),
to: toDate(now(null, timezone).endOf("week"), false),
};
},

[options.NEXT_WEEK]: (timezone) => {
[rangeOptions.NEXT_WEEK]: (timezone) => {
const nextWeek = now(null, timezone).add(1, "week");
return {
from: toDate(nextWeek.startOf("week")),
to: toDate(nextWeek.endOf("week"), false),
};
},

[options.LEADING_WEEK]: (timezone) => {
[rangeOptions.LEADING_WEEK]: (timezone) => {
const today = now(null, timezone);
return {
from: toDate(today.startOf("day")),
to: toDate(today.add(1, "week").subtract(1, "day").endOf("day"), false),
};
},

[options.LAST_MONTH]: (timezone) => {
[rangeOptions.LAST_MONTH]: (timezone) => {
const lastMonth = now(null, timezone).subtract(1, "month");
return {
from: toDate(lastMonth.startOf("month")),
to: toDate(lastMonth.endOf("month"), false),
};
},

[options.TRAILING_MONTH]: (timezone) => {
[rangeOptions.TRAILING_MONTH]: (timezone) => {
return {
from: toDate(now(null, timezone).subtract(1, "month").startOf("day")),
to: toDate(now(null, timezone).subtract(1, "day").endOf("day"), false),
};
},

[options.THIS_MONTH]: (timezone) => ({
[rangeOptions.THIS_MONTH]: (timezone) => ({
from: toDate(now(null, timezone).startOf("month")),
to: toDate(now(null, timezone).endOf("month"), false),
}),

[options.NEXT_MONTH]: (timezone) => {
[rangeOptions.NEXT_MONTH]: (timezone) => {
const nextMonth = now(null, timezone).add(1, "month");
return {
from: toDate(nextMonth.startOf("month")),
to: toDate(nextMonth.endOf("month"), false),
};
},

[options.LEADING_MONTH]: (timezone) => {
[rangeOptions.LEADING_MONTH]: (timezone) => {
const today = now(null, timezone);
return {
from: toDate(today.startOf("day")),
to: toDate(today.add(1, "month").subtract(1, "day").endOf("day"), false),
};
},

[options.LAST_QUARTER]: (timezone) => {
[rangeOptions.LAST_QUARTER]: (timezone) => {
return {
from: toDate(now(null, timezone).startOf("quarter").subtract(3, "month").startOf("month")),
to: toDate(now(null, timezone).endOf("quarter").subtract(3, "month").endOf("month"), false),
};
},

[options.TRAILING_QUARTER]: (timezone) => {
[rangeOptions.TRAILING_QUARTER]: (timezone) => {
return {
from: toDate(now(null, timezone).subtract(3, "month").startOf("day")),
to: toDate(now(null, timezone).subtract(1, "day").endOf("day"), false),
};
},

[options.THIS_QUARTER]: (timezone) => {
[rangeOptions.THIS_QUARTER]: (timezone) => {
return {
from: toDate(now(null, timezone).startOf("Q")),
to: toDate(now(null, timezone).endOf("Q"), false),
};
},

[options.NEXT_QUARTER]: (timezone) => {
[rangeOptions.NEXT_QUARTER]: (timezone) => {
const nextQuarter = now(null, timezone).add(1, "quarter");
return {
from: toDate(nextQuarter.startOf("quarter")),
to: toDate(nextQuarter.endOf("quarter"), false),
};
},

[options.LEADING_QUARTER]: (timezone) => {
[rangeOptions.LEADING_QUARTER]: (timezone) => {
const today = now(null, timezone);
return {
from: toDate(today.startOf("day")),
to: toDate(today.add(3, "month").subtract(1, "day").endOf("day"), false), // Сегодня + 3 месяца
};
},

[options.LAST_YEAR]: (timezone) => {
[rangeOptions.LAST_YEAR]: (timezone) => {
const lastYear = now(null, timezone).subtract(1, "year");
return {
from: toDate(lastYear.startOf("year")),
to: toDate(lastYear.endOf("year"), false),
};
},

[options.TRAILING_YEAR]: (timezone) => {
[rangeOptions.TRAILING_YEAR]: (timezone) => {
return {
from: toDate(now(null, timezone).subtract(1, "year").startOf("day")),
to: toDate(now(null, timezone).subtract(1, "day").endOf("day"), false),
};
},

[options.THIS_YEAR]: (timezone) => ({
[rangeOptions.THIS_YEAR]: (timezone) => ({
from: toDate(now(null, timezone).startOf("year")),
to: toDate(now(null, timezone).endOf("year"), false),
}),

[options.NEXT_YEAR]: (timezone) => {
[rangeOptions.NEXT_YEAR]: (timezone) => {
const nextYear = now(null, timezone).add(1, "year");
return {
from: toDate(nextYear.startOf("year")),
to: toDate(nextYear.endOf("year"), false),
};
},

[options.LEADING_YEAR]: (timezone) => {
[rangeOptions.LEADING_YEAR]: (timezone) => {
const today = now(null, timezone);
return {
from: toDate(today.startOf("day")),
Expand Down Expand Up @@ -311,7 +346,7 @@ export const RelativeDateRange = ({

return (
<optgroup key={rangeKey} label={range.label}>
{filterFutureDates(range.options, isFutureDatesAllowed, futureDates).map((option) => (
{filterFutureDates(range.rangeOptions, isFutureDatesAllowed, futureDates).map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export { almostZero, numberFormat, roundNumber } from "./helpers/numbers";
export { getSymbol, isZeroDecimal } from "./helpers/currency";
export { formatDate, formatTime, dateFromObjectId } from "./helpers/date";
export { formatPhoneNumber } from "./helpers/phone";
export { rangeLabels, rangeOptions } from "./components/DatePicker/RelativeDateRange";
export { isOSX, isIosBrowser } from "./helpers/browser";
export { flash } from "./helpers/flash";

Expand Down

0 comments on commit 0afc1bf

Please sign in to comment.