added: toggle-button will work now

This commit is contained in:
knoxfighter 2017-08-16 19:12:45 +09:00
parent 67436983df
commit 02a85337c2
8 changed files with 125 additions and 55 deletions

View File

@ -182,6 +182,34 @@ func ModPortalInstallHandler(w http.ResponseWriter, r *http.Request) {
}
}
func ToggleModHandler(w http.ResponseWriter, r *http.Request) {
var err error
resp := JSONResponse{
Success: false,
}
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
//Get Data out of the request
mod_name := r.FormValue("mod_name")
resp.Data, err = toggleMod(mod_name)
if err != nil {
resp.Data = fmt.Sprintf("Error in installMod: %s", err)
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("Error in installMod: %s", err)
}
return
}
resp.Success = true
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Printf("Error in ModPortalInstallHandler: %s", err)
}
}
// Toggles mod passed in through mod variable
// Updates mod-list.json file to toggle the enabled status of mods
/*func ToggleMod(w http.ResponseWriter, r *http.Request) {

View File

@ -22,8 +22,8 @@ type ModsList struct {
}
// List mods installed in the factorio/mods directory
func listInstalledMods(modDir string) (ModsList, error) {
file, err := ioutil.ReadFile(modDir + "/mod-list.json")
func listInstalledMods() (ModsList, error) {
file, err := ioutil.ReadFile(config.FactorioModsDir + "/mod-list.json")
if err != nil {
log.Println(err.Error())
@ -48,6 +48,8 @@ type ModInfo struct {
Version string `json:"version"`
Title string `json:"title"`
Author string `json:"author"`
FileName string `json:"file_name"`
Enabled bool `json:"enabled"`
}
func listInstalledModsByFolder() (ModInfoList, error) {
//scan ModFolder
@ -85,6 +87,8 @@ func listInstalledModsByFolder() (ModInfoList, error) {
return err
}
mod_info.FileName = info.Name()
mod_info.FileName = info.Name()
result.Mods = append(result.Mods, mod_info)
break
@ -99,9 +103,53 @@ func listInstalledModsByFolder() (ModInfoList, error) {
return ModInfoList{}, err_o
}
mod_list_by_json, err_o := listInstalledMods()
if err_o != nil {
return ModInfoList{}, err_o
}
for _, json_mod := range mod_list_by_json.Mods {
for result_index, result_mod := range result.Mods {
if result_mod.Name == json_mod.Name {
result.Mods[result_index].Enabled = json_mod.Enabled
break
}
}
}
return result, nil
}
func toggleMod(mod_name string)([]ModInfo, error) {
var err error
mod_list, err := listInstalledMods()
for index, mod := range mod_list.Mods {
if mod.Name == mod_name {
mod_list.Mods[index].Enabled = !mod_list.Mods[index].Enabled
break
}
}
if err != nil {
return nil, err
}
//build new json
new_json, _ := json.Marshal(mod_list)
ioutil.WriteFile(config.FactorioModsDir + "/mod-list.json", new_json, 0664)
mod_info_list, err := listInstalledModsByFolder()
if err != nil {
log.Fatal(err)
return nil, err
}
return mod_info_list.Mods, nil
}
type LoginErrorResponse struct {
Message string `json:"message"`
@ -221,7 +269,7 @@ func installMod(username string, userKey string, url string, filename string, mo
}
file.Close()
mod_list, err := listInstalledMods(config.FactorioModsDir)
mod_list, err := listInstalledMods()
if err != nil {
return nil, err, 500

View File

@ -173,6 +173,11 @@ var apiRoutes = Routes{
"POST",
"/mods/install",
ModPortalInstallHandler,
}, {
"ToggleMod",
"POST",
"/mods/toggle",
ToggleModHandler,
}, /*{
"ListMods",
"GET",

View File

@ -24,7 +24,6 @@ class InstalledMods extends React.Component {
contentType: false,
success: (data) => {
var response = JSON.parse(data)
console.log(response.success);
if (response.success === true) {
this.updateInstalledMods();
}

View File

@ -1,46 +0,0 @@
import React from 'react';
import Mod from './Mod.jsx'
class ModList extends React.Component {
render() {
return(
<div className="box">
<div className="box-header">
<h3 className="box-title">Manage Mods</h3>
</div>
<div className="box-body">
<div className="table-responsive">
<table className="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Status</th>
<th>Toggle Status</th>
</tr>
</thead>
<tbody>
{this.props.listMods.map ( (mod, i) => {
return(
<Mod
key={i}
mod={mod}
{...this.props}
/>
)
})}
</tbody>
</table>
</div>
</div>
</div>
)
}
}
ModList.propTypes = {
listMods: React.PropTypes.array.isRequired,
toggleMod: React.PropTypes.func.isRequired
}
export default ModList

View File

@ -8,7 +8,9 @@ class Mod extends React.Component {
this.modStatus = <span className="label label-success">Enabled</span>
}
return(
<tr>
<tr data-mod-name={this.props.mod.name}
data-file-name={this.props.mod.file_name}
>
<td>{this.props.mod.title}</td>
<td>{this.modStatus}</td>
<td>
@ -16,8 +18,7 @@ class Mod extends React.Component {
ref='modName'
type='submit'
value='Toggle'
name={this.props.mod.name}
//TODO toggle and remove mod
onClick={this.props.toggleMod}
/>
</td>
</tr>
@ -26,7 +27,8 @@ class Mod extends React.Component {
}
Mod.propTypes = {
mod: React.PropTypes.object.isRequired
mod: React.PropTypes.object.isRequired,
toggleMod: React.PropTypes.func.isRequired
}
export default Mod

View File

@ -40,6 +40,7 @@ class ModOverview extends React.Component {
}
render() {
console.log(this.props.installedMods)
return(
<div className="box">
<div className="box-header">
@ -95,7 +96,8 @@ class ModOverview extends React.Component {
ModOverview.propTypes = {
installedMods: React.PropTypes.array.isRequired,
submitFactorioLogin: React.PropTypes.func.isRequired
submitFactorioLogin: React.PropTypes.func.isRequired,
toggleMod: React.PropTypes.func.isRequired
};
export default ModOverview;

View File

@ -12,6 +12,7 @@ class ModsContent extends React.Component {
this.handlerFactorioLogin = this.handlerFactorioLogin.bind(this);
this.loadDownloadList = this.loadDownloadList.bind(this);
this.swalSubmitHandler = this.swalSubmitHandler.bind(this);
this.toggleModHandler = this.toggleModHandler.bind(this);
this.state = {
username: "",
@ -218,6 +219,36 @@ class ModsContent extends React.Component {
// });
};
toggleModHandler(e) {
e.preventDefault();
console.log(e.target);
let $button = $(e.target);
let $row = $button.parents("tr");
let mod_name = $row.data("mod-name");
$.ajax({
url: "/api/mods/toggle",
method: "POST",
data: {
mod_name: mod_name
},
dataType: "JSON",
success: (data) => {
this.setState({
installedMods: data.data
});
},
error: (jqXHR, status, err) => {
console.log('api/mods/details', status, err.toString());
swal({
title: "Toggle Mod went wrong",
text: err.toString(),
type: "error"
});
}
});
}
render() {
return(
<div className="content-wrapper">
@ -237,6 +268,7 @@ class ModsContent extends React.Component {
{...this.state}
loadDownloadList={this.loadDownloadList}
submitFactorioLogin={this.handlerFactorioLogin}
toggleMod={this.toggleModHandler}
/>
</section>
</div>