2020-08-07 09:28:29 +02:00
|
|
|
import React, { useCallback, useMemo, useRef, useState } from 'react';
|
2020-08-04 14:33:43 +02:00
|
|
|
import { useIntl } from 'react-intl';
|
2021-08-17 18:51:41 +02:00
|
|
|
// import { Header, List } from '@buffetjs/custom';
|
|
|
|
// import { Text } from '@buffetjs/core';
|
|
|
|
// import { Pencil } from '@buffetjs/icons';
|
2020-08-07 09:48:28 +02:00
|
|
|
import {
|
|
|
|
SettingsPageTitle,
|
|
|
|
SizedInput,
|
2021-05-28 09:28:16 +02:00
|
|
|
useTracking,
|
2020-08-07 09:48:28 +02:00
|
|
|
getYupInnerErrors,
|
|
|
|
request,
|
2021-05-17 15:55:46 +02:00
|
|
|
useNotification,
|
2021-05-17 18:38:00 +02:00
|
|
|
useOverlayBlocker,
|
2021-04-29 13:51:12 +02:00
|
|
|
} from '@strapi/helper-plugin';
|
2020-10-06 11:45:22 +02:00
|
|
|
import { get, upperFirst, has } from 'lodash';
|
2020-08-06 18:40:22 +02:00
|
|
|
import { Row } from 'reactstrap';
|
2021-08-17 18:51:41 +02:00
|
|
|
// import ListBaselineAlignment from '../../components/ListBaselineAlignment';
|
|
|
|
// import ListRow from '../../components/ListRow';
|
|
|
|
|
|
|
|
// DS INTEGRATION
|
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
|
|
|
|
import { HeaderLayout, Layout, ContentLayout } from '@strapi/parts/Layout';
|
|
|
|
import { Main } from '@strapi/parts/Main';
|
|
|
|
import { Stack } from '@strapi/parts/Stack';
|
|
|
|
import { Table, Thead, Tr, Th, Tbody, Td } from '@strapi/parts/Table';
|
|
|
|
import { Text, TableLabel } from '@strapi/parts/Text';
|
2021-08-18 11:45:34 +02:00
|
|
|
import { Row as FlexRow } from '@strapi/parts/Row';
|
2021-08-17 18:51:41 +02:00
|
|
|
import { VisuallyHidden } from '@strapi/parts/VisuallyHidden';
|
2021-08-18 11:45:34 +02:00
|
|
|
import { Box } from '@strapi/parts/Box';
|
2021-08-17 18:51:41 +02:00
|
|
|
import { IconButton } from '@strapi/parts/IconButton';
|
|
|
|
import EditIcon from '@strapi/icons/EditIcon';
|
2020-08-06 18:40:22 +02:00
|
|
|
import forms from './utils/forms';
|
2021-08-17 18:51:41 +02:00
|
|
|
import createProvidersArray from './utils/createProvidersArray';
|
|
|
|
import ModalForm from '../../components/ModalForm';
|
|
|
|
import { getRequestURL, getTrad } from '../../utils';
|
|
|
|
import { useForm } from '../../hooks';
|
|
|
|
import pluginPermissions from '../../permissions';
|
2021-08-18 11:45:34 +02:00
|
|
|
import Settings from '@strapi/icons/Settings';
|
2020-08-03 16:49:40 +02:00
|
|
|
|
|
|
|
const ProvidersPage = () => {
|
2020-08-04 14:33:43 +02:00
|
|
|
const { formatMessage } = useIntl();
|
2021-05-28 09:28:16 +02:00
|
|
|
const { trackUsage } = useTracking();
|
|
|
|
const trackUsageRef = useRef(trackUsage);
|
2020-08-06 18:40:22 +02:00
|
|
|
const [isOpen, setIsOpen] = useState(false);
|
|
|
|
const [isSubmiting, setIsSubmiting] = useState(false);
|
|
|
|
const buttonSubmitRef = useRef(null);
|
|
|
|
const [showForm, setShowForm] = useState(false);
|
|
|
|
const [providerToEditName, setProviderToEditName] = useState(null);
|
2021-05-17 15:55:46 +02:00
|
|
|
const toggleNotification = useNotification();
|
2021-05-17 18:38:00 +02:00
|
|
|
const { lockApp, unlockApp } = useOverlayBlocker();
|
2020-08-06 18:40:22 +02:00
|
|
|
|
2020-08-04 15:59:19 +02:00
|
|
|
const updatePermissions = useMemo(() => {
|
|
|
|
return { update: pluginPermissions.updateProviders };
|
2020-08-04 16:35:16 +02:00
|
|
|
}, []);
|
2021-08-17 18:51:41 +02:00
|
|
|
console.log(updatePermissions);
|
2020-08-07 09:28:29 +02:00
|
|
|
|
2020-08-04 15:59:19 +02:00
|
|
|
const {
|
|
|
|
allowedActions: { canUpdate },
|
2020-08-07 09:28:29 +02:00
|
|
|
dispatchResetForm,
|
|
|
|
dispatchSetFormErrors,
|
|
|
|
dispatchSubmitSucceeded,
|
|
|
|
formErrors,
|
|
|
|
handleChange,
|
2021-08-17 18:51:41 +02:00
|
|
|
// isLoading,
|
|
|
|
// isLoadingForPermissions,
|
2020-08-07 09:28:29 +02:00
|
|
|
modifiedData,
|
|
|
|
} = useForm('providers', updatePermissions);
|
|
|
|
|
2020-08-04 16:27:07 +02:00
|
|
|
const providers = useMemo(() => createProvidersArray(modifiedData), [modifiedData]);
|
2021-08-18 11:45:34 +02:00
|
|
|
const rowCount = providers.length;
|
2021-08-17 18:51:41 +02:00
|
|
|
// const enabledProvidersCount = useMemo(
|
|
|
|
// () => providers.filter(provider => provider.enabled).length,
|
|
|
|
// [providers]
|
|
|
|
// );
|
|
|
|
|
2020-10-06 11:45:22 +02:00
|
|
|
const isProviderWithSubdomain = useMemo(() => {
|
|
|
|
if (!providerToEditName) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const providerToEdit = providers.find(obj => obj.name === providerToEditName);
|
|
|
|
|
|
|
|
return has(providerToEdit, 'subdomain');
|
|
|
|
}, [providers, providerToEditName]);
|
2021-08-17 18:51:41 +02:00
|
|
|
|
|
|
|
// const disabledProvidersCount = useMemo(() => {
|
|
|
|
// return providers.length - enabledProvidersCount;
|
|
|
|
// }, [providers, enabledProvidersCount]);
|
|
|
|
|
|
|
|
// const listTitle = useMemo(() => {
|
|
|
|
// const enabledMessage = formatMessage(
|
|
|
|
// {
|
|
|
|
// id: getTrad(
|
|
|
|
// `List.title.providers.enabled.${enabledProvidersCount > 1 ? 'plural' : 'singular'}`
|
|
|
|
// ),
|
|
|
|
// },
|
|
|
|
// { number: enabledProvidersCount }
|
|
|
|
// );
|
|
|
|
// const disabledMessage = formatMessage(
|
|
|
|
// {
|
|
|
|
// id: getTrad(
|
|
|
|
// `List.title.providers.disabled.${disabledProvidersCount > 1 ? 'plural' : 'singular'}`
|
|
|
|
// ),
|
|
|
|
// },
|
|
|
|
// { number: disabledProvidersCount }
|
|
|
|
// );
|
|
|
|
|
|
|
|
// return `${enabledMessage} ${disabledMessage}`;
|
|
|
|
// }, [formatMessage, enabledProvidersCount, disabledProvidersCount]);
|
2020-08-04 15:59:19 +02:00
|
|
|
|
2020-08-06 18:40:22 +02:00
|
|
|
const pageTitle = formatMessage({ id: getTrad('HeaderNav.link.providers') });
|
|
|
|
|
2020-08-07 08:32:40 +02:00
|
|
|
const formToRender = useMemo(() => {
|
2020-10-06 11:45:22 +02:00
|
|
|
if (providerToEditName === 'email') {
|
|
|
|
return forms.email;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isProviderWithSubdomain) {
|
|
|
|
return forms.providersWithSubdomain;
|
|
|
|
}
|
|
|
|
|
|
|
|
return forms.providers;
|
|
|
|
}, [providerToEditName, isProviderWithSubdomain]);
|
2020-08-07 08:32:40 +02:00
|
|
|
|
2020-08-06 18:40:22 +02:00
|
|
|
const handleClick = useCallback(() => {
|
|
|
|
buttonSubmitRef.current.click();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const handleToggle = useCallback(() => {
|
|
|
|
setIsOpen(prev => !prev);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const handleClickEdit = useCallback(
|
|
|
|
provider => {
|
2020-08-28 11:02:10 +02:00
|
|
|
if (canUpdate) {
|
|
|
|
setProviderToEditName(provider.name);
|
|
|
|
handleToggle();
|
|
|
|
}
|
2020-08-06 18:40:22 +02:00
|
|
|
},
|
2020-08-28 11:02:10 +02:00
|
|
|
[canUpdate, handleToggle]
|
2020-08-06 18:40:22 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
const handleClosed = useCallback(() => {
|
|
|
|
setProviderToEditName(null);
|
|
|
|
setShowForm(false);
|
2020-08-07 09:28:29 +02:00
|
|
|
dispatchResetForm();
|
|
|
|
}, [dispatchResetForm]);
|
2020-08-06 18:40:22 +02:00
|
|
|
|
|
|
|
const handleOpened = useCallback(() => {
|
|
|
|
setShowForm(true);
|
|
|
|
}, []);
|
|
|
|
|
2020-08-07 08:32:40 +02:00
|
|
|
const handleSubmit = useCallback(
|
|
|
|
async e => {
|
|
|
|
e.preventDefault();
|
|
|
|
const { schema } = formToRender;
|
|
|
|
let errors = {};
|
|
|
|
|
|
|
|
setIsSubmiting(true);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await schema.validate(modifiedData[providerToEditName], { abortEarly: false });
|
2021-05-17 18:38:00 +02:00
|
|
|
lockApp();
|
2020-08-07 08:32:40 +02:00
|
|
|
|
|
|
|
try {
|
2021-05-28 09:28:16 +02:00
|
|
|
trackUsageRef.current('willEditAuthenticationProvider');
|
2020-08-07 09:48:28 +02:00
|
|
|
|
2020-08-07 08:32:40 +02:00
|
|
|
await request(getRequestURL('providers'), {
|
|
|
|
method: 'PUT',
|
|
|
|
body: { providers: modifiedData },
|
|
|
|
});
|
|
|
|
|
2021-05-28 09:28:16 +02:00
|
|
|
trackUsageRef.current('didEditAuthenticationProvider');
|
2020-08-07 09:48:28 +02:00
|
|
|
|
2021-05-17 15:55:46 +02:00
|
|
|
toggleNotification({
|
2020-10-02 18:58:15 +02:00
|
|
|
type: 'success',
|
|
|
|
message: { id: getTrad('notification.success.submit') },
|
|
|
|
});
|
2020-08-07 08:32:40 +02:00
|
|
|
|
2020-08-07 09:28:29 +02:00
|
|
|
dispatchSubmitSucceeded();
|
2020-08-07 08:32:40 +02:00
|
|
|
|
|
|
|
handleToggle();
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
2021-05-17 15:55:46 +02:00
|
|
|
toggleNotification({
|
2020-10-02 18:58:15 +02:00
|
|
|
type: 'warning',
|
|
|
|
message: { id: 'notification.error' },
|
|
|
|
});
|
2020-08-07 08:32:40 +02:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
errors = getYupInnerErrors(err);
|
|
|
|
console.log(errors);
|
|
|
|
}
|
|
|
|
|
2020-08-07 09:28:29 +02:00
|
|
|
dispatchSetFormErrors(errors);
|
2020-08-07 08:32:40 +02:00
|
|
|
|
|
|
|
setIsSubmiting(false);
|
2021-05-17 18:38:00 +02:00
|
|
|
unlockApp();
|
2020-08-07 08:32:40 +02:00
|
|
|
},
|
2020-08-07 09:28:29 +02:00
|
|
|
[
|
|
|
|
dispatchSetFormErrors,
|
|
|
|
dispatchSubmitSucceeded,
|
|
|
|
formToRender,
|
|
|
|
handleToggle,
|
|
|
|
modifiedData,
|
2020-10-02 18:58:15 +02:00
|
|
|
providerToEditName,
|
2021-05-17 15:55:46 +02:00
|
|
|
toggleNotification,
|
2021-05-17 18:38:00 +02:00
|
|
|
lockApp,
|
|
|
|
unlockApp,
|
2020-08-07 09:28:29 +02:00
|
|
|
]
|
2020-08-07 08:32:40 +02:00
|
|
|
);
|
2020-08-06 18:40:22 +02:00
|
|
|
|
2020-08-04 14:33:43 +02:00
|
|
|
return (
|
2021-08-17 18:51:41 +02:00
|
|
|
<Layout>
|
2020-08-04 14:33:43 +02:00
|
|
|
<SettingsPageTitle name={pageTitle} />
|
2021-08-17 18:51:41 +02:00
|
|
|
<Main labelledBy="providers">
|
|
|
|
<HeaderLayout
|
|
|
|
as="h1"
|
|
|
|
id="providers"
|
|
|
|
title={formatMessage({ id: getTrad('HeaderNav.link.providers') })}
|
|
|
|
/>
|
|
|
|
<ContentLayout>
|
2021-08-18 11:45:34 +02:00
|
|
|
<Table colCount={4} rowCount={rowCount + 1}>
|
2021-08-17 18:51:41 +02:00
|
|
|
<Thead>
|
|
|
|
<Tr>
|
2021-08-18 11:45:34 +02:00
|
|
|
<Th>
|
|
|
|
<TableLabel>
|
|
|
|
<VisuallyHidden>image</VisuallyHidden>
|
|
|
|
</TableLabel>
|
|
|
|
</Th>
|
2021-08-17 18:51:41 +02:00
|
|
|
<Th>
|
|
|
|
<TableLabel>name</TableLabel>
|
|
|
|
</Th>
|
|
|
|
<Th>
|
|
|
|
<TableLabel>status</TableLabel>
|
|
|
|
</Th>
|
|
|
|
<Th>
|
|
|
|
<TableLabel>
|
|
|
|
<VisuallyHidden>Settings</VisuallyHidden>
|
|
|
|
</TableLabel>
|
|
|
|
</Th>
|
|
|
|
</Tr>
|
|
|
|
</Thead>
|
|
|
|
<Tbody>
|
|
|
|
{providers.map(provider => (
|
2021-08-18 11:45:34 +02:00
|
|
|
|
2021-08-17 18:51:41 +02:00
|
|
|
<Tr key={provider.name}>
|
2021-08-18 11:45:34 +02:00
|
|
|
<Td width="">
|
|
|
|
<FontAwesomeIcon icon={provider.icon} />
|
|
|
|
</Td>
|
|
|
|
<Td width="45%">
|
|
|
|
<Text highlighted textColor="neutral800">
|
|
|
|
{provider.name}
|
|
|
|
</Text>
|
2021-08-17 18:51:41 +02:00
|
|
|
</Td>
|
2021-08-18 11:45:34 +02:00
|
|
|
<Td width="65%">
|
|
|
|
<Text textColor={provider.enabled ? 'success600' : 'danger600'}>{provider.enabled ? 'enabled' : 'disabled'}</Text>
|
2021-08-17 18:51:41 +02:00
|
|
|
</Td>
|
|
|
|
<Td>
|
|
|
|
{canUpdate && (
|
|
|
|
<IconButton
|
|
|
|
onClick={() => handleClickEdit(provider)}
|
|
|
|
noBorder
|
|
|
|
icon={<EditIcon />}
|
|
|
|
label="Edit"
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Td>
|
|
|
|
</Tr>
|
|
|
|
))}
|
|
|
|
</Tbody>
|
|
|
|
</Table>
|
|
|
|
</ContentLayout>
|
|
|
|
</Main>
|
|
|
|
{/* <div>
|
2020-08-04 15:59:19 +02:00
|
|
|
<List
|
|
|
|
title={listTitle}
|
|
|
|
items={providers}
|
2020-08-04 17:48:23 +02:00
|
|
|
isLoading={isLoadingForPermissions || isLoading}
|
2020-08-04 15:59:19 +02:00
|
|
|
customRowComponent={provider => (
|
|
|
|
<ListRow
|
|
|
|
{...provider}
|
2020-08-06 16:27:51 +02:00
|
|
|
onClick={() => handleClickEdit(provider)}
|
2020-08-04 15:59:19 +02:00
|
|
|
links={[
|
|
|
|
{
|
|
|
|
icon: canUpdate ? <Pencil fill="#0e1622" /> : null,
|
2020-08-06 16:27:51 +02:00
|
|
|
onClick: e => {
|
|
|
|
e.stopPropagation();
|
2020-08-06 18:40:22 +02:00
|
|
|
handleClickEdit(provider);
|
2020-08-04 15:59:19 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
<td key="enabled">
|
|
|
|
<Text
|
|
|
|
fontWeight="semiBold"
|
|
|
|
lineHeight="18px"
|
|
|
|
color={provider.enabled ? 'green' : 'lightOrange'}
|
|
|
|
>
|
|
|
|
{provider.enabled ? 'Enabled' : 'Disabled'}
|
|
|
|
</Text>
|
|
|
|
</td>
|
|
|
|
</ListRow>
|
|
|
|
)}
|
|
|
|
/>
|
2021-08-17 18:51:41 +02:00
|
|
|
</div> */}
|
2020-08-06 18:40:22 +02:00
|
|
|
<ModalForm
|
|
|
|
isOpen={isOpen}
|
|
|
|
onClick={handleClick}
|
|
|
|
onCancel={handleToggle}
|
|
|
|
isLoading={isSubmiting}
|
|
|
|
onOpened={handleOpened}
|
|
|
|
onClosed={handleClosed}
|
|
|
|
onToggle={handleToggle}
|
|
|
|
headerBreadcrumbs={[
|
|
|
|
getTrad('PopUpForm.header.edit.providers'),
|
|
|
|
upperFirst(providerToEditName),
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
{showForm && (
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
<Row>
|
2020-08-07 08:32:40 +02:00
|
|
|
{formToRender.form.map(input => {
|
2020-08-06 18:40:22 +02:00
|
|
|
const label = input.label.params
|
2020-10-02 18:58:15 +02:00
|
|
|
? { ...input.label, params: { provider: upperFirst(providerToEditName) } }
|
|
|
|
: input.label;
|
2020-08-06 18:40:22 +02:00
|
|
|
|
|
|
|
const value =
|
2020-10-02 18:58:15 +02:00
|
|
|
input.name === 'noName'
|
|
|
|
? `${strapi.backendURL}/connect/${providerToEditName}/callback`
|
|
|
|
: get(modifiedData, [providerToEditName, ...input.name.split('.')], '');
|
2020-08-06 18:40:22 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<SizedInput
|
|
|
|
key={input.name}
|
|
|
|
{...input}
|
|
|
|
label={label}
|
|
|
|
error={formErrors[input.name]}
|
|
|
|
name={`${providerToEditName}.${input.name}`}
|
|
|
|
onChange={handleChange}
|
|
|
|
value={value}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</Row>
|
|
|
|
<button type="submit" style={{ display: 'none' }} ref={buttonSubmitRef}>
|
|
|
|
hidden button to use the native form event
|
|
|
|
</button>
|
|
|
|
</form>
|
|
|
|
)}
|
|
|
|
</ModalForm>
|
2021-08-17 18:51:41 +02:00
|
|
|
</Layout>
|
2020-08-04 14:33:43 +02:00
|
|
|
);
|
2020-08-03 16:49:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ProvidersPage;
|