|
1 | 1 | import React from "react"; |
2 | | -import {Button, Card, Col, Row} from "antd"; |
| 2 | +import {Button, Card, Col, notification, Row} from "antd"; |
| 3 | +import {ServiceType1} from "../../../../client/models"; |
| 4 | +import {ProjectModel} from "../../../../client/bindings"; |
3 | 5 | import {Link} from "react-router-dom"; |
4 | | -import {buildQuery} from "../../../../classes/utils/url/QueryBuilder"; |
5 | 6 |
|
6 | 7 | interface IProps { |
7 | 8 | repo: { |
8 | 9 | name: string, |
9 | 10 | description: string, |
10 | | - serviceType: string, |
| 11 | + serviceType: ServiceType1, |
11 | 12 | originId: string |
12 | 13 | } |
13 | 14 | } |
14 | 15 |
|
15 | | -interface IState {} |
| 16 | +interface IState { |
| 17 | + importButton: { |
| 18 | + isLoading: boolean, |
| 19 | + label: string |
| 20 | + }, |
| 21 | + importedProject: ProjectModel|null |
| 22 | +} |
16 | 23 |
|
17 | 24 | class ExternalRepoCard extends React.Component<IProps, IState> { |
18 | | - getImportUrl(): string { |
19 | | - return "/user/repository/import?" + buildQuery({ |
20 | | - origin_id: this.props.repo.originId, |
21 | | - service_type: this.props.repo.serviceType |
22 | | - }); |
| 25 | + constructor(props: IProps) { |
| 26 | + super(props); |
| 27 | + this.state = { |
| 28 | + importButton: { |
| 29 | + isLoading: false, |
| 30 | + label: "Import" |
| 31 | + }, |
| 32 | + importedProject: null |
| 33 | + } |
23 | 34 | } |
24 | 35 |
|
| 36 | + /* TODO: get repo status and if already imported - show 'go to project' button */ |
| 37 | + |
25 | 38 | getExternalUrl(): string { |
26 | 39 | let repo = this.props.repo; |
27 | 40 | return `https://${repo.serviceType}.com/${repo.name}`; |
28 | 41 | } |
29 | 42 |
|
| 43 | + sendImportRequest() { |
| 44 | + this.setState({ |
| 45 | + importButton: { |
| 46 | + label: 'Importing...', |
| 47 | + isLoading: true |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + const repo = this.props.repo; |
| 52 | + window.App.apiClient.postImportRepository( |
| 53 | + window.App.apiToken, |
| 54 | + repo.originId, |
| 55 | + repo.serviceType |
| 56 | + ) |
| 57 | + .then((res) => { |
| 58 | + const project = JSON.parse(res._response.bodyAsText).data.project; |
| 59 | + if (!project.base_uri) return; |
| 60 | + notification['success']({ |
| 61 | + message: `Project '${project.name}' imported successfully!` |
| 62 | + }); |
| 63 | + this.setState({ |
| 64 | + importedProject: project |
| 65 | + }); |
| 66 | + }) |
| 67 | + .catch((err) => { |
| 68 | + const project = JSON.parse(err.response.body).metadata.project; |
| 69 | + if (!project.base_uri) return; |
| 70 | + this.setState({ |
| 71 | + importedProject: project |
| 72 | + }); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
30 | 76 | render() { |
| 77 | + let importButton = this.state.importButton; |
| 78 | + const importedProject = this.state.importedProject; |
| 79 | + |
31 | 80 | return <Card className="text-left"> |
32 | 81 | <Row> |
33 | 82 | <Col xs={16}> |
34 | 83 | <b> {this.props.repo.name} </b> <br/> |
35 | | - <b>Description:</b> <p className="inline"> description here </p> <br/> |
36 | | - <b>Status:</b> <p className="inline"> unknown </p> |
| 84 | + <b>Description:</b> <p className="inline"> {this.props.repo.description} </p> <br/> |
37 | 85 | </Col> |
38 | 86 | <Col xs={8} className="text-center"> |
39 | 87 | <Row> |
40 | | - <Link to={this.getImportUrl()}><Button type={"primary"}>Import</Button></Link> |
| 88 | + { |
| 89 | + importedProject ? |
| 90 | + <div> |
| 91 | + <p>Project imported</p> |
| 92 | + <Link to={`/${importedProject!.base_uri}`}> |
| 93 | + <Button type={"primary"}>Open page</Button> |
| 94 | + </Link> |
| 95 | + </div> |
| 96 | + : |
| 97 | + <Button |
| 98 | + onClick={() => this.sendImportRequest()} |
| 99 | + loading={importButton.isLoading} |
| 100 | + type={"primary"} |
| 101 | + >{importButton.label}</Button> |
| 102 | + } |
41 | 103 | </Row> |
42 | 104 | <Row className="margin-sm-top"> |
43 | 105 | <a target="_blank" rel="noopener noreferrer" href={this.getExternalUrl()}><Button |
|
0 commit comments