Skip to content

Commit

Permalink
[MPDX-8061] Google Import (#1001)
Browse files Browse the repository at this point in the history
* Get Google ContactGroup data from rest API

* Add Google Import UI

* Add optional button to NoData

* Add appName to Google Account Deletion

* Renaming variables, adding button, adding modal, cleaning up code

* Add API request to import data

* Move import pages to a folder

* Add message for when the account has no contact groups

* Add tests

* Use router.push and update tests

* Update tool page links
  • Loading branch information
caleballdrin authored Aug 19, 2024
1 parent bb8186c commit 9803ed9
Show file tree
Hide file tree
Showing 15 changed files with 1,197 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';
import { ThemeProvider } from '@mui/material/styles';
import { render } from '@testing-library/react';
import TestRouter from '__tests__/util/TestRouter';
import TestWrapper from '__tests__/util/TestWrapper';
import theme from 'src/theme';
import GoogleImportPage from './google.page';

const accountListId = 'accountListId';
const router = {
query: { accountListId },
isReady: true,
};

const RenderGoogleImportPage = () => (
<TestWrapper>
<TestRouter router={router}>
<ThemeProvider theme={theme}>
<GoogleImportPage />
</ThemeProvider>
</TestRouter>
</TestWrapper>
);
describe('render', () => {
it('google import page', async () => {
const { findByText } = render(<RenderGoogleImportPage />);

expect(await findByText('Import from Google')).toBeVisible();
});
});
33 changes: 33 additions & 0 deletions pages/accountLists/[accountListId]/tools/import/google.page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Head from 'next/head';
import React from 'react';
import { useTranslation } from 'react-i18next';
import { loadSession } from 'pages/api/utils/pagePropsHelpers';
import Loading from 'src/components/Loading';
import GoogleImport from 'src/components/Tool/GoogleImport/GoogleImport';
import { useAccountListId } from 'src/hooks/useAccountListId';
import useGetAppSettings from 'src/hooks/useGetAppSettings';

const GoogleImportPage: React.FC = () => {
const { t } = useTranslation();
const accountListId = useAccountListId();
const { appName } = useGetAppSettings();

return (
<>
<Head>
<title>
{appName} | {t('Import from Google')}
</title>
</Head>
{accountListId ? (
<GoogleImport accountListId={accountListId} />
) : (
<Loading loading />
)}
</>
);
};

export const getServerSideProps = loadSession;

export default GoogleImportPage;
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { snakeToCamel } from 'src/lib/snakeToCamel';
import { fetchAllData } from 'src/lib/deserializeJsonApi';

export interface GoogleAccountsResponse {
attributes: Omit<GoogleAccountAttributes, 'id'>;
id: string;
relationships: {
contact_groups: {
data: unknown[];
data: {
attributes: Omit<GoogleAccountAttributes, 'id'>;
id: string;
relationships: {
contact_groups: {
data: unknown[];
};
};
type: string;
};
type: string;
}

export interface GoogleAccountAttributes {
Expand Down Expand Up @@ -37,17 +39,24 @@ interface GoogleAccountAttributesCamel {
tokenExpired: boolean;
updatedAt: string;
updatedInDbAt: string;
contactGroups: ContactGroupCamel[];
}

export const GoogleAccounts = (
data: GoogleAccountsResponse[],
): GoogleAccountAttributesCamel[] => {
return data.map((accounts) => {
const attributes = {} as Omit<GoogleAccountAttributesCamel, 'id'>;
Object.keys(accounts.attributes).map((key) => {
attributes[snakeToCamel(key)] = accounts.attributes[key];
});
type ContactGroupCamel = {
id: string;
createdAt: string;
tag: string;
title: string;
updatedAt: string;
updatedInDbAt: string;
};

return { id: accounts.id, ...attributes };
export const GoogleAccounts = (response): GoogleAccountAttributesCamel[] => {
return response.data.map((account) => {
const attributes = fetchAllData(account, response.included) as Omit<
GoogleAccountAttributesCamel,
'id'
>;
return { id: account.id, ...attributes };
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,14 @@ type GoogleAccountAttributes {
tokenExpired: Boolean!
updatedAt: String!
updatedInDbAt: String!
contactGroups: [ContactGroup]!
}

type ContactGroup {
id: String!
createdAt: String
tag: String
title: String
updatedAt: String
updatedInDbAt: String
}
4 changes: 2 additions & 2 deletions pages/api/graphql-rest.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -900,14 +900,14 @@ class MpdxRestApi extends RESTDataSource {
//

async googleAccounts() {
const { data }: { data: GoogleAccountsResponse[] } = await this.get(
const response: GoogleAccountsResponse[] = await this.get(
'user/google_accounts',
{
sort: 'created_at',
include: 'contact_groups',
},
);
return GoogleAccounts(data);
return GoogleAccounts(response);
}

async googleAccountIntegrations(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
SubmitButton,
} from 'src/components/common/Modal/ActionButtons/ActionButtons';
import Modal from 'src/components/common/Modal/Modal';
import useGetAppSettings from 'src/hooks/useGetAppSettings';
import { GoogleAccountAttributesSlimmed } from '../GoogleAccordion';
import { useDeleteGoogleAccountMutation } from '../GoogleAccounts.generated';

Expand All @@ -24,6 +25,7 @@ export const DeleteGoogleAccountModal: React.FC<
DeleteGoogleAccountModalProps
> = ({ account, handleClose }) => {
const { t } = useTranslation();
const { appName } = useGetAppSettings();
const [isSubmitting, setIsSubmitting] = useState(false);
const { enqueueSnackbar } = useSnackbar();

Expand All @@ -45,7 +47,7 @@ export const DeleteGoogleAccountModal: React.FC<
},
onCompleted: () => {
enqueueSnackbar(
t('{{appName}} removed your integration with Google.'),
t('{{appName}} removed your integration with Google.', { appName }),
{
variant: 'success',
},
Expand All @@ -54,7 +56,10 @@ export const DeleteGoogleAccountModal: React.FC<
},
onError: () => {
enqueueSnackbar(
t("{{appName}} couldn't save your configuration changes for Google."),
t(
"{{appName}} couldn't save your configuration changes for Google.",
{ appName },
),
{
variant: 'error',
},
Expand Down
Loading

0 comments on commit 9803ed9

Please sign in to comment.