TasksList.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. import React, { Component } from 'react'
  2. import Button from 'material-ui/Button'
  3. import Base from '../common/Base'
  4. import Paper from 'material-ui/Paper'
  5. import Table, { TableHead, TableBody, TableRow, TableCell } from 'material-ui/Table'
  6. import Dialog, { DialogActions, DialogContent, DialogContentText, DialogTitle } from 'material-ui/Dialog'
  7. import TextField from 'material-ui/TextField'
  8. import { InputLabel } from 'material-ui/Input'
  9. import { FormControl } from 'material-ui/Form'
  10. import Select from 'material-ui/Select'
  11. import MenuItem from 'material-ui/Menu/MenuItem'
  12. import Typography from 'material-ui/Typography'
  13. import List, { ListItem, ListItemText, ListItemSecondaryAction } from 'material-ui/List'
  14. import IconButton from 'material-ui/IconButton'
  15. import AddIcon from '../icons/AddIcon'
  16. import RemoveIcon from '../icons/RemoveIcon'
  17. import { isEqual, isEmpty, union, difference, defer } from 'lodash'
  18. import { ODOO, DOCKER, REQUEST, GIT } from '../../constants/ResourceNames'
  19. import { post, get } from '../../actions'
  20. import { connect } from 'react-redux'
  21. import { withStyles } from 'material-ui/styles'
  22. const styles = theme => ({
  23. gap: {
  24. marginTop: 25
  25. },
  26. transfer: {
  27. display: 'flex',
  28. marginTop: theme.spacing.unit * 3
  29. },
  30. transferPanels: {
  31. height: '100%',
  32. padding: 15,
  33. flexGrow: 1
  34. },
  35. transferModules: {
  36. height: 300,
  37. overflowY: 'auto'
  38. }
  39. })
  40. class TasksList extends Component {
  41. constructor(props) {
  42. super(props)
  43. this.state = {
  44. showDialog: null,
  45. isDialogValid: true,
  46. name: '',
  47. nameConfirmation: '',
  48. odoos: [],
  49. modules: [],
  50. selectedOdoo: '',
  51. modulesToInstall: []
  52. }
  53. }
  54. /**
  55. *
  56. */
  57. componentDidMount() {
  58. this.props.loadData()
  59. }
  60. /**
  61. *
  62. */
  63. handleShowDialog = (taskName, e) => {
  64. if (taskName === 'create-odoo') {
  65. this.showDialog(taskName)
  66. }
  67. if (taskName === 'show-modules') {
  68. Promise.all([
  69. this.props.loadContainers(),
  70. this.props.loadRepositories()
  71. ]).then(data => {
  72. this.setState({
  73. odoos: data[0].payload.containers,
  74. modules: data[1].payload.repositories
  75. }, () => this.showDialog(taskName))
  76. })
  77. }
  78. }
  79. /**
  80. *
  81. */
  82. showDialog = dialog => {
  83. this.setState({
  84. showDialog: dialog
  85. })
  86. }
  87. /**
  88. *
  89. */
  90. handleCloseDialog = taskName => {
  91. defer(() => {
  92. this.setState({
  93. showDialog: null
  94. })
  95. })
  96. }
  97. /**
  98. *
  99. */
  100. handleChangeName = e => {
  101. defer(() => {
  102. if (isEqual(e.target.id, 'name')) {
  103. this.setState({
  104. name: e.target.value
  105. }, this.handleStateChange)
  106. }
  107. if (isEqual(e.target.id, 'nameConfirmation')) {
  108. this.setState({
  109. nameConfirmation: e.target.value
  110. }, this.handleStateChange)
  111. }
  112. })
  113. }
  114. /**
  115. *
  116. */
  117. handleStateChange = () => {
  118. if (isEqual(this.state.name, this.state.nameConfirmation)) {
  119. this.setState({
  120. isDialogValid: true
  121. })
  122. } else {
  123. this.setState({
  124. isDialogValid: false
  125. })
  126. }
  127. }
  128. /**
  129. *
  130. */
  131. handleChangeOdooSystem = e => {
  132. defer(() => {
  133. this.setState({
  134. [e.target.name]: e.target.value
  135. })
  136. })
  137. }
  138. /**
  139. *
  140. */
  141. handleAcceptDialog = e => {
  142. // handling odoo creating
  143. if (this.state.showDialog === 'create-odoo') {
  144. const { name, nameConfirmation } = this.state
  145. if (isEmpty(name) || isEmpty(nameConfirmation)) {
  146. return
  147. }
  148. if (isEqual(name, nameConfirmation)) {
  149. this.props.createOdoo(name)
  150. this.setState({
  151. showDialog: null,
  152. name: '',
  153. nameConfirmation: ''
  154. })
  155. }
  156. }
  157. // handling copying modules
  158. if (this.state.showDialog === 'show-modules') {
  159. const { selectedOdoo, modulesToInstall } = this.state
  160. if (isEmpty(selectedOdoo) || isEmpty(modulesToInstall)) {
  161. return
  162. }
  163. this.setState({
  164. showDialog: null,
  165. selectedOdoo: '',
  166. modulesToInstall: []
  167. }, () => this.props.installModules(selectedOdoo, modulesToInstall))
  168. }
  169. }
  170. /**
  171. *
  172. */
  173. handleAddModule = (index, e) => {
  174. defer(() => {
  175. const item = this.state.modules[index]
  176. this.setState({
  177. modules: difference(this.state.modules, [item]),
  178. modulesToInstall: union(this.state.modulesToInstall, [item])
  179. })
  180. })
  181. }
  182. /**
  183. *
  184. */
  185. handleRemoveModule = (index, e) => {
  186. defer(() => {
  187. const item = this.state.modulesToInstall[index]
  188. this.setState({
  189. modules: union(this.state.modules, [item]),
  190. modulesToInstall: difference(this.state.modulesToInstall, [item])
  191. })
  192. })
  193. }
  194. /**
  195. *
  196. */
  197. render() {
  198. const { classes } = this.props
  199. const { isDialogValid, showDialog } = this.state
  200. return (
  201. <Base title={this.props.title}>
  202. <Paper>
  203. <Table>
  204. <TableHead>
  205. <TableRow>
  206. <TableCell>Nombre</TableCell>
  207. <TableCell>Última ejecución</TableCell>
  208. <TableCell>Usuario</TableCell>
  209. <TableCell />
  210. </TableRow>
  211. </TableHead>
  212. <TableBody>
  213. <TableRow>
  214. <TableCell>Crear contenedor Odoo</TableCell>
  215. <TableCell>Nunca</TableCell>
  216. <TableCell>Anónimo</TableCell>
  217. <TableCell>
  218. <Button variant='raised' color='primary' data-action='create' onClick={e => this.handleShowDialog('create-odoo', e)}>Ejecutar</Button>
  219. </TableCell>
  220. </TableRow>
  221. <TableRow>
  222. <TableCell>Copiar/actualizar módulos de Odoo</TableCell>
  223. <TableCell>Nunca</TableCell>
  224. <TableCell>Anónimo</TableCell>
  225. <TableCell>
  226. <Button variant='raised' color='primary' data-action='create' onClick={e => this.handleShowDialog('show-modules', e)}>Ejecutar</Button>
  227. </TableCell>
  228. </TableRow>
  229. </TableBody>
  230. </Table>
  231. </Paper>
  232. {/* Dialog for create odoo container */}
  233. <Dialog open={showDialog === 'create-odoo'} onClose={this.handleCloseDialog}>
  234. <DialogTitle>Confirmar</DialogTitle>
  235. <DialogContent>
  236. <DialogContentText>Estás solicitando crear un nuevo contenedor Odoo. Tenga en cuenta que ésta tarea puede tardar unos instantes dependiendo del tráfico de red y del uso de los recursos del servidor</DialogContentText>
  237. <TextField
  238. id='name'
  239. margin='dense'
  240. label='Nombre del sistema'
  241. type='text'
  242. autoComplete='off'
  243. onKeyUp={this.handleChangeName}
  244. autoFocus
  245. fullWidth
  246. />
  247. <TextField
  248. id='nameConfirmation'
  249. margin='dense'
  250. label='Repita el nombre del sistema'
  251. type='text'
  252. autoComplete='off'
  253. error={!isDialogValid}
  254. onKeyUp={this.handleChangeName}
  255. fullWidth
  256. />
  257. </DialogContent>
  258. <DialogActions>
  259. <Button color='primary' onClick={this.handleCloseDialog}>Cancelar</Button>
  260. <Button color='primary' onClick={this.handleAcceptDialog}>Aceptar</Button>
  261. </DialogActions>
  262. </Dialog>
  263. {/* Dialog for copy odoo modules */}
  264. <Dialog open={showDialog === 'show-modules'} onClose={this.handleCloseDialog} fullScreen={true}>
  265. <DialogTitle>Seleccionar modulos</DialogTitle>
  266. <DialogContent>
  267. <DialogContentText>Seleccione los módulos que desea copiar/actualizar en un sistema desplegado.</DialogContentText>
  268. <FormControl margin='normal' fullWidth={true}>
  269. <InputLabel>Nombre del Sistema</InputLabel>
  270. <Select
  271. value={this.state.selectedOdoo}
  272. onChange={this.handleChangeOdooSystem}
  273. inputProps={{
  274. name: 'selectedOdoo'
  275. }}>
  276. {this.state.odoos.map(o =>
  277. <MenuItem key={o.id} value={o.name}>{o.name}</MenuItem>
  278. )}
  279. </Select>
  280. </FormControl>
  281. <div className={classes.transfer}>
  282. <div className={classes.transferPanels}>
  283. <Typography variant='title'>Módulos disponibles</Typography>
  284. <TextField type='search' placeholder='Buscar' fullWidth={true} margin='normal' />
  285. <div className={classes.transferModules}>
  286. <List>
  287. {this.state.modules.map((repo, index) =>
  288. <ListItem key={index}>
  289. <ListItemText primary={repo} />
  290. <ListItemSecondaryAction onClick={e => this.handleAddModule(index, e)}>
  291. <IconButton>
  292. <AddIcon />
  293. </IconButton>
  294. </ListItemSecondaryAction>
  295. </ListItem>
  296. )}
  297. </List>
  298. </div>
  299. </div>
  300. <div className={classes.transferPanels}>
  301. <Typography variant='title'>Módulos a copiar/actualizar</Typography>
  302. <div className={classes.transferModules}>
  303. <List>
  304. {this.state.modulesToInstall.map((toInstall, index) =>
  305. <ListItem key={index}>
  306. <ListItemText primary={toInstall} />
  307. <ListItemSecondaryAction onClick={e => this.handleRemoveModule(index, e)}>
  308. <IconButton>
  309. <RemoveIcon />
  310. </IconButton>
  311. </ListItemSecondaryAction>
  312. </ListItem>
  313. )}
  314. </List>
  315. </div>
  316. </div>
  317. </div>
  318. </DialogContent>
  319. <DialogActions>
  320. <Button color='primary' onClick={this.handleCloseDialog}>Cancelar</Button>
  321. <Button color='primary' onClick={this.handleAcceptDialog}>Aceptar</Button>
  322. </DialogActions>
  323. </Dialog>
  324. </Base>
  325. )
  326. }
  327. }
  328. /**
  329. *
  330. * @param {*} state
  331. * @param {*} props
  332. */
  333. const mapStateToProps = (state, props) => {
  334. return {
  335. containers: state.containers,
  336. repositories: state.repositories
  337. }
  338. }
  339. /**
  340. *
  341. * @param {*} dispatch
  342. * @param {*} props
  343. */
  344. const mapDispatchToProps = (dispatch, props) => ({
  345. /**
  346. *
  347. */
  348. loadData() {
  349. dispatch(get(`${REQUEST}?last`))
  350. },
  351. /**
  352. *
  353. */
  354. loadRepositories() {
  355. return dispatch(get(`${GIT}repositories/`))
  356. },
  357. /**
  358. *
  359. */
  360. loadContainers() {
  361. return dispatch(get(`${DOCKER}container/all/`))
  362. },
  363. /**
  364. *
  365. */
  366. createOdoo(name) {
  367. dispatch(post(`${ODOO}create/`, { name }))
  368. },
  369. /**
  370. *
  371. * @param {*} name
  372. * @param {*} modules
  373. */
  374. installModules(system, modules) {
  375. dispatch(post(`${ODOO}install_modules/`, {system, modules}))
  376. }
  377. })
  378. TasksList = withStyles(styles)(TasksList)
  379. export default connect(mapStateToProps, mapDispatchToProps)(TasksList)