Skip to content

Commit

Permalink
dashboard plugin update
Browse files Browse the repository at this point in the history
  • Loading branch information
MZshnik committed Aug 14, 2024
1 parent dfab0d7 commit df20902
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 43 deletions.
13 changes: 5 additions & 8 deletions docs/plugins/dashboard/dashboard.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging

from MZscript import BasePlugin, MZClient
from quart import Quart, jsonify, render_template, request
from quart import Quart, jsonify, render_template, redirect, request


class Dashboard(BasePlugin):
Expand Down Expand Up @@ -47,24 +47,21 @@ async def create_code():
self.client.add_event(content["name"], content["code"])
return Quart.response_class()

@self.app.post("/edit-code")
@self.app.get("/edit-code")
async def edit_code_page():
content = await request.json
return await render_template(
"editcode.html",
name=content["name"],
code=content["code"],
type=content["type"]
name=request.args.get("name"),
code=request.args.get("code"),
type=request.args.get("type")
)

@self.app.post("/api/edit-code")
async def edit_code():
content = await request.json
if content["type"] == "command":
print("edit command")
self.client.edit_command(content["name"], content["code"])
else:
print("edit event")
self.client.edit_event(content["name"], content["code"])
return Quart.response_class()

Expand Down
26 changes: 5 additions & 21 deletions docs/plugins/dashboard/static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,9 @@ document.getElementsByClassName("newevn").item(0).onclick = () => {
function editCode(cmd, type) {
const name = cmd.parentElement.getElementsByTagName("h2")[0].textContent;
const code = cmd.parentElement.getElementsByTagName("pre")[0].textContent;

fetch("edit-code", {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: name,
code: code,
type: type
})
})
.then(response => response.text())
.then(data => {
const newPage = document.implementation.createHTMLDocument();
newPage.documentElement.innerHTML = data;
document.documentElement.replaceWith(newPage.documentElement);
})
.catch(error => {
console.error('Error:', error);
});
const queryParams = new URLSearchParams();
queryParams.append('name', name);
queryParams.append('code', code);
queryParams.append('type', type);
window.location.href = `edit-code?${queryParams.toString()}`;
}
3 changes: 2 additions & 1 deletion src/MZscript/Functions/Core/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ async def read_response(response: aiohttp.ClientResponse):
'text': await response.text()
}

# TODO: Add head, options and patch methods
if method == "get":
async with session.get(url, json=payload, headers=headers) as response:
await read_response(response)
Expand All @@ -94,7 +95,7 @@ async def read_response(response: aiohttp.ClientResponse):
async with session.delete(url, json=payload, headers=headers) as response:
await read_response(response)
else:
error_msg = f"$request: Unsupported method \"{method}\"" # TODO: Add head, options and patch methods
error_msg = f"$request: Unsupported method \"{method}\""
if self.handler.debug_console:
raise ValueError(error_msg)
else:
Expand Down
20 changes: 7 additions & 13 deletions src/MZscript/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,8 @@ def add_command(self, name: str, code: str):
"""
self.user_commands.append([name, code])
self.user_command_names.append(name)
chunks = asyncio.run(self.funcs.get_chunks(name))
for chunk in chunks:
if chunk.startswith("$"):
self.exec_on_start.append(len(self.user_commands) - 1)
break
if '$' in name:
self.exec_on_start.append(len(self.user_commands) - 1)

def edit_command(self, name: str, code: str):
"""
Expand All @@ -121,14 +118,11 @@ def edit_command(self, name: str, code: str):
code_index = self.user_command_names.index(name)
self.user_commands[code_index] = [name, code]
self.user_command_names[code_index] = name
chunks = asyncio.run(self.funcs.get_chunks(name))
for chunk in chunks:
if chunk.startswith("$"):
if code_index in self.exec_on_start:
self.exec_on_start.remove(code_index)
else:
self.exec_on_start.append(len(self.user_commands) - 1)
break
if '$' in name:
if code_index in self.exec_on_start:
self.exec_on_start.remove(code_index)
else:
self.exec_on_start.append(len(self.user_commands) - 1)

def add_slash( # TODO: Make full support of slash commands and options
self, name: str, code: str,
Expand Down

0 comments on commit df20902

Please sign in to comment.