I am trying to put in a form component in my app using Redux for the first time and I am trying to get my head around what I have to create Reducers / Actions for.
In other components I have my user and messages passed into mapStateToProps and they work correctly. However in this component I am pulling data from my backend for table fields in the componentDidMount method and I am not sure if it is only data that is to be changed that get stored in Redux.
Do I need to create a reducer for the form as well? or does it get posted straight to the backend / node / postgresql. I intend to have a table that updates with all the most recent data so I can see the logic of it being automatically added to retrieved data.
I am pretty new to React / JavaScript so my logic may be a bit off so any advice would be appreciated.
diveLogForm.component.js
export class DiveLogForm extends Component {
constructor(props){
super(props);
this.handleSubmitDive = this.handleSubmitDive.bind(this);
this.onChangeDiveType = this.onChangeDiveType.bind(this);
this.onChangeSchoolName = this.onChangeSchoolName.bind(this);
this.onChangeCurrent = this.onChangeCurrent.bind(this);
this.onChangeVisibility = this.onChangeVisibility.bind(this);
this.onChangeDiveDate = this.onChangeDiveDate.bind(this);
this.onChangeMaxDepth = this.onChangeMaxDepth.bind(this);
this.onChangeDiverUserNumber = this.onChangeDiverUserNumber.bind(this);
this.onChangeVerifiedBySchool = this.onChangeVerifiedBySchool.bind(this);
this.onChangeDiveNotes = this.onChangeDiveNotes.bind(this);
this.onChangeDivePoint = this.onChangeDivePoint.bind(this);
this.state = {
diveTypeID: "",
diveSchoolNameID: "",
diveCurrentID: "",
diveVisibilityID: "",
diveDate: "",
diveMaxDepth: "",
diverUserNumber: "",
diveVerifiedBySchool: "",
diveNotes: "",
divePoint: "",
currentList: [],
regionList: [],
diveTypeList: [],
visibilityList: [],
diveSpotList: [],
currentUser: [],
loading: false,
};
}
componentDidMount() {
pullCurrentFields().then((response) => {
const { data } = response;
this.setState({ currentList: data.data });
});
pullRegionFields().then((response) => {
const { data } = response;
this.setState({ regionList: data.data });
});
pullDiveTypeFields().then((response) => {
const { data } = response;
this.setState({ diveTypeList: data.data });
});
pullVisibilityFields().then((response) => {
const { data } = response;
this.setState({ visibilityList: data.data });
});
pullDiveSpotFields().then((response) => {
const { data } = response;
this.setState({ diveSpotList: data.data });
});
//this.props.userDiveLogList();
}
onChangeDiveType(e) {
this.setState({
diveTypeID: e.target.value,
});
}
onChangeSchoolName(e) {
this.setState({
diveSchoolNameID: e.target.value,
});
}
onChangeCurrent(e) {
this.setState({
diveCurrentID: e.target.value,
});
}
onChangeVisibility(e){
this.setState({
diveVisibilityID: e.target.value,
});
}
onChangeDiveDate(e) {
this.setState({
diveDate: e.target.value,
});
}
onChangeMaxDepth(e){
this.setState({
diveMaxDepth: e.target.value,
});
}
onChangeDiverUserNumber(e){
this.setState({
diverUserNumber: e.target.value,
});
}
onChangeVerifiedBySchool(e){
this.setState({
diveVerifiedBySchool: e.target.value,
});
}
onChangeDiveNotes(e) {
this.setState({
diveNotes: e.target.value,
});
}
onChangeDivePoint(e){
this.setState({
divePoint: e.target.value,
});
}
handleSubmitDive(e) {
e.preventDefault();
this.setState({
loading: true,
});
this.form.validateAll();
//const {dispatch, history} = this.props;
if (this.checkBtn.context._errors.length === 0) {
this.props
.dispatch(registerUserDive(
this.state.diveTypeID,
this.state.diveSchoolNameID,
this.state.diveCurrentID,
this.state.diveVisibilityID,
this.state.diveDate,
this.state.diveMaxDepth,
this.state.diverUserNumber,
this.state.diveVerifiedBySchool,
this.state.diveNotes,
this.state.diveNotes))
.then(() => {
window.history.push("/divelogtable");
window.location.reload();
})
.catch(() => {
this.setState({
loading: false
});
});
}
}
render() {
const { classes } = this.props;
const { user: currentUser } = this.props;
if (this.state.currentList.length > 0) {
console.log("currentList", this.state.currentList);
}
if (this.state.regionList.length > 0) {
console.log("regionList", this.state.regionList);
}
if (this.state.diveTypeList.length > 0) {
console.log("diveTypeList", this.state.diveTypeList);
}
if (this.state.visibilityList.length > 0) {
console.log("visibilityList", this.state.visibilityList);
}
if (this.state.diveSpotList.length > 0) {
console.log("diveSpotList", this.state.diveSpotList);
}
return (
...materialUI form code
function mapStateToProps(state){
const { user } = state.auth;
const { regionList } = state.region;
const { currentList } = state.current;
const { diveTypeList } = state.diveType;
const { visibilityList } = state.visibility;
const { diveSpotList } = state.diveSpot;
return {
user,
regionList,
currentList,
diveTypeList,
visibilityList,
diveSpotList,
};
}
export default compose(
connect(
mapStateToProps,
),
withStyles(useStyles)
)(DiveLogForm);
As I am primarily concerned with adding my form data to the backend. I have included the diveLog.service.js file etc
export const registerDive = (diveTypeID, diveSchoolNameID, diveCurrentID, diveVisibilityID, diveDate, diveMaxDepth, diveEquipmentWorn, diverUserNumber, diveVerifiedBySchool, diveNotes, divePoint) => {
return axios.post(API_URL + "registerdive", {
diveTypeID,
diveSchoolNameID,
diveCurrentID,
diveVisibilityID,
diveDate,
diveMaxDepth,
diveVerifiedBySchool,
diveNotes,
divePoint
});
};
diveLog.action.js
export const registerUserDive = (
diveTypeID,
diveSchoolNameID,
diveCurrentID,
diveVisibilityID,
diveDate,
diveMaxDepth,
diverUserNumber,
diveVerifiedBySchool,
diveNotes,
divePoint) => (dispatch) => {
return registerDive(
diveTypeID,
diveSchoolNameID,
diveCurrentID,
diveVisibilityID,
diveDate,
diveMaxDepth,
diveVerifiedBySchool,
diveNotes,
divePoint).then(
(response) => {
dispatch ({
type: successful_reg,
});
dispatch({
type: set_message,
payload: response.data.message,
});
return Promise.resolve();
},
(error) => {
const message =
(error.response &&
error.response.data &&
error.response.data.message) ||
error.message ||
error.toString();
dispatch({
type: set_message,
payload: message,
});
return Promise.resolve();
},
(error) => {
(error.response &&
error.response.data &&
error.response.data.message) ||
error.message ||
error.toString();
dispatch({
type: failed_reg,
});
return Promise.reject();
}
);
};
My diveLog register action is likely to be a good bit off as I didn't understand the reducer concept when coding it.