Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guess layer of AddThing, open popup #9

Merged
merged 1 commit into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/components/MapDisplay/ThingEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from "react";
import React, { useEffect, useState } from "react";
import { Dialog, InputGroup, Button, NumericInput, Divider } from "@blueprintjs/core";
import { useDispatch } from "react-redux";
import { Thing } from "../../models";
Expand All @@ -11,6 +11,10 @@ interface ThingEditorProps {
}

const ThingEditor: React.FC<ThingEditorProps> = ({ thing, isOpen, onClose }) => {
useEffect(() => {
setEditedThing({ ...thing });
}, [thing]);

const [editedThing, setEditedThing] = useState<Thing>({ ...thing });
const dispatch = useDispatch();

Expand Down
37 changes: 29 additions & 8 deletions src/components/RunMosaic/AddThing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import React, { useState } from "react";
import { Thing } from "../../models";
import { useDispatch } from "react-redux";
import { addThing } from "../../store/routeSlice";
import { Dialog, Button, InputGroup } from "@blueprintjs/core";
import ThingEditor from "../MapDisplay/ThingEditor";

export const AddThing: React.FC = () => {
const [isModalOpen, setModalOpen] = useState(false);
const [uid, setUid] = useState("");
const [isEditorOpen, setIsEditorOpen] = useState(false);
const [newThing, setNewThing] = useState<Thing | null>(null);
const dispatch = useDispatch();

const handleButtonClick = () => {
Expand All @@ -20,34 +24,51 @@ export const AddThing: React.FC = () => {
const response = await fetch(`https://radar-totk.zeldamods.org/obj_by_hash/${uid}`);
const data = await response.json();

const adjusted_z = data.pos[1] - 105.499;

let estimated_layer = "surface";
if (adjusted_z < -200) estimated_layer = "depths";
if (adjusted_z > 850) estimated_layer = "sky";

const newThing: Thing = {
uid: data.hash_id,
name: data.name,
coordinates: {
x: -data.pos[2],
y: data.pos[0],
z: data.pos[1] - 105.499,
z: adjusted_z,
},
layerId: "surface",
layerId: estimated_layer,
dependencyIds: [],
icon: "",
type: "Thing",
isNativeObject: true,
};

dispatch(addThing(newThing));
setNewThing(newThing);
setIsEditorOpen(true);
setModalOpen(false);
};

const closeEditor = () => {
setIsEditorOpen(false);
};

return (
<div>
<button onClick={handleButtonClick}>Add Thing</button>
{isModalOpen && (
<div className="modal">
<input type="text" value={uid} onChange={handleInputChange} placeholder="Enter UID" />
<button onClick={handleSubmit}>Submit</button>
<Button onClick={handleButtonClick}>Add Thing</Button>

<Dialog isOpen={isModalOpen} title="Add Thing" onClose={() => setModalOpen(false)}>
<div className="bp3-dialog-body">
<InputGroup value={uid} onChange={handleInputChange} placeholder="Enter UID" />
</div>
<div className="bp3-dialog-footer">
<Button onClick={handleSubmit}>Submit</Button>
</div>
)}
</Dialog>

{newThing && <ThingEditor thing={newThing} isOpen={isEditorOpen} onClose={closeEditor} />}
</div>
);
};
Loading