Skip to content

Commit

Permalink
Upload tariff zones.
Browse files Browse the repository at this point in the history
  • Loading branch information
mansoor-sajjad committed Nov 21, 2023
1 parent 7a085ac commit 8b9073b
Show file tree
Hide file tree
Showing 11 changed files with 296 additions and 185 deletions.
58 changes: 58 additions & 0 deletions src/actions/SuppliersActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ SuppliersActions.uploadFiles = (files, providerId) => async (
) => {
dispatch(sendData(0, types.UPDATED_FILE_UPLOAD_PROGRESS));

console.log('uploadFiles - Files to upload', files);

const url = `${window.config.timetableAdminBaseUrl}${providerId}/files`;

var data = new FormData();
Expand Down Expand Up @@ -185,6 +187,62 @@ SuppliersActions.uploadFiles = (files, providerId) => async (
});
};

SuppliersActions.uploadTariffZonesFiles = (files, providerId) => async (
dispatch,
getState
) => {
dispatch(sendData(0, types.UPDATED_TARIFF_ZONE_FILE_UPLOAD_PROGRESS));

console.log('uploadTariffZonesFiles - Files to upload', files);

const url = `${window.config.tariffZonesUrl}SOF/files`;

var data = new FormData();

files.forEach(file => {
data.append('files', file);
});

var config = {
onUploadProgress: function(progressEvent) {
let percentCompleted = (progressEvent.loaded / progressEvent.total) * 100;
dispatch(
sendData(
percentCompleted,
types.UPDATED_TARIFF_ZONE_FILE_UPLOAD_PROGRESS
)
);
},
...(await getApiConfig(getState().UserReducer.auth))
};

return axios
.post(url, data, config)
.then(function(response) {
dispatch(
SuppliersActions.addNotification(
'Uploaded tariff zone file(s)',
'success'
)
);
dispatch(
SuppliersActions.logEvent({
title: 'Uploaded tariff zone file(s): ' + files.join(',')
})
);
dispatch(sendData(0, types.UPDATED_TARIFF_ZONE_FILE_UPLOAD_PROGRESS));
})
.catch(function(response) {
dispatch(
SuppliersActions.addNotification(
'Unable to upload tariff zone file(s)',
'error'
)
);
dispatch(sendData(0, types.UPDATED_TARIFF_ZONE_FILE_UPLOAD_PROGRESS));
});
};

