| 1 |
import React, { Component, Fragment } from 'react'; |
| 2 |
|
| 3 |
import '../../../css/admin/modal/grid.scss'; |
| 4 |
|
| 5 |
import Api from 'Shared/Api'; |
| 6 |
import Loader from 'Shared/Loader'; |
| 7 |
import { __wpupg } from 'Shared/Translations'; |
| 8 |
|
| 9 |
import Edit from './Edit'; |
| 10 |
import Preview from './Preview'; |
| 11 |
import Header from '../general/Header'; |
| 12 |
import Footer from '../general/Footer'; |
| 13 |
|
| 14 |
export default class Grid extends Component { |
| 15 |
constructor(props) { |
| 16 |
super(props); |
| 17 |
|
| 18 |
const mode = props.hasOwnProperty( 'mode' ) ? props.mode : 'create'; |
| 19 |
|
| 20 |
// Get grid fields. |
| 21 |
let grid = JSON.parse( JSON.stringify( wpupg_admin_manage_modal.grid ) ); |
| 22 |
let loadingGrid = false; |
| 23 |
let versionWarning = false; |
| 24 |
|
| 25 |
if ( 'edit' === mode && ( props.args.hasOwnProperty( 'grid' ) || props.args.hasOwnProperty( 'gridId' ) ) ) { |
| 26 |
if ( props.args.hasOwnProperty( 'grid' ) ) { |
| 27 |
grid = JSON.parse( JSON.stringify( props.args.grid ) ); |
| 28 |
versionWarning = this.checkGridVersionWarning( grid ); |
| 29 |
} else { |
| 30 |
loadingGrid = true; |
| 31 |
Api.grid.get(props.args.gridId).then((data) => { |
| 32 |
if ( data ) { |
| 33 |
const grid = JSON.parse( JSON.stringify( data.grid ) ); |
| 34 |
this.setState({ |
| 35 |
grid, |
| 36 |
originalGrid: JSON.parse( JSON.stringify( grid ) ), |
| 37 |
loadingGrid: false, |
| 38 |
versionWarning: this.checkGridVersionWarning( grid ), |
| 39 |
}); |
| 40 |
} |
| 41 |
}); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
// Set default field values. |
| 46 |
if ( 'create' === mode && props.args.hasOwnProperty( 'grid' ) ) { |
| 47 |
grid = { |
| 48 |
...grid, |
| 49 |
...props.args.grid, |
| 50 |
} |
| 51 |
|
| 52 |
if ( props.args.cloneGrid ) { |
| 53 |
delete grid.id; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// Set initial state. |
| 58 |
this.state = { |
| 59 |
grid, |
| 60 |
mode, |
| 61 |
originalGrid: JSON.parse( JSON.stringify( grid ) ), |
| 62 |
saveCallback: props.args.hasOwnProperty( 'saveCallback' ) ? props.args.saveCallback : false, |
| 63 |
savingChanges: false, |
| 64 |
loadingGrid, |
| 65 |
versionWarning, |
| 66 |
}; |
| 67 |
|
| 68 |
// Bind functions. |
| 69 |
this.onGridChange = this.onGridChange.bind(this); |
| 70 |
this.resetGrid = this.resetGrid.bind(this); |
| 71 |
this.saveGrid = this.saveGrid.bind(this); |
| 72 |
this.allowCloseModal = this.allowCloseModal.bind(this); |
| 73 |
} |
| 74 |
|
| 75 |
checkGridVersionWarning( grid ) { |
| 76 |
return '0.0.0' === grid.version; |
| 77 |
} |
| 78 |
|
| 79 |
onGridChange(fields) { |
| 80 |
this.setState((prevState) => ({ |
| 81 |
grid: { |
| 82 |
...JSON.parse( JSON.stringify( prevState.grid ) ), |
| 83 |
...JSON.parse( JSON.stringify( fields ) ), |
| 84 |
} |
| 85 |
})); |
| 86 |
} |
| 87 |
|
| 88 |
resetGrid() { |
| 89 |
if ( this.changesMade() ) { |
| 90 |
this.setState({ |
| 91 |
grid: JSON.parse( JSON.stringify( this.state.originalGrid ) ), |
| 92 |
}); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
saveGrid( closeModal ) { |
| 97 |
if ( this.changesMade() ) { |
| 98 |
this.setState({ |
| 99 |
savingChanges: true, |
| 100 |
}, () => { |
| 101 |
const asNewGrid = 'edit' === this.state.mode ? false : true; |
| 102 |
Api.grid.save(asNewGrid, this.state.grid).then((data) => { |
| 103 |
let newState = { |
| 104 |
savingChanges: false, |
| 105 |
} |
| 106 |
|
| 107 |
if ( data ) { |
| 108 |
newState.grid = JSON.parse( JSON.stringify( data.grid ) ); |
| 109 |
newState.originalGrid = JSON.parse( JSON.stringify( data.grid ) ); |
| 110 |
newState.mode = 'edit'; |
| 111 |
} |
| 112 |
|
| 113 |
this.setState(newState, () => { |
| 114 |
if ( 'function' === typeof this.state.saveCallback ) { |
| 115 |
this.state.saveCallback(this.state.grid); |
| 116 |
} |
| 117 |
|
| 118 |
if ( closeModal ) { |
| 119 |
this.props.maybeCloseModal(); |
| 120 |
} |
| 121 |
}); |
| 122 |
}); |
| 123 |
}); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
allowCloseModal() { |
| 128 |
return ! this.state.savingChanges && ( ! this.changesMade() || confirm( __wpupg( 'Are you sure you want to close without saving changes?' ) ) ); |
| 129 |
} |
| 130 |
|
| 131 |
changesMade() { |
| 132 |
return JSON.stringify( this.state.grid ) !== JSON.stringify( this.state.originalGrid ); |
| 133 |
} |
| 134 |
|
| 135 |
render() { |
| 136 |
return ( |
| 137 |
<Fragment> |
| 138 |
<Header |
| 139 |
onCloseModal={ this.props.maybeCloseModal } |
| 140 |
> |
| 141 |
{ |
| 142 |
'edit' === this.state.mode |
| 143 |
? |
| 144 |
`${ __wpupg( 'Edit Grid' ) }${ this.state.loadingGrid ? '' : ` #${this.state.grid.id}${ this.state.grid.name ? ` - ${this.state.grid.name}` : `` }` }` |
| 145 |
: |
| 146 |
__wpupg( 'Create new Grid' ) |
| 147 |
} |
| 148 |
</Header> |
| 149 |
<div className="wpupg-admin-modal-content"> |
| 150 |
{ |
| 151 |
this.state.loadingGrid |
| 152 |
? |
| 153 |
<Loader/> |
| 154 |
: |
| 155 |
<Fragment> |
| 156 |
{ |
| 157 |
this.state.versionWarning |
| 158 |
? |
| 159 |
<Fragment> |
| 160 |
<p> |
| 161 |
This grid was created before WP Ultimate Post Grid 3.0.0, which was a major update. |
| 162 |
</p> |
| 163 |
<p> |
| 164 |
Editing the grid now will break any backwards compatibility, so make sure you are ready to move to version 3.0.0 before editing. |
| 165 |
</p> |
| 166 |
<p> |
| 167 |
<a href="https://help.bootstrapped.ventures/article/217-wp-ultimate-post-grid-legacy" target="_blank">Learn More</a> |
| 168 |
</p> |
| 169 |
</Fragment> |
| 170 |
: |
| 171 |
<Fragment> |
| 172 |
<Edit |
| 173 |
grid={ this.state.grid } |
| 174 |
onGridChange={ this.onGridChange } |
| 175 |
/> |
| 176 |
<Preview |
| 177 |
grid={ this.state.grid } |
| 178 |
/> |
| 179 |
</Fragment> |
| 180 |
} |
| 181 |
</Fragment> |
| 182 |
} |
| 183 |
</div> |
| 184 |
<Footer |
| 185 |
savingChanges={ this.state.savingChanges } |
| 186 |
> |
| 187 |
{ |
| 188 |
this.state.versionWarning |
| 189 |
? |
| 190 |
<Fragment> |
| 191 |
<button |
| 192 |
className="button" |
| 193 |
onClick={ this.props.maybeCloseModal } |
| 194 |
> |
| 195 |
{ __wpupg( 'Close' ) } |
| 196 |
</button> |
| 197 |
<button |
| 198 |
className="button button-primary" |
| 199 |
onClick={ () => { |
| 200 |
this.setState({ |
| 201 |
versionWarning: false, |
| 202 |
}, () => { |
| 203 |
this.onGridChange({ version: '3.0.0' } ); // Allows saving to prevent warning next time. |
| 204 |
} ); |
| 205 |
} } |
| 206 |
> |
| 207 |
{ __wpupg( 'I Understand and want to edit the grid' ) } |
| 208 |
</button> |
| 209 |
</Fragment> |
| 210 |
: |
| 211 |
<Fragment> |
| 212 |
<button |
| 213 |
className="button" |
| 214 |
onClick={ this.resetGrid } |
| 215 |
disabled={ ! this.changesMade() } |
| 216 |
> |
| 217 |
{ __wpupg( 'Cancel Changes' ) } |
| 218 |
</button> |
| 219 |
<button |
| 220 |
className="button button-primary" |
| 221 |
onClick={ () => this.saveGrid( false ) } |
| 222 |
disabled={ ! this.changesMade() } |
| 223 |
> |
| 224 |
{ 'edit' === this.state.mode ? __wpupg( 'Save Changes' ) : __wpupg( 'Create Grid' ) } |
| 225 |
</button> |
| 226 |
<button |
| 227 |
className="button button-primary" |
| 228 |
onClick={ () => this.saveGrid( true ) } |
| 229 |
disabled={ ! this.changesMade() } |
| 230 |
> |
| 231 |
{ 'edit' === this.state.mode ? __wpupg( 'Save & Close' ) : __wpupg( 'Create & Close' ) } |
| 232 |
</button> |
| 233 |
</Fragment> |
| 234 |
} |
| 235 |
</Footer> |
| 236 |
</Fragment> |
| 237 |
); |
| 238 |
} |
| 239 |
} |