Skip to content

Commit

Permalink
make coderabbit suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
meetulr committed Oct 28, 2024
1 parent 879ef42 commit 6a79d51
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 63 deletions.
3 changes: 2 additions & 1 deletion public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@
"tagName": "Name",
"tagUpdationSuccess": "Tag updated successfully",
"tagRemovalSuccess": "Tag deleted successfully",
"noTagSelected": "No Tag Selected"
"noTagSelected": "No Tag Selected",
"changeNameToEdit": "Change the name to make an update"
},
"userListCard": {
"addAdmin": "Add Admin",
Expand Down
3 changes: 2 additions & 1 deletion public/locales/fr/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@
"tagName": "Nom",
"tagUpdationSuccess": "Étiquette mise à jour avec succès",
"tagRemovalSuccess": "Étiquette supprimée avec succès",
"noTagSelected": "Aucun tag sélectionné"
"noTagSelected": "Aucun tag sélectionné",
"changeNameToEdit": "Modifiez le nom pour faire une mise à jour"
},
"userListCard": {
"addAdmin": "Ajouter un administrateur",
Expand Down
3 changes: 2 additions & 1 deletion public/locales/hi/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@
"tagName": "नाम",
"tagUpdationSuccess": "टैग सफलतापूर्वक अपडेट की गई",
"tagRemovalSuccess": "टैग सफलतापूर्वक हटाई गई",
"noTagSelected": "कोई टैग चयनित नहीं"
"noTagSelected": "कोई टैग चयनित नहीं",
"changeNameToEdit": "अपडेट करने के लिए नाम बदलें"
},
"userListCard": {
"addAdmin": "व्यवस्थापक जोड़ें",
Expand Down
3 changes: 2 additions & 1 deletion public/locales/sp/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@
"tagName": "Nombre",
"tagUpdationSuccess": "Etiqueta actualizada con éxito",
"tagRemovalSuccess": "Etiqueta eliminada con éxito",
"noTagSelected": "Ninguna etiqueta seleccionada"
"noTagSelected": "Ninguna etiqueta seleccionada",
"changeNameToEdit": "Cambia el nombre para hacer una actualización"
},
"userListCard": {
"joined": "Unido",
Expand Down
3 changes: 2 additions & 1 deletion public/locales/zh/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@
"tagName": "名称",
"tagUpdationSuccess": "标签更新成功",
"tagRemovalSuccess": "标签删除成功",
"noTagSelected": "未选择标签"
"noTagSelected": "未选择标签",
"changeNameToEdit": "更改名称以进行更新"
},
"userListCard": {
"addAdmin": "添加管理员",
Expand Down
10 changes: 6 additions & 4 deletions src/components/TagActions/TagActions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
fireEvent,
cleanup,
waitFor,
act,
} from '@testing-library/react';
import { Provider } from 'react-redux';
import { MemoryRouter, Route, Routes } from 'react-router-dom';
Expand All @@ -26,10 +27,11 @@ import { MOCKS, MOCKS_ERROR } from './TagActionsMocks';
const link = new StaticMockLink(MOCKS, true);
const link2 = new StaticMockLink(MOCKS_ERROR, true);

