Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/common/interfaces/company.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export interface Settings {
custom_value3: string;
custom_value4: string;
default_task_rate: number;
default_task_description: string;
payment_terms: string;
send_reminders: boolean;
custom_message_dashboard: string;
Expand Down
26 changes: 25 additions & 1 deletion src/pages/settings/task-settings/TaskSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { NumberInputField } from '$app/components/forms/NumberInputField';
import { PropertyCheckbox } from '$app/components/PropertyCheckbox';
import { SettingsLabel } from '$app/components/SettingsLabel';
import { Card, Element } from '../../../components/cards';
import { SelectField } from '../../../components/forms';
import { InputField, SelectField } from '../../../components/forms';
import Toggle from '../../../components/forms/Toggle';
import { Settings } from '../../../components/layouts/Settings';
import { TaskStatuses } from '..';
Expand Down Expand Up @@ -125,6 +125,30 @@ export function TaskSettings() {
/>
</Element>

<Element
leftSide={
<PropertyCheckbox
propertyKey="default_task_description"
labelElement={
<SettingsLabel
label={t('default_task_description')}
helpLabel={t('default_task_description_help')}
/>
}
/>
}
>
<InputField
element="textarea"
value={companyChanges?.settings?.default_task_description || ''}
onValueChange={(value) =>
handleSettingsChange('settings.default_task_description', value)
}
disabled={disableSettingsField('default_task_description')}
errorMessage={errors?.errors['settings.default_task_description']}
/>
</Element>

{isCompanySettingsActive && (
<Element
leftSide={t('auto_start_tasks')}
Expand Down
1 change: 1 addition & 0 deletions src/pages/tasks/common/components/CreateTaskModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export function CreateTaskModal(props: Props) {
handleChange={handleChange}
errors={errors}
taskModal={true}
withDefaultDescription
/>
)}
{task && <TaskTable task={task} handleChange={handleChange} />}
Expand Down
30 changes: 20 additions & 10 deletions src/pages/tasks/common/components/TaskDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
import classNames from 'classnames';
import dayjs from 'dayjs';
import duration from 'dayjs/plugin/duration';
import { useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { MdLaunch } from 'react-icons/md';
import { useMediaQuery } from 'react-responsive';
import { useColorScheme } from '$app/common/colors';
import { sanitizeHTML } from '$app/common/helpers/html-string';
import { route } from '$app/common/helpers/route';
import { useHasPermission } from '$app/common/hooks/permissions/useHasPermission';
import { useCurrentCompany } from '$app/common/hooks/useCurrentCompany';
import { useDisableNavigation } from '$app/common/hooks/useDisableNavigation';
import { useEntityAssigned } from '$app/common/hooks/useEntityAssigned';
import { Client } from '$app/common/interfaces/client';
import { TAG_ENTITY_TYPES } from '$app/common/interfaces/tag';
import { Task } from '$app/common/interfaces/task';
import { TaskStatus } from '$app/common/interfaces/task-status';
Expand All @@ -41,7 +42,9 @@ import { ClientActionButtons } from '$app/pages/invoices/common/components/Clien
import { formatTime, TaskClock } from '../../kanban/components/TaskClock';
import { isTaskRunning } from '../helpers/calculate-entity-state';
import { calculateTime } from '../helpers/calculate-time';
import { sanitizeTaskDescription } from '../helpers/sanitize-description';
import { shouldShowStartTaskButton } from '../helpers/task';
import { useDefaultTaskDescription } from '../hooks/useDefaultTaskDescription';
import { useResolveTaskRate } from '../hooks/useResolveTaskRate';
import { useStart } from '../hooks/useStart';
import { useStop } from '../hooks/useStop';
Expand All @@ -56,6 +59,7 @@ interface Props {
errors: ValidationBag | undefined;
taskModal?: boolean;
page?: 'create' | 'edit';
withDefaultDescription?: boolean;
}

export function TaskDetails(props: Props) {
Expand All @@ -75,6 +79,18 @@ export function TaskDetails(props: Props) {
const start = useStart();
const resolveTaskRate = useResolveTaskRate();

const descriptionRef = useRef<HTMLTextAreaElement>(null);

const [selectedClient, setSelectedClient] = useState<Client>();

useDefaultTaskDescription({
task,
selectedClient,
enabled: Boolean(props.withDefaultDescription),
inputRef: descriptionRef,
onChange: (description) => handleChange('description', description),
});

const calculation = calculateTime(task.time_log, {
inSeconds: true,
calculateLastTimeLog: false,
Expand Down Expand Up @@ -221,6 +237,7 @@ export function TaskDetails(props: Props) {
inputLabel={t('client')}
value={task.client_id}
onChange={(client) => {
setSelectedClient(client);
handleChange('client_id', client.id);

if (!task.id) {
Expand Down Expand Up @@ -389,19 +406,12 @@ export function TaskDetails(props: Props) {

<div className="w-full lg:w-3/5">
<InputField
innerRef={descriptionRef}
label={t('description')}
element="textarea"
value={task.description}
onValueChange={(value) =>
handleChange(
'description',
sanitizeHTML(value, {
ALLOWED_TAGS: [],
ALLOWED_ATTR: [],
KEEP_CONTENT: true,
USE_PROFILES: { html: false },
})
)
handleChange('description', sanitizeTaskDescription(value))
}
errorMessage={errors?.errors.description}
width="100%"
Expand Down
20 changes: 20 additions & 0 deletions src/pages/tasks/common/helpers/sanitize-description.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/

import { sanitizeHTML } from '$app/common/helpers/html-string';

export const sanitizeTaskDescription = (value: string) => {
return sanitizeHTML(value.replace(/\r\n?/g, '\n'), {
ALLOWED_TAGS: [],
ALLOWED_ATTR: [],
KEEP_CONTENT: true,
USE_PROFILES: { html: false },
});
};
82 changes: 82 additions & 0 deletions src/pages/tasks/common/hooks/useDefaultTaskDescription.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/

import { RefObject, useEffect, useRef } from 'react';
import { useHasPermission } from '$app/common/hooks/permissions/useHasPermission';
import { useCurrentCompany } from '$app/common/hooks/useCurrentCompany';
import { Client } from '$app/common/interfaces/client';
import { Task } from '$app/common/interfaces/task';
import { useClientQuery } from '$app/common/queries/clients';
import { useGroupSettingsQuery } from '$app/common/queries/group-settings';
import { sanitizeTaskDescription } from '../helpers/sanitize-description';

interface Params {
task: Task;
selectedClient: Client | undefined;
enabled: boolean;
inputRef: RefObject<HTMLTextAreaElement>;
onChange: (description: string) => void;
}

export function useDefaultTaskDescription(params: Params) {
const { task, selectedClient, enabled, inputRef, onChange } = params;

const company = useCurrentCompany();
const hasPermission = useHasPermission();

const previousDefault = useRef<string | null>(task.description);

const { data: groupSettings } = useGroupSettingsQuery({ perPage: 1000 });

const isClientSelected =
Boolean(task.client_id) && selectedClient?.id === task.client_id;

const { data: fetchedClient } = useClientQuery({
id: task.client_id,
enabled:
enabled &&
Boolean(task.client_id) &&
!isClientSelected &&
(hasPermission('view_client') || hasPermission('edit_client')),
});

const client = isClientSelected ? selectedClient : fetchedClient;

useEffect(() => {
if (!enabled) {
previousDefault.current = null;
return;
}

const currentClient = client?.id === task.client_id ? client : undefined;

const group =
currentClient?.group_settings ??
groupSettings?.find(({ id }) => id === currentClient?.group_settings_id);

const description = sanitizeTaskDescription(
[
currentClient?.settings?.default_task_description,
group?.settings?.default_task_description,
company?.settings?.default_task_description,
].find((value) => Boolean(value)) ?? ''
);

if (
task.description === previousDefault.current &&
task.description === inputRef.current?.value &&
task.description !== description
) {
onChange(description);
}

previousDefault.current = description;
}, [enabled, task.client_id, client, groupSettings]);
}
16 changes: 13 additions & 3 deletions src/pages/tasks/create/Create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default function Create() {
const [isFormBusy, setIsFormBusy] = useState<boolean>(false);
const [isInitialConfiguration, setIsInitialConfiguration] =
useState<boolean>(true);
const [isTaskReady, setIsTaskReady] = useState<boolean>(false);

const { data: taskStatuses } = useTaskStatusesQuery({ status: 'active' });
const { data } = useBlankTaskQuery({ enabled: typeof task === 'undefined' });
Expand Down Expand Up @@ -89,6 +90,8 @@ export default function Create() {

return value;
});

setIsTaskReady(true);
}, [data]);

useEffect(() => {
Expand Down Expand Up @@ -150,10 +153,17 @@ export default function Create() {
onSaveClick={() => task && handleSave(task)}
breadcrumbs={pages}
>
{task && (
<TaskDetails task={task} handleChange={handleChange} errors={errors} />
{task && isTaskReady && (
<TaskDetails
task={task}
handleChange={handleChange}
errors={errors}
withDefaultDescription={searchParams.get('action') !== 'clone'}
/>
)}
{task && isTaskReady && (
<TaskTable task={task} handleChange={handleChange} />
)}
{task && <TaskTable task={task} handleChange={handleChange} />}
</Default>
);
}
Loading