-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sync role for !IS_COMMUNITY && is_superadmin
- Loading branch information
Showing
10 changed files
with
181 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { LegacyAPI } from './legacy'; | ||
|
||
export class API extends LegacyAPI { | ||
apiPath = 'v1/sync/'; | ||
|
||
sync(data) { | ||
return this.http.post(this.apiPath, data); | ||
} | ||
} | ||
|
||
export const LegacySyncAPI = new API(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { Trans, t } from '@lingui/macro'; | ||
import { ActionGroup, Button } from '@patternfly/react-core'; | ||
import React, { useState } from 'react'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import { LegacySyncAPI } from 'src/api'; | ||
import { AlertType, DataForm, ExternalLink } from 'src/components'; | ||
import { Paths, formatPath } from 'src/paths'; | ||
import { ErrorMessagesType, handleHttpError, taskAlert } from 'src/utilities'; | ||
|
||
interface IProps { | ||
addAlert: (alert: AlertType) => void; | ||
} | ||
|
||
export const RoleSyncForm = ({ addAlert }: IProps) => { | ||
const [data, setData] = useState<{ | ||
github_user: string; | ||
role_name: string; | ||
}>({ github_user: '', role_name: '' }); | ||
const [errors, setErrors] = useState<ErrorMessagesType>(null); | ||
const navigate = useNavigate(); | ||
|
||
const formFields = [ | ||
{ id: 'github_user', title: t`GitHub user` }, | ||
{ id: 'role_name', title: t`Role name` }, | ||
]; | ||
|
||
const requiredFields = ['github_user', 'role_name']; | ||
|
||
const onCancel = () => navigate(formatPath(Paths.standaloneRoles)); | ||
|
||
const onSaved = ({ data: { pulp_id } }) => | ||
addAlert(taskAlert(pulp_id, t`Sync started`)); | ||
|
||
const onSave = () => | ||
LegacySyncAPI.sync(data) | ||
.then(onSaved) | ||
.catch(handleHttpError(t`Failed to sync role`, null, addAlert)); | ||
|
||
const link = | ||
data.github_user && | ||
data.role_name && | ||
`https://galaxy.ansible.com/ui/standalone/roles/${data.github_user}/${data.role_name}/`; | ||
|
||
const anyErrors = | ||
!!Object.keys(errors || {}).length || requiredFields.some((k) => !data[k]); | ||
|
||
const formSuffix = ( | ||
<> | ||
{link ? ( | ||
<div> | ||
<Trans> | ||
Will sync <ExternalLink href={link}>{link}</ExternalLink> | ||
</Trans> | ||
</div> | ||
) : null} | ||
<ActionGroup key='actions'> | ||
<Button type='submit' isDisabled={anyErrors}> | ||
{t`Sync`} | ||
</Button> | ||
<Button onClick={onCancel} variant='link'> | ||
{t`Cancel`} | ||
</Button> | ||
</ActionGroup> | ||
</> | ||
); | ||
|
||
const updateField = (k, v) => { | ||
setData((data) => ({ ...data, [k]: v })); | ||
|
||
setErrors((errors) => { | ||
const e = { ...errors }; | ||
delete e[k]; | ||
return e; | ||
}); | ||
|
||
if (requiredFields.includes(k) && !v) { | ||
setErrors((errors) => ({ ...errors, [k]: t`Field is required.` })); | ||
} | ||
}; | ||
|
||
return ( | ||
<DataForm | ||
errorMessages={errors || {}} | ||
formFields={formFields} | ||
formSuffix={formSuffix} | ||
model={data} | ||
requiredFields={requiredFields} | ||
updateField={(v, e) => updateField(e.target.id, v)} | ||
onSave={onSave} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { t } from '@lingui/macro'; | ||
import React from 'react'; | ||
import { | ||
AlertList, | ||
AlertType, | ||
BaseHeader, | ||
Main, | ||
RoleSyncForm, | ||
closeAlertMixin, | ||
} from 'src/components'; | ||
import { RouteProps, withRouter } from 'src/utilities'; | ||
|
||
interface RoleState { | ||
alerts: AlertType[]; | ||
} | ||
|
||
class AnsibleRoleSync extends React.Component<RouteProps, RoleState> { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
alerts: [], | ||
}; | ||
} | ||
|
||
private addAlert(alert: AlertType) { | ||
this.setState({ | ||
alerts: [...this.state.alerts, alert], | ||
}); | ||
} | ||
|
||
private get closeAlert() { | ||
return closeAlertMixin('alerts'); | ||
} | ||
|
||
render() { | ||
const { alerts } = this.state; | ||
const addAlert = (alert) => this.addAlert(alert); | ||
const closeAlert = (i) => this.closeAlert(i); | ||
|
||
return ( | ||
<> | ||
<AlertList alerts={alerts} closeAlert={closeAlert} /> | ||
<BaseHeader title={t`Sync role`} /> | ||
<Main> | ||
<section className='body'> | ||
<RoleSyncForm addAlert={addAlert} /> | ||
</section> | ||
</Main> | ||
</> | ||
); | ||
} | ||
} | ||
|
||
export default withRouter(AnsibleRoleSync); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters