2021-08-26 15:12:19 +02:00
|
|
|
import React, { useMemo, useRef, useState } from 'react';
|
2020-08-04 14:33:43 +02:00
|
|
|
import { useIntl } from 'react-intl';
|
2020-08-07 09:48:28 +02:00
|
|
|
import {
|
|
|
|
SettingsPageTitle,
|
2021-08-23 11:14:08 +02:00
|
|
|
LoadingIndicatorPage,
|
2021-08-26 15:12:19 +02:00
|
|
|
useTracking,
|
|
|
|
useNotification,
|
|
|
|
useOverlayBlocker,
|
2021-07-28 08:16:15 +02:00
|
|
|
CheckPagePermissions,
|
2021-08-26 16:22:29 +02:00
|
|
|
useRBAC,
|
2021-04-29 13:51:12 +02:00
|
|
|
} from '@strapi/helper-plugin';
|
2021-08-26 12:08:07 +02:00
|
|
|
import has from 'lodash/has';
|
2021-08-26 11:25:03 +02:00
|
|
|
import upperFirst from 'lodash/upperFirst';
|
2021-08-17 18:51:41 +02:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
import { HeaderLayout, Layout, ContentLayout } from '@strapi/parts/Layout';
|
|
|
|
import { Main } from '@strapi/parts/Main';
|
|
|
|
import { Table, Thead, Tr, Th, Tbody, Td } from '@strapi/parts/Table';
|
|
|
|
import { Text, TableLabel } from '@strapi/parts/Text';
|
|
|
|
import { VisuallyHidden } from '@strapi/parts/VisuallyHidden';
|
|
|
|
import { IconButton } from '@strapi/parts/IconButton';
|
|
|
|
import EditIcon from '@strapi/icons/EditIcon';
|
2021-08-26 16:45:49 +02:00
|
|
|
import { useQuery, useMutation, useQueryClient } from 'react-query';
|
2021-08-26 12:08:07 +02:00
|
|
|
import forms from './utils/forms';
|
2021-08-26 16:45:49 +02:00
|
|
|
import { fetchData, putProvider } from './utils/api';
|
2021-08-17 18:51:41 +02:00
|
|
|
import createProvidersArray from './utils/createProvidersArray';
|
2021-08-26 15:23:02 +02:00
|
|
|
import { getRequestURL, getTrad } from '../../utils';
|
2021-08-17 18:51:41 +02:00
|
|
|
import { useForm } from '../../hooks';
|
|
|
|
import pluginPermissions from '../../permissions';
|
2021-08-26 12:08:07 +02:00
|
|
|
import FormModal from '../../components/FormModal';
|
2021-08-26 15:23:02 +02:00
|
|
|
import { axiosInstance } from '../../../../../../core/admin/admin/src/core/utils';
|
2020-08-03 16:49:40 +02:00
|
|
|
|
2021-08-24 16:13:02 +02:00
|
|
|
export const ProvidersPage = () => {
|
2020-08-04 14:33:43 +02:00
|
|
|
const { formatMessage } = useIntl();
|
2021-08-26 16:45:49 +02:00
|
|
|
const queryClient = useQueryClient();
|
2021-08-26 15:12:19 +02:00
|
|
|
const { trackUsage } = useTracking();
|
|
|
|
const trackUsageRef = useRef(trackUsage);
|
2021-08-26 11:25:03 +02:00
|
|
|
const [isOpen, setIsOpen] = useState(false);
|
2021-08-26 15:12:19 +02:00
|
|
|
const [isSubmiting, setIsSubmiting] = useState(false);
|
2021-08-26 11:25:03 +02:00
|
|
|
const [providerToEditName, setProviderToEditName] = useState(null);
|
2021-08-26 15:12:19 +02:00
|
|
|
const toggleNotification = useNotification();
|
|
|
|
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
|
|
|
}, []);
|
2020-08-07 09:28:29 +02:00
|
|
|
|
2020-08-04 15:59:19 +02:00
|
|
|
const {
|
2021-08-26 16:22:29 +02:00
|
|
|
// allowedActions: { canUpdate },
|
2021-08-26 15:23:02 +02:00
|
|
|
dispatchSubmitSucceeded,
|
2021-08-26 16:22:29 +02:00
|
|
|
// isLoading,
|
|
|
|
// isLoadingForPermissions,
|
|
|
|
// modifiedData,
|
2020-08-07 09:28:29 +02:00
|
|
|
} = useForm('providers', updatePermissions);
|
|
|
|
|
2021-08-26 16:22:29 +02:00
|
|
|
const {
|
|
|
|
isLoading: isLoadingForPermissions,
|
|
|
|
allowedActions: { canUpdate },
|
|
|
|
} = useRBAC(updatePermissions);
|
2021-08-26 16:45:49 +02:00
|
|
|
|
2021-08-26 16:22:29 +02:00
|
|
|
const { isLoading, data: modifiedData } = useQuery(
|
|
|
|
'get-providers',
|
|
|
|
() => fetchData(toggleNotification),
|
|
|
|
{ initialData: {} }
|
|
|
|
);
|
|
|
|
|
2021-08-26 16:45:49 +02:00
|
|
|
const submitMutation = useMutation(putProvider, {
|
|
|
|
onSuccess: () => {
|
|
|
|
queryClient.invalidateQueries('get-providers');
|
|
|
|
toggleNotification({
|
|
|
|
type: 'info',
|
|
|
|
message: { id: getTrad('notification.success.submit') },
|
|
|
|
});
|
|
|
|
|
|
|
|
trackUsageRef.current('didEditAuthenticationProvider');
|
|
|
|
|
|
|
|
// handleToggleModal();
|
|
|
|
},
|
|
|
|
onError: () => {
|
|
|
|
toggleNotification({
|
|
|
|
type: 'warning',
|
|
|
|
message: { id: 'notification.error' },
|
|
|
|
});
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2020-08-04 16:27:07 +02:00
|
|
|
const providers = useMemo(() => createProvidersArray(modifiedData), [modifiedData]);
|
2021-08-23 13:13:24 +02:00
|
|
|
|
2021-08-18 11:45:34 +02:00
|
|
|
const rowCount = providers.length;
|
2021-08-17 18:51:41 +02:00
|
|
|
|
2021-08-26 12:08:07 +02:00
|
|
|
const isProviderWithSubdomain = useMemo(() => {
|
|
|
|
if (!providerToEditName) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-06 11:45:22 +02:00
|
|
|
|
2021-08-26 12:08:07 +02:00
|
|
|
const providerToEdit = providers.find(obj => obj.name === providerToEditName);
|
2020-10-06 11:45:22 +02:00
|
|
|
|
2021-08-26 12:08:07 +02:00
|
|
|
return has(providerToEdit, 'subdomain');
|
|
|
|
}, [providers, providerToEditName]);
|
2021-08-17 18:51:41 +02:00
|
|
|
|
2021-08-23 13:13:24 +02:00
|
|
|
const pageTitle = formatMessage({
|
|
|
|
id: getTrad('HeaderNav.link.providers'),
|
|
|
|
defaultMessage: 'Providers',
|
|
|
|
});
|
2020-08-06 18:40:22 +02:00
|
|
|
|
2021-08-26 12:08:07 +02:00
|
|
|
const layoutToRender = useMemo(() => {
|
|
|
|
if (providerToEditName === 'email') {
|
|
|
|
return forms.email;
|
|
|
|
}
|
2020-10-06 11:45:22 +02:00
|
|
|
|
2021-08-26 12:08:07 +02:00
|
|
|
if (isProviderWithSubdomain) {
|
|
|
|
return forms.providersWithSubdomain;
|
|
|
|
}
|
2020-10-06 11:45:22 +02:00
|
|
|
|
2021-08-26 12:08:07 +02:00
|
|
|
return forms.providers;
|
|
|
|
}, [providerToEditName, isProviderWithSubdomain]);
|
2020-08-07 08:32:40 +02:00
|
|
|
|
2021-08-26 11:25:03 +02:00
|
|
|
const handleToggleModal = () => {
|
|
|
|
setIsOpen(prev => !prev);
|
|
|
|
};
|
2020-08-06 18:40:22 +02:00
|
|
|
|
2021-08-26 11:25:03 +02:00
|
|
|
const handleClickEdit = provider => {
|
|
|
|
if (canUpdate) {
|
|
|
|
setProviderToEditName(provider.name);
|
|
|
|
handleToggleModal();
|
|
|
|
}
|
|
|
|
};
|
2020-08-06 18:40:22 +02:00
|
|
|
|
2021-08-26 15:23:02 +02:00
|
|
|
const handleSubmit = async values => {
|
2021-08-26 16:45:49 +02:00
|
|
|
console.log('is sub')
|
2021-08-26 15:12:19 +02:00
|
|
|
setIsSubmiting(true);
|
|
|
|
|
|
|
|
lockApp();
|
|
|
|
|
|
|
|
try {
|
|
|
|
trackUsageRef.current('willEditAuthenticationProvider');
|
|
|
|
|
2021-08-26 15:23:02 +02:00
|
|
|
const body = { ...modifiedData, [providerToEditName]: values };
|
2021-08-26 15:12:19 +02:00
|
|
|
|
2021-08-26 16:45:49 +02:00
|
|
|
// submitMutation.mutate({providers: body});
|
2021-08-26 15:12:19 +02:00
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
toggleNotification({
|
|
|
|
type: 'warning',
|
|
|
|
message: { id: 'notification.error' },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setIsSubmiting(false);
|
|
|
|
unlockApp();
|
|
|
|
};
|
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-23 13:13:24 +02:00
|
|
|
<Main
|
|
|
|
labelledBy={formatMessage({
|
|
|
|
id: getTrad('HeaderNav.link.providers'),
|
|
|
|
defaultMessage: 'Providers',
|
|
|
|
})}
|
|
|
|
>
|
2021-08-17 18:51:41 +02:00
|
|
|
<HeaderLayout
|
|
|
|
as="h1"
|
|
|
|
id="providers"
|
2021-08-20 15:09:29 +02:00
|
|
|
title={formatMessage({
|
|
|
|
id: getTrad('HeaderNav.link.providers'),
|
|
|
|
defaultMessage: 'Providers',
|
|
|
|
})}
|
2021-08-17 18:51:41 +02:00
|
|
|
/>
|
2021-08-20 15:09:29 +02:00
|
|
|
{isLoading || isLoadingForPermissions ? (
|
|
|
|
<LoadingIndicatorPage />
|
|
|
|
) : (
|
|
|
|
<ContentLayout>
|
|
|
|
<Table colCount={4} rowCount={rowCount + 1}>
|
|
|
|
<Thead>
|
|
|
|
<Tr>
|
|
|
|
<Th>
|
|
|
|
<TableLabel>
|
|
|
|
<VisuallyHidden>
|
|
|
|
{formatMessage({ id: getTrad('Providers.image'), defaultMessage: 'Image' })}
|
|
|
|
</VisuallyHidden>
|
|
|
|
</TableLabel>
|
|
|
|
</Th>
|
|
|
|
<Th>
|
|
|
|
<TableLabel>
|
|
|
|
{formatMessage({ id: getTrad('Providers.name'), defaultMessage: 'Name' })}
|
|
|
|
</TableLabel>
|
|
|
|
</Th>
|
|
|
|
<Th>
|
|
|
|
<TableLabel>
|
|
|
|
{formatMessage({ id: getTrad('Providers.status'), defaultMessage: 'Status' })}
|
|
|
|
</TableLabel>
|
|
|
|
</Th>
|
|
|
|
<Th>
|
|
|
|
<TableLabel>
|
|
|
|
<VisuallyHidden>
|
|
|
|
{formatMessage({
|
|
|
|
id: getTrad('Providers.settings'),
|
|
|
|
defaultMessage: 'Settings',
|
|
|
|
})}
|
|
|
|
</VisuallyHidden>
|
|
|
|
</TableLabel>
|
|
|
|
</Th>
|
2021-08-17 18:51:41 +02:00
|
|
|
</Tr>
|
2021-08-20 15:09:29 +02:00
|
|
|
</Thead>
|
|
|
|
<Tbody>
|
|
|
|
{providers.map(provider => (
|
|
|
|
<Tr key={provider.name}>
|
|
|
|
<Td width="">
|
|
|
|
<FontAwesomeIcon icon={provider.icon} />
|
|
|
|
</Td>
|
|
|
|
<Td width="45%">
|
|
|
|
<Text highlighted textColor="neutral800">
|
|
|
|
{provider.name}
|
|
|
|
</Text>
|
|
|
|
</Td>
|
|
|
|
<Td width="65%">
|
2021-08-23 13:13:24 +02:00
|
|
|
<Text
|
|
|
|
textColor={provider.enabled ? 'success600' : 'danger600'}
|
|
|
|
data-testid={`enable-${provider.name}`}
|
|
|
|
>
|
2021-08-20 15:09:29 +02:00
|
|
|
{provider.enabled
|
|
|
|
? formatMessage({
|
|
|
|
id: getTrad('Providers.enabled'),
|
|
|
|
defaultMessage: 'Enabled',
|
|
|
|
})
|
|
|
|
: formatMessage({
|
|
|
|
id: getTrad('Providers.disabled'),
|
|
|
|
defaultMessage: 'Disabled',
|
|
|
|
})}
|
|
|
|
</Text>
|
|
|
|
</Td>
|
|
|
|
<Td>
|
|
|
|
{canUpdate && (
|
|
|
|
<IconButton
|
|
|
|
onClick={() => handleClickEdit(provider)}
|
|
|
|
noBorder
|
|
|
|
icon={<EditIcon />}
|
|
|
|
label="Edit"
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Td>
|
|
|
|
</Tr>
|
|
|
|
))}
|
|
|
|
</Tbody>
|
|
|
|
</Table>
|
|
|
|
</ContentLayout>
|
|
|
|
)}
|
2021-08-17 18:51:41 +02:00
|
|
|
</Main>
|
2021-08-26 11:25:03 +02:00
|
|
|
<FormModal
|
2021-08-26 15:04:47 +02:00
|
|
|
initialData={modifiedData[providerToEditName]}
|
2021-08-26 12:59:56 +02:00
|
|
|
isOpen={isOpen}
|
2021-08-26 15:12:19 +02:00
|
|
|
isSubmiting={isSubmiting}
|
2021-08-26 12:08:07 +02:00
|
|
|
layout={layoutToRender}
|
2021-08-26 11:25:03 +02:00
|
|
|
headerBreadcrumbs={[
|
|
|
|
formatMessage({
|
|
|
|
id: getTrad('PopUpForm.header.edit.providers'),
|
|
|
|
defaultMessage: 'Edit Provider',
|
|
|
|
}),
|
|
|
|
upperFirst(providerToEditName),
|
|
|
|
]}
|
|
|
|
onToggle={handleToggleModal}
|
2021-08-26 15:12:19 +02:00
|
|
|
onSubmit={handleSubmit}
|
2021-08-26 12:59:56 +02:00
|
|
|
providerToEditName={providerToEditName}
|
2021-08-26 11:25:03 +02:00
|
|
|
/>
|
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
|
|
|
};
|
|
|
|
|
2021-08-24 16:13:02 +02:00
|
|
|
const ProtectedProvidersPage = () => (
|
|
|
|
<CheckPagePermissions permissions={pluginPermissions.readProviders}>
|
|
|
|
<ProvidersPage />
|
|
|
|
</CheckPagePermissions>
|
|
|
|
);
|
|
|
|
|
2021-07-28 08:16:15 +02:00
|
|
|
export default ProtectedProvidersPage;
|