Impl config editing, but properly.

This commit is contained in:
oitsjustjose 2020-12-29 10:47:31 -06:00
commit cc60bbe998
9 changed files with 208 additions and 75 deletions

View file

@ -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={(
<Tooltip id={`biome-input-${idx}`}>
{'Format: <modid:biome> OR '}
<a rel="noreferrer noopener" href="https://oitsjustjo.se/u/GelX-uC1s" target="_blank">
Biome Type
</a>
@ -68,6 +69,7 @@ export default ({
<Form.Control
required
type="text"
value={biomes[idx]}
onChange={(evt) => onChange(idx, evt.target.value)}
/>
</OverlayTrigger>
@ -78,6 +80,7 @@ export default ({
Whitelist?
</InputGroup.Text>
<InputGroup.Checkbox
checked={isWhitelist}
onChange={(evt) => onWlChange(evt.target.checked)}
aria-label="Checkbox for following text input"
/>

View file

@ -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 ({
<Form.Control
required
type={type}
value={state[idx]}
onChange={(evt) => onChange(idx, evt.target.value)}
/>
</OverlayTrigger>

View file

@ -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 (
<CSSTransition classNames="react-router" appear in timeout={300}>
<div className="v-center"><h1>Loading...</h1></div>
</CSSTransition>
);
}
return (
<CSSTransition classNames="react-router" appear in timeout={300}>
<div className="v-center text-danger">
<h3>
Missing Query Param
{' '}
<code>id</code>
</h3>
</div>
</CSSTransition>
);
}
return (
<CSSTransition classNames="react-router" appear in timeout={300}>
<div>
@ -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 = ({
</Container>
<Container>
<h4>Stones</h4>
<h4 className="mt-3">Stones</h4>
<Accordion defaultActiveKey="0">
{stoneConfigs.map((enabled, idx) => enabled && (
<ConfigCreateStone
@ -155,7 +111,6 @@ const Configurator = ({
onEntryCreation={addStoneEntry}
onEntryDeletion={removeStoneEntry}
onConfigChange={onStoneConfigChange}
prefill={editing && prefill.json.stones[idx]}
idx={idx}
/>
))}
@ -163,11 +118,18 @@ const Configurator = ({
</Container>
<Button
onClick={onSubmit}
variant={editing ? 'danger' : 'primary'}
onClick={() => PutConfig({
name,
json: {
ores: oreState,
stones: stoneState,
},
}).then((data) => {
window.location.href = `/view?id=${data}`;
})}
className="d-block mx-auto my-4"
>
{editing ? 'Save (Overwrites)' : 'Generate!'}
Generate!
</Button>
</div>
</CSSTransition>
@ -176,7 +138,6 @@ const Configurator = ({
const mapStateToProps = (state) => ({
mc114: state.mc114,
prefill: state.editing || null,
});
export default connect(mapStateToProps)(Configurator);

View file

@ -59,6 +59,7 @@ export default ({
</Form.Label>
<Form.Control
type="text"
value={state.name}
onChange={(evt) => {
setStateWithPropogation(
{ ...state, name: evt.target.value },
@ -255,10 +256,12 @@ export default ({
<Form.Label>
Biomes To Spawn / Not Spawn
</Form.Label>
<ExtendableBiomeInput onChangeSuper={(wlState) => setStateWithPropogation({
...state,
...wlState,
})}
<ExtendableBiomeInput
prefill={prefill || null}
onChangeSuper={(wlState) => setStateWithPropogation({
...state,
...wlState,
})}
/>
</Form.Group>
)}
@ -271,6 +274,7 @@ export default ({
<ExtendableInput
type={mc114 ? 'text' : 'number'}
tooltipText={mc114 ? 'Format: <modid:dimension>' : 'Any Dimension ID (e.g. -1, 1)'}
prefill={prefill && prefill.dimBlacklist}
onChangeSuper={(dimBlacklist) => setStateWithPropogation(
{ ...state, dimBlacklist },
)}
@ -285,6 +289,7 @@ export default ({
<ExtendableInput
type="text"
tooltipText="Format: <modid:block> or <modid:block:meta>"
prefill={prefill && prefill.blockStateMatchers}
onChangeSuper={(blockStateMatchers) => setStateWithPropogation(
{ ...state, blockStateMatchers },
)}

View file

@ -52,6 +52,7 @@ export default ({
<Form.Control
required
value={state.block}
onChange={(evt) => setStateWithPropogation({ ...state, block: evt.target.value })}
type="text"
/>
@ -140,6 +141,7 @@ export default ({
<ExtendableInput
type={mc114 ? 'text' : 'number'}
tooltipText={mc114 ? 'Format: <modid:dimension>' : 'Any Dimension ID (e.g. -1, 1)'}
prefill={prefill && prefill.dimBlacklist}
onChangeSuper={(dimBlacklist) => setStateWithPropogation(
{ ...state, dimBlacklist },
)}

View file

@ -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 (
<CSSTransition classNames="react-router" appear in timeout={300}>
<div className="v-center"><h1>Loading...</h1></div>
</CSSTransition>
);
}
return (
<CSSTransition classNames="react-router" appear in timeout={300}>
<div className="v-center text-danger">
<h3>
Missing Query Param
{' '}
<code>id</code>
</h3>
</div>
</CSSTransition>
);
}
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 (
<CSSTransition classNames="react-router" appear in timeout={300}>
<div>
<div className="text-center d-block">
<img className="header" src={`${process.env.PUBLIC_URL}/logo.png`} alt="Geolosys Logo" />
<h4>Geolosys Deposit Configurator</h4>
</div>
<Container className="w-50">
<Form.Group>
<Form.Label>Config Name:</Form.Label>
<Form.Control
type="text"
name="configName"
value={name}
onChange={(evt) => setName(evt.target.value)}
/>
</Form.Group>
</Container>
<Container>
<h4>Ores</h4>
<Accordion defaultActiveKey="0">
{oreConfigs.map((enabled, idx) => enabled && (
<ConfigCreateOre
mc114={mc114}
onEntryCreation={addOreEntry}
onEntryDeletion={removeOreEntry}
onConfigChange={onOreConfigChange}
prefill={prefill.json.ores[idx]}
idx={idx}
/>
))}
</Accordion>
</Container>
<Container>
<h4 className="mt-3">Stones</h4>
{' '}
<Accordion defaultActiveKey="0">
{stoneConfigs.map((enabled, idx) => enabled && (
<ConfigCreateStone
mc114={mc114}
onEntryCreation={addStoneEntry}
onEntryDeletion={removeStoneEntry}
onConfigChange={onStoneConfigChange}
prefill={prefill && prefill.json.stones[idx]}
idx={idx}
/>
))}
</Accordion>
</Container>
<Button
onClick={onSubmit}
variant="danger"
className="d-block mx-auto my-4"
>
Save (Overwrites)
</Button>
</div>
</CSSTransition>
);
};
const mapStateToProps = (state) => ({
mc114: state.mc114,
prefill: state.editing,
});
export default connect(mapStateToProps)(Configurator);

View file

@ -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);
});