Skip to content

Commit

Permalink
Incorporate feedback from code review.
Browse files Browse the repository at this point in the history
  • Loading branch information
dakotablair committed Sep 8, 2023
1 parent c9e290c commit cd655a2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 50 deletions.
21 changes: 16 additions & 5 deletions src/common/api/authService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// import { store } from '../../app/store';
import { Me } from '../types/auth';
import { uriEncodeTemplateTag as encode } from '../utils/stringUtils';
import { baseApi } from './index';
import { httpService } from './utils/serviceHelpers';
import { uriEncodeTemplateTag as encode } from '../utils/stringUtils';
import { Me } from '../types/auth';

const authService = httpService({
url: '/services/auth',
Expand Down Expand Up @@ -51,14 +52,24 @@ export const authApi = baseApi.injectEndpoints({
}),
}),
getMe: builder.query<AuthResults['getMe'], AuthParams['getMe']>({
query: ({ token }) =>
authService({
query: ({ token }) => {
/* I want to do
const token = store.getState().auth.token;
but authSlice imports revokeToken defined here,
so this becomes a circular depenency.
Specifically the error is:
7022: 'token' implicitly has type 'any' because it does not have a
type annotation and is referenced directly or indirectly in its own
initializer.
*/
return authService({
headers: {
Authorization: token,
},
method: 'GET',
url: '/api/V2/me',
}),
});
},
}),
getUsers: builder.query<AuthResults['getUsers'], AuthParams['getUsers']>({
query: ({ token, users }) =>
Expand Down
20 changes: 1 addition & 19 deletions src/features/navigator/NarrativeControl/Delete.test.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
/* Delete.test */
import { FC } from 'react';
import { act, render, screen } from '@testing-library/react';
import fetchMock, {
//MockParams,
disableFetchMocks,
enableFetchMocks,
} from 'jest-fetch-mock';
import { ErrorBoundary, FallbackProps } from 'react-error-boundary';
import { noOp } from '../../common';
import { testNarrativeDoc, testNarrativeDocFactory } from '../fixtures';
import { DeleteTemplate } from './NarrativeControl.stories';
Expand Down Expand Up @@ -40,21 +37,11 @@ const testResponseErrorFactory = ({
{ status: 500 },
];

const TestingError: FC<FallbackProps> = ({ error }) => {
return <>Error: {JSON.stringify(error)}</>;
};

const consoleError = jest.spyOn(console, 'error');
// This mockImplementation supresses console.error calls.
// eslint-disable-next-line @typescript-eslint/no-empty-function
consoleError.mockImplementation(() => {});

const logError = (error: Error, info: { componentStack: string }) => {
console.log({ error }); // eslint-disable-line no-console
console.log(info.componentStack); // eslint-disable-line no-console
screen.debug();
};

describe('The <Delete /> component...', () => {
beforeAll(() => {
enableFetchMocks();
Expand Down Expand Up @@ -122,12 +109,7 @@ describe('The <Delete /> component...', () => {
test('throws an error if the delete request fails.', async () => {
fetchMock.mockRejectedValue(null);
const { container } = render(
<ErrorBoundary FallbackComponent={TestingError} onError={logError}>
<DeleteTemplate
modalClose={noOp}
narrativeDoc={testNarrativeDocError}
/>
</ErrorBoundary>
<DeleteTemplate modalClose={noOp} narrativeDoc={testNarrativeDocError} />
);
expect(container).toBeTruthy();
const buttonDelete = container.querySelector('button');
Expand Down
17 changes: 11 additions & 6 deletions src/features/navigator/NarrativeControl/Delete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import toast from 'react-hot-toast';
import { Button } from '../../../common/components';
import { useAppDispatch, useAppSelector } from '../../../common/hooks';
import { isKBaseBaseQueryError } from '../../../common/api/utils/kbaseBaseQuery';
import { parseError } from '../../../common/api/utils/parseError';
import { deleteWorkspace } from '../../../common/api/workspaceApi';
import {
generatePathWithSearchParams,
Expand All @@ -13,6 +14,13 @@ import {
import { deleteNarrative, loading, setLoading } from '../navigatorSlice';
import { ControlProps } from './common';

const ErrorMessage: FC<{ err: unknown }> = ({ err }) => (
<>
<span>There was an error! Guru meditation:</span>
<span>{JSON.stringify(err)}</span>
</>
);

export const Delete: FC<ControlProps> = ({ narrativeDoc, modalClose }) => {
const dispatch = useAppDispatch();
const loadState = useAppSelector(loading);
Expand All @@ -38,13 +46,10 @@ export const Delete: FC<ControlProps> = ({ narrativeDoc, modalClose }) => {
} catch (err) {
if (!isKBaseBaseQueryError(err)) {
console.error({ err }); // eslint-disable-line no-console
toast(
<>
<span>There was an error! Guru meditation:</span>
<span>{JSON.stringify(err)}</span>
</>
);
toast(ErrorMessage({ err }));
return;
}
toast(ErrorMessage({ err: parseError(err) }));
dispatch(setLoading(false));
return;
}
Expand Down
42 changes: 22 additions & 20 deletions src/features/navigator/NarrativeControl/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* NarrativeControl */
import { FC, useState } from 'react';
import { FC, useMemo, useState } from 'react';
import { useParams } from 'react-router-dom';
import { Dropdown } from '../../../common/components';
import { NarrativeDoc } from '../../../common/types/NarrativeDoc';
Expand Down Expand Up @@ -30,25 +30,27 @@ interface ControlLatestProps {
const ControlLatest: FC<ControlLatestProps> = ({ narrativeDoc }) => {
const [modalView, setModalView] = useState('Copy this Narrative');
const modal = useModalControls();
const modalClose = () => modal?.close();
const { version } = narrativeDoc;
const controlLatestDialogs: Record<string, JSX.Element> = {
'Copy this Narrative': (
<Copy
narrativeDoc={narrativeDoc}
modalClose={modalClose}
version={version}
/>
),
Delete: <Delete narrativeDoc={narrativeDoc} modalClose={modalClose} />,
'Link to Organization': (
<LinkOrg narrativeDoc={narrativeDoc} modalClose={modalClose} />
),
'Manage Sharing': (
<Share narrativeDoc={narrativeDoc} modalClose={modalClose} />
),
Rename: <Rename narrativeDoc={narrativeDoc} modalClose={modalClose} />,
};
const controlLatestDialogs: Record<string, JSX.Element> = useMemo(() => {
const modalClose = () => modal?.close();
const { version } = narrativeDoc;
return {
'Copy this Narrative': (
<Copy
narrativeDoc={narrativeDoc}
modalClose={modalClose}
version={version}
/>
),
Delete: <Delete narrativeDoc={narrativeDoc} modalClose={modalClose} />,
'Link to Organization': (
<LinkOrg narrativeDoc={narrativeDoc} modalClose={modalClose} />
),
'Manage Sharing': (
<Share narrativeDoc={narrativeDoc} modalClose={modalClose} />
),
Rename: <Rename narrativeDoc={narrativeDoc} modalClose={modalClose} />,
};
}, [modal, narrativeDoc]);
return (
<>
<Dropdown
Expand Down

0 comments on commit cd655a2

Please sign in to comment.