From 9d3a6f0e4e914a07526a907c83f5e4edbaecaae8 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Wed, 10 Jan 2024 15:06:01 +1300 Subject: [PATCH] ENH Gracefully handle AJAX failures --- client/dist/js/bundle.js | 2 +- client/lang/en.js | 7 +++++++ client/lang/src/en.json | 2 ++ client/src/components/LinkField/LinkField.js | 12 +++++------- client/src/components/LinkModal/LinkModal.js | 12 +++++++++++- client/src/containers/LinkModalContainer.js | 2 +- 6 files changed, 27 insertions(+), 10 deletions(-) diff --git a/client/dist/js/bundle.js b/client/dist/js/bundle.js index e4457d97..69702b5d 100644 --- a/client/dist/js/bundle.js +++ b/client/dist/js/bundle.js @@ -1 +1 @@ -!function(){"use strict";var e={274:function(e,t,n){var r,l=(r=n(521))&&r.__esModule?r:{default:r};document.addEventListener("DOMContentLoaded",(()=>{(0,l.default)()}))},521:function(e,t,n){Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var r=u(n(648)),l=u(n(809)),o=u(n(852)),a=u(n(117)),i=u(n(606));function u(e){return e&&e.__esModule?e:{default:e}}var s=()=>{r.default.component.registerMany({LinkPicker:l.default,LinkField:o.default,"LinkModal.FormBuilderModal":a.default,"LinkModal.InsertMediaModal":i.default})};t.default=s},852:function(e,t,n){Object.defineProperty(t,"__esModule",{value:!0}),t.default=t.LinkFieldContext=t.Component=void 0;var r=h(n(363)),l=n(827),o=n(624),a=(n(648),_(n(42))),i=_(n(809)),u=_(n(734)),s=(_(n(686)),_(n(697))),d=h(n(123)),f=_(n(159)),c=_(n(510)),p=_(n(86)),y=_(n(754)),v=_(n(872)),k=_(n(902));function _(e){return e&&e.__esModule?e:{default:e}}function m(e){if("function"!=typeof WeakMap)return null;var t=new WeakMap,n=new WeakMap;return(m=function(e){return e?n:t})(e)}function h(e,t){if(!t&&e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var n=m(t);if(n&&n.has(e))return n.get(e);var r={},l=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var o in e)if("default"!==o&&Object.prototype.hasOwnProperty.call(e,o)){var a=l?Object.getOwnPropertyDescriptor(e,o):null;a&&(a.get||a.set)?Object.defineProperty(r,o,a):r[o]=e[o]}return r.default=e,n&&n.set(e,r),r}const O=(0,r.createContext)(null);t.LinkFieldContext=O;const g="SilverStripe\\LinkField\\Controllers\\LinkFieldController",b=e=>{var t;let{value:n=null,onChange:l,types:o=[],actions:a,isMulti:d=!1,canCreate:p,ownerID:_,ownerClass:m,ownerRelation:h}=e;const[b,C]=(0,r.useState)({}),[M,R]=(0,r.useState)(0);let w=n;Array.isArray(w)||("number"==typeof w&&0!=w&&(w=[w]),w||(w=[])),(0,r.useEffect)((()=>{if(!M&&w.length>0){const e=[];for(const t of w)e.push(`itemIDs[]=${t}`);const t=`${c.default.getSection(g).form.linkForm.dataUrl}?${e.join("&")}`;f.default.get(t).then((e=>e.json())).then((e=>{C(e)}))}}),[M,n&&n.length]);const E=()=>{R(0)},D=e=>{R(0);const t=[...w];t.includes(e)||t.push(e),l(d?t:t[0]),a.toasts.success(y.default._t("LinkField.SAVE_SUCCESS","Saved link"))},j=e=>{var t;let n=`${c.default.getSection(g).form.linkForm.deleteUrl}/${e}`;const r=v.default.parse(n),o=k.default.parse(r.query);o.ownerID=_,o.ownerClass=m,o.ownerRelation=h,n=v.default.format({...r,search:k.default.stringify(o)});const i=(null===(t=b[e])||void 0===t?void 0:t.versionState)||"",u=["draft","modified","published"].includes(i),s=u?y.default._t("LinkField.ARCHIVE_SUCCESS","Archived link"):y.default._t("LinkField.DELETE_SUCCESS","Deleted link"),p=u?y.default._t("LinkField.ARCHIVE_ERROR","Failed to archive link"):y.default._t("LinkField.DELETE_ERROR","Failed to delete link");f.default.delete(n,{},{"X-SecurityID":c.default.get("SecurityID")}).then((()=>a.toasts.success(s))).catch((()=>a.toasts.error(p)));const O={...b};delete O[e],C(O),l(d?Object.keys(O):0)},I=0===_,S=!I&&(d||0===Object.keys(b).length),L=!I&&Boolean(M),P=y.default._t("LinkField.SAVE_RECORD_FIRST","Cannot add links until the record has been saved");return r.default.createElement(O.Provider,{value:{ownerID:_,ownerClass:m,ownerRelation:h}},I&&r.default.createElement("div",{className:"link-field__save-record-first"},P),S&&r.default.createElement(i.default,{onModalSuccess:D,onModalClosed:E,types:o,canCreate:p}),r.default.createElement("div",null," ",(()=>{const e=[];for(const d of w){var t,n,l,a,i,s;if(!b[d])continue;const f=o.hasOwnProperty(null===(t=b[d])||void 0===t?void 0:t.typeKey)?o[null===(n=b[d])||void 0===n?void 0:n.typeKey]:{};e.push(r.default.createElement(u.default,{key:d,id:d,title:null===(l=b[d])||void 0===l?void 0:l.Title,description:null===(a=b[d])||void 0===a?void 0:a.description,versionState:null===(i=b[d])||void 0===i?void 0:i.versionState,typeTitle:f.title||"",onDelete:j,onClick:()=>{R(d)},canDelete:!(null===(s=b[d])||void 0===s||!s.canDelete)}))}return e})()," "),L&&r.default.createElement(s.default,{types:o,typeKey:null===(t=b[M])||void 0===t?void 0:t.typeKey,isOpen:Boolean(M),onSuccess:D,onClosed:E,linkID:M}))};t.Component=b,b.propTypes={value:p.default.oneOfType([p.default.arrayOf(p.default.number),p.default.number]),onChange:p.default.func.isRequired,types:p.default.array.isRequired,actions:p.default.object.isRequired,isMulti:p.default.bool,canCreate:p.default.bool.isRequired,ownerID:p.default.number.isRequired,ownerClass:p.default.string.isRequired,ownerRelation:p.default.string.isRequired};var C=(0,l.compose)(a.default,(0,o.connect)(null,(e=>({actions:{toasts:(0,l.bindActionCreators)(d,e)}}))))(b);t.default=C},606:function(e,t,n){Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;s(n(754));var r=function(e,t){if(!t&&e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var n=u(t);if(n&&n.has(e))return n.get(e);var r={},l=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var o in e)if("default"!==o&&Object.prototype.hasOwnProperty.call(e,o)){var a=l?Object.getOwnPropertyDescriptor(e,o):null;a&&(a.get||a.set)?Object.defineProperty(r,o,a):r[o]=e[o]}r.default=e,n&&n.set(e,r);return r}(n(363)),l=s(n(475)),o=n(624),a=s(n(686)),i=s(n(86));function u(e){if("function"!=typeof WeakMap)return null;var t=new WeakMap,n=new WeakMap;return(u=function(e){return e?n:t})(e)}function s(e){return e&&e.__esModule?e:{default:e}}function d(){return d=Object.assign?Object.assign.bind():function(e){for(var t=1;t{let{type:t,editing:n,data:o,actions:a,onSubmit:i,...u}=e;if(!t)return!1;(0,r.useEffect)((()=>{n?a.initModal():a.reset()}),[n]);const s=o?{ID:o.FileID,Description:o.Title,TargetBlank:!!o.OpenInNew}:{};return r.default.createElement(l.default,d({isOpen:n,type:"insert-link",title:!1,bodyClassName:"modal__dialog",className:"insert-link__dialog-wrapper--internal",fileAttributes:s,onInsert:e=>{let{ID:n,Description:r,TargetBlank:l}=e;return i({FileID:n,Title:r,OpenInNew:l,typeKey:t.key},"",(()=>{}))}},u))};f.propTypes={type:a.default.isRequired,editing:i.default.bool.isRequired,data:i.default.object.isRequired,actions:i.default.object.isRequired,onClick:i.default.func.isRequired};var c=(0,o.connect)((function(){return{}}),(function(e){return{actions:{initModal:()=>e({type:"INIT_FORM_SCHEMA_STACK",payload:{formSchema:{type:"insert-link",nextType:"admin"}}}),reset:()=>e({type:"RESET"})}}}))(f);t.default=c},117:function(e,t,n){Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var r=function(e,t){if(!t&&e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var n=f(t);if(n&&n.has(e))return n.get(e);var r={},l=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var o in e)if("default"!==o&&Object.prototype.hasOwnProperty.call(e,o)){var a=l?Object.getOwnPropertyDescriptor(e,o):null;a&&(a.get||a.set)?Object.defineProperty(r,o,a):r[o]=e[o]}r.default=e,n&&n.set(e,r);return r}(n(363)),l=d(n(912)),o=n(852),a=d(n(872)),i=d(n(902)),u=d(n(510)),s=d(n(86));function d(e){return e&&e.__esModule?e:{default:e}}function f(e){if("function"!=typeof WeakMap)return null;var t=new WeakMap,n=new WeakMap;return(f=function(e){return e?n:t})(e)}const c=(e,t)=>{const{schemaUrl:n}=u.default.getSection("SilverStripe\\LinkField\\Controllers\\LinkFieldController").form.linkForm,l=a.default.parse(n),s=i.default.parse(l.query);s.typeKey=e;const{ownerID:d,ownerClass:f,ownerRelation:c}=(0,r.useContext)(o.LinkFieldContext);s.ownerID=d,s.ownerClass=f,s.ownerRelation=c;for(const e of["href","path","pathname"])l[e]=`${l[e]}/${t}`;return a.default.format({...l,search:i.default.stringify(s)})},p=e=>{let{typeTitle:t,typeKey:n,linkID:o=0,isOpen:a,onSuccess:i,onClosed:u}=e;if(!n)return!1;return r.default.createElement(l.default,{title:t,isOpen:a,schemaUrl:c(n,o),identifier:"Link.EditingLinkInfo",onSubmit:async(e,t,n)=>{const r=await n();if(!r.id.match(/\/schema\/linkfield\/([0-9]+)/)){const e=r.id.match(/\/linkForm\/([0-9]+)/),t=parseInt(e[1],10);i(t)}return Promise.resolve()},onClosed:u})};p.propTypes={typeTitle:s.default.string.isRequired,typeKey:s.default.string.isRequired,linkID:s.default.number,isOpen:s.default.bool.isRequired,onSuccess:s.default.func.isRequired,onClosed:s.default.func.isRequired};var y=p;t.default=y},809:function(e,t,n){Object.defineProperty(t,"__esModule",{value:!0}),t.default=t.Component=void 0;var r=d(n(754)),l=function(e,t){if(!t&&e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var n=s(t);if(n&&n.has(e))return n.get(e);var r={},l=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var o in e)if("default"!==o&&Object.prototype.hasOwnProperty.call(e,o)){var a=l?Object.getOwnPropertyDescriptor(e,o):null;a&&(a.get||a.set)?Object.defineProperty(r,o,a):r[o]=e[o]}r.default=e,n&&n.set(e,r);return r}(n(363)),o=d(n(86)),a=d(n(820)),i=d(n(97)),u=(d(n(686)),d(n(697)));function s(e){if("function"!=typeof WeakMap)return null;var t=new WeakMap,n=new WeakMap;return(s=function(e){return e?n:t})(e)}function d(e){return e&&e.__esModule?e:{default:e}}const f=e=>{let{types:t,onModalSuccess:n,onModalClosed:o,canCreate:s}=e;const[d,f]=(0,l.useState)(""),c=""!==d,p=(0,a.default)("link-picker","form-control"),y=Object.values(t);return s?l.default.createElement("div",{className:p},l.default.createElement(i.default,{types:y,onSelect:e=>{f(e)}}),c&&l.default.createElement(u.default,{types:t,typeKey:d,isOpen:c,onSuccess:e=>{f(""),n(e)},onClosed:()=>{"function"==typeof o&&o(),f("")}})):l.default.createElement("div",{className:p},l.default.createElement("div",{className:"link-picker__cannot-create"},r.default._t("LinkField.CANNOT_CREATE_LINK","Cannot create link")))};t.Component=f,f.propTypes={types:o.default.array.isRequired,onModalSuccess:o.default.func.isRequired,onModalClosed:o.default.func,canCreate:o.default.bool.isRequired};var c=f;t.default=c},97:function(e,t,n){Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var r=s(n(754)),l=function(e,t){if(!t&&e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var n=u(t);if(n&&n.has(e))return n.get(e);var r={},l=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var o in e)if("default"!==o&&Object.prototype.hasOwnProperty.call(e,o)){var a=l?Object.getOwnPropertyDescriptor(e,o):null;a&&(a.get||a.set)?Object.defineProperty(r,o,a):r[o]=e[o]}r.default=e,n&&n.set(e,r);return r}(n(363)),o=s(n(86)),a=n(127),i=s(n(686));function u(e){if("function"!=typeof WeakMap)return null;var t=new WeakMap,n=new WeakMap;return(u=function(e){return e?n:t})(e)}function s(e){return e&&e.__esModule?e:{default:e}}const d=e=>{let{types:t,onSelect:n}=e;const[o,i]=(0,l.useState)(!1);return l.default.createElement(a.Dropdown,{isOpen:o,toggle:()=>i((e=>!e)),className:"link-picker__menu"},l.default.createElement(a.DropdownToggle,{className:"link-picker__menu-toggle font-icon-plus-1",caret:!0},r.default._t("LinkField.ADD_LINK","Add Link")),l.default.createElement(a.DropdownMenu,null,t.map((e=>{let{key:t,title:r}=e;return l.default.createElement(a.DropdownItem,{key:t,onClick:()=>n(t)},r)}))))};d.propTypes={types:o.default.arrayOf(i.default).isRequired,onSelect:o.default.func.isRequired};var f=d;t.default=f},734:function(e,t,n){Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var r=u(n(820)),l=u(n(754)),o=u(n(363)),a=u(n(86)),i=n(127);function u(e){return e&&e.__esModule?e:{default:e}}const s=e=>t=>{t.nativeEvent.stopImmediatePropagation(),t.preventDefault(),t.nativeEvent.preventDefault(),t.stopPropagation(),e&&e()},d=e=>{let{id:t,title:n,description:a,versionState:u,typeTitle:d,onDelete:f,onClick:c,canDelete:p}=e;const y={"link-picker__link":!0,"form-control":!0};u&&(y[` link-picker__link--${u}`]=!0);const v=(0,r.default)(y),k=["unversioned","unsaved"].includes(u)?l.default._t("LinkField.DELETE","Delete"):l.default._t("LinkField.ARCHIVE","Archive");return o.default.createElement("div",{className:v},o.default.createElement(i.Button,{className:"link-picker__button font-icon-link",color:"secondary",onClick:s(c)},o.default.createElement("div",{className:"link-picker__link-detail"},o.default.createElement("div",{className:"link-picker__title"},o.default.createElement("span",{className:"link-picker__title-text"},n),(e=>{let t="",n="";if("draft"===e)t=l.default._t("LinkField.LINK_DRAFT_TITLE","Link has draft changes"),n=l.default._t("LinkField.LINK_DRAFT_LABEL","Draft");else{if("modified"!==e)return null;t=l.default._t("LinkField.LINK_MODIFIED_TITLE","Link has unpublished changes"),n=l.default._t("LinkField.LINK_MODIFIED_LABEL","Modified")}const a=(0,r.default)("badge",`status-${e}`);return o.default.createElement("span",{className:a,title:t},n)})(u)),o.default.createElement("small",{className:"link-picker__type"},d,": ",o.default.createElement("span",{className:"link-picker__url"},a)))),p&&o.default.createElement(i.Button,{className:"link-picker__delete",color:"link",onClick:s((()=>f(t)))},k))};d.propTypes={id:a.default.number.isRequired,title:a.default.string,description:a.default.string,versionState:a.default.string,typeTitle:a.default.string.isRequired,onDelete:a.default.func.isRequired,onClick:a.default.func.isRequired,canDelete:a.default.bool.isRequired};var f=d;t.default=f},697:function(e,t,n){Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var r=a(n(363)),l=n(648),o=a(n(86));function a(e){return e&&e.__esModule?e:{default:e}}const i=e=>{let{types:t,typeKey:n,linkID:o=0,isOpen:a,onSuccess:i,onClosed:u}=e;if(!n)return!1;const s=t.hasOwnProperty(n)?t[n]:{},d=s&&s.hasOwnProperty("handlerName")?s.handlerName:"FormBuilderModal",f=(0,l.loadComponent)(`LinkModal.${d}`);return r.default.createElement(f,{typeTitle:s.title||"",typeKey:n,linkID:o,isOpen:a,onSuccess:i,onClosed:u})};i.propTypes={types:o.default.array.isRequired,typeKey:o.default.string.isRequired,linkID:o.default.number,isOpen:o.default.bool.isRequired,onSuccess:o.default.func.isRequired,onClosed:o.default.func.isRequired};var u=i;t.default=u},41:function(e,t,n){var r=i(n(311)),l=i(n(363)),o=i(n(691)),a=n(648);function i(e){return e&&e.__esModule?e:{default:e}}function u(){return u=Object.assign?Object.assign.bind():function(e){for(var t=1;t{e(".js-injector-boot .entwine-linkfield").entwine({Component:null,Root:null,onmatch(){const e=this.closest(".cms-content").attr("id"),t=e?{context:e}:{},n=this.data("schema-component"),r=(0,a.loadComponent)(n,t);this.setComponent(r),this.setRoot(o.default.createRoot(this[0])),this._super(),this.refresh()},refresh(){const e=this.getProps();this.getInputField().val(e.value);const t=this.getComponent();this.getRoot().render(l.default.createElement(t,u({},e,{noHolder:!0})))},handleChange(e){this.getInputField().data("value",e),this.refresh()},getProps(){const e=this.getInputField();return{value:e.data("value"),ownerID:e.data("owner-id"),ownerClass:e.data("owner-class"),ownerRelation:e.data("owner-relation"),onChange:this.handleChange.bind(this),isMulti:this.data("is-multi")??!1,types:this.data("types")??[],canCreate:this.getInputField().data("can-create")??!1}},getInputField(){const t=this.data("field-id");return e(`#${t}`)},onunmatch(){const e=this.getRoot();e&&e.unmount()}})}))},686:function(e,t,n){Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var r,l=(r=n(86))&&r.__esModule?r:{default:r};var o=l.default.shape({key:l.default.string.isRequired,title:l.default.string.isRequired});t.default=o},159:function(e){e.exports=Backend},510:function(e){e.exports=Config},42:function(e){e.exports=FieldHolder},912:function(e){e.exports=FormBuilderModal},648:function(e){e.exports=Injector},475:function(e){e.exports=InsertMediaModal},872:function(e){e.exports=NodeUrl},86:function(e){e.exports=PropTypes},363:function(e){e.exports=React},691:function(e){e.exports=ReactDomClient},624:function(e){e.exports=ReactRedux},127:function(e){e.exports=Reactstrap},827:function(e){e.exports=Redux},123:function(e){e.exports=ToastsActions},820:function(e){e.exports=classnames},754:function(e){e.exports=i18n},311:function(e){e.exports=jQuery},902:function(e){e.exports=qs}},t={};function n(r){var l=t[r];if(void 0!==l)return l.exports;var o=t[r]={exports:{}};return e[r](o,o.exports,n),o.exports}n(274),n(41)}(); \ No newline at end of file +!function(){"use strict";var e={274:function(e,t,n){var r,l=(r=n(521))&&r.__esModule?r:{default:r};document.addEventListener("DOMContentLoaded",(()=>{(0,l.default)()}))},521:function(e,t,n){Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var r=u(n(648)),l=u(n(809)),o=u(n(852)),i=u(n(117)),a=u(n(606));function u(e){return e&&e.__esModule?e:{default:e}}var s=()=>{r.default.component.registerMany({LinkPicker:l.default,LinkField:o.default,"LinkModal.FormBuilderModal":i.default,"LinkModal.InsertMediaModal":a.default})};t.default=s},852:function(e,t,n){Object.defineProperty(t,"__esModule",{value:!0}),t.default=t.LinkFieldContext=t.Component=void 0;var r=h(n(363)),l=n(827),o=n(624),i=(n(648),k(n(42))),a=k(n(809)),u=k(n(734)),s=(k(n(686)),k(n(697))),d=h(n(123)),c=k(n(159)),f=k(n(510)),p=k(n(86)),y=k(n(754)),v=k(n(872)),_=k(n(902));function k(e){return e&&e.__esModule?e:{default:e}}function m(e){if("function"!=typeof WeakMap)return null;var t=new WeakMap,n=new WeakMap;return(m=function(e){return e?n:t})(e)}function h(e,t){if(!t&&e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var n=m(t);if(n&&n.has(e))return n.get(e);var r={},l=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var o in e)if("default"!==o&&Object.prototype.hasOwnProperty.call(e,o)){var i=l?Object.getOwnPropertyDescriptor(e,o):null;i&&(i.get||i.set)?Object.defineProperty(r,o,i):r[o]=e[o]}return r.default=e,n&&n.set(e,r),r}const O=(0,r.createContext)(null);t.LinkFieldContext=O;const g="SilverStripe\\LinkField\\Controllers\\LinkFieldController",b=e=>{var t;let{value:n=null,onChange:l,types:o=[],actions:i,isMulti:d=!1,canCreate:p,ownerID:k,ownerClass:m,ownerRelation:h}=e;const[b,C]=(0,r.useState)({}),[M,R]=(0,r.useState)(0);let E=n;Array.isArray(E)||("number"==typeof E&&0!=E&&(E=[E]),E||(E=[])),(0,r.useEffect)((()=>{if(!M&&E.length>0){const e=[];for(const t of E)e.push(`itemIDs[]=${t}`);const t=`${f.default.getSection(g).form.linkForm.dataUrl}?${e.join("&")}`;c.default.get(t).then((e=>e.json())).then((e=>{C(e)})).catch((()=>{i.toasts.error(y.default._t("LinkField.FAILED_TO_LOAD_LINKS","Failed to load links"))}))}}),[M,n&&n.length]);const w=()=>{R(0)},D=e=>{R(0);const t=[...E];t.includes(e)||t.push(e),l(d?t:t[0]),i.toasts.success(y.default._t("LinkField.SAVE_SUCCESS","Saved link"))},j=e=>{var t;let n=`${f.default.getSection(g).form.linkForm.deleteUrl}/${e}`;const r=v.default.parse(n),o=_.default.parse(r.query);o.ownerID=k,o.ownerClass=m,o.ownerRelation=h,n=v.default.format({...r,search:_.default.stringify(o)});const a=(null===(t=b[e])||void 0===t?void 0:t.versionState)||"",u=["draft","modified","published"].includes(a),s=u?y.default._t("LinkField.ARCHIVE_SUCCESS","Archived link"):y.default._t("LinkField.DELETE_SUCCESS","Deleted link"),p=u?y.default._t("LinkField.ARCHIVE_ERROR","Failed to archive link"):y.default._t("LinkField.DELETE_ERROR","Failed to delete link");c.default.delete(n,{},{"X-SecurityID":f.default.get("SecurityID")}).then((()=>i.toasts.success(s))).catch((()=>i.toasts.error(p)));const O={...b};delete O[e],C(O),l(d?Object.keys(O):0)},I=0===k,L=!I&&(d||0===Object.keys(b).length),F=!I&&Boolean(M),S=y.default._t("LinkField.SAVE_RECORD_FIRST","Cannot add links until the record has been saved");return r.default.createElement(O.Provider,{value:{ownerID:k,ownerClass:m,ownerRelation:h,actions:i}},I&&r.default.createElement("div",{className:"link-field__save-record-first"},S),L&&r.default.createElement(a.default,{onModalSuccess:D,onModalClosed:w,types:o,canCreate:p}),r.default.createElement("div",null," ",(()=>{const e=[];for(const d of E){var t,n,l,i,a,s;if(!b[d])continue;const c=o.hasOwnProperty(null===(t=b[d])||void 0===t?void 0:t.typeKey)?o[null===(n=b[d])||void 0===n?void 0:n.typeKey]:{};e.push(r.default.createElement(u.default,{key:d,id:d,title:null===(l=b[d])||void 0===l?void 0:l.Title,description:null===(i=b[d])||void 0===i?void 0:i.description,versionState:null===(a=b[d])||void 0===a?void 0:a.versionState,typeTitle:c.title||"",onDelete:j,onClick:()=>{R(d)},canDelete:!(null===(s=b[d])||void 0===s||!s.canDelete)}))}return e})()," "),F&&r.default.createElement(s.default,{types:o,typeKey:null===(t=b[M])||void 0===t?void 0:t.typeKey,isOpen:Boolean(M),onSuccess:D,onClosed:w,linkID:M}))};t.Component=b,b.propTypes={value:p.default.oneOfType([p.default.arrayOf(p.default.number),p.default.number]),onChange:p.default.func.isRequired,types:p.default.array.isRequired,actions:p.default.object.isRequired,isMulti:p.default.bool,canCreate:p.default.bool.isRequired,ownerID:p.default.number.isRequired,ownerClass:p.default.string.isRequired,ownerRelation:p.default.string.isRequired};var C=(0,l.compose)(i.default,(0,o.connect)(null,(e=>({actions:{toasts:(0,l.bindActionCreators)(d,e)}}))))(b);t.default=C},606:function(e,t,n){Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;s(n(754));var r=function(e,t){if(!t&&e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var n=u(t);if(n&&n.has(e))return n.get(e);var r={},l=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var o in e)if("default"!==o&&Object.prototype.hasOwnProperty.call(e,o)){var i=l?Object.getOwnPropertyDescriptor(e,o):null;i&&(i.get||i.set)?Object.defineProperty(r,o,i):r[o]=e[o]}r.default=e,n&&n.set(e,r);return r}(n(363)),l=s(n(475)),o=n(624),i=s(n(686)),a=s(n(86));function u(e){if("function"!=typeof WeakMap)return null;var t=new WeakMap,n=new WeakMap;return(u=function(e){return e?n:t})(e)}function s(e){return e&&e.__esModule?e:{default:e}}function d(){return d=Object.assign?Object.assign.bind():function(e){for(var t=1;t{let{type:t,editing:n,data:o,actions:i,onSubmit:a,...u}=e;if(!t)return!1;(0,r.useEffect)((()=>{n?i.initModal():i.reset()}),[n]);const s=o?{ID:o.FileID,Description:o.Title,TargetBlank:!!o.OpenInNew}:{};return r.default.createElement(l.default,d({isOpen:n,type:"insert-link",title:!1,bodyClassName:"modal__dialog",className:"insert-link__dialog-wrapper--internal",fileAttributes:s,onInsert:e=>{let{ID:n,Description:r,TargetBlank:l}=e;return a({FileID:n,Title:r,OpenInNew:l,typeKey:t.key},"",(()=>{}))}},u))};c.propTypes={type:i.default.isRequired,editing:a.default.bool.isRequired,data:a.default.object.isRequired,actions:a.default.object.isRequired,onClick:a.default.func.isRequired};var f=(0,o.connect)((function(){return{}}),(function(e){return{actions:{initModal:()=>e({type:"INIT_FORM_SCHEMA_STACK",payload:{formSchema:{type:"insert-link",nextType:"admin"}}}),reset:()=>e({type:"RESET"})}}}))(c);t.default=f},117:function(e,t,n){Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var r=function(e,t){if(!t&&e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var n=c(t);if(n&&n.has(e))return n.get(e);var r={},l=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var o in e)if("default"!==o&&Object.prototype.hasOwnProperty.call(e,o)){var i=l?Object.getOwnPropertyDescriptor(e,o):null;i&&(i.get||i.set)?Object.defineProperty(r,o,i):r[o]=e[o]}r.default=e,n&&n.set(e,r);return r}(n(363)),l=d(n(912)),o=n(852),i=d(n(872)),a=d(n(902)),u=d(n(510)),s=d(n(86));function d(e){return e&&e.__esModule?e:{default:e}}function c(e){if("function"!=typeof WeakMap)return null;var t=new WeakMap,n=new WeakMap;return(c=function(e){return e?n:t})(e)}const f=(e,t)=>{const{schemaUrl:n}=u.default.getSection("SilverStripe\\LinkField\\Controllers\\LinkFieldController").form.linkForm,l=i.default.parse(n),s=a.default.parse(l.query);s.typeKey=e;const{ownerID:d,ownerClass:c,ownerRelation:f}=(0,r.useContext)(o.LinkFieldContext);s.ownerID=d,s.ownerClass=c,s.ownerRelation=f;for(const e of["href","path","pathname"])l[e]=`${l[e]}/${t}`;return i.default.format({...l,search:a.default.stringify(s)})},p=e=>{let{typeTitle:t,typeKey:n,linkID:i=0,isOpen:a,onSuccess:u,onClosed:s}=e;const{actions:d}=(0,r.useContext)(o.LinkFieldContext);if(!n)return!1;return r.default.createElement(l.default,{title:t,isOpen:a,schemaUrl:f(n,i),identifier:"Link.EditingLinkInfo",onSubmit:async(e,t,n)=>{let r=null;try{r=await n()}catch(e){return d.toasts.error(i18n._t("LinkField.FAILED_TO_SAVE_LINK","Failed to save link")),Promise.resolve()}if(!r.id.match(/\/schema\/linkfield\/([0-9]+)/)){const e=r.id.match(/\/linkForm\/([0-9]+)/),t=parseInt(e[1],10);u(t)}return Promise.resolve()},onClosed:s})};p.propTypes={typeTitle:s.default.string.isRequired,typeKey:s.default.string.isRequired,linkID:s.default.number,isOpen:s.default.bool.isRequired,onSuccess:s.default.func.isRequired,onClosed:s.default.func.isRequired};var y=p;t.default=y},809:function(e,t,n){Object.defineProperty(t,"__esModule",{value:!0}),t.default=t.Component=void 0;var r=d(n(754)),l=function(e,t){if(!t&&e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var n=s(t);if(n&&n.has(e))return n.get(e);var r={},l=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var o in e)if("default"!==o&&Object.prototype.hasOwnProperty.call(e,o)){var i=l?Object.getOwnPropertyDescriptor(e,o):null;i&&(i.get||i.set)?Object.defineProperty(r,o,i):r[o]=e[o]}r.default=e,n&&n.set(e,r);return r}(n(363)),o=d(n(86)),i=d(n(820)),a=d(n(97)),u=(d(n(686)),d(n(697)));function s(e){if("function"!=typeof WeakMap)return null;var t=new WeakMap,n=new WeakMap;return(s=function(e){return e?n:t})(e)}function d(e){return e&&e.__esModule?e:{default:e}}const c=e=>{let{types:t,onModalSuccess:n,onModalClosed:o,canCreate:s}=e;const[d,c]=(0,l.useState)(""),f=""!==d,p=(0,i.default)("link-picker","form-control"),y=Object.values(t);return s?l.default.createElement("div",{className:p},l.default.createElement(a.default,{types:y,onSelect:e=>{c(e)}}),f&&l.default.createElement(u.default,{types:t,typeKey:d,isOpen:f,onSuccess:e=>{c(""),n(e)},onClosed:()=>{"function"==typeof o&&o(),c("")}})):l.default.createElement("div",{className:p},l.default.createElement("div",{className:"link-picker__cannot-create"},r.default._t("LinkField.CANNOT_CREATE_LINK","Cannot create link")))};t.Component=c,c.propTypes={types:o.default.array.isRequired,onModalSuccess:o.default.func.isRequired,onModalClosed:o.default.func,canCreate:o.default.bool.isRequired};var f=c;t.default=f},97:function(e,t,n){Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var r=s(n(754)),l=function(e,t){if(!t&&e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var n=u(t);if(n&&n.has(e))return n.get(e);var r={},l=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var o in e)if("default"!==o&&Object.prototype.hasOwnProperty.call(e,o)){var i=l?Object.getOwnPropertyDescriptor(e,o):null;i&&(i.get||i.set)?Object.defineProperty(r,o,i):r[o]=e[o]}r.default=e,n&&n.set(e,r);return r}(n(363)),o=s(n(86)),i=n(127),a=s(n(686));function u(e){if("function"!=typeof WeakMap)return null;var t=new WeakMap,n=new WeakMap;return(u=function(e){return e?n:t})(e)}function s(e){return e&&e.__esModule?e:{default:e}}const d=e=>{let{types:t,onSelect:n}=e;const[o,a]=(0,l.useState)(!1);return l.default.createElement(i.Dropdown,{isOpen:o,toggle:()=>a((e=>!e)),className:"link-picker__menu"},l.default.createElement(i.DropdownToggle,{className:"link-picker__menu-toggle font-icon-plus-1",caret:!0},r.default._t("LinkField.ADD_LINK","Add Link")),l.default.createElement(i.DropdownMenu,null,t.map((e=>{let{key:t,title:r}=e;return l.default.createElement(i.DropdownItem,{key:t,onClick:()=>n(t)},r)}))))};d.propTypes={types:o.default.arrayOf(a.default).isRequired,onSelect:o.default.func.isRequired};var c=d;t.default=c},734:function(e,t,n){Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var r=u(n(820)),l=u(n(754)),o=u(n(363)),i=u(n(86)),a=n(127);function u(e){return e&&e.__esModule?e:{default:e}}const s=e=>t=>{t.nativeEvent.stopImmediatePropagation(),t.preventDefault(),t.nativeEvent.preventDefault(),t.stopPropagation(),e&&e()},d=e=>{let{id:t,title:n,description:i,versionState:u,typeTitle:d,onDelete:c,onClick:f,canDelete:p}=e;const y={"link-picker__link":!0,"form-control":!0};u&&(y[` link-picker__link--${u}`]=!0);const v=(0,r.default)(y),_=["unversioned","unsaved"].includes(u)?l.default._t("LinkField.DELETE","Delete"):l.default._t("LinkField.ARCHIVE","Archive");return o.default.createElement("div",{className:v},o.default.createElement(a.Button,{className:"link-picker__button font-icon-link",color:"secondary",onClick:s(f)},o.default.createElement("div",{className:"link-picker__link-detail"},o.default.createElement("div",{className:"link-picker__title"},o.default.createElement("span",{className:"link-picker__title-text"},n),(e=>{let t="",n="";if("draft"===e)t=l.default._t("LinkField.LINK_DRAFT_TITLE","Link has draft changes"),n=l.default._t("LinkField.LINK_DRAFT_LABEL","Draft");else{if("modified"!==e)return null;t=l.default._t("LinkField.LINK_MODIFIED_TITLE","Link has unpublished changes"),n=l.default._t("LinkField.LINK_MODIFIED_LABEL","Modified")}const i=(0,r.default)("badge",`status-${e}`);return o.default.createElement("span",{className:i,title:t},n)})(u)),o.default.createElement("small",{className:"link-picker__type"},d,": ",o.default.createElement("span",{className:"link-picker__url"},i)))),p&&o.default.createElement(a.Button,{className:"link-picker__delete",color:"link",onClick:s((()=>c(t)))},_))};d.propTypes={id:i.default.number.isRequired,title:i.default.string,description:i.default.string,versionState:i.default.string,typeTitle:i.default.string.isRequired,onDelete:i.default.func.isRequired,onClick:i.default.func.isRequired,canDelete:i.default.bool.isRequired};var c=d;t.default=c},697:function(e,t,n){Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var r=i(n(363)),l=n(648),o=i(n(86));function i(e){return e&&e.__esModule?e:{default:e}}const a=e=>{let{types:t,typeKey:n,linkID:o=0,isOpen:i,onSuccess:a,onClosed:u}=e;if(!n)return!1;const s=t.hasOwnProperty(n)?t[n]:{},d=s&&s.hasOwnProperty("handlerName")?s.handlerName:"FormBuilderModal",c=(0,l.loadComponent)(`LinkModal.${d}`);return r.default.createElement(c,{typeTitle:s.title||"",typeKey:n,linkID:o,isOpen:i,onSuccess:a,onClosed:u})};a.propTypes={types:o.default.array.isRequired,typeKey:o.default.string.isRequired,linkID:o.default.number,isOpen:o.default.bool.isRequired,onSuccess:o.default.func.isRequired,onClosed:o.default.func.isRequired};var u=a;t.default=u},41:function(e,t,n){var r=a(n(311)),l=a(n(363)),o=a(n(691)),i=n(648);function a(e){return e&&e.__esModule?e:{default:e}}function u(){return u=Object.assign?Object.assign.bind():function(e){for(var t=1;t{e(".js-injector-boot .entwine-linkfield").entwine({Component:null,Root:null,onmatch(){const e=this.closest(".cms-content").attr("id"),t=e?{context:e}:{},n=this.data("schema-component"),r=(0,i.loadComponent)(n,t);this.setComponent(r),this.setRoot(o.default.createRoot(this[0])),this._super(),this.refresh()},refresh(){const e=this.getProps();this.getInputField().val(e.value);const t=this.getComponent();this.getRoot().render(l.default.createElement(t,u({},e,{noHolder:!0})))},handleChange(e){this.getInputField().data("value",e),this.refresh()},getProps(){const e=this.getInputField();return{value:e.data("value"),ownerID:e.data("owner-id"),ownerClass:e.data("owner-class"),ownerRelation:e.data("owner-relation"),onChange:this.handleChange.bind(this),isMulti:this.data("is-multi")??!1,types:this.data("types")??[],canCreate:this.getInputField().data("can-create")??!1}},getInputField(){const t=this.data("field-id");return e(`#${t}`)},onunmatch(){const e=this.getRoot();e&&e.unmount()}})}))},686:function(e,t,n){Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var r,l=(r=n(86))&&r.__esModule?r:{default:r};var o=l.default.shape({key:l.default.string.isRequired,title:l.default.string.isRequired});t.default=o},159:function(e){e.exports=Backend},510:function(e){e.exports=Config},42:function(e){e.exports=FieldHolder},912:function(e){e.exports=FormBuilderModal},648:function(e){e.exports=Injector},475:function(e){e.exports=InsertMediaModal},872:function(e){e.exports=NodeUrl},86:function(e){e.exports=PropTypes},363:function(e){e.exports=React},691:function(e){e.exports=ReactDomClient},624:function(e){e.exports=ReactRedux},127:function(e){e.exports=Reactstrap},827:function(e){e.exports=Redux},123:function(e){e.exports=ToastsActions},820:function(e){e.exports=classnames},754:function(e){e.exports=i18n},311:function(e){e.exports=jQuery},902:function(e){e.exports=qs}},t={};function n(r){var l=t[r];if(void 0!==l)return l.exports;var o=t[r]={exports:{}};return e[r](o,o.exports,n),o.exports}n(274),n(41)}(); \ No newline at end of file diff --git a/client/lang/en.js b/client/lang/en.js index e51ab71d..9e00cc42 100644 --- a/client/lang/en.js +++ b/client/lang/en.js @@ -14,6 +14,13 @@ if (typeof(ss) === 'undefined' || typeof(ss.i18n) === 'undefined') { "LinkField.ADD_LINK": "Add Link", "LinkField.ARCHIVE": "Archive", "LinkField.DELETE": "Delete", + "LinkField.LINK_DRAFT_TITLE": "Link has draft changes", + "LinkField.LINK_DRAFT_LABEL": "Draft", + "LinkField.LINK_MODIFIED_TITLE": "Link has unpublished changes", + "LinkField.LINK_MODIFIED_LABEL": "Modified", + "LinkField.CANNOT_CREATE_LINK": "Cannot create link", + "LinkField.FAILED_TO_LOAD_LINKS": "Failed to load links", + "LinkField.FAILED_TO_SAVE_LINK": "Failed to save link", "LinkField.SAVE_RECORD_FIRST": "Cannot add links until the record has been saved" }); } \ No newline at end of file diff --git a/client/lang/src/en.json b/client/lang/src/en.json index cb7b3ac7..67c0c3af 100644 --- a/client/lang/src/en.json +++ b/client/lang/src/en.json @@ -12,5 +12,7 @@ "LinkField.LINK_MODIFIED_TITLE": "Link has unpublished changes", "LinkField.LINK_MODIFIED_LABEL": "Modified", "LinkField.CANNOT_CREATE_LINK": "Cannot create link", + "LinkField.FAILED_TO_LOAD_LINKS": "Failed to load links", + "LinkField.FAILED_TO_SAVE_LINK": "Failed to save link", "LinkField.SAVE_RECORD_FIRST": "Cannot add links until the record has been saved" } diff --git a/client/src/components/LinkField/LinkField.js b/client/src/components/LinkField/LinkField.js index 11a6dade..274488c2 100644 --- a/client/src/components/LinkField/LinkField.js +++ b/client/src/components/LinkField/LinkField.js @@ -70,6 +70,9 @@ const LinkField = ({ .then(response => response.json()) .then(responseJson => { setData(responseJson); + }) + .catch(() => { + actions.toasts.error(i18n._t('LinkField.FAILED_TO_LOAD_LINKS', 'Failed to load links')) }); } }, [editingID, value && value.length]); @@ -99,12 +102,7 @@ const LinkField = ({ onChange(isMulti ? ids : ids[0]); // success toast - actions.toasts.success( - i18n._t( - 'LinkField.SAVE_SUCCESS', - 'Saved link', - ) - ); + actions.toasts.success(i18n._t('LinkField.SAVE_SUCCESS', 'Saved link')); } /** @@ -174,7 +172,7 @@ const LinkField = ({ const renderModal = !saveRecordFirst && Boolean(editingID); const saveRecordFirstText = i18n._t('LinkField.SAVE_RECORD_FIRST', 'Cannot add links until the record has been saved'); - return + return { saveRecordFirst &&
{saveRecordFirstText}
} { renderPicker && { } const LinkModal = ({ typeTitle, typeKey, linkID = 0, isOpen, onSuccess, onClosed }) => { + const { actions } = useContext(LinkFieldContext); + if (!typeKey) { return false; } @@ -31,7 +33,15 @@ const LinkModal = ({ typeTitle, typeKey, linkID = 0, isOpen, onSuccess, onClosed * Call back used by LinkModal after the form has been submitted and the response has been received */ const onSubmit = async (modalData, action, submitFn) => { - const formSchema = await submitFn(); + let formSchema = null; + try { + formSchema = await submitFn(); + } catch (error) { + actions.toasts.error(i18n._t('LinkField.FAILED_TO_SAVE_LINK', 'Failed to save link')) + // Intentionally using Promise.resolve() instead of Promise.reject() as existing code in FormBuilder.js + // will raise console warnings if we use Promise.reject(). From a UX point of view it makes no difference. + return Promise.resolve(); + } // slightly annoyingly, on validation error formSchema at this point will not have an errors node // instead it will have the original formSchema id used for the GET request to get the formSchema i.e. diff --git a/client/src/containers/LinkModalContainer.js b/client/src/containers/LinkModalContainer.js index 11299475..3e2ba69b 100644 --- a/client/src/containers/LinkModalContainer.js +++ b/client/src/containers/LinkModalContainer.js @@ -6,7 +6,7 @@ import PropTypes from 'prop-types'; /** * Contains the LinkModal and determines which modal component to render based on the link type. */ -const LinkModalContainer = ({ types, typeKey, linkID = 0, isOpen, onSuccess, onClosed}) => { +const LinkModalContainer = ({ types, typeKey, linkID = 0, isOpen, onSuccess, onClosed }) => { if (!typeKey) { return false; }