Skip to content

Commit

Permalink
this will partially fix the ECLK#159
Browse files Browse the repository at this point in the history
  • Loading branch information
YujithIsura committed Jul 9, 2020
1 parent 0d5333e commit 180ad7d
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 45 deletions.
35 changes: 27 additions & 8 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"npm": "^6.10.0",
"prop-types": "^15.7.2",
"react": "^16.12.0",
"react-csv-reader": "^3.0.6",
"react-datepicker": "^2.0.0",
"react-datetime": "^2.16.3",
"react-dom": "^16.12.0",
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/CandidateProfile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class TextFields extends React.Component {
// this.refs.btn.setAttribute("disabled", "disabled");
const { index, customProps,getNominationCandidates,candidateMessage } = this.props;
let {jsonSchemaProperties} = this.state;
let candidateKeyValues = { "nominationId" : customProps,
let candidateKeyValues = { "nominationId" : customProps,"from" : "form",
"candidateData":[] };
for (var configItem in data) {
if(jsonSchemaProperties[configItem]){
Expand Down
148 changes: 142 additions & 6 deletions client/src/components/NominationStep1/NominationStep1.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ import CustomToolbarEdit from "./CustomToolbarEdit";
import CustomToolbarDelete from "./CustomToolbarDelete";
import PropTypes from "prop-types";
import { getNominationCandidates } from '../../modules/nomination/state/NominationAction';
import { candidateMessage } from '../../modules/election/state/ElectionAction';
import { connect } from 'react-redux';
import Grid from '@material-ui/core/Grid';
import { de } from 'date-fns/locale';

import { Button } from '@material-ui/core';
import CSVReader from 'react-csv-reader'
import axios from 'axios';
import _ from 'lodash';
import download from 'downloadjs';
import DownloadIcon from '@material-ui/icons/GetApp';
import { API_BASE_URL } from "../../config.js";

const styles = theme => ({
root: {
Expand Down Expand Up @@ -46,13 +53,120 @@ class CustomizedTable extends React.Component {
open: true,
nominations: [],
candidateCount: '0',
jsonSchemaProperties: null,
}

}

handleForce = (data, fileInfo) => console.log("ddd",data, fileInfo);

handleSubmit = (data, callback) => {

// first get the keys out of the first sub array:
const keys = data[0];

// then map over the rest of the sub arrays:
const result = data.slice(1).map(function(item) {

// create an object with key names and item values:
const obj = {};
keys.forEach(function(k,i) {
obj[k] = item[i];
});
return obj;
});

const { customProps,getNominationCandidates,candidateMessage } = this.props;
let {jsonSchemaProperties} = this.state;
let candidateKeyValues = { "nominationId" : customProps,
"candidateData":[] };

result.forEach(function(k,i) {
var data2 = k;
for (var configItem in data2) {
if(jsonSchemaProperties[configItem]){
candidateKeyValues.candidateData.push(
{"candidateConfigId" : jsonSchemaProperties[configItem].id,
"value" : data2[configItem]});
}
}
});

var data3 = candidateKeyValues.candidateData,
ids = ['1', '2', '3', '4', '5'],
result4 = data3
.reduce((r, o) => {
let index = r.indices[o.candidateConfigId]++;
r.data3[index] = r.data3[index] || [];
r.data3[index].push(o);
return r;
}, { indices: Object.fromEntries(ids.map(k => [k, 0])), data3: [] })
.data3;

candidateKeyValues.candidateData = result4;
data.forEach((element,i) => {

});

let url = 'nominations/candidates';

const onCloseModal = this.props.onCloseModal;
axios({
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
url: url,
data: candidateKeyValues
})
.then(function (response) {
candidateMessage('Candidate Added Sccessfully...');
getNominationCandidates(customProps);
})
.catch(function (e) {
const message = _.get(e, 'response.data.message');
if(message) {
candidateMessage(message);
}
});
};

componentDidMount() {
const { customProps, getNominationCandidates } = this.props;
const { customProps, getNominationCandidates, moduleId } = this.props;
getNominationCandidates(customProps);

axios.get("modules/"+ moduleId +"/candidate-form-config", {}).then(
(response) => {
var properties = {
// TODO: remove following three
"counsilName": { "type": "hidden", "title": "counsilName", "default": "council", "id": 999 },
"electoralDivisionCode": { "type": "hidden", "title": "electoralDivisionCode", "default": "K01", "id": 998 },
"electoralDivisionName": { "type": "hidden", "title": "electoralDivisionName", "default": "kalutara", "id": 997 },

"nominationId": { "type": "hidden", "title": "nominationId", "default": this.props.customProps, "id": 996},
};

const sortedData = _.orderBy(response.data, ['candidate_config_id'], "asc");
var configLength = sortedData.length;
for (var i = 0; i < configLength; i++) {
const config = sortedData[i];
const keyName = config['key_name'];
const schema = config["json_schema"];
if (schema) {
properties[keyName] = JSON.parse(schema);
} else {
properties[keyName] = { "type": "string"};
}
properties[keyName].title = config['description'];
properties[keyName].id = config['candidate_config_id'];
}
let progress = 1;
// if(index) {
// progress = 0.5;
// }
this.setState({ ajaxState: this.state.ajaxState + progress, jsonSchemaProperties: properties});
});
}

handleDrawerOpen = () => {
Expand All @@ -63,6 +177,15 @@ class CustomizedTable extends React.Component {
this.setState({ open: false });
};

handleFileDownload = () => {
axios.get(`${API_BASE_URL}/nominations/candidate-template/download`, {responseType: 'blob'}, {
}).then((response) => {
download(new Blob([response.data]), 'candidate_upload_template', "text/csv", response.headers['content-type']);
}).catch(err => {
console.log(err)
});
};

render() {
const { classes, CandidateList } = this.props;
const rows = CandidateList;
Expand Down Expand Up @@ -170,9 +293,20 @@ class CustomizedTable extends React.Component {
const options = {
filterType: "dropdown",
responsive: "scroll",
filter: false,
customToolbar: () => {
return (
<CustomToolbar customProps={customProps} modalType="Add" />
<Grid container direction="row" justify="flex-end" spacing={2}>
<Grid item lg={2}>
<CustomToolbar customProps={customProps} modalType="Add" />
</Grid>
{/* <Grid style={{marginTop:15}} item lg={6}>
<DownloadIcon onClick={() => { this.handleFileDownload() }} color="red"/>
</Grid> */}
<Grid style={{marginTop:15}} item lg={6}>
<CSVReader onFileLoaded={this.handleSubmit} />
</Grid>
</Grid>
);
}
};
Expand All @@ -189,14 +323,16 @@ class CustomizedTable extends React.Component {
}
}

const mapStateToProps = ({ Nomination }) => {
const mapStateToProps = ({ Nomination,Election }) => {
const { getNominationCandidates } = Nomination;
const moduleId = Election.ElectionTimeLineData.moduleId;
const CandidateList = Nomination.getNominationCandidates;
return { getNominationCandidates, CandidateList };
return { getNominationCandidates, CandidateList,moduleId };
};

const mapActionsToProps = {
getNominationCandidates
getNominationCandidates,
candidateMessage
};

export default connect(mapStateToProps, mapActionsToProps)(withStyles(styles)(CustomizedTable));
Expand Down
101 changes: 77 additions & 24 deletions server/src/repository/candidate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DBError } from 'Errors';
import { DbConnection } from './dataSource';
import { formatQueryToBulkInsert, formatDataToBulkInsert} from './sqlHelper';
const uuidv4 = require('uuid/v4');


const CANDIDATE_BY_NOMINATION_SELECT_QUERY = `SELECT CD.ID AS CANDIDATE_ID,
Expand Down Expand Up @@ -179,33 +180,85 @@ const updateNominationStatus = (nominationId,transaction) => {
* save candidate data
* @returns {Promise.<T>}
*/
const saveCandidate = async (candidateId,nominationId,data, transaction) => {
const saveCandidate = async (candidateId,nominationId,data,from, transaction) => {
const params = {candidateId:candidateId};
data = data.map((record) => {
record.nominationId = nominationId;
record.id = candidateId
return record;
});
await DbConnection()
.query(CANDIDATE_DELETE_QUERY,
{
replacements: params,
type: DbConnection().QueryTypes.DELETE,
transaction
}).catch((error) => {
throw new DBError(error);
});

if(candidateId == undefined){
console.log("from",from);
if(from == "form"){
var uuid2 = uuidv4();
data = data.map((record) => {
record.nominationId = nominationId;
record.id = uuid2;
return record;
});
}else{
data = data.map((record) => {
var uuid = uuidv4();
record.map((val) => {
val.id = uuid
val.nominationId = nominationId;
})
return record;
});
}
}else{
data = data.map((record) => {
record.nominationId = nominationId;
record.id = candidateId
return record;
});
}
if(candidateId !== undefined){
await DbConnection()
.query(CANDIDATE_DELETE_QUERY,
{
replacements: params,
type: DbConnection().QueryTypes.DELETE,
transaction
}).catch((error) => {
throw new DBError(error);
});
}

if( data instanceof Array && data.length > 0){
try {
return DbConnection()
.query(formatQueryToBulkInsert(CANDIDATE_DATA_INSERT_BASE_QUERY, data),
{
replacements: formatDataToBulkInsert(data, CANDIDATE_DATA_COLUMN_ORDER),
type: DbConnection().QueryTypes.INSERT,
transaction,
}).catch((error) => {
throw new DBError(error);
});
if(candidateId){
return DbConnection()
.query(formatQueryToBulkInsert(CANDIDATE_DATA_INSERT_BASE_QUERY, data),
{
replacements: formatDataToBulkInsert(data, CANDIDATE_DATA_COLUMN_ORDER),
type: DbConnection().QueryTypes.INSERT,
transaction,
}).catch((error) => {
throw new DBError(error);
});
}else{
if(from == "form"){
return DbConnection()
.query(formatQueryToBulkInsert(CANDIDATE_DATA_INSERT_BASE_QUERY, data),
{
replacements: formatDataToBulkInsert(data, CANDIDATE_DATA_COLUMN_ORDER),
type: DbConnection().QueryTypes.INSERT,
transaction,
}).catch((error) => {
throw new DBError(error);
});
}else{
for(let i=0;i<data.length;i++){
DbConnection()
.query(formatQueryToBulkInsert(CANDIDATE_DATA_INSERT_BASE_QUERY, data[i]),
{
replacements: formatDataToBulkInsert(data[i], CANDIDATE_DATA_COLUMN_ORDER),
type: DbConnection().QueryTypes.INSERT,
transaction,
}).catch((error) => {
throw new DBError(error);
});
}
}
}

}catch (e){
console.log(e);
}
Expand Down
Loading

0 comments on commit 180ad7d

Please sign in to comment.