Skip to content

Commit

Permalink
do not format advanced json in admin panel
Browse files Browse the repository at this point in the history
  • Loading branch information
Kostya Bats authored and kbats183 committed Mar 23, 2024
1 parent e0113e4 commit a074863
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
7 changes: 6 additions & 1 deletion src/backend/src/main/kotlin/org/icpclive/admin/Routing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import org.icpclive.cds.tunning.toAdvancedProperties
import org.icpclive.data.Controllers
import org.icpclive.data.DataBus
import org.icpclive.util.sendFlow
import java.io.IOException
import java.nio.file.Files

fun Route.configureAdminApiRouting() {
Expand Down Expand Up @@ -98,7 +99,11 @@ fun Route.configureAdminApiRouting() {

route("/advancedJson") {
get {
call.respondFile(Config.cdsSettings.advancedJsonPath.toFile())
try {
call.respondFile(Config.cdsSettings.advancedJsonPath.toFile())
} catch (e: IOException) {
call.respondText("{}")
}
}
post {
call.adminApiAction {
Expand Down
24 changes: 12 additions & 12 deletions src/frontend/admin/src/components/AdvancedJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import { createApiGet, createApiPost } from "../utils";
import JsonEditor from "./atoms/JsonEditor";
import Box from "@mui/material/Box";

const SCHEMA_URL = SCHEMAS_LOCATION + "/advanced.schema.json";
const API_URL = BASE_URL_BACKEND + "/advancedJson";

function AdvancedJson() {
const { enqueueSnackbar } = useSnackbar();
const errorHandler = errorHandlerWithSnackbar(enqueueSnackbar);

const schemaUrl = SCHEMAS_LOCATION + "/advanced.schema.json";
const apiUrl = BASE_URL_BACKEND + "/advancedJson";

const schemaGet = createApiGet(schemaUrl);
const apiGet = createApiGet(apiUrl);
const apiPost = createApiPost(apiUrl);
const schemaGet = createApiGet(SCHEMA_URL);
const apiGet = createApiGet(API_URL);
const apiPost = createApiPost(API_URL);

const [schema, setSchema] = useState();
const [content, setContent] = useState();
Expand All @@ -26,16 +26,16 @@ function AdvancedJson() {
schemaGet("")
.then(data => setSchema(data))
.catch(errorHandler("Failed to load advanced json schema"));
}, [schemaUrl]);
}, []);

useEffect(() => {
apiGet("")
apiGet("", undefined, true)
.then(data => setContent(data))
.catch(errorHandler("Failed to load advanced json data"));
}, [apiUrl]);
}, []);

const onSubmit = () => {
apiPost("", content)
apiPost("", content, "POST", true)
.catch(errorHandler("Failed to save advanced json data"));
};

Expand All @@ -52,8 +52,8 @@ function AdvancedJson() {
<Box height="75vh">
<JsonEditor
schema={schema}
onChange={(value) => setContent(JSON.parse(value))}
defaultValue={JSON.stringify(content, null, 2)}
onChange={(value) => setContent(value)}
defaultValue={content}
/>
</Box>
<Button type="submit" onClick={onSubmit}>
Expand Down
10 changes: 5 additions & 5 deletions src/frontend/admin/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export const useLocalStorageState = (key, defaultValue) => {
};

export const createApiPost = (apiUrl) =>
function (path, body = {}, method = "POST") {
function (path, body = {}, method = "POST", sendRaw = false) {
const requestOptions = {
method: method,
headers: { "Content-Type": "application/json" },
body: method === "GET" ? undefined : JSON.stringify(body),
headers: { "Content-Type": sendRaw ? "text/plain" : "application/json" },
body: method === "GET" ? undefined : (sendRaw ? body : JSON.stringify(body)),
};
return fetch(apiUrl + path, requestOptions)
.then(response => response.json())
Expand All @@ -30,13 +30,13 @@ export const createApiPost = (apiUrl) =>
});
};
export const createApiGet = (apiUrl) =>
function (path, body = undefined) {
function (path, body = undefined, rawText = false) {
const requestOptions = {
headers: { "Content-Type": "application/json" },
body: body !== undefined ? JSON.stringify(body) : undefined,
};
return fetch(apiUrl + path, requestOptions)
.then(response => response.json());
.then(response => rawText ? response.text() : response.json());
};

export const timeMsToDuration = (timeMs) => DateTime.fromMillis(timeMs, { zone: "utc" }).toFormat("H:mm:ss");
Expand Down

0 comments on commit a074863

Please sign in to comment.