Updated date range handling. Fixed share page.

This commit is contained in:
Mike Cao 2023-03-08 16:37:43 -08:00
parent 3fc5f5151b
commit 696d9c978c
28 changed files with 166 additions and 280 deletions

View file

@ -1,10 +1,18 @@
import create from 'zustand';
import { DEFAULT_LOCALE, DEFAULT_THEME, LOCALE_CONFIG, THEME_CONFIG } from 'lib/constants';
import {
DATE_RANGE_CONFIG,
DEFAULT_DATE_RANGE,
DEFAULT_LOCALE,
DEFAULT_THEME,
LOCALE_CONFIG,
THEME_CONFIG,
} from 'lib/constants';
import { getItem } from 'next-basics';
const initialState = {
locale: getItem(LOCALE_CONFIG) || DEFAULT_LOCALE,
theme: getItem(THEME_CONFIG) || DEFAULT_THEME,
dateRange: getItem(DATE_RANGE_CONFIG) || DEFAULT_DATE_RANGE,
shareToken: null,
user: null,
config: null,
@ -32,4 +40,8 @@ export function setConfig(config) {
store.setState({ config });
}
export function setDateRange(dateRange) {
store.setState({ dateRange });
}
export default store;

View file

@ -1,11 +1,15 @@
import create from 'zustand';
import produce from 'immer';
import app from './app';
import { getDateRange } from 'lib/date';
import { parseDateRange } from 'lib/date';
const store = create(() => ({}));
export function setDateRange(websiteId, value) {
export function getWebsiteDateRange(websiteId) {
return store.getState()?.[websiteId];
}
export function setWebsiteDateRange(websiteId, value) {
store.setState(
produce(state => {
if (!state[websiteId]) {
@ -16,7 +20,7 @@ export function setDateRange(websiteId, value) {
if (typeof value === 'string') {
const { locale } = app.getState();
dateRange = getDateRange(value, locale);
dateRange = parseDateRange(value, locale);
}
state[websiteId].dateRange = { ...dateRange, modified: Date.now() };