From 40b5f01e4a41d6002fcb534b109f5f6665d1cf9f Mon Sep 17 00:00:00 2001 From: knoxfighter Date: Mon, 25 Sep 2017 18:43:50 +0200 Subject: [PATCH] mods inside modpacks can now be deleted --- src/handlers.go | 39 ++++++ src/routes.go | 7 +- ui/App/components/Mods/InstalledMods.jsx | 125 ------------------ ui/App/components/Mods/ModUpload.jsx | 1 - .../components/Mods/packs/ModPackOverview.jsx | 48 ++++++- 5 files changed, 92 insertions(+), 128 deletions(-) delete mode 100644 ui/App/components/Mods/InstalledMods.jsx diff --git a/src/handlers.go b/src/handlers.go index 5499def..0739eca 100644 --- a/src/handlers.go +++ b/src/handlers.go @@ -13,6 +13,7 @@ import ( "time" "github.com/gorilla/mux" "archive/zip" + "errors" ) type JSONResponse struct { @@ -544,6 +545,44 @@ func ModPackToggleModHandler(w http.ResponseWriter, r *http.Request) { } } +func ModPackDeleteModHandler(w http.ResponseWriter, r *http.Request) { + var err error + resp := JSONResponse{ + Success: false, + } + + w.Header().Set("Content-Type", "application/json;charset=UTF-8") + + mod_name := r.FormValue("mod_name") + mod_pack_name := r.FormValue("mod_pack_name") + + mod_pack_map, err := newModPackMap() + if err == nil { + if mod_pack_map.checkModPackExists(mod_pack_name) { + err = mod_pack_map[mod_pack_name].Mods.deleteMod(mod_name) + } else { + err = errors.New("ModPack " + mod_pack_name + " does not exist") + } + } + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + resp.Data = fmt.Sprintf("Error loading modpack file: %s", err) + if err := json.NewEncoder(w).Encode(resp); err != nil { + log.Printf("Error loading modpack: %s", err) + } + return + } + + resp.Data = mod_pack_map.listInstalledModPacks() + resp.Success = true + + if err := json.NewEncoder(w).Encode(resp); err != nil { + log.Printf("Error creating loading modpack response: %s", err) + } + + return +} + // Lists all save files in the factorio/saves directory func ListSaves(w http.ResponseWriter, r *http.Request) { var err error diff --git a/src/routes.go b/src/routes.go index a512c9e..c68ff0f 100644 --- a/src/routes.go +++ b/src/routes.go @@ -308,7 +308,12 @@ var apiRoutes = Routes{ "POST", "/mods/packs/mod/toggle", ModPackToggleModHandler, - } ,{ + }, { + "ModPackDeleteMod", + "POST", + "/mods/packs/mod/delete", + ModPackDeleteModHandler, + } ,{ "GetServerSettings", "GET", "/settings", diff --git a/ui/App/components/Mods/InstalledMods.jsx b/ui/App/components/Mods/InstalledMods.jsx deleted file mode 100644 index 88cd2f4..0000000 --- a/ui/App/components/Mods/InstalledMods.jsx +++ /dev/null @@ -1,125 +0,0 @@ -import React from 'react'; -import swal from 'sweetalert'; - -class InstalledMods extends React.Component { - componentDidMount() { - this.uploadFile = this.uploadFile.bind(this); - this.removeMod = this.removeMod.bind(this); - } - - updateInstalledMods() { - this.props.loadInstalledModList(); - } - - uploadFile(e) { - e.preventDefault(); - var fd = new FormData(); - fd.append('modfile', this.refs.file.files[0]); - - $.ajax({ - url: "/api/mods/upload", - type: "POST", - data: fd, - processData: false, - contentType: false, - success: (data) => { - var response = JSON.parse(data) - if (response.success === true) { - this.updateInstalledMods(); - } - } - }); - } - - removeMod(i) { - var self = this; - swal({ - title: "Are you sure?", - text: "Save: " + self.props.installedMods[i] + " will be deleted", - type: "warning", - showCancelButton: true, - confirmButtonColor: "#DD6B55", - confirmButtonText: "Yes, delete it!", - closeOnConfirm: false - }, - () => { - $.ajax({ - url: "/api/mods/rm/" + self.props.installedMods[i], - success: (resp) => { - if (resp.success === true) { - swal("Deleted!", resp.data, "success"); - self.updateInstalledMods(); - } - } - }) - }); - } - - render() { - return( -
-
-

Installed Mods

-
- -
-

Upload Mod

-
-
- - -
-
- -
-
- -
- - - - - - - - - - {this.props.installedMods.map ( (mod, i) => { - var saveLocation = "/api/mods/dl/" + mod; - return( - - - - - - ) - })} - -
Mod NameDownloadDelete
- {mod} - - Download - - -
-
-
-
- ) - } -} - -InstalledMods.propTypes = { - installedMods: React.PropTypes.array.isRequired, - loadInstalledModList: React.PropTypes.func.isRequired -} - -export default InstalledMods diff --git a/ui/App/components/Mods/ModUpload.jsx b/ui/App/components/Mods/ModUpload.jsx index ae744c1..d3342d5 100644 --- a/ui/App/components/Mods/ModUpload.jsx +++ b/ui/App/components/Mods/ModUpload.jsx @@ -24,7 +24,6 @@ class ModUpload extends React.Component { The mods you upload will override every mod, that is already uploaded!
Uploaded mods will be treated same, as if you install mods with the mod-portal-api.

Only zip-files are allowed to upload !! - {/*TODO make better english*/} diff --git a/ui/App/components/Mods/packs/ModPackOverview.jsx b/ui/App/components/Mods/packs/ModPackOverview.jsx index 51e4993..9a293ac 100644 --- a/ui/App/components/Mods/packs/ModPackOverview.jsx +++ b/ui/App/components/Mods/packs/ModPackOverview.jsx @@ -11,6 +11,7 @@ class ModPackOverview extends React.Component { this.deleteModPack = this.deleteModPack.bind(this); this.loadModPack = this.loadModPack.bind(this); this.modPackToggleModHandler = this.modPackToggleModHandler.bind(this); + this.modPackDeleteModHandler = this.modPackDeleteModHandler.bind(this); this.state = { listPacks: [] @@ -213,6 +214,51 @@ class ModPackOverview extends React.Component { }); } + modPackDeleteModHandler(e) { + e.preventDefault(); + let $button = $(e.target); + let $row = $button.parents("tr"); + let mod_name = $row.data("mod-name"); + let mod_pack = $row.parents(".single-modpack").find("h3").html();7 + let class_this = this; + + swal({ + title: "Delete Mod?", + text: "This will delete the mod forever", + type: "info", + showCancelButton: true, + closeOnConfirm: false, + confirmButtonText: "Delete it!", + cancelButtonText: "Close", + showLoaderOnConfirm: true, + confirmButtonColor: "#DD6B55", + }, function () { + $.ajax({ + url: "/api/mods/packs/mod/delete", + method: "POST", + data: { + mod_name: mod_name, + mod_pack_name: mod_pack + }, + dataType: "JSON", + success: (data) => { + swal("Delete of mod " + mod_name + " inside modPack " + mod_pack + " successful", "", "success"); + class_this.setState({ + listPacks: data.data.mod_packs + }); + }, + error: (jqXHR, status, err) => { + console.log('api/mods/packs/mod/delete', status, err.toString()); + swal({ + title: "Delete Mod went wrong", + text: jqXHR.responseJSON.data, + type: "error" + }); + } + }); + }); + } + test() { console.log("test called"); } @@ -246,7 +292,7 @@ class ModPackOverview extends React.Component {