mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-15 17:55:08 +01:00
Add website ordering to state and settings page
This commit is contained in:
parent
70c6211e2c
commit
3926d3fe93
@ -1,4 +1,4 @@
|
|||||||
import React, { useState } from 'react';
|
import React, { useMemo, useState } from 'react';
|
||||||
import { FormattedMessage } from 'react-intl';
|
import { FormattedMessage } from 'react-intl';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import Link from 'components/common/Link';
|
import Link from 'components/common/Link';
|
||||||
@ -24,8 +24,12 @@ import Code from 'assets/code.svg';
|
|||||||
import LinkIcon from 'assets/link.svg';
|
import LinkIcon from 'assets/link.svg';
|
||||||
import useFetch from 'hooks/useFetch';
|
import useFetch from 'hooks/useFetch';
|
||||||
import useUser from 'hooks/useUser';
|
import useUser from 'hooks/useUser';
|
||||||
|
import { orderByWebsiteMap } from 'lib/format';
|
||||||
|
import useStore, { setDashboard } from 'store/app';
|
||||||
import styles from './WebsiteSettings.module.css';
|
import styles from './WebsiteSettings.module.css';
|
||||||
|
|
||||||
|
const selector = state => state.dashboard;
|
||||||
|
|
||||||
export default function WebsiteSettings() {
|
export default function WebsiteSettings() {
|
||||||
const { user } = useUser();
|
const { user } = useUser();
|
||||||
const [editWebsite, setEditWebsite] = useState();
|
const [editWebsite, setEditWebsite] = useState();
|
||||||
@ -36,8 +40,14 @@ export default function WebsiteSettings() {
|
|||||||
const [showUrl, setShowUrl] = useState();
|
const [showUrl, setShowUrl] = useState();
|
||||||
const [saved, setSaved] = useState(0);
|
const [saved, setSaved] = useState(0);
|
||||||
const [message, setMessage] = useState();
|
const [message, setMessage] = useState();
|
||||||
|
|
||||||
|
const store = useStore(selector);
|
||||||
|
const { websiteOrdering } = store;
|
||||||
|
|
||||||
const { data } = useFetch('/websites', { params: { include_all: !!user?.is_admin } }, [saved]);
|
const { data } = useFetch('/websites', { params: { include_all: !!user?.is_admin } }, [saved]);
|
||||||
|
|
||||||
|
const ordered = useMemo(() => orderByWebsiteMap(data, websiteOrdering), [data, websiteOrdering]);
|
||||||
|
|
||||||
const Buttons = row => (
|
const Buttons = row => (
|
||||||
<ButtonLayout align="right">
|
<ButtonLayout align="right">
|
||||||
{row.share_id && (
|
{row.share_id && (
|
||||||
@ -157,6 +167,21 @@ export default function WebsiteSettings() {
|
|||||||
setShowUrl(null);
|
setShowUrl(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handleWebsiteDrag({ destination, source }) {
|
||||||
|
if (!destination || destination.index === source.index) return;
|
||||||
|
|
||||||
|
const orderedWebsites = [...ordered];
|
||||||
|
const [removed] = orderedWebsites.splice(source.index, 1);
|
||||||
|
orderedWebsites.splice(destination.index, 0, removed);
|
||||||
|
|
||||||
|
setDashboard({
|
||||||
|
...store,
|
||||||
|
websiteOrdering: orderedWebsites
|
||||||
|
.map((i, k) => ({ [i.website_uuid]: k }))
|
||||||
|
.reduce((a, b) => ({ ...a, ...b })),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (!data) {
|
if (!data) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -186,7 +211,14 @@ export default function WebsiteSettings() {
|
|||||||
<FormattedMessage id="label.add-website" defaultMessage="Add website" />
|
<FormattedMessage id="label.add-website" defaultMessage="Add website" />
|
||||||
</Button>
|
</Button>
|
||||||
</PageHeader>
|
</PageHeader>
|
||||||
<Table columns={user.is_admin ? adminColumns : columns} rows={data} empty={empty} />
|
<Table
|
||||||
|
columns={user.is_admin ? adminColumns : columns}
|
||||||
|
rows={ordered}
|
||||||
|
empty={empty}
|
||||||
|
isDraggable
|
||||||
|
onDragEnd={handleWebsiteDrag}
|
||||||
|
dragId={'website-settings-list'}
|
||||||
|
/>
|
||||||
{editWebsite && (
|
{editWebsite && (
|
||||||
<Modal title={<FormattedMessage id="label.edit-website" defaultMessage="Edit website" />}>
|
<Modal title={<FormattedMessage id="label.edit-website" defaultMessage="Edit website" />}>
|
||||||
<WebsiteEditForm values={editWebsite} onSave={handleSave} onClose={handleClose} />
|
<WebsiteEditForm values={editWebsite} onSave={handleSave} onClose={handleClose} />
|
||||||
|
@ -78,3 +78,14 @@ 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;
|
||||||
|
}
|
||||||
|
@ -12,6 +12,7 @@ import { getItem, setItem } from 'lib/web';
|
|||||||
export const defaultDashboardConfig = {
|
export const defaultDashboardConfig = {
|
||||||
showCharts: true,
|
showCharts: true,
|
||||||
limit: DEFAULT_WEBSITE_LIMIT,
|
limit: DEFAULT_WEBSITE_LIMIT,
|
||||||
|
websiteOrdering: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
|
Loading…
Reference in New Issue
Block a user