Enhancement Split out the fetch call easier mocking

API shirted getFileExtension to DataFormat
This commit is contained in:
Christopher Joe 2016-12-15 12:09:25 +13:00
parent bb71a37cc8
commit d8843c6fe2
3 changed files with 34 additions and 17 deletions

View File

@ -568,14 +568,14 @@ return t.state||(t=c({},t,{state:this.props.schema.state})),t=c({},t,{state:c({}
})}),messages:e.errors.filter(function(e){return!e.field})})}),delete t.errors,(0,b["default"])(t)}},{key:"overrideStateData",value:function m(e){if(!this.props.stateOverrides||!e)return e
var t=this.props.stateOverrides.fields,n=e.fields
return t&&n&&(n=n.map(function(e){var n=t.find(function(t){return t.name===e.name})
return n?E["default"].recursive(!0,e,n):e})),c({},e,this.props.stateOverrides,{fields:n})}},{key:"fetch",value:function g(){var e=this,t=arguments.length<=0||void 0===arguments[0]||arguments[0],n=arguments.length<=1||void 0===arguments[1]||arguments[1],i=[]
return n?E["default"].recursive(!0,e,n):e})),c({},e,this.props.stateOverrides,{fields:n})}},{key:"callFetch",value:function g(e){return(0,v["default"])(this.props.schemaUrl,{headers:{"X-FormSchema-Request":e.join(",")
},credentials:"same-origin"}).then(function(e){return e.json()})}},{key:"fetch",value:function y(){var e=this,t=arguments.length<=0||void 0===arguments[0]||arguments[0],n=arguments.length<=1||void 0===arguments[1]||arguments[1],i=[]
return t&&i.push("schema"),n&&i.push("state"),this.props.loading?Promise.resolve({}):(this.props.actions.schema.setSchemaLoading(this.props.schemaUrl,!0),(0,v["default"])(this.props.schemaUrl,{headers:{
"X-FormSchema-Request":i.join()},credentials:"same-origin"}).then(function(e){return e.json()}).then(function(t){if(e.props.actions.schema.setSchemaLoading(e.props.schemaUrl,!1),"undefined"!=typeof t.id){
var n=c({},t,{state:e.overrideStateData(t.state)})
return e.props.actions.schema.setSchema(e.props.schemaUrl,n),n}return t}))}},{key:"handleAutofill",value:function y(e,t){this.props.actions.reduxForm.autofill(this.props.schemaUrl,e,t)}},{key:"render",
value:function w(){if(!this.props.schema||!this.props.schema.schema||this.props.loading)return null
return t&&i.push("schema"),n&&i.push("state"),this.props.loading?Promise.resolve({}):(this.props.actions.schema.setSchemaLoading(this.props.schemaUrl,!0),this.callFetch(i).then(function(t){if(e.props.actions.schema.setSchemaLoading(e.props.schemaUrl,!1),
"undefined"!=typeof t.id){var n=c({},t,{state:e.overrideStateData(t.state)})
return e.props.actions.schema.setSchema(e.props.schemaUrl,n),n}return t}))}},{key:"handleAutofill",value:function w(e,t){this.props.actions.reduxForm.autofill(this.props.schemaUrl,e,t)}},{key:"render",
value:function C(){if(!this.props.schema||!this.props.schema.schema||this.props.loading)return null
var e=c({},this.props,{form:this.props.schemaUrl,onSubmitSuccess:this.props.onSubmitSuccess,handleSubmit:this.handleSubmit,onAutofill:this.handleAutofill})
return p["default"].createElement(k["default"],e)}}]),t}(f.Component)
j.propTypes=c({},S.basePropTypes,{actions:f.PropTypes.shape({schema:f.PropTypes.object,reduxFrom:f.PropTypes.object}),schemaUrl:f.PropTypes.string.isRequired,schema:S.schemaPropType,form:f.PropTypes.string,
@ -858,14 +858,15 @@ n(this,e)}return i(e,null,[{key:"get",value:function t(e){return window.ss.confi
}}]),e}()
t["default"]=r},function(e,t,n){(function(t){e.exports=t.DataFormat=n(151)}).call(t,function(){return this}())},function(e,t,n){"use strict"
function i(e){return e&&e.__esModule?e:{"default":e}}function r(e,t){if(null===t)return null
if(t){var n=a({},e.query,t),i=c["default"].stringify(n)
if(t){var n=s({},e.query,t),i=d["default"].stringify(n)
return i?"?"+i:""}return e.search}function o(e){var t=null,n=""
return e<1024?(t=e,n="bytes"):e<10240?(t=Math.round(e/1024*10)/10,n="KB"):e<1048576?(t=Math.round(e/1024),n="KB"):e<10485760?(t=Math.round(e/1024*1024*10)/10,n="MB"):e<1073741824&&(t=Math.round(e/1024*1024),
n="MB"),t&&n||(t=Math.round(e/1073741824*10)/10,n="GB"),isNaN(t)?l["default"]._t("File.NO_SIZE","N/A"):t+" "+n}Object.defineProperty(t,"__esModule",{value:!0})
var a=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t]
n="MB"),t&&n||(t=Math.round(e/1073741824*10)/10,n="GB"),isNaN(t)?u["default"]._t("File.NO_SIZE","N/A"):t+" "+n}function a(e){return/[.]/.exec(e)?e.replace(/^.+[.]/,""):""}Object.defineProperty(t,"__esModule",{
value:!0})
var s=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t]
for(var i in n)Object.prototype.hasOwnProperty.call(n,i)&&(e[i]=n[i])}return e}
t.urlQuery=r,t.fileSize=o
var s=n(116),l=i(s),u=n(152),c=i(u)},,,function(e,t,n){(function(t){e.exports=t.ReducerRegister=n(155)}).call(t,function(){return this}())},function(e,t){"use strict"
t.urlQuery=r,t.fileSize=o,t.getFileExtension=a
var l=n(116),u=i(l),c=n(152),d=i(c)},,,function(e,t,n){(function(t){e.exports=t.ReducerRegister=n(155)}).call(t,function(){return this}())},function(e,t){"use strict"
function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0})
var i=function(){function e(e,t){for(var n=0;n<t.length;n++){var i=t[n]
i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}return function(t,n,i){return n&&e(t.prototype,n),i&&e(t,i),t}}(),r={},o=function(){function e(){

View File

@ -185,7 +185,21 @@ class FormBuilderLoader extends Component {
}
/**
* Fetches data used to generate a form. This can be form schema and or form state data.
* Call to make the fetching happen
*
* @param headerValues
* @returns {*}
*/
callFetch(headerValues) {
return fetch(this.props.schemaUrl, {
headers: { 'X-FormSchema-Request': headerValues.join(',') },
credentials: 'same-origin',
})
.then(response => response.json());
}
/**
* Fetches data used to generate a form. This can be form schema and/or form state data.
* When the response comes back the data is saved to state.
*
* @param {Boolean} schema If form schema data should be returned in the response.
@ -211,11 +225,7 @@ class FormBuilderLoader extends Component {
// using `this.state.fetching` caused race-condition issues.
this.props.actions.schema.setSchemaLoading(this.props.schemaUrl, true);
return fetch(this.props.schemaUrl, {
headers: { 'X-FormSchema-Request': headerValues.join() },
credentials: 'same-origin',
})
.then(response => response.json())
return this.callFetch(headerValues)
.then(formSchema => {
this.props.actions.schema.setSchemaLoading(this.props.schemaUrl, false);

View File

@ -47,3 +47,9 @@ export function fileSize(size) {
}
return `${number} ${metric}`;
}
export function getFileExtension(filename) {
return /[.]/.exec(filename)
? filename.replace(/^.+[.]/, '')
: '';
}