Skip to content

Commit

Permalink
[Workspace]Show add collaborators modal until collaborators added (op…
Browse files Browse the repository at this point in the history
…ensearch-project#8574)

* Show add collaborators modal until update success

Signed-off-by: Lin Wang <[email protected]>

* Changeset file for PR opensearch-project#8574 created/updated

---------

Signed-off-by: Lin Wang <[email protected]>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and Qxisylolo committed Oct 30, 2024
1 parent c42d070 commit 00546e6
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 6 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/8574.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feat:
- [Workspace]Show add collaborators modal until collaborators added ([#8574](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8574))
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,28 @@ describe('AddCollaboratorsModal', () => {
expect(screen.getByText(instruction.detail)).toBeInTheDocument();
expect(screen.getByText('Learn more in Documentation')).toBeInTheDocument();
});

it('should disable "Add collaborators" button during onAddCollaborators execution', async () => {
const onAddCollaboratorsMock = jest.fn().mockReturnValue(
new Promise((resolve) => {
window.setTimeout(resolve, 1000);
})
);
render(<AddCollaboratorsModal {...defaultProps} onAddCollaborators={onAddCollaboratorsMock} />);
const collaboratorInput = screen.getByLabelText(defaultProps.inputLabel);
fireEvent.change(collaboratorInput, { target: { value: 'user1' } });
const addCollaboratorsButton = screen.getByRole('button', { name: 'Add collaborators' });

jest.useFakeTimers();
fireEvent.click(addCollaboratorsButton);
await waitFor(() => {
expect(addCollaboratorsButton).toBeDisabled();
});
jest.runAllTimers();
jest.useRealTimers();

await waitFor(() => {
expect(addCollaboratorsButton).not.toBeDisabled();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,15 @@ export const AddCollaboratorsModal = ({
}
return { collaboratorId, accessLevel, permissionType };
});
const [isAdding, setIsAdding] = useState(false);

const handleAddCollaborators = () => {
onAddCollaborators(validCollaborators);
const handleAddCollaborators = async () => {
setIsAdding(true);
try {
await onAddCollaborators(validCollaborators);
} finally {
setIsAdding(false);
}
};

return (
Expand Down Expand Up @@ -131,6 +137,8 @@ export const AddCollaboratorsModal = ({
type="submit"
onClick={handleAddCollaborators}
fill
isDisabled={isAdding}
isLoading={isAdding}
>
{i18n.translate('workspace.addCollaboratorsModal.addCollaboratorsButton', {
defaultMessage: 'Add collaborators',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import { WorkspacePermissionSetting } from './types';
interface Props {
displayedTypes: WorkspaceCollaboratorType[];
permissionSettings: PermissionSetting[];
handleSubmitPermissionSettings: (permissionSettings: WorkspacePermissionSetting[]) => void;
handleSubmitPermissionSettings: (
permissionSettings: WorkspacePermissionSetting[]
) => Promise<void>;
}

export const AddCollaboratorButton = ({
Expand Down Expand Up @@ -55,7 +57,7 @@ export const AddCollaboratorButton = ({
}),
}));
const newPermissionSettings = [...permissionSettings, ...addedSettings];
handleSubmitPermissionSettings(newPermissionSettings as WorkspacePermissionSetting[]);
await handleSubmitPermissionSettings(newPermissionSettings as WorkspacePermissionSetting[]);
};

const panelItems = displayedTypes.map(({ id, buttonLabel, onAdd }) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ export const getDisplayedType = (
interface Props {
permissionSettings: PermissionSetting[];
displayedCollaboratorTypes: WorkspaceCollaboratorType[];
handleSubmitPermissionSettings: (permissionSettings: WorkspacePermissionSetting[]) => void;
handleSubmitPermissionSettings: (
permissionSettings: WorkspacePermissionSetting[]
) => Promise<void>;
}

export const WorkspaceCollaboratorTable = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ interface WorkspaceFormContextProps {
>;
setSelectedDataSourceConnections: React.Dispatch<React.SetStateAction<DataSourceConnection[]>>;
onAppLeave: AppMountParameters['onAppLeave'];
handleSubmitPermissionSettings: (permissionSettings: WorkspacePermissionSetting[]) => void;
handleSubmitPermissionSettings: (
permissionSettings: WorkspacePermissionSetting[]
) => Promise<void>;
}

const initialContextValue: WorkspaceFormContextProps = {} as WorkspaceFormContextProps;
Expand Down

0 comments on commit 00546e6

Please sign in to comment.