SuppliersActions.getAllProviderStatus = () => async (dispatch, getState) => {
dispatch(sendData(null, types.REQUESTED_ALL_SUPPLIERS_STATUS));
const state = getState();
Expand Down
2 changes: 2 additions & 0 deletions src/actions/actionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ export const REQUESTED_ALL_SUPPLIERS_STATUS = 'REQUESTED_ALL_SUPPLIERS_STATUS';
export const RECEIVED_ALL_SUPPLIERS_STATUS = 'RECEIVED_ALL_SUPPLIERS_STATUS';

export const UPDATED_FILE_UPLOAD_PROGRESS = 'UPDATED_FILE_UPLOAD_PROGRESS';
export const UPDATED_TARIFF_ZONE_FILE_UPLOAD_PROGRESS =
'UPDATED_TARIFF_ZONE_FILE_UPLOAD_PROGRESS';

/* organization actions */

Expand Down
16 changes: 0 additions & 16 deletions src/app/components/Header.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
/*
* Licensed under the EUPL, Version 1.2 or – as soon they will be approved by
* the European Commission - subsequent versions of the EUPL (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* https://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*
*/

import React from 'react';
import { connect } from 'react-redux';
import AppBar from 'material-ui/AppBar';
Expand Down
5 changes: 5 additions & 0 deletions src/reducers/SuppliersReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ const SuppliersReducer = (state = initialState, action) => {
case types.UPDATED_FILE_UPLOAD_PROGRESS:
return Object.assign({}, state, { fileUploadProgress: action.payLoad });

case types.UPDATED_TARIFF_ZONE_FILE_UPLOAD_PROGRESS:
return Object.assign({}, state, {
tariffZoneFileUploadProgress: action.payLoad
});

case types.RECEIVED_GRAPH_STATUS:
return Object.assign({}, state, { ...action.payLoad });

Expand Down
119 changes: 119 additions & 0 deletions src/screens/common/components/FileUpload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import React, { useEffect, useState } from 'react';
import Dropzone from 'react-dropzone';
import Button from 'muicss/lib/react/button';
import { Line as Progress } from 'rc-progress';

export const FileUpload = ({ fileUploadProgress, handleFileUpload }) => {
const [files, setFiles] = useState([]);
const [showSuccess, setShowSuccess] = useState(false);
const [uploadProgress, setUploadProgress] = useState(0);

useEffect(() => {
if (fileUploadProgress === 100) {
setFiles([]);
setShowSuccess(true);
}
}, [fileUploadProgress]);

useEffect(() => {
let showSuccessTimer = setTimeout(() => resetProgress(), 3000);
return () => {
clearTimeout(showSuccessTimer);
};
}, [showSuccess]);

useEffect(() => {
if (!showSuccess) {
setUploadProgress(fileUploadProgress);
}
}, [fileUploadProgress]);

const resetProgress = () => {
setShowSuccess(false);
setUploadProgress(0);
};

const handleOnDrop = files => {
if (files.length) {
setFiles(files);
}
};

const dropStyle = {
height: '150px',
borderWidth: 1,
borderColor: '#666',
borderStyle: 'dashed',
borderRadius: 2,
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
cursor: 'pointer'
};

const filesStyle = {
marginTop: '5%',
minHeight: '300px',
maxHeight: '300px'
};

const uploadButtonStyle = {
width: 100,
fontSize: '0.8em',
textAlign: 'center',
marginLeft: 'auto',
marginTop: 10
};

return (
<div
style={{
display: 'flex',
flexDirection: 'column'
}}
>
<Dropzone
style={dropStyle}
accept="application/zip,
application/octet-stream,
application/x-zip,
application/x-rar,
application/x-zip-compressed,
application/x-rar-compressed,
compressed/rar,
application/rar,
application/xml,
text/xml"
onDrop={(files, rejected) => {
handleOnDrop(files);
console.warn('rejected', rejected);
}}
>
<div style={{ textAlign: 'center' }}>
Try dropping some files here, or click to select files to upload.
</div>
</Dropzone>
{files.length ? (
<select style={filesStyle} multiple>
{files.map((file, index) => {
return <option key={'file-' + index}>{file.name}</option>;
})}
</select>
) : (
<div style={{ marginTop: '5%' }}>No files added</div>
)}
<Progress strokeColor="#8dcc91" percent={uploadProgress} />
<Button
disabled={!files.length}
style={uploadButtonStyle}
color="primary"
onClick={() => files.length && handleFileUpload(files)}
>
Upload
</Button>
{showSuccess && (
<div style={{ color: 'green' }}>Files successfully uploaded</div>
)}
</div>
);
};
74 changes: 38 additions & 36 deletions src/screens/common/components/SelectSupplier.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,45 @@ import React from 'react';
export const SelectSupplier = ({
suppliers,
selectSupplier,
selectedSupplierId
selectedSupplierId,
errorText
}) => {
console.log(suppliers, selectSupplier, selectedSupplierId);

return (
<SelectField
id="select-supplier"
floatingLabelFixed={true}
style={{ minWidth: 350 }}
floatingLabelText={'Provider'}
onChange={(e, k, v) => selectSupplier(v)}
autoWidth={true}
value={Number(selectedSupplierId) || -1}
iconStyle={{ fill: 'rgba(22, 82, 149, 0.69)' }}
>
{suppliers.map(supplier => {
const isLevel1Provider =
(supplier.chouetteInfo &&
supplier.chouetteInfo.migrateDataToProvider) ||
supplier.id === -1;
return (
<MenuItem
key={supplier.id}
value={supplier.id}
label={supplier.name}
primaryText={
<span
style={{
color: isLevel1Provider ? 'intial' : '#d9a51b'
}}
>
{supplier.name}
</span>
}
/>
);
})}
</SelectField>
<>
<SelectField
id="select-supplier"
floatingLabelFixed={true}
style={{ minWidth: 350 }}
floatingLabelText={'Provider'}
onChange={(e, k, v) => selectSupplier(v)}
autoWidth={true}
value={Number(selectedSupplierId) || -1}
iconStyle={{ fill: 'rgba(22, 82, 149, 0.69)' }}
>
{suppliers.map(supplier => {
const isLevel1Provider =
(supplier.chouetteInfo &&
supplier.chouetteInfo.migrateDataToProvider) ||
supplier.id === -1;
return (
<MenuItem
key={supplier.id}
value={supplier.id}
label={supplier.name}
primaryText={
<span
style={{
color: isLevel1Provider ? 'intial' : '#d9a51b'
}}
>
{supplier.name}
</span>
}
/>
);
})}
</SelectField>
{errorText && <div style={{ color: 'red' }}>{errorText}</div>}
</>
);
};
Original file line number Diff line number Diff line change
@@ -1,17 +1,62 @@
import { SelectSupplier } from '../../../common/components/SelectSupplier';
import { useSelector } from 'react-redux';
import { useState } from 'react';
import { useSelector, shallowEqual, useDispatch } from 'react-redux';
import React, { useState } from 'react';
import { FileUpload } from '../../../common/components/FileUpload';
import SuppliersActions from '../../../../actions/SuppliersActions';

export const TariffZonesImport = () => {
const suppliers = useSelector(state => state.SuppliersReducer.data, []);
const [selectedSupplier, setSelectedSupplier] = useState();
const suppliers = useSelector(
state => state.SuppliersReducer.data,
shallowEqual
);

return null;
{
/*
<SelectSupplier
const tariffZoneFileUploadProgress = useSelector(
state => state.SuppliersReducer.tariffZoneFileUploadProgress,
shallowEqual
);

/>;
*/
}
const [selectedSupplier, setSelectedSupplier] = useState(null);
const [noSupplierSelected, setNoSupplierSelected] = useState(null);
const dispatch = useDispatch();

const handleFileUpload = files => {
if (selectedSupplier === null) {
setNoSupplierSelected(true);
} else {
dispatch(
SuppliersActions.uploadTariffZonesFiles(files, selectedSupplier)
);
}
};

const onSelectSupplier = selectedSupplierId => {
setNoSupplierSelected(false);
setSelectedSupplier(
suppliers.find(supplier => supplier.id === selectedSupplierId)
);
};

return (
<div
style={{
backgroundColor: '#fff',
padding: '20px',
display: 'flex',
flexDirection: 'column'
}}
>
<SelectSupplier
suppliers={suppliers}
selectSupplier={onSelectSupplier}
selectedSupplierId={selectedSupplier && selectedSupplier.id}
errorText={noSupplierSelected === true && 'No Supplier selected'}
/>
<div style={{ width: '50%', marginTop: '20px' }}>
<FileUpload
fileUploadProgress={tariffZoneFileUploadProgress}
handleFileUpload={handleFileUpload}
/>
</div>
</div>
);
};
Loading

0 comments on commit 8b9073b

Please sign in to comment.