Skip to content

Commit aceb904

Browse files
mikecaobriancao
andauthored
v1.39.2 (#1599)
* Fixed issue with realtime page rendering. * fix auth, add pg extension (#1596) * Fixed change password issue. API refactoring. Closes #1592. * Fixed account lookup. * Fixed issue with accessing user dashboards. Closes #1590 * fix sort on dashboard (#1600) Co-authored-by: Brian Cao <[email protected]>
1 parent 94dc915 commit aceb904

38 files changed

+145
-169
lines changed

components/forms/ChangePasswordForm.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import FormLayout, {
99
FormRow,
1010
} from 'components/layout/FormLayout';
1111
import useApi from 'hooks/useApi';
12-
import useUser from '../../hooks/useUser';
12+
import useUser from 'hooks/useUser';
1313

1414
const initialValues = {
1515
current_password: '',
@@ -43,13 +43,13 @@ export default function ChangePasswordForm({ values, onSave, onClose }) {
4343
const { user } = useUser();
4444

4545
const handleSubmit = async values => {
46-
const { ok, data } = await post(`/accounts/${user.userId}/password`, values);
46+
const { ok, error } = await post(`/accounts/${user.accountUuid}/password`, values);
4747

4848
if (ok) {
4949
onSave();
5050
} else {
5151
setMessage(
52-
data || <FormattedMessage id="message.failure" defaultMessage="Something went wrong." />,
52+
error || <FormattedMessage id="message.failure" defaultMessage="Something went wrong." />,
5353
);
5454
}
5555
};

components/forms/EventDataForm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const filterOptions = [
2222
{ label: 'Count', value: 'count' },
2323
{ label: 'Average', value: 'avg' },
2424
{ label: 'Minimum', value: 'min' },
25-
{ label: 'Maxmimum', value: 'max' },
25+
{ label: 'Maximum', value: 'max' },
2626
{ label: 'Sum', value: 'sum' },
2727
];
2828

components/metrics/RealtimeHeader.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ import styles from './RealtimeHeader.module.css';
99

1010
export default function RealtimeHeader({ websites, data, websiteId, onSelect }) {
1111
const options = [
12-
{ label: <FormattedMessage id="label.all-websites" defaultMessage="All websites" />, value: 0 },
12+
{
13+
label: <FormattedMessage id="label.all-websites" defaultMessage="All websites" />,
14+
value: null,
15+
},
1316
].concat(
14-
websites.map(({ name, id }, index) => ({
17+
websites.map(({ name, websiteUuid }, index) => ({
1518
label: name,
16-
value: id,
19+
value: websiteUuid,
1720
divider: index === 0,
1821
})),
1922
);

components/pages/Dashboard.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { useState } from 'react';
22
import { defineMessages, useIntl } from 'react-intl';
3-
import { useRouter } from 'next/router';
43
import Page from 'components/layout/Page';
54
import PageHeader from 'components/layout/PageHeader';
65
import WebsiteList from 'components/pages/WebsiteList';
@@ -16,10 +15,7 @@ const messages = defineMessages({
1615
more: { id: 'label.more', defaultMessage: 'More' },
1716
});
1817

19-
export default function Dashboard() {
20-
const router = useRouter();
21-
const { id } = router.query;
22-
const userId = id?.[0];
18+
export default function Dashboard({ userId }) {
2319
const dashboard = useDashboard();
2420
const { showCharts, limit, editing } = dashboard;
2521
const [max, setMax] = useState(limit);

components/pages/DashboardEdit.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default function DashboardEdit({ websites }) {
2424
const ordered = useMemo(
2525
() =>
2626
websites
27-
.map(website => ({ ...website, order: order.indexOf(website.websiteId) }))
27+
.map(website => ({ ...website, order: order.indexOf(website.websiteUuid) }))
2828
.sort(firstBy('order')),
2929
[websites, order],
3030
);
@@ -36,7 +36,7 @@ export default function DashboardEdit({ websites }) {
3636
const [removed] = orderedWebsites.splice(source.index, 1);
3737
orderedWebsites.splice(destination.index, 0, removed);
3838

39-
setOrder(orderedWebsites.map(website => website?.websiteId || 0));
39+
setOrder(orderedWebsites.map(website => website?.websiteUuid || 0));
4040
}
4141

4242
function handleSave() {
@@ -76,8 +76,12 @@ export default function DashboardEdit({ websites }) {
7676
ref={provided.innerRef}
7777
style={{ marginBottom: snapshot.isDraggingOver ? 260 : null }}
7878
>
79-
{ordered.map(({ websiteId, name, domain }, index) => (
80-
<Draggable key={websiteId} draggableId={`${dragId}-${websiteId}`} index={index}>
79+
{ordered.map(({ websiteUuid, name, domain }, index) => (
80+
<Draggable
81+
key={websiteUuid}
82+
draggableId={`${dragId}-${websiteUuid}`}
83+
index={index}
84+
>
8185
{(provided, snapshot) => (
8286
<div
8387
ref={provided.innerRef}

components/pages/RealtimeDashboard.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default function RealtimeDashboard() {
3232
const { locale } = useLocale();
3333
const countryNames = useCountryNames(locale);
3434
const [data, setData] = useState();
35-
const [websiteId, setWebsiteId] = useState(0);
35+
const [websiteUuid, setWebsiteUuid] = useState(null);
3636
const { data: init, loading } = useFetch('/realtime/init');
3737
const { data: updates } = useFetch('/realtime/update', {
3838
params: { start_at: data?.timestamp },
@@ -50,17 +50,18 @@ export default function RealtimeDashboard() {
5050
if (data) {
5151
const { pageviews, sessions, events } = data;
5252

53-
if (websiteId) {
53+
if (websiteUuid) {
54+
const { id } = init.websites.find(n => n.websiteUuid === websiteUuid);
5455
return {
55-
pageviews: filterWebsite(pageviews, websiteId),
56-
sessions: filterWebsite(sessions, websiteId),
57-
events: filterWebsite(events, websiteId),
56+
pageviews: filterWebsite(pageviews, id),
57+
sessions: filterWebsite(sessions, id),
58+
events: filterWebsite(events, id),
5859
};
5960
}
6061
}
6162

6263
return data;
63-
}, [data, websiteId]);
64+
}, [data, websiteUuid]);
6465

6566
const countries = useMemo(() => {
6667
if (realtimeData?.sessions) {
@@ -117,25 +118,20 @@ export default function RealtimeDashboard() {
117118
<Page>
118119
<RealtimeHeader
119120
websites={websites}
120-
websiteId={websiteId}
121+
websiteId={websiteUuid}
121122
data={{ ...realtimeData, countries }}
122-
onSelect={setWebsiteId}
123+
onSelect={setWebsiteUuid}
123124
/>
124125
<div className={styles.chart}>
125-
<RealtimeChart
126-
websiteId={websiteId}
127-
data={realtimeData}
128-
unit="minute"
129-
records={REALTIME_RANGE}
130-
/>
126+
<RealtimeChart data={realtimeData} unit="minute" records={REALTIME_RANGE} />
131127
</div>
132128
<GridLayout>
133129
<GridRow>
134130
<GridColumn xs={12} lg={4}>
135-
<RealtimeViews websiteId={websiteId} data={realtimeData} websites={websites} />
131+
<RealtimeViews websiteId={websiteUuid} data={realtimeData} websites={websites} />
136132
</GridColumn>
137133
<GridColumn xs={12} lg={8}>
138-
<RealtimeLog websiteId={websiteId} data={realtimeData} websites={websites} />
134+
<RealtimeLog websiteId={websiteUuid} data={realtimeData} websites={websites} />
139135
</GridColumn>
140136
</GridRow>
141137
<GridRow>

components/settings/AccountSettings.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ export default function AccountSettings() {
2929

3030
const Checkmark = ({ isAdmin }) => (isAdmin ? <Icon icon={<Check />} size="medium" /> : null);
3131

32-
const DashboardLink = row => (
33-
<Link href={`/dashboard/${row.userId}/${row.username}`}>
34-
<a>
35-
<Icon icon={<LinkIcon />} />
36-
</a>
37-
</Link>
38-
);
32+
const DashboardLink = row => {
33+
return (
34+
<Link href={`/dashboard/${row.accountUuid}/${row.username}`}>
35+
<a>
36+
<Icon icon={<LinkIcon />} />
37+
</a>
38+
</Link>
39+
);
40+
};
3941

4042
const Buttons = row => (
4143
<ButtonLayout align="right">

db/postgresql/migrations/04_add_uuid/migration.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
-- CreateExtension
2+
CREATE EXTENSION IF NOT EXISTS pgcrypto;
13

24
-- AlterTable
35
ALTER TABLE "account" ADD COLUMN "account_uuid" UUID NULL;

lib/auth.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { parseSecureToken, parseToken } from 'next-basics';
2-
import { getWebsite } from 'queries';
3-
import { SHARE_TOKEN_HEADER } from 'lib/constants';
2+
import { getAccount, getWebsite } from 'queries';
3+
import { SHARE_TOKEN_HEADER, TYPE_ACCOUNT, TYPE_WEBSITE } from 'lib/constants';
44
import { secret } from 'lib/crypto';
55

66
export function getAuthToken(req) {
@@ -35,7 +35,7 @@ export function isValidToken(token, validation) {
3535
return false;
3636
}
3737

38-
export async function allowQuery(req) {
38+
export async function allowQuery(req, type) {
3939
const { id } = req.query;
4040

4141
const { userId, isAdmin, shareToken } = req.auth ?? {};
@@ -49,9 +49,15 @@ export async function allowQuery(req) {
4949
}
5050

5151
if (userId) {
52-
const website = await getWebsite({ id });
52+
if (type === TYPE_WEBSITE) {
53+
const website = await getWebsite({ websiteUuid: id });
5354

54-
return website && website.userId === userId;
55+
return website && website.userId === userId;
56+
} else if (type === TYPE_ACCOUNT) {
57+
const account = await getAccount({ accountUuid: id });
58+
59+
return account && account.accountUuid === id;
60+
}
5561
}
5662

5763
return false;

lib/constants.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ export const DEFAULT_WEBSITE_LIMIT = 10;
2121
export const REALTIME_RANGE = 30;
2222
export const REALTIME_INTERVAL = 3000;
2323

24+
export const TYPE_WEBSITE = 'website';
25+
export const TYPE_ACCOUNT = 'account';
26+
2427
export const THEME_COLORS = {
2528
light: {
2629
primary: '#2680eb',

0 commit comments

Comments
 (0)