mirror of
https://github.com/umami-software/umami.git
synced 2026-02-17 11:05:36 +01:00
Cohorts editing.
This commit is contained in:
parent
07665f4824
commit
8c8e36c63b
26 changed files with 1066 additions and 985 deletions
|
|
@ -5,14 +5,11 @@ import {
|
|||
FormField,
|
||||
FormSubmitButton,
|
||||
TextField,
|
||||
Label,
|
||||
Loading,
|
||||
Label,
|
||||
} from '@umami/react-zen';
|
||||
import { subMonths, endOfDay } from 'date-fns';
|
||||
import { FieldFilters } from '@/components/input/FieldFilters';
|
||||
import { useState } from 'react';
|
||||
import { useMessages, useUpdateQuery, useWebsiteSegmentQuery } from '@/components/hooks';
|
||||
import { filtersArrayToObject } from '@/lib/params';
|
||||
import { messages } from '@/components/messages';
|
||||
|
||||
export function SegmentEditForm({
|
||||
|
|
@ -32,30 +29,23 @@ export function SegmentEditForm({
|
|||
}) {
|
||||
const { data } = useWebsiteSegmentQuery(websiteId, segmentId);
|
||||
const { formatMessage, labels } = useMessages();
|
||||
const [currentFilters, setCurrentFilters] = useState(filters);
|
||||
const startDate = subMonths(endOfDay(new Date()), 6);
|
||||
const endDate = endOfDay(new Date());
|
||||
|
||||
const { mutate, error, isPending, touch, toast } = useUpdateQuery(
|
||||
`/websites/${websiteId}/segments${segmentId ? `/${segmentId}` : ''}`,
|
||||
{
|
||||
...data,
|
||||
type: 'segment',
|
||||
},
|
||||
);
|
||||
|
||||
const handleSubmit = async (data: any) => {
|
||||
mutate(
|
||||
{ ...data, parameters: filtersArrayToObject(currentFilters) },
|
||||
{
|
||||
onSuccess: async () => {
|
||||
toast(formatMessage(messages.saved));
|
||||
touch('segments');
|
||||
onSave?.();
|
||||
onClose?.();
|
||||
},
|
||||
const handleSubmit = async (formData: any) => {
|
||||
mutate(formData, {
|
||||
onSuccess: async () => {
|
||||
toast(formatMessage(messages.saved));
|
||||
touch('segments');
|
||||
onSave?.();
|
||||
onClose?.();
|
||||
},
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
if (segmentId && !data) {
|
||||
|
|
@ -63,35 +53,27 @@ export function SegmentEditForm({
|
|||
}
|
||||
|
||||
return (
|
||||
<Form error={error} onSubmit={handleSubmit} defaultValues={data}>
|
||||
<Form error={error} onSubmit={handleSubmit} defaultValues={data || { parameters: { filters } }}>
|
||||
<FormField
|
||||
name="name"
|
||||
label={formatMessage(labels.name)}
|
||||
rules={{ required: formatMessage(labels.required) }}
|
||||
>
|
||||
<TextField autoFocus />
|
||||
<TextField autoFocus={!segmentId} />
|
||||
</FormField>
|
||||
{showFilters && (
|
||||
<>
|
||||
<Label>{formatMessage(labels.filters)}</Label>
|
||||
<FieldFilters
|
||||
websiteId={websiteId}
|
||||
filters={currentFilters}
|
||||
startDate={startDate}
|
||||
endDate={endDate}
|
||||
onSave={setCurrentFilters}
|
||||
/>
|
||||
<FormField name="parameters.filters" rules={{ required: formatMessage(labels.required) }}>
|
||||
<FieldFilters websiteId={websiteId} />
|
||||
</FormField>
|
||||
</>
|
||||
)}
|
||||
<FormButtons>
|
||||
<Button isDisabled={isPending} onPress={onClose}>
|
||||
{formatMessage(labels.cancel)}
|
||||
</Button>
|
||||
<FormSubmitButton
|
||||
variant="primary"
|
||||
data-test="button-submit"
|
||||
isDisabled={isPending || currentFilters.length === 0}
|
||||
>
|
||||
<FormSubmitButton variant="primary" data-test="button-submit" isDisabled={isPending}>
|
||||
{formatMessage(labels.save)}
|
||||
</FormSubmitButton>
|
||||
</FormButtons>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue