fixes field-type null values on validate, adds locale to data retrieval for edit / archive:

This commit is contained in:
James
2019-01-21 20:53:10 -05:00
parent 6cd9d1d27b
commit 2f9fff391e
12 changed files with 73 additions and 27 deletions

View File

@@ -1,6 +1,7 @@
import Cookies from 'universal-cookie';
import superagentPromise from 'superagent-promise';
import _superagent from 'superagent';
import qs from 'qs';
const cookies = new Cookies();
const superagent = superagentPromise(_superagent, global.Promise);
@@ -12,8 +13,10 @@ const setJwt = () => {
}
const requests = {
get: url =>
superagent.get(`${url}`).set(setJwt()).then(responseBody),
get: (url, params) => {
const query = qs.stringify(params, { addQueryPrefix: true });
return superagent.get(`${url}${query}`).set(setJwt()).then(responseBody);
},
post: (url, body) =>
superagent.post(`${url}`, body).set(setJwt()).then(responseBody),

View File

@@ -1,6 +1,11 @@
import React, { Component } from 'react';
import { connect } from 'react-redux';
import api from 'payload/api';
const mapState = state => ({
locale: state.common.locale
})
const withArchiveData = PassedComponent => {
class ArchiveData extends Component {
@@ -13,19 +18,33 @@ const withArchiveData = PassedComponent => {
}
}
componentDidMount() {
api.requests.get(`${this.props.config.serverUrl}/${this.props.collection.slug}`).then(
fetchData = () => {
const params = {
lang: this.props.locale
};
api.requests.get(`${this.props.config.serverUrl}/${this.props.collection.slug}`, params).then(
res => this.setState({ data: res }),
err => console.warn(err)
)
}
componentDidMount() {
this.fetchData();
}
componentDidUpdate(prevProps) {
if (prevProps.locale !== this.props.locale) {
this.fetchData();
}
}
render() {
return <PassedComponent {...this.props} data={this.state.data} />;
}
}
return ArchiveData;
return connect(mapState)(ArchiveData);
}
export default withArchiveData;

View File

@@ -1,7 +1,12 @@
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { withRouter } from 'react-router-dom';
import api from 'payload/api';
const mapState = state => ({
locale: state.common.locale
})
const withEditData = PassedComponent => {
class EditData extends Component {
@@ -13,11 +18,14 @@ const withEditData = PassedComponent => {
}
}
componentDidMount() {
fetchData = () => {
const slug = this.props.match.params.slug;
const params = {
lang: this.props.locale
};
if (slug) {
api.requests.get(`${this.props.config.serverUrl}/${this.props.collection.slug}/${slug}`).then(
api.requests.get(`${this.props.config.serverUrl}/${this.props.collection.slug}/${slug}`, params).then(
res => this.setState({ data: res }),
err => {
console.warn(err);
@@ -27,12 +35,22 @@ const withEditData = PassedComponent => {
}
}
componentDidMount() {
this.fetchData();
}
componentDidUpdate(prevProps) {
if (prevProps.locale !== this.props.locale) {
this.fetchData();
}
}
render() {
return <PassedComponent {...this.props} data={this.state.data } />;
return <PassedComponent {...this.props} data={this.state.data} />;
}
}
return withRouter(EditData);
return withRouter(connect(mapState)(EditData));
}
export default withEditData;

View File

@@ -16,7 +16,7 @@ const Email = props => {
{props.error}
{props.label}
<input
value={props.value}
value={props.value || ''}
onChange={props.onChange}
disabled={props.disabled}
placeholder={props.placeholder}

View File

@@ -1,7 +1,7 @@
import React from 'react';
import { fieldType } from 'payload/components';
const HiddenInput = props => <input type="hidden" value={props.value}
const HiddenInput = props => <input type="hidden" value={props.value || ''}
onChange={props.onChange} id={props.id ? props.id : props.name} name={props.name} />;
export default fieldType(HiddenInput, 'hiddenInput');

View File

@@ -16,7 +16,7 @@ const Input = props => {
{props.error}
{props.label}
<input
value={props.value}
value={props.value || ''}
onChange={props.onChange}
disabled={props.disabled}
placeholder={props.placeholder}

View File

@@ -19,7 +19,7 @@ const Password = props => {
{props.error}
{props.label}
<input
value={props.value}
value={props.value || ''}
onChange={props.onChange}
disabled={props.disabled}
placeholder={props.placeholder}

View File

@@ -13,7 +13,7 @@ const Textarea = props => {
{props.error}
{props.label}
<textarea
value={ props.value }
value={props.value || ''}
onChange={props.onChange}
disabled={props.disabled}
placeholder={props.placeholder}

View File

@@ -20,7 +20,7 @@ const fieldType = (PassedComponent, type, validate, errors) => {
name: this.props.name,
value: value,
valid: this.props.required && validate
? validate(value, this.props.type)
? validate(value || '', this.props.type)
: true
});
}
@@ -48,8 +48,8 @@ const fieldType = (PassedComponent, type, validate, errors) => {
render() {
const valid = this.props.context.fields[this.props.name]
? this.props.context.fields[this.props.name].valid
: true;
? this.props.context.fields[this.props.name].valid
: true;
const showError = valid === false && this.props.context.submitted;
@@ -62,14 +62,14 @@ const fieldType = (PassedComponent, type, validate, errors) => {
return (
<PassedComponent {...this.props}
className={className}
value={value}
label={<Label {...this.props} />}
error={<Error showError={showError} type={this.props.type} />}
onChange={e => {
this.sendField(e.target.value);
this.props.onChange && this.props.onChange(e);
}} />
className={className}
value={value}
label={<Label {...this.props} />}
error={<Error showError={showError} type={this.props.type} />}
onChange={e => {
this.sendField(e.target.value);
this.props.onChange && this.props.onChange(e);
}} />
)
}
}

View File

@@ -12,12 +12,16 @@ const mapState = state => ({
const Localizer = props => {
const { languages } = props.config.localization;
if (languages.length <= 1) return null;
return (
<div className="localizer">
<span>{props.locale}</span>
<ul>
{languages.map((lang, i) => {
if (lang === props.locale) return null;
const newParams = {
...props.searchParams,
lang: lang

View File

@@ -67,7 +67,6 @@ export default (state = defaultState, action) => {
};
case 'SET_LOCALE':
console.log(action.payload);
return {
...state,
locale: action.payload