async function wait(): Promise<void> {
await waitFor(() => {
// The waitFor utility automatically uses optimal timing
return Promise.resolve();
async function wait(ms = 500): Promise<void> {
await act(() => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
});
}

Expand Down
59 changes: 20 additions & 39 deletions src/components/TagActions/TagActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const TagActions: React.FC<InterfaceTagActionsProps> = ({
skip: !assignToTagsModalIsOpen,
});

const userTagsList = orgUserTagsData?.organizations[0].userTags.edges.map(
const userTagsList = orgUserTagsData?.organizations[0]?.userTags.edges.map(
(edge) => edge.node,
);

Expand Down Expand Up @@ -246,49 +246,32 @@ const TagActions: React.FC<InterfaceTagActionsProps> = ({
};

const [assignToTags] = useMutation(ASSIGN_TO_TAGS);

const assignToSelectedTags = async (
e: FormEvent<HTMLFormElement>,
): Promise<void> => {
e.preventDefault();

try {
const { data } = await assignToTags({
variables: {
currentTagId,
selectedTagIds: selectedTags.map((selectedTag) => selectedTag._id),
},
});

if (data) {
toast.success(t('successfullyAssignedToTags'));
hideAssignToTagsModal();
}
} catch (error: unknown) {
/* istanbul ignore next */
if (error instanceof Error) {
toast.error(error.message);
}
}
};

const [removeFromTags] = useMutation(REMOVE_FROM_TAGS);

const removeFromSelectedTags = async (
const handleTagAction = async (
e: FormEvent<HTMLFormElement>,
): Promise<void> => {
e.preventDefault();

const mutationObject = {
variables: {
currentTagId,
selectedTagIds: selectedTags.map((selectedTag) => selectedTag._id),
},
};

try {
const { data } = await removeFromTags({
variables: {
currentTagId,
selectedTagIds: selectedTags.map((selectedTag) => selectedTag._id),
},
});
const { data } =
tagActionType === 'assignToTags'
? await assignToTags(mutationObject)
: await removeFromTags(mutationObject);

if (data) {
toast.success(t('successfullyRemovedFromTags'));
if (tagActionType === 'assignToTags') {
toast.success(t('successfullyAssignedToTags'));
} else {
toast.success(t('successfullyRemovedFromTags'));
}
hideAssignToTagsModal();
}
} catch (error: unknown) {
Expand All @@ -306,8 +289,6 @@ const TagActions: React.FC<InterfaceTagActionsProps> = ({
<WarningAmberRounded className={styles.errorIcon} fontSize="large" />
<h6 className="fw-bold text-danger text-center">
{t('errorOccurredWhileLoadingOrganizationUserTags')}
<br />
{orgUserTagsError.message}
</h6>
</div>
</div>
Expand Down Expand Up @@ -337,8 +318,8 @@ const TagActions: React.FC<InterfaceTagActionsProps> = ({
<Form
onSubmitCapture={(e) =>
tagActionType === 'assignToTags'
? assignToSelectedTags(e)
: removeFromSelectedTags(e)
? handleTagAction(e)
: handleTagAction(e)
}
>
<Modal.Body className="pb-0">
Expand Down
15 changes: 6 additions & 9 deletions src/components/TagActions/TagActionsMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,8 @@ export const MOCKS = [
},
],
pageInfo: {
startCursor: '1',
endCursor: '10',
startCursor: 'subTag1',
endCursor: 'subTag10',
hasNextPage: true,
hasPreviousPage: false,
},
Expand All @@ -382,7 +382,7 @@ export const MOCKS = [
query: USER_TAG_SUB_TAGS,
variables: {
id: '1',
after: '10',
after: 'subTag10',
first: TAGS_QUERY_LIMIT,
},
},
Expand All @@ -407,8 +407,8 @@ export const MOCKS = [
},
],
pageInfo: {
startCursor: '11',
endCursor: '11',
startCursor: 'subTag11',
endCursor: 'subTag11',
hasNextPage: false,
hasPreviousPage: true,
},
Expand Down Expand Up @@ -458,10 +458,7 @@ export const MOCKS_ERROR = [
query: ORGANIZATION_USER_TAGS_LIST,
variables: {
id: '123',
after: null,
before: null,
first: 5,
last: null,
first: TAGS_QUERY_LIMIT,
},
},
error: new Error('Mock Graphql Error'),
Expand Down
7 changes: 7 additions & 0 deletions src/screens/ManageTag/ManageTag.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ async function wait(ms = 500): Promise<void> {
jest.mock('react-toastify', () => ({
toast: {
success: jest.fn(),
info: jest.fn(),
error: jest.fn(),
},
}));
Expand Down Expand Up @@ -394,6 +395,12 @@ describe('Manage Tag Page', () => {
});
userEvent.click(screen.getByTestId('editTag'));

userEvent.click(screen.getByTestId('editTagSubmitBtn'));

await waitFor(() => {
expect(toast.info).toHaveBeenCalledWith(translations.changeNameToEdit);
});

const tagNameInput = screen.getByTestId('tagNameInput');
await userEvent.clear(tagNameInput);
await userEvent.type(tagNameInput, 'tag 1 edited');
Expand Down
18 changes: 12 additions & 6 deletions src/screens/ManageTag/ManageTag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ function ManageTag(): JSX.Element {
setAssignToTagsModalIsOpen(false);
};

const showEditTagModal = (): void => {
setEditTagModalIsOpen(true);
};

const hideEditTagModal = (): void => {
setEditTagModalIsOpen(false);
};
Expand Down Expand Up @@ -159,6 +163,7 @@ function ManageTag(): JSX.Element {
const [edit] = useMutation(UPDATE_USER_TAG);

const [newTagName, setNewTagName] = useState<string>('');
const currentTagName = userTagAssignedMembersData?.getUserTag.name ?? '';

useEffect(() => {
setNewTagName(userTagAssignedMembersData?.getUserTag.name ?? '');
Expand All @@ -167,6 +172,11 @@ function ManageTag(): JSX.Element {
const editTag = async (e: FormEvent<HTMLFormElement>): Promise<void> => {
e.preventDefault();

if (newTagName === currentTagName) {
toast.info(t('changeNameToEdit'));
return;
}

try {
const { data } = await edit({
variables: {
Expand Down Expand Up @@ -524,18 +534,14 @@ function ManageTag(): JSX.Element {
<hr className="mb-1 mt-2" />

<div
onClick={() => {
setEditTagModalIsOpen(true);
}}
onClick={showEditTagModal}
className="ms-5 mt-3 mb-2 btn btn-primary btn-sm w-75"
data-testid="editTag"
>
{tCommon('edit')}
</div>
<div
onClick={() => {
setRemoveTagModalIsOpen(true);
}}
onClick={toggleRemoveUserTagModal}
className="ms-5 mb-2 btn btn-danger btn-sm w-75"
data-testid="removeTag"
>
Expand Down

0 comments on commit 6a79d51

Please sign in to comment.