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
77 changes: 77 additions & 0 deletions src/common/hooks/useOpenPortal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* 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 { AxiosError, AxiosResponse } from 'axios';
import { endpoint } from '$app/common/helpers';
import { request } from '$app/common/helpers/request';
import { toast } from '$app/common/helpers/toast/toast';
import { ValidationBag } from '$app/common/interfaces/validation-bag';

export type PortalEntity =
| 'invoice'
| 'quote'
| 'credit'
| 'recurring_invoice'
| 'purchase_order';

export type PortalTarget =
| { entity: PortalEntity; key: string }
| { contact_key: string };

export function useOpenPortal() {
return (target: PortalTarget) => {
const key = 'contact_key' in target ? target.contact_key : target.key;

if (!key) {
return;
}

const tab = window.open('', '_blank');

if (tab) {
tab.opener = null;
}

request('POST', endpoint('/api/v1/one_time_token/temporary_url'), {
context: 'portal',
...target,
})
.then((response: AxiosResponse<string>) => {
const url =
typeof response?.data === 'string' ? response.data.trim() : '';

if (!url.startsWith('http')) {
tab?.close();

if (response) {
toast.error();
}

return;
}

if (tab) {
tab.location.replace(url);
} else {
window.open(url, '_blank', 'noopener');
}
})
.catch((error: AxiosError<ValidationBag>) => {
tab?.close();

if (error.response?.status === 422) {
const [message] =
Object.values(error.response.data.errors ?? {})[0] ?? [];

toast.error(message ?? error.response.data.message);
}
});
};
}
2 changes: 2 additions & 0 deletions src/common/interfaces/company.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ export interface Settings {
show_all_tasks_client_portal: string;
enable_client_portal_password: boolean;
enable_client_portal: boolean;
enable_portal_key_login: boolean;
portal_link_otp_after_days: number;
enable_client_portal_dashboard: boolean;
signature_on_pdf: boolean;
document_email_attachment: boolean;
Expand Down
48 changes: 48 additions & 0 deletions src/components/OpenPortalLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* 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 classNames from 'classnames';
import { ReactNode } from 'react';
import { useAccentColor } from '$app/common/hooks/useAccentColor';
import { PortalTarget, useOpenPortal } from '$app/common/hooks/useOpenPortal';
import { ExternalLink } from './icons/ExternalLink';

interface Props {
target: PortalTarget;
children: ReactNode;
className?: string;
withExternalIcon?: boolean;
}

export function OpenPortalLink(props: Props) {
const { target, children, className, withExternalIcon } = props;

const accentColor = useAccentColor();
const openPortal = useOpenPortal();

return (
<div className={classNames('flex space-x-2 items-center', className)}>
{withExternalIcon && (
<div>
<ExternalLink size="1rem" color={accentColor} />
</div>
)}

<button
type="button"
className="text-center text-sm hover:underline"
style={{ color: accentColor }}
onClick={() => openPortal(target)}
>
{children}
</button>
</div>
);
}
16 changes: 5 additions & 11 deletions src/pages/clients/common/components/ClientSlider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { Slider } from '$app/components/cards/Slider';
import { CopyToClipboardIconOnly } from '$app/components/CopyToClipBoardIconOnly';
import { EntityStatus } from '$app/components/EntityStatus';
import { Button, Link } from '$app/components/forms';
import { OpenPortalLink } from '$app/components/OpenPortalLink';
import { ResourceActions } from '$app/components/ResourceActions';
import { Spinner } from '$app/components/Spinner';
import { Tooltip } from '$app/components/Tooltip';
Expand Down Expand Up @@ -383,26 +384,19 @@ export function ClientSlider() {
)}

<div className="flex items-center space-x-3 pt-1">
<Link
to={route(
`${contact.link}?silent=true&client_hash=:clientHash`,
{ clientHash: client.client_hash }
)}
external
withoutExternalIcon
<OpenPortalLink
target={{ contact_key: contact.contact_key }}
>
{t('client_portal')}
</Link>
</OpenPortalLink>

