mirror of
https://github.com/kremalicious/umami.git
synced 2024-12-18 07:13:37 +01:00
Merge pull request #752 from cywio/reset-stats
Add reset website statistics to settings
This commit is contained in:
commit
54c39b7cc9
98
components/forms/ResetForm.js
Normal file
98
components/forms/ResetForm.js
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
import React, { useState } from 'react';
|
||||||
|
import { FormattedMessage } from 'react-intl';
|
||||||
|
import { Formik, Form, Field } from 'formik';
|
||||||
|
import Button from 'components/common/Button';
|
||||||
|
import FormLayout, {
|
||||||
|
FormButtons,
|
||||||
|
FormError,
|
||||||
|
FormMessage,
|
||||||
|
FormRow,
|
||||||
|
} from 'components/layout/FormLayout';
|
||||||
|
import usePost from 'hooks/usePost';
|
||||||
|
|
||||||
|
const CONFIRMATION_WORD = 'RESET';
|
||||||
|
|
||||||
|
const validate = ({ confirmation }) => {
|
||||||
|
const errors = {};
|
||||||
|
|
||||||
|
if (confirmation !== CONFIRMATION_WORD) {
|
||||||
|
errors.confirmation = !confirmation ? (
|
||||||
|
<FormattedMessage id="label.required" defaultMessage="Required" />
|
||||||
|
) : (
|
||||||
|
<FormattedMessage id="label.invalid" defaultMessage="Invalid" />
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return errors;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function ResetForm({ values, onSave, onClose }) {
|
||||||
|
const post = usePost();
|
||||||
|
const [message, setMessage] = useState();
|
||||||
|
|
||||||
|
const handleSubmit = async ({ type, id }) => {
|
||||||
|
const { ok, data } = await post(`/api/${type}/${id}/reset`);
|
||||||
|
|
||||||
|
if (ok) {
|
||||||
|
onSave();
|
||||||
|
} else {
|
||||||
|
setMessage(
|
||||||
|
data || <FormattedMessage id="message.failure" defaultMessage="Something went wrong." />,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<FormLayout>
|
||||||
|
<Formik
|
||||||
|
initialValues={{ confirmation: '', ...values }}
|
||||||
|
validate={validate}
|
||||||
|
onSubmit={handleSubmit}
|
||||||
|
>
|
||||||
|
{props => (
|
||||||
|
<Form>
|
||||||
|
<div>
|
||||||
|
<FormattedMessage
|
||||||
|
id="message.confirm-reset"
|
||||||
|
defaultMessage="Are your sure you want to reset {target}'s statistics?"
|
||||||
|
values={{ target: <b>{values.name}</b> }}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<FormattedMessage
|
||||||
|
id="message.reset-warning"
|
||||||
|
defaultMessage="All statistics for this website will be deleted, but your tracking code will remain intact."
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<p>
|
||||||
|
<FormattedMessage
|
||||||
|
id="message.type-reset"
|
||||||
|
defaultMessage="Type {reset} in the box below to confirm."
|
||||||
|
values={{ reset: <b>{CONFIRMATION_WORD}</b> }}
|
||||||
|
/>
|
||||||
|
</p>
|
||||||
|
<FormRow>
|
||||||
|
<div>
|
||||||
|
<Field name="confirmation" type="text" />
|
||||||
|
<FormError name="confirmation" />
|
||||||
|
</div>
|
||||||
|
</FormRow>
|
||||||
|
<FormButtons>
|
||||||
|
<Button
|
||||||
|
type="submit"
|
||||||
|
variant="danger"
|
||||||
|
disabled={props.values.confirmation !== CONFIRMATION_WORD}
|
||||||
|
>
|
||||||
|
<FormattedMessage id="label.reset" defaultMessage="Reset" />
|
||||||
|
</Button>
|
||||||
|
<Button onClick={onClose}>
|
||||||
|
<FormattedMessage id="label.cancel" defaultMessage="Cancel" />
|
||||||
|
</Button>
|
||||||
|
</FormButtons>
|
||||||
|
<FormMessage>{message}</FormMessage>
|
||||||
|
</Form>
|
||||||
|
)}
|
||||||
|
</Formik>
|
||||||
|
</FormLayout>
|
||||||
|
);
|
||||||
|
}
|
@ -7,6 +7,7 @@ import Button from 'components/common/Button';
|
|||||||
import PageHeader from 'components/layout/PageHeader';
|
import PageHeader from 'components/layout/PageHeader';
|
||||||
import Modal from 'components/common/Modal';
|
import Modal from 'components/common/Modal';
|
||||||
import WebsiteEditForm from 'components/forms/WebsiteEditForm';
|
import WebsiteEditForm from 'components/forms/WebsiteEditForm';
|
||||||
|
import ResetForm from 'components/forms/ResetForm';
|
||||||
import DeleteForm from 'components/forms/DeleteForm';
|
import DeleteForm from 'components/forms/DeleteForm';
|
||||||
import TrackingCodeForm from 'components/forms/TrackingCodeForm';
|
import TrackingCodeForm from 'components/forms/TrackingCodeForm';
|
||||||
import ShareUrlForm from 'components/forms/ShareUrlForm';
|
import ShareUrlForm from 'components/forms/ShareUrlForm';
|
||||||
@ -16,6 +17,7 @@ import Toast from 'components/common/Toast';
|
|||||||
import Favicon from 'components/common/Favicon';
|
import Favicon from 'components/common/Favicon';
|
||||||
import Pen from 'assets/pen.svg';
|
import Pen from 'assets/pen.svg';
|
||||||
import Trash from 'assets/trash.svg';
|
import Trash from 'assets/trash.svg';
|
||||||
|
import Reset from 'assets/redo.svg';
|
||||||
import Plus from 'assets/plus.svg';
|
import Plus from 'assets/plus.svg';
|
||||||
import Code from 'assets/code.svg';
|
import Code from 'assets/code.svg';
|
||||||
import LinkIcon from 'assets/link.svg';
|
import LinkIcon from 'assets/link.svg';
|
||||||
@ -24,6 +26,7 @@ import styles from './WebsiteSettings.module.css';
|
|||||||
|
|
||||||
export default function WebsiteSettings() {
|
export default function WebsiteSettings() {
|
||||||
const [editWebsite, setEditWebsite] = useState();
|
const [editWebsite, setEditWebsite] = useState();
|
||||||
|
const [resetWebsite, setResetWebsite] = useState();
|
||||||
const [deleteWebsite, setDeleteWebsite] = useState();
|
const [deleteWebsite, setDeleteWebsite] = useState();
|
||||||
const [addWebsite, setAddWebsite] = useState();
|
const [addWebsite, setAddWebsite] = useState();
|
||||||
const [showCode, setShowCode] = useState();
|
const [showCode, setShowCode] = useState();
|
||||||
@ -55,6 +58,9 @@ export default function WebsiteSettings() {
|
|||||||
<Button icon={<Pen />} size="small" onClick={() => setEditWebsite(row)}>
|
<Button icon={<Pen />} size="small" onClick={() => setEditWebsite(row)}>
|
||||||
<FormattedMessage id="label.edit" defaultMessage="Edit" />
|
<FormattedMessage id="label.edit" defaultMessage="Edit" />
|
||||||
</Button>
|
</Button>
|
||||||
|
<Button icon={<Reset />} size="small" onClick={() => setResetWebsite(row)}>
|
||||||
|
<FormattedMessage id="label.reset" defaultMessage="Reset" />
|
||||||
|
</Button>
|
||||||
<Button icon={<Trash />} size="small" onClick={() => setDeleteWebsite(row)}>
|
<Button icon={<Trash />} size="small" onClick={() => setDeleteWebsite(row)}>
|
||||||
<FormattedMessage id="label.delete" defaultMessage="Delete" />
|
<FormattedMessage id="label.delete" defaultMessage="Delete" />
|
||||||
</Button>
|
</Button>
|
||||||
@ -96,6 +102,7 @@ export default function WebsiteSettings() {
|
|||||||
function handleClose() {
|
function handleClose() {
|
||||||
setAddWebsite(null);
|
setAddWebsite(null);
|
||||||
setEditWebsite(null);
|
setEditWebsite(null);
|
||||||
|
setResetWebsite(null);
|
||||||
setDeleteWebsite(null);
|
setDeleteWebsite(null);
|
||||||
setShowCode(null);
|
setShowCode(null);
|
||||||
setShowUrl(null);
|
setShowUrl(null);
|
||||||
@ -141,6 +148,17 @@ export default function WebsiteSettings() {
|
|||||||
<WebsiteEditForm onSave={handleSave} onClose={handleClose} />
|
<WebsiteEditForm onSave={handleSave} onClose={handleClose} />
|
||||||
</Modal>
|
</Modal>
|
||||||
)}
|
)}
|
||||||
|
{resetWebsite && (
|
||||||
|
<Modal
|
||||||
|
title={<FormattedMessage id="label.reset-website" defaultMessage="Reset statistics" />}
|
||||||
|
>
|
||||||
|
<ResetForm
|
||||||
|
values={{ type: 'website', id: resetWebsite.website_id, name: resetWebsite.name }}
|
||||||
|
onSave={handleSave}
|
||||||
|
onClose={handleClose}
|
||||||
|
/>
|
||||||
|
</Modal>
|
||||||
|
)}
|
||||||
{deleteWebsite && (
|
{deleteWebsite && (
|
||||||
<Modal
|
<Modal
|
||||||
title={<FormattedMessage id="label.delete-website" defaultMessage="Delete website" />}
|
title={<FormattedMessage id="label.delete-website" defaultMessage="Delete website" />}
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
"label.delete": "Delete",
|
"label.delete": "Delete",
|
||||||
"label.delete-account": "Delete account",
|
"label.delete-account": "Delete account",
|
||||||
"label.delete-website": "Delete website",
|
"label.delete-website": "Delete website",
|
||||||
|
"label.reset-website": "Reset statistics",
|
||||||
"label.dismiss": "Dismiss",
|
"label.dismiss": "Dismiss",
|
||||||
"label.domain": "Domain",
|
"label.domain": "Domain",
|
||||||
"label.edit": "Edit",
|
"label.edit": "Edit",
|
||||||
@ -58,8 +59,10 @@
|
|||||||
"label.view-details": "View details",
|
"label.view-details": "View details",
|
||||||
"label.websites": "Websites",
|
"label.websites": "Websites",
|
||||||
"message.active-users": "{x} current {x, plural, one {visitor} other {visitors}}",
|
"message.active-users": "{x} current {x, plural, one {visitor} other {visitors}}",
|
||||||
|
"message.confirm-reset": "Are your sure you want to reset {target}'s statistics?",
|
||||||
"message.confirm-delete": "Are your sure you want to delete {target}?",
|
"message.confirm-delete": "Are your sure you want to delete {target}?",
|
||||||
"message.copied": "Copied!",
|
"message.copied": "Copied!",
|
||||||
|
"message.reset-warning": "All statistics for this website will be deleted, but your tracking code will remain intact.",
|
||||||
"message.delete-warning": "All associated data will be deleted as well.",
|
"message.delete-warning": "All associated data will be deleted as well.",
|
||||||
"message.failure": "Something went wrong.",
|
"message.failure": "Something went wrong.",
|
||||||
"message.get-share-url": "Get share URL",
|
"message.get-share-url": "Get share URL",
|
||||||
|
@ -141,6 +141,10 @@ export async function updateWebsite(website_id, data) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function resetWebsite(website_id) {
|
||||||
|
return runQuery(prisma.$queryRaw`delete from session where website_id=${website_id}`);
|
||||||
|
}
|
||||||
|
|
||||||
export async function deleteWebsite(website_id) {
|
export async function deleteWebsite(website_id) {
|
||||||
return runQuery(
|
return runQuery(
|
||||||
/* Prisma bug, does not cascade on non-nullable foreign keys
|
/* Prisma bug, does not cascade on non-nullable foreign keys
|
||||||
|
20
pages/api/website/[id]/reset.js
Normal file
20
pages/api/website/[id]/reset.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import { resetWebsite } from 'lib/queries';
|
||||||
|
import { methodNotAllowed, ok, unauthorized } from 'lib/response';
|
||||||
|
import { allowQuery } from 'lib/auth';
|
||||||
|
|
||||||
|
export default async (req, res) => {
|
||||||
|
const { id } = req.query;
|
||||||
|
const websiteId = +id;
|
||||||
|
|
||||||
|
if (req.method === 'POST') {
|
||||||
|
if (!(await allowQuery(req))) {
|
||||||
|
return unauthorized(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
await resetWebsite(websiteId);
|
||||||
|
|
||||||
|
return ok(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
return methodNotAllowed(res);
|
||||||
|
};
|
@ -5193,12 +5193,7 @@ lodash.truncate@^4.4.2:
|
|||||||
resolved "https://registry.yarnpkg.com/lodash.truncate/-/lodash.truncate-4.4.2.tgz#5a350da0b1113b837ecfffd5812cbe58d6eae193"
|
resolved "https://registry.yarnpkg.com/lodash.truncate/-/lodash.truncate-4.4.2.tgz#5a350da0b1113b837ecfffd5812cbe58d6eae193"
|
||||||
integrity sha1-WjUNoLERO4N+z//VgSy+WNbq4ZM=
|
integrity sha1-WjUNoLERO4N+z//VgSy+WNbq4ZM=
|
||||||
|
|
||||||
lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.19:
|
lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.21:
|
||||||
version "4.17.20"
|
|
||||||
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.20.tgz#b44a9b6297bcb698f1c51a3545a2b3b368d59c52"
|
|
||||||
integrity sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==
|
|
||||||
|
|
||||||
lodash@^4.17.21:
|
|
||||||
version "4.17.21"
|
version "4.17.21"
|
||||||
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
|
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
|
||||||
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
|
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
|
||||||
|
Loading…
Reference in New Issue
Block a user