|
| 1 | +const React = require("react"); |
| 2 | +const ReactDOM = require("react-dom"); |
| 3 | + |
| 4 | +import MarkdownEditor from "./markdown-editor"; |
| 5 | +import Select from "react-dropdown-select"; |
| 6 | +import CommitList from "./commit-list"; |
| 7 | + |
| 8 | +export default function DiscussionCreateMr() { |
| 9 | + const [statusMessage, setStatusMessage] = React.useState(""); |
| 10 | + const [statusMessageColour, setStatusMessageColour] = React.useState(""); |
| 11 | + |
| 12 | + const [title, setTitle] = React.useState(""); |
| 13 | + const [sourceDbName, setSourceDbName] = React.useState(createMrData.sourceDbName); |
| 14 | + const [sourceDbOwner, setSourceDbOwner] = React.useState(createMrData.sourceDbOwner); |
| 15 | + const [sourceBranch, setSourceBranch] = React.useState(createMrData.sourceDbDefaultBranch); |
| 16 | + const [sourceBranches, setSourceBranches] = React.useState(createMrData.sourceBranches); |
| 17 | + const [destDbName, setDestDbName] = React.useState(createMrData.destDbName); |
| 18 | + const [destDbOwner, setDestDbOwner] = React.useState(createMrData.destDbOwner); |
| 19 | + const [destBranch, setDestBranch] = React.useState(createMrData.destDbDefaultBranch); |
| 20 | + const [destBranches, setDestBranches] = React.useState(createMrData.destBranches); |
| 21 | + const [commitList, setCommitList] = React.useState(createMrData.commitList); |
| 22 | + |
| 23 | + // Updates the commit list showing the difference between source and destination databases |
| 24 | + function updateCommitList() { |
| 25 | + fetch("/x/diffcommitlist/", { |
| 26 | + method: "post", |
| 27 | + headers: { |
| 28 | + "Content-Type": "application/x-www-form-urlencoded" |
| 29 | + }, |
| 30 | + body: new URLSearchParams({ |
| 31 | + "destbranch": encodeURIComponent(destBranch), |
| 32 | + "destdbname": encodeURIComponent(destDbName), |
| 33 | + "destowner": encodeURIComponent(destDbOwner), |
| 34 | + "sourcebranch": encodeURIComponent(sourceBranch), |
| 35 | + "sourcedbname": encodeURIComponent(sourceDbName), |
| 36 | + "sourceowner": encodeURIComponent(sourceDbOwner), |
| 37 | + }), |
| 38 | + }).then((response) => { |
| 39 | + if (!response.ok) { |
| 40 | + return Promise.reject(response); |
| 41 | + } |
| 42 | + |
| 43 | + response.json().then(data => { |
| 44 | + // Retrieving the commit list succeeded, so update the displayed commit list |
| 45 | + setCommitList(data.commit_list); |
| 46 | + |
| 47 | + setStatusMessageColour("green"); |
| 48 | + setStatusMessage(""); |
| 49 | + }); |
| 50 | + }) |
| 51 | + .catch((error) => { |
| 52 | + // Retrieving the commit list failed, so clear out the existing displayed list and display a message |
| 53 | + // about it |
| 54 | + setCommitList([]); |
| 55 | + setStatusMessageColour("orange"); |
| 56 | + setStatusMessage("The selected source and destination can't be merged. Please choose a different source and destination."); |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + // Update name of the source or destination branch in the drop down selector |
| 61 | + function changeBranch(sourceDest, newBranch) { |
| 62 | + if (sourceDest === "source") { |
| 63 | + setSourceBranch(newBranch); |
| 64 | + } else if(sourceDest === "dest") { |
| 65 | + setDestBranch(newBranch); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Updates the source or destination database name and branch list in the drop down selectors |
| 70 | + function changeDb(sourceDest, newRow) { |
| 71 | + // Retrieve the branch list for the newly selected database |
| 72 | + fetch("/x/branchnames/", { |
| 73 | + method: "post", |
| 74 | + headers: { |
| 75 | + "Content-Type": "application/x-www-form-urlencoded" |
| 76 | + }, |
| 77 | + body: new URLSearchParams({ |
| 78 | + "dbname": newRow.database_name, |
| 79 | + "username": newRow.database_owner, |
| 80 | + }), |
| 81 | + }).then((response) => { |
| 82 | + if (!response.ok) { |
| 83 | + return Promise.reject(response); |
| 84 | + } |
| 85 | + |
| 86 | + response.json().then(data => { |
| 87 | + // Clear any previous error message |
| 88 | + setStatusMessageColour("green"); |
| 89 | + setStatusMessage(""); |
| 90 | + |
| 91 | + // Update the values used when sending the creation request and update the branch list |
| 92 | + if (sourceDest === "source") { |
| 93 | + setSourceDbName(newRow.database_name); |
| 94 | + setSourceDbOwner(newRow.database_owner); |
| 95 | + |
| 96 | + setSourceBranches(data.branches); |
| 97 | + setSourceBranch(data.default_branch); |
| 98 | + } else if(sourceDest === "dest") { |
| 99 | + setDestDbName(newRow.database_name); |
| 100 | + setDestDbOwner(newRow.database_owner); |
| 101 | + |
| 102 | + setDestBranches(data.branches); |
| 103 | + setDestBranch(data.default_branch); |
| 104 | + } |
| 105 | + }); |
| 106 | + }) |
| 107 | + .catch((error) => { |
| 108 | + // Retrieving the branch names failed, so display an error message |
| 109 | + setStatusMessageColour("red"); |
| 110 | + setStatusMessage("Retrieving branch names for the database failed."); |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + // Handler for the cancel button. Just bounces back to the database page |
| 115 | + function cancelCreate() { |
| 116 | + window.location = "/" + meta.owner + "/" + meta.database; |
| 117 | + } |
| 118 | + |
| 119 | + // Sends the merge request creation details, and if successful then bounces to the newly created MR for it |
| 120 | + function createMR() { |
| 121 | + if (authInfo.loggedInUser === "") { |
| 122 | + // User needs to be logged in |
| 123 | + lock.show(); |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + // Send the MR creation request |
| 128 | + fetch("/x/createmerge/", { |
| 129 | + method: "post", |
| 130 | + headers: { |
| 131 | + "Content-Type": "application/x-www-form-urlencoded" |
| 132 | + }, |
| 133 | + body: new URLSearchParams({ |
| 134 | + "desc": encodeURIComponent(document.getElementById("desc").value), |
| 135 | + "destbranch": encodeURIComponent(destBranch), |
| 136 | + "destdbname": encodeURIComponent(destDbName), |
| 137 | + "destowner": encodeURIComponent(destDbOwner), |
| 138 | + "sourcebranch": encodeURIComponent(sourceBranch), |
| 139 | + "sourcedbname": encodeURIComponent(sourceDbName), |
| 140 | + "sourceowner": encodeURIComponent(sourceDbOwner), |
| 141 | + "title": encodeURIComponent(title), |
| 142 | + }), |
| 143 | + }).then((response) => { |
| 144 | + if (!response.ok) { |
| 145 | + return Promise.reject(response); |
| 146 | + } |
| 147 | + |
| 148 | + response.json().then(data => { |
| 149 | + // MR creation succeeded. The response should include the MR # we'll bounce to |
| 150 | + window.location = "/merge/" + destDbOwner + "/" + destDbName + "?id=" + data.mr_id; |
| 151 | + }); |
| 152 | + }) |
| 153 | + .catch((error) => { |
| 154 | + // Creating the MR failed, so display an error message |
| 155 | + error.text().then(text => { |
| 156 | + setStatusMessageColour("red"); |
| 157 | + setStatusMessage("Merge Request creation failed: " + text); |
| 158 | + }); |
| 159 | + }); |
| 160 | + }; |
| 161 | + |
| 162 | + // Update commit list when branches change |
| 163 | + React.useEffect(() => { |
| 164 | + updateCommitList(); |
| 165 | + }, [sourceBranch, destBranch]); |
| 166 | + |
| 167 | + const dbListData = createMrData.forkList.map(f => new Object({name: f.database_owner + "/" + f.database_name, database_owner: f.database_owner, database_name: f.database_name})); |
| 168 | + const sourceBranchListData = sourceBranches.map(b => new Object({name: b})); |
| 169 | + const destBranchListData = destBranches.map(b => new Object({name: b})); |
| 170 | + |
| 171 | + return (<> |
| 172 | + <h3 className="text-center">Create a Merge Request</h3> |
| 173 | + {statusMessage !== "" ? ( |
| 174 | + <div className="row"> |
| 175 | + <div className="col-md-12 text-center"> |
| 176 | + <div style={{paddingBottom: "1em"}}> |
| 177 | + <h4 style={{color: statusMessageColour}}>{statusMessage}</h4> |
| 178 | + </div> |
| 179 | + </div> |
| 180 | + </div> |
| 181 | + ) : null} |
| 182 | + <form> |
| 183 | + <div className="form-group"> |
| 184 | + <label for="title">Title</label> |
| 185 | + <input type="text" className="form-control" id="title" placeholder="Please fill in a title for the new merge request" maxlength={80} value={title} onChange={e => setTitle(e.target.value)} required /> |
| 186 | + </div> |
| 187 | + <div className="form-group"> |
| 188 | + <label for="sourcedb">Source database</label> |
| 189 | + <Select name="sourcedb" required={true} labelField="name" valueField="name" onChange={(values) => changeDb("source", values[0])} options={dbListData} values={[{name: sourceDbOwner + "/" + sourceDbName}]} /> |
| 190 | + <p className="help-block">Where the new data is coming from</p> |
| 191 | + </div> |
| 192 | + <div className="form-group"> |
| 193 | + <label for="sourcebranch">Source branch</label> |
| 194 | + <Select name="sourcebranch" required={true} labelField="name" valueField="name" onChange={(values) => setSourceBranch(values[0].name)} options={sourceBranchListData} values={[{name: sourceBranch}]} /> |
| 195 | + <p className="help-block">The branch in the source database to use</p> |
| 196 | + </div> |
| 197 | + <div className="form-group"> |
| 198 | + <label for="destdb">Destination database</label> |
| 199 | + <Select name="destdb" required={true} labelField="name" valueField="name" onChange={(values) => changeDb("dest", values[0])} options={dbListData} values={[{name: destDbOwner + "/" + destDbName}]} /> |
| 200 | + <p className="help-block">Where you'd like the data merged into</p> |
| 201 | + </div> |
| 202 | + <div className="form-group"> |
| 203 | + <label for="destbranch">Destination branch</label> |
| 204 | + <Select name="destbranch" required={true} labelField="name" valueField="name" onChange={(values) => setDestBranch(values[0].name)} options={destBranchListData} values={[{name: destBranch}]} /> |
| 205 | + <p className="help-block">The target branch in the destination database</p> |
| 206 | + </div> |
| 207 | + <div className="form-group"> |
| 208 | + <label for="desc">Description</label> |
| 209 | + <MarkdownEditor editorId="desc" rows={10} placeholder="Please add a summary for this merge request, describing what the new or changed data is for" /> |
| 210 | + <p className="help-block">The purpose of this merge request</p> |
| 211 | + </div> |
| 212 | + <button type="button" className="btn btn-success" onClick={() => createMR()}>Create</button> |
| 213 | + <button type="button" className="btn btn-default" onClick={() => cancelCreate()}>Cancel</button> |
| 214 | + </form> |
| 215 | + <div className="panel panel-default" style={{marginTop: "1em"}}> |
| 216 | + <div className="panel-heading"> |
| 217 | + Changes between the source and destination |
| 218 | + </div> |
| 219 | + <CommitList commits={commitList} owner={sourceDbOwner} database={sourceDbName} /> |
| 220 | + </div> |
| 221 | + </>); |
| 222 | +} |
0 commit comments