mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-15 09:45:04 +01:00
Refactored dashboard sort logic.
This commit is contained in:
parent
42e87a4691
commit
62b032ab19
@ -40,8 +40,8 @@ export default function Dashboard() {
|
|||||||
<div>{formatMessage(messages.dashboard)}</div>
|
<div>{formatMessage(messages.dashboard)}</div>
|
||||||
{!editing && <DashboardSettingsButton />}
|
{!editing && <DashboardSettingsButton />}
|
||||||
</PageHeader>
|
</PageHeader>
|
||||||
{editing && <DashboardEdit data={data} />}
|
{editing && <DashboardEdit websites={data} />}
|
||||||
{!editing && <WebsiteList data={data} showCharts={showCharts} limit={max} />}
|
{!editing && <WebsiteList websites={data} showCharts={showCharts} limit={max} />}
|
||||||
{max < data.length && (
|
{max < data.length && (
|
||||||
<Button className={styles.button} onClick={handleMore}>
|
<Button className={styles.button} onClick={handleMore}>
|
||||||
{formatMessage(messages.more)}
|
{formatMessage(messages.more)}
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
import { useState } from 'react';
|
import { useState, useMemo } from 'react';
|
||||||
import { defineMessages, useIntl } from 'react-intl';
|
import { defineMessages, useIntl } from 'react-intl';
|
||||||
import { DragDropContext, Draggable, Droppable } from 'react-beautiful-dnd';
|
import { DragDropContext, Draggable, Droppable } from 'react-beautiful-dnd';
|
||||||
import useDashboard, { saveDashboard } from 'store/dashboard';
|
|
||||||
import Button from 'components/common/Button';
|
|
||||||
import { useMemo } from 'react';
|
|
||||||
import { orderByWebsiteMap } from 'lib/format';
|
|
||||||
import styles from './DashboardEdit.module.css';
|
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
|
import Button from 'components/common/Button';
|
||||||
|
import { sortArrayByMap } from 'lib/array';
|
||||||
|
import useDashboard, { saveDashboard } from 'store/dashboard';
|
||||||
|
import styles from './DashboardEdit.module.css';
|
||||||
|
|
||||||
const messages = defineMessages({
|
const messages = defineMessages({
|
||||||
save: { id: 'label.save', defaultMessage: 'Save' },
|
save: { id: 'label.save', defaultMessage: 'Save' },
|
||||||
@ -16,13 +15,13 @@ const messages = defineMessages({
|
|||||||
|
|
||||||
const dragId = 'dashboard-website-ordering';
|
const dragId = 'dashboard-website-ordering';
|
||||||
|
|
||||||
export default function DashboardEdit({ data: websites }) {
|
export default function DashboardEdit({ websites }) {
|
||||||
const settings = useDashboard();
|
const settings = useDashboard();
|
||||||
const { websiteOrder } = settings;
|
const { websiteOrder } = settings;
|
||||||
const { formatMessage } = useIntl();
|
const { formatMessage } = useIntl();
|
||||||
const [order, setOrder] = useState(websiteOrder);
|
const [order, setOrder] = useState(websiteOrder || []);
|
||||||
|
|
||||||
const ordered = useMemo(() => orderByWebsiteMap(websites, order), [websites, order]);
|
const ordered = useMemo(() => sortArrayByMap(websites, order, 'website_id'), [websites, order]);
|
||||||
|
|
||||||
console.log({ order, ordered });
|
console.log({ order, ordered });
|
||||||
|
|
||||||
@ -33,9 +32,7 @@ export default function DashboardEdit({ data: websites }) {
|
|||||||
const [removed] = orderedWebsites.splice(source.index, 1);
|
const [removed] = orderedWebsites.splice(source.index, 1);
|
||||||
orderedWebsites.splice(destination.index, 0, removed);
|
orderedWebsites.splice(destination.index, 0, removed);
|
||||||
|
|
||||||
setOrder(
|
setOrder(orderedWebsites.map(({ website_id }) => website_id));
|
||||||
orderedWebsites.map((i, k) => ({ [i.website_uuid]: k })).reduce((a, b) => ({ ...a, ...b })),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleSave() {
|
function handleSave() {
|
||||||
@ -50,7 +47,7 @@ export default function DashboardEdit({ data: websites }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function handleReset() {
|
function handleReset() {
|
||||||
setOrder({});
|
setOrder([]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -94,6 +91,7 @@ export default function DashboardEdit({ data: websites }) {
|
|||||||
)}
|
)}
|
||||||
</Draggable>
|
</Draggable>
|
||||||
))}
|
))}
|
||||||
|
{provided.placeholder}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</Droppable>
|
</Droppable>
|
||||||
|
@ -6,7 +6,7 @@ import EmptyPlaceholder from 'components/common/EmptyPlaceholder';
|
|||||||
import Arrow from 'assets/arrow-right.svg';
|
import Arrow from 'assets/arrow-right.svg';
|
||||||
import styles from './WebsiteList.module.css';
|
import styles from './WebsiteList.module.css';
|
||||||
import useDashboard from 'store/dashboard';
|
import useDashboard from 'store/dashboard';
|
||||||
import { orderByWebsiteMap } from 'lib/format';
|
import { sortArrayByMap } from 'lib/array';
|
||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
|
|
||||||
const messages = defineMessages({
|
const messages = defineMessages({
|
||||||
@ -20,11 +20,16 @@ const messages = defineMessages({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export default function WebsiteList({ data, showCharts, limit }) {
|
export default function WebsiteList({ websites, showCharts, limit }) {
|
||||||
const { websiteOrder } = useDashboard();
|
const { websiteOrder } = useDashboard();
|
||||||
const { formatMessage } = useIntl();
|
const { formatMessage } = useIntl();
|
||||||
|
|
||||||
const websites = useMemo(() => orderByWebsiteMap(data, websiteOrder), [data, websiteOrder]);
|
console.log({ websiteOrder });
|
||||||
|
|
||||||
|
const ordered = useMemo(
|
||||||
|
() => sortArrayByMap(websites, websiteOrder, 'website_id'),
|
||||||
|
[websites, websiteOrder],
|
||||||
|
);
|
||||||
|
|
||||||
if (websites.length === 0) {
|
if (websites.length === 0) {
|
||||||
return (
|
return (
|
||||||
@ -40,7 +45,7 @@ export default function WebsiteList({ data, showCharts, limit }) {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
{websites.map(({ website_id, name, domain }, index) =>
|
{ordered.map(({ website_id, name, domain }, index) =>
|
||||||
index < limit ? (
|
index < limit ? (
|
||||||
<div key={website_id} className={styles.website}>
|
<div key={website_id} className={styles.website}>
|
||||||
<WebsiteChart
|
<WebsiteChart
|
||||||
|
@ -25,11 +25,7 @@ export default function DashboardSettingsButton() {
|
|||||||
|
|
||||||
function handleSelect(value) {
|
function handleSelect(value) {
|
||||||
if (value === 'charts') {
|
if (value === 'charts') {
|
||||||
saveDashboard(state => {
|
saveDashboard(state => ({ showCharts: !state.showCharts }));
|
||||||
const bs = { showCharts: !state.showCharts };
|
|
||||||
console.log('WTF', { state, bs });
|
|
||||||
return bs;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
if (value === 'order') {
|
if (value === 'order') {
|
||||||
saveDashboard({ editing: true });
|
saveDashboard({ editing: true });
|
||||||
|
@ -9,3 +9,10 @@ export function chunk(arr, size) {
|
|||||||
|
|
||||||
return chunks;
|
return chunks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function sortArrayByMap(arr, map = [], key) {
|
||||||
|
if (!arr) return [];
|
||||||
|
if (map.length === 0) return arr;
|
||||||
|
|
||||||
|
return map.map(id => arr.find(item => item[key] === id));
|
||||||
|
}
|
||||||
|
@ -78,14 +78,3 @@ export function stringToColor(str) {
|
|||||||
}
|
}
|
||||||
return color;
|
return color;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function orderByWebsiteMap(websites, orderMap) {
|
|
||||||
if (!websites) return [];
|
|
||||||
|
|
||||||
let ordered = [...websites];
|
|
||||||
for (let website of websites) {
|
|
||||||
ordered[orderMap[website.website_uuid]] = website;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ordered;
|
|
||||||
}
|
|
||||||
|
@ -69,8 +69,6 @@ export const getItem = (key, session) => {
|
|||||||
return JSON.parse(value);
|
return JSON.parse(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const removeItem = (key, session) => {
|
export const removeItem = (key, session) => {
|
||||||
|
@ -5,15 +5,14 @@ import { getItem, setItem } from 'lib/web';
|
|||||||
export const initialState = {
|
export const initialState = {
|
||||||
showCharts: true,
|
showCharts: true,
|
||||||
limit: DEFAULT_WEBSITE_LIMIT,
|
limit: DEFAULT_WEBSITE_LIMIT,
|
||||||
websiteOrder: {},
|
websiteOrder: [],
|
||||||
editing: false,
|
editing: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
const store = create(() => ({ ...initialState, ...getItem(DASHBOARD_CONFIG) }));
|
const store = create(() => ({ ...initialState, ...getItem(DASHBOARD_CONFIG) }));
|
||||||
|
|
||||||
export function saveDashboard(settings) {
|
export function saveDashboard(settings) {
|
||||||
const state = typeof settings === 'function' ? settings(store.getState()) : settings;
|
store.setState(settings);
|
||||||
store.setState(state);
|
|
||||||
|
|
||||||
setItem(DASHBOARD_CONFIG, store.getState());
|
setItem(DASHBOARD_CONFIG, store.getState());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user