|
| 1 | +const React = require("react"); |
| 2 | +const ReactDOM = require("react-dom"); |
| 3 | + |
| 4 | +import MarkdownEditor from "./markdown-editor"; |
| 5 | + |
| 6 | +function DatabaseTagRow({name, data, releases, setStatusMessage, setStatusMessageColour}) { |
| 7 | + // This is the tag name currently shown in the front end |
| 8 | + const [tagName, setTagName] = React.useState(name); |
| 9 | + |
| 10 | + // This is the tag name currently saved in the database on the server |
| 11 | + const [savedTagName, setSavedTagName] = React.useState(name); |
| 12 | + |
| 13 | + // Delete the tag |
| 14 | + function deleteTag() { |
| 15 | + fetch(releases ? "/x/deleterelease/" : "/x/deletetag/" , { |
| 16 | + method: "post", |
| 17 | + headers: { |
| 18 | + "Content-Type": "application/x-www-form-urlencoded" |
| 19 | + }, |
| 20 | + body: new URLSearchParams({ |
| 21 | + "tag": savedTagName, |
| 22 | + "dbname": meta.database, |
| 23 | + "username": meta.owner |
| 24 | + }), |
| 25 | + }).then((response) => { |
| 26 | + if (!response.ok) { |
| 27 | + return Promise.reject(response); |
| 28 | + } |
| 29 | + |
| 30 | + window.location = "/" + (releases ? "releases" : "tags") + "/" + meta.owner + "/" + meta.database; |
| 31 | + }) |
| 32 | + .catch((error) => { |
| 33 | + // The delete failed, so display an error message |
| 34 | + setStatusMessageColour("red"); |
| 35 | + setStatusMessage("Error: Something went wrong when trying to delete."); |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + // Send the update details to the server |
| 40 | + function updateTag() { |
| 41 | + let newDesc = document.getElementById(savedTagName + "_desc").value; |
| 42 | + |
| 43 | + fetch(releases ? "/x/updaterelease/" : "/x/updatetag/" , { |
| 44 | + method: "post", |
| 45 | + headers: { |
| 46 | + "Content-Type": "application/x-www-form-urlencoded" |
| 47 | + }, |
| 48 | + body: new URLSearchParams({ |
| 49 | + "tag": savedTagName, |
| 50 | + "dbname": meta.database, |
| 51 | + "username": meta.owner, |
| 52 | + "newmsg": newDesc, |
| 53 | + "newtag": tagName, |
| 54 | + }), |
| 55 | + }).then((response) => { |
| 56 | + if (!response.ok) { |
| 57 | + return Promise.reject(response); |
| 58 | + } |
| 59 | + |
| 60 | + setSavedTagName(tagName); |
| 61 | + setStatusMessageColour("green"); |
| 62 | + setStatusMessage(releases ? "Release updated" : "Tag updated"); |
| 63 | + }) |
| 64 | + .catch((error) => { |
| 65 | + // The delete failed, so display an error message |
| 66 | + setStatusMessageColour("red"); |
| 67 | + setStatusMessage(releases ? "Release update failed" : "Tag update failed"); |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + let actionsCol = null; |
| 72 | + if (meta.owner === authInfo.loggedInUser) { |
| 73 | + actionsCol = ( |
| 74 | + <td> |
| 75 | + <p><button className="btn btn-primary" onClick={() => updateTag()} data-cy="updatebtn">Update</button></p> |
| 76 | + <p><button className="btn btn-danger" onClick={() => deleteTag()} data-cy="delbtn">Delete</button></p> |
| 77 | + </td> |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + let nameCol = null; |
| 82 | + if (meta.owner === authInfo.loggedInUser) { |
| 83 | + nameCol = ( |
| 84 | + <td> |
| 85 | + <input name={savedTagName + "_name"} id={savedTagName + "_name"} size="20" maxlength="20" value={tagName} onChange={(e) => setTagName(e.target.value)} data-cy="nameinput" /> |
| 86 | + </td> |
| 87 | + ); |
| 88 | + } else { |
| 89 | + nameCol = ( |
| 90 | + <td> |
| 91 | + <a className="blackLink" href={"/" + meta.owner + "/" + meta.database + (releases ? "?release=" : "?tag=") + savedTagName}>{tagName}</a> |
| 92 | + </td> |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + return ( |
| 97 | + <tr> |
| 98 | + <td> |
| 99 | + {releases ? <> |
| 100 | + <a href={"/x/download/" + meta.owner + "/" + meta.database + "?commit=" + data.commit} className="btn btn-success">Download</a> |
| 101 | + <p>{Math.round(data.size / 1024).toLocaleString()} KB</p> |
| 102 | + </> : null} |
| 103 | + </td> |
| 104 | + {actionsCol} |
| 105 | + {nameCol} |
| 106 | + <td> |
| 107 | + <MarkdownEditor editorId={savedTagName + "_desc"} rows={10} placeholder={"A description for this " + (releases ? "release" : "tag")} defaultIndex={1} initialValue={data.description} viewOnly={meta.owner !== authInfo.loggedInUser} /> |
| 108 | + </td> |
| 109 | + <td> |
| 110 | + {data.avatar_url !== "" ? <img src={data.avatar_url} height="28" width="28" style={{border: "1px solid #8c8c8c"}} /> : null} |
| 111 | + <a className="blackLink" href={"/" + data.tagger_user_name} data-cy="taggerlnk">{data.tagger_display_name}</a> |
| 112 | + </td> |
| 113 | + <td> |
| 114 | + <span title={new Date(data.date).toLocaleString()}>{getTimePeriod(data.date, false)}</span> |
| 115 | + </td> |
| 116 | + <td> |
| 117 | + <a className="blackLink" href={"/" + meta.owner + "/" + meta.database + "?commit=" + data.commit} data-cy="commitlnk">{data.commit.substring(0, 8)}</a> |
| 118 | + </td> |
| 119 | + </tr> |
| 120 | + ); |
| 121 | +} |
| 122 | + |
| 123 | +export default function DatabaseTags({releases}) { |
| 124 | + const [statusMessage, setStatusMessage] = React.useState(""); |
| 125 | + const [statusMessageColour, setStatusMessageColour] = React.useState(""); |
| 126 | + |
| 127 | + let rows = []; |
| 128 | + for (const [name, data] of Object.entries(tagsData)) { |
| 129 | + rows.push(<DatabaseTagRow name={name} data={data} releases={releases} setStatusMessage={setStatusMessage} setStatusMessageColour={setStatusMessageColour} />); |
| 130 | + } |
| 131 | + |
| 132 | + if (rows.length === 0) { |
| 133 | + return <h3 data-cy="notagstxt" style={{textAlign: "center"}}>This database does not have any {releases ? "releases" : "tags"} yet</h3>; |
| 134 | + } |
| 135 | + |
| 136 | + return (<> |
| 137 | + {statusMessage !== "" ? ( |
| 138 | + <div className="row"> |
| 139 | + <div className="col-md-12"> |
| 140 | + <div style={{textAlign: "center", paddingBottom: "8px"}}> |
| 141 | + <h4 style={{color: statusMessageColour}}>{statusMessage}</h4> |
| 142 | + </div> |
| 143 | + </div> |
| 144 | + </div> |
| 145 | + ) : null} |
| 146 | + <table id="contents" className="table table-striped table-responsive"> |
| 147 | + <thead> |
| 148 | + <tr> |
| 149 | + <th></th> |
| 150 | + {meta.owner === authInfo.loggedInUser ? <th>Actions</th> : null} |
| 151 | + <th>Name</th><th>Description</th><th>Creator</th><th>Creation date</th><th>Commit ID</th> |
| 152 | + </tr> |
| 153 | + </thead> |
| 154 | + <tbody> |
| 155 | + {rows} |
| 156 | + </tbody> |
| 157 | + </table> |
| 158 | + </>); |
| 159 | +} |
0 commit comments