@@ -2,12 +2,16 @@ import React from 'react';
22import PropTypes from 'prop-types' ;
33
44import Loader from './Loader.jsx' ;
5+ import { withCookies , Cookies } from 'react-cookie' ;
56
7+ import { times } from 'lodash'
68import { TextField , Button , Icon , Typography , Menu , Grid , InputLabel ,
79 FormControl , Select , OutlinedInput , InputBase , Tooltip } from '@material-ui/core' ;
810
911import { withStyles } from '@material-ui/core/styles' ;
1012
13+ import { simple_api_req } from '../../util/util.jsx'
14+
1115const styles = {
1216 container : {
1317 display : 'flex' ,
@@ -43,14 +47,26 @@ class FigureList extends React.Component {
4347 this . handle_add = this . handle_add . bind ( this )
4448 }
4549
46- delete_figure = idx => {
47- if ( this . props . onDelete != null ) this . props . onDelete ( idx )
50+ delete_figure ( idx ) {
51+ let { figures, cookies} = this . props
52+ let pk = figures [ idx ] . id
53+ this . setState ( { loading : true } , ( ) => {
54+ simple_api_req ( 'DELETE' , `/api/key_figures/${ pk } /delete/` , null , cookies . get ( 'csrftoken' ) , ( res ) => {
55+ figures . splice ( idx , 1 )
56+ this . setState ( { loading : false } , ( ) => {
57+ if ( this . props . onChange != null ) this . props . onChange ( figures )
58+ } )
59+ } , ( err ) => {
60+ this . setState ( { loading : false } )
61+ console . error ( err )
62+ } )
63+ } )
4864 }
4965
5066 figure_click = ( idx , event ) => {
51- let { figures, showDelete } = this . props
67+ let { figures, show_delete } = this . props
5268 console . log ( `figure_click ${ idx } ` )
53- if ( showDelete ) this . delete_figure ( idx )
69+ if ( show_delete ) this . delete_figure ( idx )
5470 else this . props . onFigureClick ( figures , idx )
5571 }
5672
@@ -59,49 +75,57 @@ class FigureList extends React.Component {
5975 }
6076
6177 render_thumbnail ( kf , i ) {
62- let { classes, showDelete } = this . props
78+ let { classes, show_delete } = this . props
6379 let kind = kf . is_table ? 'Table' : 'Figure'
6480 let img = (
6581 < img key = { i }
6682 className = { classes . thumbnail }
6783 style = { { backgroundImage : `url(${ kf . image } )` } } />
6884 )
69- let tooltip = showDelete ? "Delete figure" : "Enlarge figure"
85+ let tooltip = show_delete ? "Delete figure" : "Enlarge figure"
7086 return < Tooltip title = { tooltip } key = { i } > < a key = { i } href = "#" onClick = { this . figure_click . bind ( this , i ) } > { img } </ a > </ Tooltip >
7187 }
7288
7389 render ( ) {
74- let { classes, figures, showAdd, loading} = this . props
75- let addButton , spinner
90+ let { classes, figures, showAdd, loading, number_images_loading} = this . props
7691 if ( figures == null ) figures = [ ]
77- if ( showAdd ) addButton = < a href = "#" onClick = { this . handle_add } className = { classes . thumbnail } >
78- < span className = { classes . add } >
79- < Icon fontSize = 'large' > add</ Icon > < Typography variant = "body2" > Add</ Typography >
80- </ span >
81- </ a >
92+
93+ let spinner
8294 if ( loading ) spinner = < Loader size = "25" />
95+
96+ let loading_images = [ ]
97+ times ( number_images_loading , ( i ) => {
98+ loading_images . push (
99+ < div className = { classes . thumbnail } key = { `uploading_image_${ i } ` } style = { { display : 'flex' , justifyContent : 'center' , alignItems : 'center' } } >
100+ < Loader />
101+ </ div >
102+ )
103+ } )
104+
83105 return (
84106 < div className = { classes . container } >
85107 { figures . map ( this . render_thumbnail ) }
86- { addButton }
87108 { spinner }
109+ { loading_images }
88110 </ div >
89111 )
90112 }
91113}
92114
93115FigureList . propTypes = {
94- onDelete : PropTypes . func ,
95- showDelete : PropTypes . bool ,
116+ onChange : PropTypes . func ,
117+ show_delete : PropTypes . bool ,
96118 showAdd : PropTypes . bool ,
97- loading : PropTypes . bool
119+ loading : PropTypes . bool ,
120+ number_images_loading : PropTypes . number ,
98121}
99122
100123FigureList . defaultProps = {
101124 figures : [ ] ,
102- showDelete : false ,
125+ show_delete : false ,
103126 showAdd : false ,
104- loading : false
127+ loading : false ,
128+ number_images_loading : 0 ,
105129} ;
106130
107- export default withStyles ( styles ) ( FigureList ) ;
131+ export default withCookies ( withStyles ( styles ) ( FigureList ) ) ;
0 commit comments