@@ -6,10 +6,17 @@ import CustomToolbarEdit from "./CustomToolbarEdit";
66import CustomToolbarDelete from "./CustomToolbarDelete" ;
77import PropTypes from "prop-types" ;
88import { getNominationCandidates } from '../../modules/nomination/state/NominationAction' ;
9+ import { candidateMessage } from '../../modules/election/state/ElectionAction' ;
910import { connect } from 'react-redux' ;
1011import Grid from '@material-ui/core/Grid' ;
1112import { de } from 'date-fns/locale' ;
12-
13+ import { Button } from '@material-ui/core' ;
14+ import CSVReader from 'react-csv-reader'
15+ import axios from 'axios' ;
16+ import _ from 'lodash' ;
17+ import download from 'downloadjs' ;
18+ import DownloadIcon from '@material-ui/icons/GetApp' ;
19+ import { API_BASE_URL } from "../../config.js" ;
1320
1421const styles = theme => ( {
1522 root : {
@@ -46,13 +53,120 @@ class CustomizedTable extends React.Component {
4653 open : true ,
4754 nominations : [ ] ,
4855 candidateCount : '0' ,
56+ jsonSchemaProperties : null ,
4957 }
5058
5159 }
5260
61+ handleForce = ( data , fileInfo ) => console . log ( "ddd" , data , fileInfo ) ;
62+
63+ handleSubmit = ( data , callback ) => {
64+
65+ // first get the keys out of the first sub array:
66+ const keys = data [ 0 ] ;
67+
68+ // then map over the rest of the sub arrays:
69+ const result = data . slice ( 1 ) . map ( function ( item ) {
70+
71+ // create an object with key names and item values:
72+ const obj = { } ;
73+ keys . forEach ( function ( k , i ) {
74+ obj [ k ] = item [ i ] ;
75+ } ) ;
76+ return obj ;
77+ } ) ;
78+
79+ const { customProps, getNominationCandidates, candidateMessage } = this . props ;
80+ let { jsonSchemaProperties} = this . state ;
81+ let candidateKeyValues = { "nominationId" : customProps ,
82+ "candidateData" :[ ] } ;
83+
84+ result . forEach ( function ( k , i ) {
85+ var data2 = k ;
86+ for ( var configItem in data2 ) {
87+ if ( jsonSchemaProperties [ configItem ] ) {
88+ candidateKeyValues . candidateData . push (
89+ { "candidateConfigId" : jsonSchemaProperties [ configItem ] . id ,
90+ "value" : data2 [ configItem ] } ) ;
91+ }
92+ }
93+ } ) ;
94+
95+ var data3 = candidateKeyValues . candidateData ,
96+ ids = [ '1' , '2' , '3' , '4' , '5' ] ,
97+ result4 = data3
98+ . reduce ( ( r , o ) => {
99+ let index = r . indices [ o . candidateConfigId ] ++ ;
100+ r . data3 [ index ] = r . data3 [ index ] || [ ] ;
101+ r . data3 [ index ] . push ( o ) ;
102+ return r ;
103+ } , { indices : Object . fromEntries ( ids . map ( k => [ k , 0 ] ) ) , data3 : [ ] } )
104+ . data3 ;
105+
106+ candidateKeyValues . candidateData = result4 ;
107+ data . forEach ( ( element , i ) => {
108+
109+ } ) ;
110+
111+ let url = 'nominations/candidates' ;
112+
113+ const onCloseModal = this . props . onCloseModal ;
114+ axios ( {
115+ method : 'post' ,
116+ headers : {
117+ 'Accept' : 'application/json' ,
118+ 'Content-Type' : 'application/json' ,
119+ } ,
120+ url : url ,
121+ data : candidateKeyValues
122+ } )
123+ . then ( function ( response ) {
124+ candidateMessage ( 'Candidate Added Sccessfully...' ) ;
125+ getNominationCandidates ( customProps ) ;
126+ } )
127+ . catch ( function ( e ) {
128+ const message = _ . get ( e , 'response.data.message' ) ;
129+ if ( message ) {
130+ candidateMessage ( message ) ;
131+ }
132+ } ) ;
133+ } ;
134+
53135 componentDidMount ( ) {
54- const { customProps, getNominationCandidates } = this . props ;
136+ const { customProps, getNominationCandidates, moduleId } = this . props ;
55137 getNominationCandidates ( customProps ) ;
138+
139+ axios . get ( "modules/" + moduleId + "/candidate-form-config" , { } ) . then (
140+ ( response ) => {
141+ var properties = {
142+ // TODO: remove following three
143+ "counsilName" : { "type" : "hidden" , "title" : "counsilName" , "default" : "council" , "id" : 999 } ,
144+ "electoralDivisionCode" : { "type" : "hidden" , "title" : "electoralDivisionCode" , "default" : "K01" , "id" : 998 } ,
145+ "electoralDivisionName" : { "type" : "hidden" , "title" : "electoralDivisionName" , "default" : "kalutara" , "id" : 997 } ,
146+
147+ "nominationId" : { "type" : "hidden" , "title" : "nominationId" , "default" : this . props . customProps , "id" : 996 } ,
148+ } ;
149+
150+ const sortedData = _ . orderBy ( response . data , [ 'candidate_config_id' ] , "asc" ) ;
151+ var configLength = sortedData . length ;
152+ for ( var i = 0 ; i < configLength ; i ++ ) {
153+ const config = sortedData [ i ] ;
154+ const keyName = config [ 'key_name' ] ;
155+ const schema = config [ "json_schema" ] ;
156+ if ( schema ) {
157+ properties [ keyName ] = JSON . parse ( schema ) ;
158+ } else {
159+ properties [ keyName ] = { "type" : "string" } ;
160+ }
161+ properties [ keyName ] . title = config [ 'description' ] ;
162+ properties [ keyName ] . id = config [ 'candidate_config_id' ] ;
163+ }
164+ let progress = 1 ;
165+ // if(index) {
166+ // progress = 0.5;
167+ // }
168+ this . setState ( { ajaxState : this . state . ajaxState + progress , jsonSchemaProperties : properties } ) ;
169+ } ) ;
56170 }
57171
58172 handleDrawerOpen = ( ) => {
@@ -63,6 +177,15 @@ class CustomizedTable extends React.Component {
63177 this . setState ( { open : false } ) ;
64178 } ;
65179
180+ handleFileDownload = ( ) => {
181+ axios . get ( `${ API_BASE_URL } /nominations/candidate-template/download` , { responseType : 'blob' } , {
182+ } ) . then ( ( response ) => {
183+ download ( new Blob ( [ response . data ] ) , 'candidate_upload_template' , "text/csv" , response . headers [ 'content-type' ] ) ;
184+ } ) . catch ( err => {
185+ console . log ( err )
186+ } ) ;
187+ } ;
188+
66189 render ( ) {
67190 const { classes, CandidateList } = this . props ;
68191 const rows = CandidateList ;
@@ -170,9 +293,20 @@ class CustomizedTable extends React.Component {
170293 const options = {
171294 filterType : "dropdown" ,
172295 responsive : "scroll" ,
296+ filter : false ,
173297 customToolbar : ( ) => {
174298 return (
175- < CustomToolbar customProps = { customProps } modalType = "Add" />
299+ < Grid container direction = "row" justify = "flex-end" spacing = { 2 } >
300+ < Grid item lg = { 2 } >
301+ < CustomToolbar customProps = { customProps } modalType = "Add" />
302+ </ Grid >
303+ { /* <Grid style={{marginTop:15}} item lg={6}>
304+ <DownloadIcon onClick={() => { this.handleFileDownload() }} color="red"/>
305+ </Grid> */ }
306+ < Grid style = { { marginTop :15 } } item lg = { 6 } >
307+ < CSVReader onFileLoaded = { this . handleSubmit } />
308+ </ Grid >
309+ </ Grid >
176310 ) ;
177311 }
178312 } ;
@@ -189,14 +323,16 @@ class CustomizedTable extends React.Component {
189323 }
190324}
191325
192- const mapStateToProps = ( { Nomination } ) => {
326+ const mapStateToProps = ( { Nomination, Election } ) => {
193327 const { getNominationCandidates } = Nomination ;
328+ const moduleId = Election . ElectionTimeLineData . moduleId ;
194329 const CandidateList = Nomination . getNominationCandidates ;
195- return { getNominationCandidates, CandidateList } ;
330+ return { getNominationCandidates, CandidateList, moduleId } ;
196331} ;
197332
198333const mapActionsToProps = {
199- getNominationCandidates
334+ getNominationCandidates,
335+ candidateMessage
200336} ;
201337
202338export default connect ( mapStateToProps , mapActionsToProps ) ( withStyles ( styles ) ( CustomizedTable ) ) ;
0 commit comments