Add website ordering to state and settings page

This commit is contained in:
Chris Walsh 2022-07-23 23:01:59 -07:00
parent 70c6211e2c
commit 3926d3fe93
No known key found for this signature in database
GPG Key ID: 28EE0CCA6032019E
3 changed files with 46 additions and 2 deletions

View File

@ -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} />

View File

@ -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;
}

View File

@ -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 = {