Create System is working... ask me how I know!

This commit is contained in:
Luke Berndt
2023-03-20 22:10:33 -04:00
parent e7497a7d7a
commit 109d8c11ed
5 changed files with 402 additions and 401 deletions

View File

@@ -174,6 +174,7 @@ exports.ownSystem = async function (req, res, next) {
if (!system) { if (!system) {
console.error("ERROR: OwnSystem - system does not exist: " + req.body.shortName.toLowerCase()); console.error("ERROR: OwnSystem - system does not exist: " + req.body.shortName.toLowerCase());
res.status(500)
res.json({ res.json({
success: false, success: false,
message: "That System does not exist." message: "That System does not exist."
@@ -182,7 +183,7 @@ exports.ownSystem = async function (req, res, next) {
} }
if (!system.userId.equals(req.user._id)) { if (!system.userId.equals(req.user._id)) {
console.error("ERROR: OwnSystem - not users system " + req.body.shortName.toLowerCase()); console.error("ERROR: OwnSystem - not users system " + req.body.shortName.toLowerCase());
res.status(500)
res.json({ res.json({
success: false, success: false,
message: "You are not the user associated with this system." message: "You are not the user associated with this system."
@@ -195,6 +196,7 @@ exports.ownSystem = async function (req, res, next) {
} catch (err) { } catch (err) {
console.error("ERROR: ownSystem - caught: " + err); console.error("ERROR: ownSystem - caught: " + err);
res.status(500)
res.json({ res.json({
success: false, success: false,
message: err message: err
@@ -215,6 +217,7 @@ exports.uniqueShortName = async function (req, res, next) {
} }
if (typeof shortName == 'undefined') { if (typeof shortName == 'undefined') {
console.error("ShortName is not in req.body instead") console.error("ShortName is not in req.body instead")
res.status(500)
res.json({ res.json({
success: false, success: false,
message: "Short Name not provided in form" message: "Short Name not provided in form"
@@ -228,6 +231,7 @@ exports.uniqueShortName = async function (req, res, next) {
if (system) { if (system) {
console.error("ERROR: Unique Shortname already in use: " + req.body.shortName.toLowerCase()); console.error("ERROR: Unique Shortname already in use: " + req.body.shortName.toLowerCase());
res.status(500)
res.json({ res.json({
success: false, success: false,
message: "Short Name already in use" message: "Short Name already in use"
@@ -240,10 +244,8 @@ exports.uniqueShortName = async function (req, res, next) {
} catch (err) { } catch (err) {
console.error("ERROR: Unique ShortName - caught: " + err); console.error("ERROR: Unique ShortName - caught: " + err);
res.json({ res.status(500);
success: false, res.json({ message: "ERROR: Unique ShortName - caught: " + err});
message: err
});
return; return;
} }
@@ -300,10 +302,7 @@ exports.updateSystem = async function (req, res, next) {
ignoreUnknownTalkgroup ignoreUnknownTalkgroup
}))(res.locals.system); }))(res.locals.system);
returnSys.id = res.locals.system._id; returnSys.id = res.locals.system._id;
res.json({ res.json(returnSys);
success: true,
system: returnSys
});
return; return;
} }
}); });
@@ -317,6 +316,7 @@ exports.validateSystem = async function (req, res, next) {
res.locals.ignoreUnknownTalkgroup = req.body.ignoreUnknownTalkgroup; res.locals.ignoreUnknownTalkgroup = req.body.ignoreUnknownTalkgroup;
if (!req.body.name || (req.body.name.length < 2)) { if (!req.body.name || (req.body.name.length < 2)) {
console.error("ERROR: Validate System - req.body.name"); console.error("ERROR: Validate System - req.body.name");
res.status(500)
res.json({ res.json({
success: false, success: false,
message: "System Name is Required" message: "System Name is Required"
@@ -327,6 +327,7 @@ exports.validateSystem = async function (req, res, next) {
if (!req.body.description || (req.body.description.length < 2)) { if (!req.body.description || (req.body.description.length < 2)) {
console.error("ERROR: Validate System - req.body.description"); console.error("ERROR: Validate System - req.body.description");
res.status(500)
res.json({ res.json({
success: false, success: false,
message: "System Description is Required" message: "System Description is Required"
@@ -337,6 +338,7 @@ exports.validateSystem = async function (req, res, next) {
if (!req.body.shortName || (req.body.shortName.length < 2)) { if (!req.body.shortName || (req.body.shortName.length < 2)) {
console.error("ERROR: Validate System - req.body.shortName"); console.error("ERROR: Validate System - req.body.shortName");
res.status(500)
res.json({ res.json({
success: false, success: false,
message: "System ShortName is Required" message: "System ShortName is Required"
@@ -352,6 +354,7 @@ exports.validateSystem = async function (req, res, next) {
// make sure it is one of the expected types // make sure it is one of the expected types
if ((req.body.systemType != "state") && (req.body.systemType != "city") && (req.body.systemType != "county") && (req.body.systemType != "international")) { if ((req.body.systemType != "state") && (req.body.systemType != "city") && (req.body.systemType != "county") && (req.body.systemType != "international")) {
console.error("ERROR: Validate System - req.body.systemType: " + req.body.systemType); console.error("ERROR: Validate System - req.body.systemType: " + req.body.systemType);
res.status(500)
res.json({ res.json({
success: false, success: false,
message: "Select system location" message: "Select system location"
@@ -368,6 +371,7 @@ exports.validateSystem = async function (req, res, next) {
req.body.systemType === "county") { req.body.systemType === "county") {
if (!req.body.state || (req.body.state === "") || (req.body.state.length < 2)) { if (!req.body.state || (req.body.state === "") || (req.body.state.length < 2)) {
console.error("ERROR: Validate System - req.body.state: " + req.body.state); console.error("ERROR: Validate System - req.body.state: " + req.body.state);
res.status(500)
res.json({ res.json({
success: false, success: false,
message: "Select the State for the System" message: "Select the State for the System"
@@ -380,6 +384,7 @@ exports.validateSystem = async function (req, res, next) {
if (req.body.systemType === "city") { if (req.body.systemType === "city") {
if (!req.body.city || (req.body.city === "") || (req.body.city.length < 2)) { if (!req.body.city || (req.body.city === "") || (req.body.city.length < 2)) {
console.error("ERROR: Validate System - req.body.systemType: " + req.body.systemType); console.error("ERROR: Validate System - req.body.systemType: " + req.body.systemType);
res.status(500)
res.json({ res.json({
success: false, success: false,
message: "Enter the city for the System" message: "Enter the city for the System"
@@ -392,6 +397,7 @@ exports.validateSystem = async function (req, res, next) {
if (req.body.systemType === "county") { if (req.body.systemType === "county") {
if (!req.body.county || (req.body.county === "") || (req.body.county.length < 2)) { if (!req.body.county || (req.body.county === "") || (req.body.county.length < 2)) {
console.error("ERROR: Validate System - req.body.county: " + req.body.county); console.error("ERROR: Validate System - req.body.county: " + req.body.county);
res.status(500)
res.json({ res.json({
success: false, success: false,
message: "Enter the county for the System" message: "Enter the county for the System"
@@ -404,6 +410,7 @@ exports.validateSystem = async function (req, res, next) {
if (req.body.systemType === "international") { if (req.body.systemType === "international") {
if (!req.body.country || (req.body.country === "") || (req.body.country.length < 2)) { if (!req.body.country || (req.body.country === "") || (req.body.country.length < 2)) {
console.error("ERROR: Validate System - req.body.country: " + req.body.country); console.error("ERROR: Validate System - req.body.country: " + req.body.country);
res.status(500)
res.json({ res.json({
success: false, success: false,
message: "Enter the country for the System" message: "Enter the country for the System"
@@ -416,6 +423,7 @@ exports.validateSystem = async function (req, res, next) {
} catch (err) { } catch (err) {
console.error("ERROR: Validate System - caught: " + err); console.error("ERROR: Validate System - caught: " + err);
res.status(500)
res.json({ res.json({
success: false, success: false,
message: err message: err
@@ -457,15 +465,17 @@ exports.createSystem = async function (req, res, next) {
}))(res.locals); }))(res.locals);
system.key = key; system.key = key;
system.userId = new mongoose.Types.ObjectId(req.user._id); system.userId = new mongoose.Types.ObjectId(req.user._id);
System.create(system, function (err, newSys) { const newSys = await System.create(system).catch( err => {
if (err) {
console.error(err); console.error(err);
res.status(500)
res.json({ res.json({
success: false, success: false,
message: err message: err
}); });
return; return;
} });
var returnSys = (({ var returnSys = (({
name, name,
shortName, shortName,
@@ -494,10 +504,7 @@ exports.createSystem = async function (req, res, next) {
key key
}))(newSys); }))(newSys);
returnSys.id = newSys._id; returnSys.id = newSys._id;
res.json({ res.json( returnSys);
success: true,
system: returnSys
});
return; return;
});
} }

View File

@@ -8,7 +8,7 @@ import About from "../Components/About"
import System from "../System/System" import System from "../System/System"
import ListSystems from "../System/ListSystems" import ListSystems from "../System/ListSystems"
import UpdateSystemContainer from "../System/UpdateSystemContainer" import UpdateSystemContainer from "../System/UpdateSystemContainer"
import CreateSystemContainer from "../System/CreateSystemContainer" import CreateSystem from "../System/CreateSystem"
const App = (params) => { const App = (params) => {
@@ -20,7 +20,7 @@ const App = (params) => {
<Route path="/about" element={ <Restricted><About/></Restricted>} /> <Route path="/about" element={ <Restricted><About/></Restricted>} />
<Route path="/system/:shortName" element={ <Restricted><System/></Restricted>} /> <Route path="/system/:shortName" element={ <Restricted><System/></Restricted>} />
<Route path="/update-system/:shortName" component={Restricted(UpdateSystemContainer, store)} /> <Route path="/update-system/:shortName" component={Restricted(UpdateSystemContainer, store)} />
<Route path="/create-system" component={Restricted(CreateSystemContainer, store)} /> <Route path="/create-system" element={ <Restricted><CreateSystem/></Restricted>} />
<Route path="/list-systems" element={ <Restricted><ListSystems/></Restricted>} /> <Route path="/list-systems" element={ <Restricted><ListSystems/></Restricted>} />
<Route path="/" element={ <Restricted><ListSystems/></Restricted>} /> <Route path="/" element={ <Restricted><ListSystems/></Restricted>} />
<Route render={() => (<div>Miss</div>)} /> <Route render={() => (<div>Miss</div>)} />

View File

@@ -1,5 +1,5 @@
import React, { Component } from "react"; import React, { useState } from "react";
import ReactDOM from "react-dom";
import { import {
Container, Container,
Header, Header,
@@ -12,7 +12,9 @@ import {
Message, Message,
Icon Icon
} from "semantic-ui-react"; } from "semantic-ui-react";
import { useCreateSystemMutation} from '../features/api/apiSlice'
import { useSelector, useDispatch } from 'react-redux'
import { useLocation, useNavigate, useParams } from 'react-router-dom';
import SystemForm from "./SystemForm"; import SystemForm from "./SystemForm";
// ---------------------------------------------------- // ----------------------------------------------------
@@ -21,35 +23,26 @@ const requestMessageStyle = {
}; };
// ---------------------------------------------------- // ----------------------------------------------------
class CreateSystem extends Component { const CreateSystem = (props) => {
constructor(props) { const navigate = useNavigate();
super(props); const [createSystem, { error }] = useCreateSystemMutation();
this.handleSubmit = this.handleSubmit.bind(this); const { screenName } = useSelector((state) => state.user);
const [requestMessage, setRequestMessage] = useState("");
const handleSubmit = async (system) => {
try {
const originalPromiseResult = await createSystem(system).unwrap();
navigate("/list-systems")
} catch (error) {
const message = error.data.message;
console.log(message);
setRequestMessage(message);
}
} }
state = {
requestMessage: ""
}
handleSubmit(system) {
this.props
.createSystem(system)
.then(requestMessage => {
if (requestMessage) {
// report to the user is there was a problem during registration
this.setState({
requestMessage
});
} else {
this.props.changeUrl("/list-systems")
}
});
}
render() {
return ( return (
<div> <div>
<Container text> <Container text>
@@ -72,11 +65,11 @@ class CreateSystem extends Component {
<Container text> <Container text>
<Header as="h1">Create System</Header> <Header as="h1">Create System</Header>
<SystemForm onSubmit={this.handleSubmit} requestMessage={this.state.requestMessage} screenName={this.props.user.screenName}/> <SystemForm onSubmit={handleSubmit} requestMessage={requestMessage} screenName={screenName}/>
</Container> </Container>
</div> </div>
); );
}
} }
export default CreateSystem; export default CreateSystem;

View File

@@ -1,4 +1,4 @@
import React, { Component } from "react"; import React, { useEffect, useState } from "react";
import { import {
Divider, Divider,
Form, Form,
@@ -9,421 +9,387 @@ import {
} from "semantic-ui-react"; } from "semantic-ui-react";
// ---------------------------------------------------- // ----------------------------------------------------
class SystemForm extends Component { const SystemForm = (props) => {
constructor(props) {
super(props);
this.handleInputChange = this.handleInputChange.bind(this);
this.handleScreenNameCheckboxChange = this.handleScreenNameCheckboxChange.bind(this);
this.handleTalkgroupCheckboxChange = this.handleTalkgroupCheckboxChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.checkInputs = this.checkInputs.bind(this);
this.render = this.render.bind(this);
this.state = {
checkInputMessages: [],
name: "", const [checkInputMessages, setCheckInputMessage] = useState([]);
shortName: "", const [name, setName] = useState("");
description: "", const [shortName, setShortName] = useState("");
systemType: "state", const [description, setDescription] = useState("");
state: "", const [systemType, setSystemType] = useState("state");
city: "", const [systemState, setSystemState] = useState("");
county: "", const [city, setCity] = useState("");
country: "", const [county, setCounty] = useState("");
showScreenName: false, const [country, setCountry] = useState("");
ignoreUnknownTalkgroup: false, const [showScreenName, setShowScreenName] = useState(false);
nameError: false, const [ignoreUnknownTalkgroup, setIgnoreUnknownTalkgroup] = useState(false);
shortNameError: false, const [nameError, setNameError] = useState(false);
descriptionError: false, const [shortNameError, setShortNameError] = useState(false);
emailError: false, const [descriptionError, setDescriptionError] = useState(false);
systemTypeError: false, const [emailError, setEmailError] = useState(false);
stateError: false, const [systemTypeError, setSystemTypeError] = useState(false);
cityErorr: false, const [systemStateError, setSystemStateError] = useState(false);
countyError: false, const [cityError, setCityError] = useState(false);
countryError: false, const [countyError, setCountyError] = useState(false);
isEditing: this.props.isEditing const [countryError, setCountryError] = useState(false);
}; const [isEditing, setIsEditing] = useState(props.isEditing);
if (this.props.isEditing && this.props.system) {
this.state.name = this.props.system.name; useEffect(() => {
this.state.shortName = this.props.system.shortName; if (props.isEditing && props.system) {
this.state.description = this.props.system.description; setName(props.system.name);
this.state.systemType = this.props.system.systemType; setShortName(props.system.shortName);
this.state.city = this.props.system.city; setDescription(props.system.description);
this.state.state = this.props.system.state; setSystemType(props.system.systemType);
this.state.county = this.props.system.county; setCity(props.system.city);
this.state.country = this.props.system.country; setSystemState(props.system.state)
this.state.showScreenName = this.props.system.showScreenName; setCounty(props.system.county);
this.state.ignoreUnknownTalkgroup = this.props.system.ignoreUnknownTalkgroup; setCountry(props.system.country);
setShowScreenName(props.system.showScreenName);
setIgnoreUnknownTalkgroup(props.system.ignoreUnknownTalkgroup);
} }
} }, [props.system]);
// If we directly to an updating url, the system fetch will come back after the constructer, so we need to catch this and update the values
componentDidUpdate(prevProps) {
if (this.props.isEditing && this.props.system && !prevProps.system) { const checkInputs = () => {
this.setState({
name: this.props.system.name,
shortName: this.props.system.shortName,
description: this.props.system.description,
systemType: this.props.system.systemType,
city: this.props.system.city,
state: this.props.system.state,
county: this.props.system.county,
country: this.props.system.country,
showScreenName: this.props.system.showScreenName,
ignoreUnknownTalkgroup: this.props.system.ignoreUnknownTalkgroup,
});
}
}
checkInputs() {
let error = false; let error = false;
var checkInputMessages = []; var messages = [];
if (this.state.name === "") { if (name === "") {
this.setState({ nameError: true }); setNameError(true);
checkInputMessages.push("System Name is required"); messages.push("System Name is required");
error = true; error = true;
} else { } else {
this.setState({ nameError: false }); setNameError(false);
} }
if (this.state.shortName.length === 0 || this.state.shortName.length > 10) { if (shortName.length < 4 || shortName.length > 10) {
this.setState({ shortNameError: true }); setShortNameError(true)
checkInputMessages.push( messages.push(
"Short Name for the system has to be 4-10 charecters" "Short Name for the system has to be 4-10 charecters"
); );
error = true; error = true;
} else { } else {
this.setState({ shortNameError: false }); setShortNameError(false);
} }
if (this.state.description === "") { if (description === "") {
this.setState({ descriptionError: true }); setDescriptionError(true);
checkInputMessages.push("System description is required"); messages.push("System description is required");
error = true; error = true;
} else { } else {
this.setState({ descriptionError: false }); setDescriptionError(false);
} }
if ( if (
this.state.systemType === "state" || systemType === "state" ||
this.state.systemType === "city" || systemType === "city" ||
this.state.systemType === "county" systemType === "county"
) { ) {
if (this.state.state === "") { if (systemState === "") {
this.setState({ stateError: true }); setSystemStateError(true);
checkInputMessages.push("Select a state"); messages.push("Select a state");
error = true; error = true;
} else { } else {
this.setState({ stateError: false }); setSystemStateError(false);
} }
} }
if (this.state.systemType === "city") { if (systemType === "city") {
if (this.state.city === "") { if (city === "") {
this.setState({ cityError: true }); setCityError(true);
checkInputMessages.push("Enter a city"); messages.push("Enter a city");
error = true; error = true;
} else { } else {
this.setState({ cityError: false }); setCityError(false);
} }
} }
if (this.state.systemType === "county") { if (systemType === "county") {
if (this.state.county === "") { if (county === "") {
this.setState({ countyError: true }); setCountyError(true)
checkInputMessages.push("Enter a city"); messages.push("Enter a county");
error = true; error = true;
} else { } else {
this.setState({ countyError: false }); setCountyError(false);
} }
} }
if (this.state.systemType === "international") { if (systemType === "international") {
if (this.state.country === "") { if (country === "") {
this.setState({ countryError: true }); setCountryError(true);
checkInputMessages.push("Enter a city"); messages.push("Enter a country");
error = true; error = true;
} else { } else {
this.setState({ countryError: false }); setCountryError(false);
} }
} }
this.setState({ checkInputMessages: checkInputMessages }); setCheckInputMessage(messages);
return error; return error;
} }
handleInputChange = (e, { name, value }) => this.setState({ [name]: value }); const handleScreenNameCheckboxChange = () => {
handleScreenNameCheckboxChange() { setShowScreenName((current) => !current);
this.setState({ showScreenName: !this.state.showScreenName });
} }
handleTalkgroupCheckboxChange() { const handleTalkgroupCheckboxChange = () => {
this.setState({ ignoreUnknownTalkgroup: !this.state.ignoreUnknownTalkgroup }); setIgnoreUnknownTalkgroup((current) => !current);
} }
handleSubmit(event) { const handleSubmit = (event) => {
event.preventDefault(); event.preventDefault();
let inputError = this.checkInputs(); let inputError = checkInputs();
if (!inputError) { if (!inputError) {
const system = (({ const system = {
name, name,
shortName, shortName,
description, description,
systemType, systemType,
city, city,
state, state: systemState,
county, county,
country, country,
showScreenName, showScreenName,
ignoreUnknownTalkgroup ignoreUnknownTalkgroup
}) => ({ };
name, props.onSubmit(system);
shortName,
description,
systemType,
city,
state,
county,
country,
showScreenName,
ignoreUnknownTalkgroup
}))(this.state);
this.props.onSubmit(system);
} }
} }
render() {
var requestMessage = "";
var floatStyle = {
overflow: "auto"
};
const { isEditing } = this.props; var requestMessage = "";
const stateOptions = [ var floatStyle = {
{ value: "AL", text: "Alabama" }, overflow: "auto"
{ value: "AK", text: "Alaska" }, };
{ value: "AZ", text: "Arizona" },
{ value: "AR", text: "Arkansas" },
{ value: "CA", text: "California" },
{ value: "CO", text: "Colorado" },
{ value: "CT", text: "Connecticut" },
{ value: "DE", text: "Delaware" },
{ value: "DC", text: "District Of Columbia" },
{ value: "FL", text: "Florida" },
{ value: "GA", text: "Georgia" },
{ value: "HI", text: "Hawaii" },
{ value: "ID", text: "Idaho" },
{ value: "IL", text: "Illinois" },
{ value: "IN", text: "Indiana" },
{ value: "IA", text: "Iowa" },
{ value: "KS", text: "Kansas" },
{ value: "KY", text: "Kentucky" },
{ value: "LA", text: "Louisiana" },
{ value: "ME", text: "Maine" },
{ value: "MD", text: "Maryland" },
{ value: "MA", text: "Massachusetts" },
{ value: "MI", text: "Michigan" },
{ value: "MN", text: "Minnesota" },
{ value: "MS", text: "Mississippi" },
{ value: "MO", text: "Missouri" },
{ value: "MT", text: "Montana" },
{ value: "NE", text: "Nebraska" },
{ value: "NV", text: "Nevada" },
{ value: "NH", text: "New Hampshire" },
{ value: "NJ", text: "New Jersey" },
{ value: "NM", text: "New Mexico" },
{ value: "NY", text: "New York" },
{ value: "NC", text: "North Carolina" },
{ value: "ND", text: "North Dakota" },
{ value: "OH", text: "Ohio" },
{ value: "OK", text: "Oklahoma" },
{ value: "OR", text: "Oregon" },
{ value: "PA", text: "Pennsylvania" },
{ value: "RI", text: "Rhode Island" },
{ value: "SC", text: "South Carolina" },
{ value: "SD", text: "South Dakota" },
{ value: "TN", text: "Tennessee" },
{ value: "TX", text: "Texas" },
{ value: "UT", text: "Utah" },
{ value: "VT", text: "Vermont" },
{ value: "VA", text: "Virginia" },
{ value: "WA", text: "Washington" },
{ value: "WV", text: "West Virginia" },
{ value: "WI", text: "Wisconsin" },
{ value: "WY", text: "Wyoming" }
];
if (
this.state.checkInputMessages.length ||
this.props.requestMessage.length
) {
var counter = 0;
requestMessage = (
<Message compact warning icon>
<Icon name="lightning" />
<Message.Content>
<Message.Header>Problems...</Message.Header>
<ul>
{this.state.checkInputMessages.map(function (listValue) {
return <li key={`ListItem_${counter++}`}>{listValue}</li>;
})}
{this.props.requestMessage.length > 0 && (
<li key="req-mess">{this.props.requestMessage}</li>
)}
</ul>
</Message.Content>
</Message>
);
}
return (
<div>
<Form className="raised padding segment" onSubmit={this.handleSubmit}>
<Form.Group widths="equal">
<Form.Field>
<Form.Input
type="text"
name="name"
onChange={this.handleInputChange}
error={this.state.nameError}
value={this.state.name}
label="System name"
placeholder="System Name..."
/>
</Form.Field>
<Form.Field>
<Form.Input
type="text"
name="shortName"
onChange={this.handleInputChange}
error={this.state.shortNameError}
value={this.state.shortName}
disabled={isEditing}
label="System Short Name (4-10 characters)"
placeholder="Short Name..."
/>
</Form.Field>
</Form.Group>
<Divider horizontal />
<Form.Field>
<Checkbox
onChange={this.handleScreenNameCheckboxChange}
checked={this.state.showScreenName}
label="Display Screen Name in System Listing" />
</Form.Field>
<Form.Field>
<Checkbox
onChange={this.handleTalkgroupCheckboxChange}
checked={this.state.ignoreUnknownTalkgroup}
label="Ignore Unknown Talkgroups" />
</Form.Field>
<Divider horizontal />
<Form.TextArea
label="Description"
name="description"
onChange={this.handleInputChange}
error={this.state.descriptionError}
value={this.state.description}
rows={2}
placeholder="Description of system..."
/>
<Divider horizontal><Icon name="marker" />Location</Divider> const stateOptions = [
<Form.Group inline> { value: "AL", text: "Alabama" },
<label>System Type</label> { value: "AK", text: "Alaska" },
<Form.Radio { value: "AZ", text: "Arizona" },
label="State" { value: "AR", text: "Arkansas" },
name="systemType" { value: "CA", text: "California" },
value="state" { value: "CO", text: "Colorado" },
checked={this.state.systemType === "state"} { value: "CT", text: "Connecticut" },
onChange={this.handleInputChange} { value: "DE", text: "Delaware" },
/> { value: "DC", text: "District Of Columbia" },
<Form.Radio { value: "FL", text: "Florida" },
label="County" { value: "GA", text: "Georgia" },
name="systemType" { value: "HI", text: "Hawaii" },
value="county" { value: "ID", text: "Idaho" },
checked={this.state.systemType === "county"} { value: "IL", text: "Illinois" },
onChange={this.handleInputChange} { value: "IN", text: "Indiana" },
/> { value: "IA", text: "Iowa" },
<Form.Radio { value: "KS", text: "Kansas" },
label="City" { value: "KY", text: "Kentucky" },
name="systemType" { value: "LA", text: "Louisiana" },
value="city" { value: "ME", text: "Maine" },
checked={this.state.systemType === "city"} { value: "MD", text: "Maryland" },
onChange={this.handleInputChange} { value: "MA", text: "Massachusetts" },
/> { value: "MI", text: "Michigan" },
<Form.Radio { value: "MN", text: "Minnesota" },
label="International" { value: "MS", text: "Mississippi" },
name="systemType" { value: "MO", text: "Missouri" },
value="international" { value: "MT", text: "Montana" },
checked={this.state.systemType === "international"} { value: "NE", text: "Nebraska" },
onChange={this.handleInputChange} { value: "NV", text: "Nevada" },
/> { value: "NH", text: "New Hampshire" },
</Form.Group> { value: "NJ", text: "New Jersey" },
<Form.Group widths="equal"> { value: "NM", text: "New Mexico" },
{ value: "NY", text: "New York" },
{this.state.systemType === "city" && ( { value: "NC", text: "North Carolina" },
<Form.Field> { value: "ND", text: "North Dakota" },
<Form.Input { value: "OH", text: "Ohio" },
type="text" { value: "OK", text: "Oklahoma" },
name="city" { value: "OR", text: "Oregon" },
onChange={this.handleInputChange} { value: "PA", text: "Pennsylvania" },
error={this.state.cityError} { value: "RI", text: "Rhode Island" },
value={this.state.city} { value: "SC", text: "South Carolina" },
label="City" { value: "SD", text: "South Dakota" },
placeholder="City..." { value: "TN", text: "Tennessee" },
/> { value: "TX", text: "Texas" },
</Form.Field> { value: "UT", text: "Utah" },
{ value: "VT", text: "Vermont" },
{ value: "VA", text: "Virginia" },
{ value: "WA", text: "Washington" },
{ value: "WV", text: "West Virginia" },
{ value: "WI", text: "Wisconsin" },
{ value: "WY", text: "Wyoming" }
];
if (
checkInputMessages.length ||
props.requestMessage.length
) {
var counter = 0;
requestMessage = (
<Message compact warning icon>
<Icon name="lightning" />
<Message.Content>
<Message.Header>Problems...</Message.Header>
<ul>
{checkInputMessages.map(function (listValue) {
return <li key={`ListItem_${counter++}`}>{listValue}</li>;
})}
{props.requestMessage.length > 0 && (
<li key="req-mess">{props.requestMessage}</li>
)} )}
{this.state.systemType === "county" && ( </ul>
<Form.Field> </Message.Content>
<Form.Input </Message>
type="text"
name="county"
onChange={this.handleInputChange}
error={this.state.countyError}
value={this.state.county}
label="County"
placeholder="County..."
/>
</Form.Field>
)}
{this.state.systemType === "international" ? (
<Form.Field>
<Form.Input
type="text"
name="country"
onChange={this.handleInputChange}
error={this.state.countryError}
value={this.state.country}
label="Country"
placeholder="Country..."
/>
</Form.Field>
) : (
<Form.Field>
<Form.Dropdown
name="state"
onChange={this.handleInputChange}
error={this.state.stateError}
value={this.state.state}
options={stateOptions}
scrolling
label="State"
placeholder="Select State..."
/>
</Form.Field>
)}
</Form.Group>
<div style={floatStyle}>
<Button type="submit" size="large" floated="right" value="Login">
{isEditing !== true ? "Create" : "Update"}
</Button>
</div>
</Form>
{requestMessage}
</div>
); );
} }
return (
<div>
<Form className="raised padding segment" onSubmit={handleSubmit}>
<Form.Group widths="equal">
<Form.Field>
<Form.Input
type="text"
name="name"
onChange={e => setName(e.target.value)}
error={nameError}
value={name}
label="System name"
placeholder="System Name..."
/>
</Form.Field>
<Form.Field>
<Form.Input
type="text"
name="shortName"
onChange={e => setShortName(e.target.value)}
error={shortNameError}
value={shortName}
disabled={isEditing}
label="System Short Name (4-10 characters)"
placeholder="Short Name..."
/>
</Form.Field>
</Form.Group>
<Divider horizontal />
<Form.Field>
<Checkbox
onChange={handleScreenNameCheckboxChange}
checked={showScreenName}
label="Display Screen Name in System Listing" />
</Form.Field>
<Form.Field>
<Checkbox
onChange={handleTalkgroupCheckboxChange}
checked={ignoreUnknownTalkgroup}
label="Ignore Unknown Talkgroups" />
</Form.Field>
<Divider horizontal />
<Form.TextArea
label="Description"
name="description"
onChange={e => setDescription(e.target.value)}
error={descriptionError}
value={description}
rows={2}
placeholder="Description of system..."
/>
<Divider horizontal><Icon name="marker" />Location</Divider>
<Form.Group inline>
<label>System Type</label>
<Form.Radio
label="State"
name="systemType"
value="state"
checked={systemType === "state"}
onChange={e => setSystemType("state")}
/>
<Form.Radio
label="County"
name="systemType"
value="county"
checked={systemType === "county"}
onChange={e => setSystemType("county")}
/>
<Form.Radio
label="City"
name="systemType"
value="city"
checked={systemType === "city"}
onChange={e => setSystemType("city")}
/>
<Form.Radio
label="International"
name="systemType"
value="international"
checked={systemType === "international"}
onChange={e => setSystemType("international")}
/>
</Form.Group>
<Form.Group widths="equal">
{systemType === "city" && (
<Form.Field>
<Form.Input
type="text"
name="city"
onChange={e => setCity(e.target.value)}
error={cityError}
value={city}
label="City"
placeholder="City..."
/>
</Form.Field>
)}
{systemType === "county" && (
<Form.Field>
<Form.Input
type="text"
name="county"
onChange={e => setCounty(e.target.value)}
error={countyError}
value={county}
label="County"
placeholder="County..."
/>
</Form.Field>
)}
{systemType === "international" ? (
<Form.Field>
<Form.Input
type="text"
name="country"
onChange={e => setCountry(e.target.value)}
error={countryError}
value={country}
label="Country"
placeholder="Country..."
/>
</Form.Field>
) : (
<Form.Field>
<Form.Dropdown
name="state"
onChange={(e, data ) => {setSystemState(data.value); console.log(data.value)}}
error={systemStateError}
value={systemState}
options={stateOptions}
scrolling
label="State"
placeholder="Select State..."
/>
</Form.Field>
)}
</Form.Group>
<div style={floatStyle}>
<Button type="submit" size="large" floated="right" value="Login">
{isEditing !== true ? "Create" : "Update"}
</Button>
</div>
</Form>
{requestMessage}
</div>
);
} }
export default SystemForm; export default SystemForm;

View File

@@ -2,6 +2,23 @@
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react' import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
import { createSelector } from '@reduxjs/toolkit' import { createSelector } from '@reduxjs/toolkit'
/*
const axiosBaseQuery =
({ baseUrl } = { baseUrl: '' }) =>
async ({ url, method, data, params }) => {
try {
const result = await axios({ url: baseUrl + url, method, data, params })
return { data: result.data }
} catch (axiosError) {
let err = axiosError
return {
error: {
status: err.response?.status,
data: err.response?.data || err.message,
},
}
}
}*/
// Define our single API slice object // Define our single API slice object
export const apiSlice = createApi({ export const apiSlice = createApi({
@@ -109,10 +126,28 @@ export const apiSlice = createApi({
getSystems: builder.query({ getSystems: builder.query({
// The URL for the request is '/fakeApi/posts' // The URL for the request is '/fakeApi/posts'
query: () => ({ url: `/systems`, credentials: "include" }) query: () => ({ url: `/systems`, credentials: "include" })
}) }),
createSystem: builder.mutation({
query: (system) => ({
url: `/systems`,
method: 'POST',
credentials: "include",
body: system,
}),
async onQueryStarted(system, { dispatch, queryFulfilled }) {
try {
const { data: newSystem } = await queryFulfilled
const patchResult = dispatch(
apiSlice.util.updateQueryData('getSystems', system.shortName, (systems) => {
systems.push(newSystem);
})
)
} catch {}
},
}),
}) })
}) })
// Export the auto-generated hook for the `getPosts` query endpoint // Export the auto-generated hook for the `getPosts` query endpoint
export const { useGetGroupsQuery, useGetSystemsQuery, useGetTalkgroupsQuery, useGetErrorsQuery, useDeleteGroupMutation, useCreateGroupMutation, useSaveGroupOrderMutation, useUpdateGroupMutation, useImportTalkgroupsMutation } = apiSlice export const { useGetGroupsQuery, useGetSystemsQuery, useGetTalkgroupsQuery, useGetErrorsQuery, useDeleteGroupMutation, useCreateGroupMutation, useCreateSystemMutation, useSaveGroupOrderMutation, useUpdateGroupMutation, useImportTalkgroupsMutation } = apiSlice