Use useUserContext instead of passing props, also, return an object from Api
Signed-off-by: Erik Engervall <erik.engervall@gmail.com>
This commit is contained in:
@@ -76,14 +76,11 @@ export function GitReleaseManager(props: GitReleaseManagerProps) {
|
||||
return <CenteredCircularProgress />;
|
||||
}
|
||||
|
||||
if (!userResponse.value?.username) {
|
||||
if (!userResponse.value?.user.username) {
|
||||
return <Alert severity="error">Unable to retrieve username</Alert>;
|
||||
}
|
||||
|
||||
const user = {
|
||||
username: userResponse.value.username,
|
||||
email: userResponse.value.email,
|
||||
};
|
||||
const user = userResponse.value.user;
|
||||
|
||||
return (
|
||||
<ProjectContext.Provider value={{ project }}>
|
||||
@@ -92,7 +89,7 @@ export function GitReleaseManager(props: GitReleaseManagerProps) {
|
||||
<ContentHeader title="Git Release Manager" />
|
||||
|
||||
<InfoCardPlus>
|
||||
<RepoDetailsForm username={userResponse.value.username} />
|
||||
<RepoDetailsForm />
|
||||
</InfoCardPlus>
|
||||
|
||||
{isProjectValid(project) && <Features features={props.features} />}
|
||||
|
||||
@@ -118,8 +118,10 @@ export class GitReleaseApiClient implements GitReleaseApi {
|
||||
const userResponse = await octokit.users.getAuthenticated();
|
||||
|
||||
return {
|
||||
username: userResponse.data.login,
|
||||
email: userResponse.data.email ?? undefined,
|
||||
user: {
|
||||
username: userResponse.data.login,
|
||||
email: userResponse.data.email ?? undefined,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
@@ -523,8 +525,10 @@ export interface GitReleaseApi {
|
||||
getUser: (
|
||||
args: OwnerRepo,
|
||||
) => Promise<{
|
||||
username: string;
|
||||
email?: string;
|
||||
user: {
|
||||
username: string;
|
||||
email?: string;
|
||||
};
|
||||
}>;
|
||||
|
||||
getRecentCommits: (
|
||||
|
||||
@@ -21,6 +21,7 @@ import {
|
||||
mockApiClient,
|
||||
mockCalverProject,
|
||||
mockSearchCalver,
|
||||
mockUser,
|
||||
} from '../../test-helpers/test-helpers';
|
||||
import { TEST_IDS } from '../../test-helpers/test-ids';
|
||||
import { useProjectContext } from '../../contexts/ProjectContext';
|
||||
@@ -41,14 +42,17 @@ jest.mock('../../contexts/ProjectContext', () => ({
|
||||
project: mockCalverProject,
|
||||
})),
|
||||
}));
|
||||
jest.mock('../../contexts/UserContext', () => ({
|
||||
useUserContext: jest.fn(() => ({
|
||||
user: mockUser,
|
||||
})),
|
||||
}));
|
||||
|
||||
describe('Owner', () => {
|
||||
beforeEach(jest.clearAllMocks);
|
||||
|
||||
it('should render select', async () => {
|
||||
const { getByTestId } = render(
|
||||
<Owner username={mockCalverProject.owner} />,
|
||||
);
|
||||
const { getByTestId } = render(<Owner />);
|
||||
|
||||
expect(getByTestId(TEST_IDS.form.owner.loading)).toBeInTheDocument();
|
||||
|
||||
@@ -61,9 +65,7 @@ describe('Owner', () => {
|
||||
project: { ...mockCalverProject, owner: '' },
|
||||
});
|
||||
|
||||
const { getAllByTestId, getByTestId } = render(
|
||||
<Owner username={mockCalverProject.owner} />,
|
||||
);
|
||||
const { getAllByTestId, getByTestId } = render(<Owner />);
|
||||
|
||||
expect(getByTestId(TEST_IDS.form.owner.loading)).toBeInTheDocument();
|
||||
|
||||
@@ -95,9 +97,7 @@ describe('Owner', () => {
|
||||
throw new Error('Kaboom');
|
||||
});
|
||||
|
||||
const { getByTestId } = render(
|
||||
<Owner username={mockCalverProject.owner} />,
|
||||
);
|
||||
const { getByTestId } = render(<Owner />);
|
||||
|
||||
expect(getByTestId(TEST_IDS.form.owner.loading)).toBeInTheDocument();
|
||||
await waitFor(() => screen.getByTestId(TEST_IDS.form.owner.error));
|
||||
|
||||
@@ -32,10 +32,12 @@ import { TEST_IDS } from '../../test-helpers/test-ids';
|
||||
import { useFormClasses } from './styles';
|
||||
import { useProjectContext } from '../../contexts/ProjectContext';
|
||||
import { useQueryHandler } from '../../hooks/useQueryHandler';
|
||||
import { useUserContext } from '../../contexts/UserContext';
|
||||
|
||||
export function Owner({ username }: { username: string }) {
|
||||
export function Owner() {
|
||||
const pluginApiClient = useApi(gitReleaseManagerApiRef);
|
||||
const { project } = useProjectContext();
|
||||
const { user } = useUserContext();
|
||||
const formClasses = useFormClasses();
|
||||
const navigate = useNavigate();
|
||||
const { getQueryParamsWithUpdates } = useQueryHandler();
|
||||
@@ -43,7 +45,7 @@ export function Owner({ username }: { username: string }) {
|
||||
const { loading, error, value } = useAsync(() => pluginApiClient.getOwners());
|
||||
const owners = value?.owners ?? [];
|
||||
const customOwnerFromUrl = !owners
|
||||
.concat(['', username])
|
||||
.concat(['', user.username])
|
||||
.includes(project.owner);
|
||||
|
||||
return (
|
||||
@@ -80,8 +82,8 @@ export function Owner({ username }: { username: string }) {
|
||||
<em>None</em>
|
||||
</MenuItem>
|
||||
|
||||
<MenuItem value={username}>
|
||||
<strong>{username}</strong>
|
||||
<MenuItem value={user.username}>
|
||||
<strong>{user.username}</strong>
|
||||
</MenuItem>
|
||||
|
||||
{!error && customOwnerFromUrl && (
|
||||
|
||||
@@ -20,12 +20,12 @@ import { Owner } from './Owner';
|
||||
import { Repo } from './Repo';
|
||||
import { VersioningStrategy } from './VersioningStrategy';
|
||||
|
||||
export function RepoDetailsForm({ username }: { username: string }) {
|
||||
export function RepoDetailsForm() {
|
||||
return (
|
||||
<>
|
||||
<VersioningStrategy />
|
||||
|
||||
<Owner username={username} />
|
||||
<Owner />
|
||||
|
||||
<Repo />
|
||||
</>
|
||||
|
||||
@@ -192,8 +192,10 @@ export const mockApiClient: GitReleaseApi = {
|
||||
})),
|
||||
|
||||
getUser: jest.fn(async () => ({
|
||||
username: mockOwner,
|
||||
email: mockEmail,
|
||||
user: {
|
||||
username: mockOwner,
|
||||
email: mockEmail,
|
||||
},
|
||||
})),
|
||||
|
||||
getRecentCommits: jest.fn(async () => [
|
||||
|
||||
Reference in New Issue
Block a user