diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index f682578..12c9c89 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -7,6 +7,7 @@ import LoadAuth from './auth/load'; import NavBar from './components/modules/NavBar'; import ConfigBrowse from './components/view/ConfigBrowse'; import ConfigCreate from './components/view/ConfigCreate'; +import ConfigEdit from './components/view/ConfigEdit'; import ConfigRender from './components/view/ConfigRender'; import Login from './components/view/Login'; import Register from './components/view/Register'; @@ -20,8 +21,8 @@ export default () => {
- } /> - } /> + + diff --git a/frontend/src/axios/Config/GetConfig.js b/frontend/src/axios/Config/GetConfig.js index 01d085b..0816225 100644 --- a/frontend/src/axios/Config/GetConfig.js +++ b/frontend/src/axios/Config/GetConfig.js @@ -9,4 +9,5 @@ export const GetConfigForEdit = (id) => axios.get(`/api/configs/${id}`) store.dispatch({ type: 'SET_CONFIG_EDITING', id, json: resp.data.json, name: resp.data.name, }); + return resp.data; })); diff --git a/frontend/src/components/modules/Config/ExtendableBiomeInput.jsx b/frontend/src/components/modules/Config/ExtendableBiomeInput.jsx index 768b6f0..6843c83 100644 --- a/frontend/src/components/modules/Config/ExtendableBiomeInput.jsx +++ b/frontend/src/components/modules/Config/ExtendableBiomeInput.jsx @@ -6,11 +6,13 @@ import { faMinus, faPlus } from '@fortawesome/free-solid-svg-icons'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; export default ({ - onChangeSuper, + onChangeSuper, prefill, }) => { - const [entries, setEntries] = useState([true]); - const [biomes, setBiomes] = useState([null]); - const [isWhitelist, setWlMode] = useState(false); + const [entries, setEntries] = useState( + (prefill && Array(prefill.biomes.length).fill(true)) || [true], + ); + const [biomes, setBiomes] = useState((prefill && prefill.biomes) || [null]); + const [isWhitelist, setWlMode] = useState((prefill && prefill.isWhitelist) || false); const addEntry = () => { const newBiomes = [...biomes, null]; @@ -58,7 +60,6 @@ export default ({ overlay={( {'Format: OR '} - Biome Type @@ -68,6 +69,7 @@ export default ({ onChange(idx, evt.target.value)} /> @@ -78,6 +80,7 @@ export default ({ Whitelist? onWlChange(evt.target.checked)} aria-label="Checkbox for following text input" /> diff --git a/frontend/src/components/modules/Config/ExtendableInput.jsx b/frontend/src/components/modules/Config/ExtendableInput.jsx index 4244e3f..348ef41 100644 --- a/frontend/src/components/modules/Config/ExtendableInput.jsx +++ b/frontend/src/components/modules/Config/ExtendableInput.jsx @@ -6,10 +6,10 @@ import { faMinus, faPlus } from '@fortawesome/free-solid-svg-icons'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; export default ({ - type, tooltipText, onChangeSuper, + type, tooltipText, onChangeSuper, prefill, }) => { - const [entries, setEntries] = useState([true]); - const [state, setState] = useState([null]); + const [entries, setEntries] = useState((prefill && Array(prefill.length).fill(true)) || [true]); + const [state, setState] = useState(prefill || [null]); const addEntry = () => { const newState = [...state, null]; @@ -46,6 +46,7 @@ export default ({ onChange(idx, evt.target.value)} /> diff --git a/frontend/src/components/view/ConfigCreate.jsx b/frontend/src/components/view/ConfigCreate.jsx index fd0cd3b..e978807 100644 --- a/frontend/src/components/view/ConfigCreate.jsx +++ b/frontend/src/components/view/ConfigCreate.jsx @@ -4,21 +4,16 @@ import { } from 'react-bootstrap'; import { connect } from 'react-redux'; import { CSSTransition } from 'react-transition-group'; -import { GetConfigForEdit } from '../../axios/Config/GetConfig'; import PutConfig from '../../axios/Config/PutConfig'; -import PatchConfig from '../../axios/Config/PatchConfig'; import ConfigCreateOre from './ConfigCreateOre'; import ConfigCreateStone from './ConfigCreateStone'; const Configurator = ({ - mc114, editing, prefill, + mc114, }) => { - const [name, setName] = useState(prefill && prefill.name); - const [oreConfigs, setOreConfigs] = useState(prefill - ? Array(prefill.json.ores.length).fill(true) : [true]); - const [stoneConfigs, setStoneConfigs] = useState(prefill - ? Array(prefill.json.stones.length).fill(true) : [true]); - + const [name, setName] = useState(''); + const [oreConfigs, setOreConfigs] = useState([true]); + const [stoneConfigs, setStoneConfigs] = useState([true]); const [oreState, setOreState] = useState([{ name: '', type: 'dense', @@ -32,7 +27,6 @@ const Configurator = ({ dimBlacklist: [], blockStateMatchers: [], }]); - const [stoneState, setStoneState] = useState([{ block: '', maxY: -1, @@ -58,19 +52,6 @@ const Configurator = ({ setOreState(newState); }; - const onSubmit = () => { - const body = { name, json: { ores: oreState, stones: stoneState } }; - if (editing) { - PatchConfig(prefill.id, body).then((data) => { - window.location.href = `/view?id=${data}`; - }); - } else { - PutConfig(body).then((data) => { - window.location.href = `/view?id=${data}`; - }); - } - }; - const addStoneEntry = () => { setStoneConfigs([...stoneConfigs, true]); }; @@ -87,30 +68,6 @@ const Configurator = ({ setStoneState(newState); }; - if (editing && !prefill) { - const params = new URLSearchParams(window.location.search); - if (params.has('id')) { - GetConfigForEdit(params.get('id')); - return ( - -

Loading...

-
- ); - } - - return ( - -
-

- Missing Query Param - {' '} - id -

-
-
- ); - } - return (
@@ -139,7 +96,6 @@ const Configurator = ({ onEntryCreation={addOreEntry} onEntryDeletion={removeOreEntry} onConfigChange={onOreConfigChange} - prefill={editing && prefill.json.ores[idx]} idx={idx} /> ))} @@ -147,7 +103,7 @@ const Configurator = ({ -

Stones

+

Stones

{stoneConfigs.map((enabled, idx) => enabled && ( ))} @@ -163,11 +118,18 @@ const Configurator = ({
@@ -176,7 +138,6 @@ const Configurator = ({ const mapStateToProps = (state) => ({ mc114: state.mc114, - prefill: state.editing || null, }); export default connect(mapStateToProps)(Configurator); diff --git a/frontend/src/components/view/ConfigCreateOre.jsx b/frontend/src/components/view/ConfigCreateOre.jsx index b2efc4f..5b28ea3 100644 --- a/frontend/src/components/view/ConfigCreateOre.jsx +++ b/frontend/src/components/view/ConfigCreateOre.jsx @@ -59,6 +59,7 @@ export default ({ { setStateWithPropogation( { ...state, name: evt.target.value }, @@ -255,10 +256,12 @@ export default ({ Biomes To Spawn / Not Spawn - setStateWithPropogation({ - ...state, - ...wlState, - })} + setStateWithPropogation({ + ...state, + ...wlState, + })} /> )} @@ -271,6 +274,7 @@ export default ({ ' : 'Any Dimension ID (e.g. -1, 1)'} + prefill={prefill && prefill.dimBlacklist} onChangeSuper={(dimBlacklist) => setStateWithPropogation( { ...state, dimBlacklist }, )} @@ -285,6 +289,7 @@ export default ({ setStateWithPropogation( { ...state, blockStateMatchers }, )} diff --git a/frontend/src/components/view/ConfigCreateStone.jsx b/frontend/src/components/view/ConfigCreateStone.jsx index af5bae8..4e73f47 100644 --- a/frontend/src/components/view/ConfigCreateStone.jsx +++ b/frontend/src/components/view/ConfigCreateStone.jsx @@ -52,6 +52,7 @@ export default ({ setStateWithPropogation({ ...state, block: evt.target.value })} type="text" /> @@ -140,6 +141,7 @@ export default ({ ' : 'Any Dimension ID (e.g. -1, 1)'} + prefill={prefill && prefill.dimBlacklist} onChangeSuper={(dimBlacklist) => setStateWithPropogation( { ...state, dimBlacklist }, )} diff --git a/frontend/src/components/view/ConfigEdit.jsx b/frontend/src/components/view/ConfigEdit.jsx new file mode 100644 index 0000000..fc51630 --- /dev/null +++ b/frontend/src/components/view/ConfigEdit.jsx @@ -0,0 +1,161 @@ +import React, { useState } from 'react'; +import { + Accordion, Button, Container, Form, +} from 'react-bootstrap'; +import { connect } from 'react-redux'; +import { CSSTransition } from 'react-transition-group'; +import { GetConfigForEdit } from '../../axios/Config/GetConfig'; +import PatchConfig from '../../axios/Config/PatchConfig'; +import ConfigCreateOre from './ConfigCreateOre'; +import ConfigCreateStone from './ConfigCreateStone'; + +const Configurator = ({ + mc114, prefill, +}) => { + const [name, setName] = useState('Unnamed'); + + const [oreState, setOreState] = useState(null); + const [oreConfigs, setOreConfigs] = useState([true]); + + const [stoneState, setStoneState] = useState(null); + const [stoneConfigs, setStoneConfigs] = useState([true]); + + if (!prefill) { + const params = new URLSearchParams(window.location.search); + if (params.has('id')) { + GetConfigForEdit(params.get('id')).then((x) => { + x.name && setName(x.name); + setOreState(x.json.ores); + setOreConfigs(Array(x.json.ores.length).fill(true)); + setStoneState(x.json.stones); + setStoneConfigs(Array(x.json.stones.length).fill(true)); + }); + + return ( + +

Loading...

+
+ ); + } + return ( + +
+

+ Missing Query Param + {' '} + id +

+
+
+ ); + } + + const addOreEntry = () => { + setOreConfigs([...oreConfigs, true]); + }; + + const removeOreEntry = (targetIdx) => { + const cp = [...oreConfigs]; + cp[targetIdx] = false; + setOreConfigs(cp); + }; + + const onOreConfigChange = (idx, oreConfigState) => { + const newState = [...oreState]; + newState[idx] = oreConfigState; + setOreState(newState); + }; + + const onSubmit = () => { + const body = { name, json: { ores: oreState, stones: stoneState } }; + PatchConfig(prefill.id, body).then((data) => { + window.location.href = `/view?id=${data}`; + }); + }; + + const addStoneEntry = () => { + setStoneConfigs([...stoneConfigs, true]); + }; + + const removeStoneEntry = (targetIdx) => { + const cp = [...stoneConfigs]; + cp[targetIdx] = false; + setStoneConfigs(cp); + }; + + const onStoneConfigChange = (idx, stoneConfigState) => { + const newState = [...stoneState]; + newState[idx] = stoneConfigState; + setStoneState(newState); + }; + + return ( + +
+
+ Geolosys Logo +

Geolosys Deposit Configurator

+
+ + + + Config Name: + setName(evt.target.value)} + /> + + + +

Ores

+ + {oreConfigs.map((enabled, idx) => enabled && ( + + ))} + +
+ + +

Stones

+ {' '} + + {stoneConfigs.map((enabled, idx) => enabled && ( + + ))} + +
+ + +
+
+ ); +}; + +const mapStateToProps = (state) => ({ + mc114: state.mc114, + prefill: state.editing, +}); + +export default connect(mapStateToProps)(Configurator); diff --git a/frontend/src/components/view/ConfigRender.jsx b/frontend/src/components/view/ConfigRender.jsx index 8f5fe5d..183288c 100644 --- a/frontend/src/components/view/ConfigRender.jsx +++ b/frontend/src/components/view/ConfigRender.jsx @@ -9,14 +9,12 @@ const cleanJson = (json) => { json.ores.forEach((deposit) => { const clean = { ...deposit }; - if (clean.oreBlocks) { - clean.oreBlocks = clean.oreBlocks.map((x) => [x.ore, x.chance]).flat(); - } - if (clean.sampleBlocks) { - clean.sampleBlocks = clean.sampleBlocks.map((x) => [x.ore, x.chance]).flat(); - } - delete clean.depType; + clean.oreBlocks = clean.oreBlocks + && clean.oreBlocks.map((x) => [x.ore, x.chance]).flat(); + clean.sampleBlocks = clean.sampleBlocks + && clean.sampleBlocks.map((x) => [x.ore, x.chance]).flat(); + delete clean.depType; formattedOres.push(clean); });