<Tooltip
message={t('copy_link') as string}
placement="top"
width="auto"
centerVertically
>
<CopyToClipboardIconOnly
text={route(`${contact.link}?silent=true`)}
/>
<CopyToClipboardIconOnly text={contact.link} />
</Tooltip>
</div>
</div>
Expand Down
12 changes: 3 additions & 9 deletions src/pages/clients/common/hooks/useActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { route } from '$app/common/helpers/route';
import { useAdmin } from '$app/common/hooks/permissions/useHasPermission';
import { useDisplayRunTemplateActions } from '$app/common/hooks/useDisplayRunTemplateActions';
import { useEntityPageIdentifier } from '$app/common/hooks/useEntityPageIdentifier';
import { useOpenPortal } from '$app/common/hooks/useOpenPortal';
import { Client } from '$app/common/interfaces/client';
import { useBulk } from '$app/common/queries/clients';
import { Divider } from '$app/components/cards/Divider';
Expand Down Expand Up @@ -60,6 +61,7 @@ export function useActions(params?: Params) {
});

const configureClientSettings = useConfigureClientSettings();
const openPortal = useOpenPortal();

const {
setChangeTemplateVisible,
Expand Down Expand Up @@ -91,15 +93,7 @@ export function useActions(params?: Params) {
Boolean(!client.is_deleted && !isShowPage) && (
<DropdownElement
onClick={() =>
window.open(
route(
`${client.contacts[0].link}?silent=true&client_hash=:clientHash`,
{
clientHash: client.client_hash,
}
),
'__blank'
)
openPortal({ contact_key: client.contacts[0]?.contact_key ?? '' })
}
icon={<Icon element={MdCloudCircle} />}
>
Expand Down
23 changes: 5 additions & 18 deletions src/pages/clients/show/components/Contacts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@

import { useTranslation } from 'react-i18next';
import { useColorScheme } from '$app/common/colors';
import { route } from '$app/common/helpers/route';
import { useFormatCustomFieldValue } from '$app/common/hooks/useFormatCustomFieldValue';
import { Client } from '$app/common/interfaces/client';
import { ClientContact } from '$app/common/interfaces/client-contact';
import { CopyToClipboardIconOnly } from '$app/components/CopyToClipBoardIconOnly';
import { CustomFields, useCustomField } from '$app/components/CustomField';
import { Link } from '$app/components/forms';
import { InfoCard } from '$app/components/InfoCard';
import { OpenPortalLink } from '$app/components/OpenPortalLink';
import { Tooltip } from '$app/components/Tooltip';
import { UserUnsubscribedTooltip } from '../../common/components/UserUnsubscribedTooltip';

Expand Down Expand Up @@ -148,31 +147,19 @@ export function Contacts(props: Props) {
})}

<div className="flex items-center space-x-2">
<Link
className="cursor-pointer"
to={route(
`${client.contacts[index]?.link}?silent=true&client_hash=:clientHash`,
{
clientHash: client.client_hash,
}
)}
external
withoutExternalIcon
<OpenPortalLink
target={{ contact_key: contact.contact_key }}
>
{t('client_portal')}
</Link>
</OpenPortalLink>

<Tooltip
message={t('copy_link') as string}
placement="top"
width="auto"
centerVertically
>
<CopyToClipboardIconOnly
text={route(
`${client.contacts[index]?.link}?silent=true`
)}
/>
<CopyToClipboardIconOnly text={contact.link} />
</Tooltip>
</div>
</div>
Expand Down
17 changes: 11 additions & 6 deletions src/pages/credits/common/components/CreditSlider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ import { DynamicLink } from '$app/components/DynamicLink';
import { useActions } from '../hooks';
import { CreditStatus } from './CreditStatus';
import { Credit } from '$app/common/interfaces/credit';
import {
generateClientPortalUrl,
openClientPortal,
} from '$app/pages/invoices/common/helpers/open-client-portal';
import { EmailRecord } from '$app/components/EmailRecord';
import { useEffect, useState } from 'react';
import { EmailRecord as EmailRecordType } from '$app/common/interfaces/email-history';
Expand All @@ -64,6 +60,7 @@ import { useGetSetting } from '$app/common/hooks/useGetSetting';
import { useGetTimezone } from '$app/common/hooks/useGetTimezone';
import classNames from 'classnames';
import { useReactSettings } from '$app/common/hooks/useReactSettings';
import { useOpenPortal } from '$app/common/hooks/useOpenPortal';
import styled from 'styled-components';
import { useNavigate } from 'react-router-dom';
import { CloudPlay } from '$app/components/icons/CloudPlay';
Expand Down Expand Up @@ -91,6 +88,7 @@ const Box = styled.div`
export function CreditSlider() {
const invoicePaths = useInvoiceEditorPaths();
const [t] = useTranslation();
const openPortal = useOpenPortal();
const queryClient = useQueryClient();
const navigate = useNavigate();

Expand Down Expand Up @@ -287,7 +285,14 @@ export function CreditSlider() {
<div className="flex space-x-4 items-center justify-center px-6 py-5">
<Box
className="flex flex-col items-center justify-center space-y-2 shadow-sm border px-14 py-5 cursor-pointer rounded-md"
onClick={() => (credit ? openClientPortal(credit) : null)}
onClick={() =>
credit
? openPortal({
entity: 'credit',
key: credit.invitations[0]?.key ?? '',
})
: null
}
style={{
borderColor: colors.$20,
}}
Expand Down Expand Up @@ -315,7 +320,7 @@ export function CreditSlider() {
className="flex flex-col items-center justify-center space-y-2 shadow-sm border px-14 py-5 cursor-pointer rounded-md"
onClick={() => {
navigator.clipboard.writeText(
generateClientPortalUrl(credit) ?? ''
credit.invitations[0]?.link ?? ''
);

toast.success('copied_to_clipboard', { value: '' });
Expand Down
11 changes: 9 additions & 2 deletions src/pages/credits/common/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { ValidationBag } from '$app/common/interfaces/validation-bag';
import { Divider } from '$app/components/cards/Divider';
import { Action } from '$app/components/ResourceActions';
import { useAtom, useSetAtom } from 'jotai';
import { openClientPortal } from '$app/pages/invoices/common/helpers/open-client-portal';
import { useDownloadPdf } from '$app/pages/invoices/common/hooks/useDownloadPdf';
import {
DataTableColumnsExtended,
Expand Down Expand Up @@ -102,6 +101,7 @@ import classNames from 'classnames';
import { Dispatch, SetStateAction } from 'react';
import { normalizeColumnName } from '$app/common/helpers/data-table';
import { useDisplayRunTemplateActions } from '$app/common/hooks/useDisplayRunTemplateActions';
import { useOpenPortal } from '$app/common/hooks/useOpenPortal';
import { TagPills } from '$app/components/tags/TagPills';
import { PeppolSendingStatus } from '$app/components/e-invoice/PeppolSendingStatus';
import { useColorScheme } from '$app/common/colors';
Expand Down Expand Up @@ -413,6 +413,7 @@ interface Params {

export function useActions(params?: Params) {
const [t] = useTranslation();
const openPortal = useOpenPortal();

const hasPermission = useHasPermission();

Expand Down Expand Up @@ -580,7 +581,13 @@ export function useActions(params?: Params) {
actionKey="client_portal"
isCommonActionSection={!dropdown}
tooltipText={t('client_portal')}
onClick={() => credit && openClientPortal(credit)}
onClick={() =>
credit &&
openPortal({
entity: 'credit',
key: credit.invitations[0]?.key ?? '',
})
}
icon={MdCloudCircle}
disablePreventNavigation
>
Expand Down
1 change: 1 addition & 0 deletions src/pages/credits/create/components/CreatePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export default function CreatePage() {
style={{ borderColor: colors.$24 }}
>
<ClientSelector
portalEntity="credit"
resource={credit}
onChange={(id) => handleClientChange(id)}
onLocationChange={(id) => handleChange('location_id', id)}
Expand Down
1 change: 1 addition & 0 deletions src/pages/credits/edit/Edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export default function Edit() {
)}

<ClientSelector
portalEntity="credit"
resource={credit}
onChange={(id) => handleChange('client_id', id)}
onClearButtonClick={() => handleChange('client_id', '')}
Expand Down
Loading
Loading