Skip to content

Commit e51f1a5

Browse files
authored
Merge pull request #3 from EnjoyBacon7/Code-Refactor
Code refactor
2 parents 3ee495d + 25bedef commit e51f1a5

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

web/src/components/CbToastsContainer.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ function CbToastsContainer() {
2020
return (
2121
<ToastContainer className="p-3" position='bottom-end'>
2222
{notifications.map((notification) => (
23-
<Toast onClick={() => { removeNotification(notification.id) }}>
23+
<Toast onClose={() => { removeNotification(notification.id) }}>
2424
<Toast.Header>
25-
<strong className="me-auto">Upload in progress...</strong>
25+
<strong className="me-auto">{notification.header}</strong>
2626
<small>
2727
{toastAge(notification)}
2828
</small>
2929

3030
</Toast.Header>
3131
<Toast.Body>
32-
<ProgressBar animated now={notification.progress} />
32+
<ProgressBar animated={notification.progress !== 100} variant={notification.progress !== 100 ? "info" : "success"} now={notification.progress} />
3333
</Toast.Body>
3434
</Toast>
3535
))}
@@ -41,7 +41,7 @@ export default CbToastsContainer;
4141

4242
function toastAge(notification) {
4343

44-
const ageSeconds = Math.floor((Date.now() - notification.id) / 1000);
44+
const ageSeconds = Math.floor((Date.now() - notification.age) / 1000);
4545
const ageMinutes = Math.floor(ageSeconds / 60);
4646

4747
if (ageSeconds < 60) {

web/src/components/CbToastsContext.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,23 @@ const NotificationContext = createContext();
66
export function NotificationProvider({ children }) {
77
const [notifications, setNotifications] = useState([]);
88

9-
const addNotification = (header, progress) => {
10-
const newNotification = { id: Date.now(), header, progress };
9+
const addNotification = (id, type, progress) => {
10+
11+
var header = ""
12+
13+
switch (type) {
14+
case 1: // Upload
15+
if (progress === 100) {
16+
header = "Upload Complete"
17+
} else {
18+
header = "Upload in progress..."
19+
}
20+
break;
21+
default:
22+
break;
23+
}
24+
25+
const newNotification = { id, header, progress, age: Date.now() };
1126
setNotifications([...notifications, newNotification]);
1227
};
1328

web/src/components/CbUpload.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useParams } from 'react-router-dom';
44
import Button from 'react-bootstrap/Button';
55
import Form from 'react-bootstrap/Form';
66
import { useNavigate } from 'react-router-dom';
7+
import { v4 as uuidv4 } from 'uuid';
78

89
// Local imports
910
import { useNotification } from './CbToastsContext';
@@ -66,7 +67,7 @@ export function CbUpload({ loadFiles }) {
6667
let start = 0;
6768
let end = 0;
6869

69-
const uploadFile = (file, start, end, callback) => {
70+
const uploadFile = (file, start, end, callback, uploadId) => {
7071

7172
// Ceate form data using chunk info and shareId
7273
const chunk = file.slice(start, end);
@@ -81,11 +82,11 @@ export function CbUpload({ loadFiles }) {
8182
request.open('POST', `/api/upload?shareId=${encodeURIComponent(shareId)}`, true);
8283
request.onreadystatechange = function () {
8384
if (request.status >= 200 && request.status < 400) {
84-
addNotification('Uploading ' + file.name, start / file.size * 100);
85+
addNotification(uploadId, 1, start / file.size * 100);
8586
start = end;
8687
end = Math.min(end + chunkSize, file.size);
8788
if (start != end && !lastChunkSent) {
88-
uploadFile(file, start, end, callback)
89+
uploadFile(file, start, end, callback, uploadId)
8990
}
9091
if (start == end && !lastChunkSent) {
9192
callback();
@@ -100,6 +101,8 @@ export function CbUpload({ loadFiles }) {
100101

101102
const handleUpload = (files) => {
102103

104+
let uploadId = uuidv4();
105+
103106
const uploadNextFile = () => {
104107
if (files.length > 0) {
105108
let start = 0;
@@ -108,7 +111,7 @@ export function CbUpload({ loadFiles }) {
108111
uploadFile(files.pop(), start, end, () => {
109112
loadFiles(); // Load files after each file is uploaded
110113
uploadNextFile(); // Upload the next file
111-
});
114+
}, uploadId);
112115
}
113116
};
114117

web/src/routes/CbRoot.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ export function CbRoot( { isHome } ) {
1515

1616
// Useful hooks
1717
const [shareId, setShareId] = useState(useParams().shareId);
18-
console.log(shareId)
19-
console.log(isHome)
2018
const navigate = useNavigate();
2119
// Hooks if in share
2220
const [viewMode, setViewMode] = useState("list");
@@ -34,7 +32,6 @@ export function CbRoot( { isHome } ) {
3432

3533
function loadFiles() {
3634
const shareId_forLoad = window.location.pathname.split('/')[2]; // Use useRef instead?
37-
console.log("loadFiles")
3835
var request = new XMLHttpRequest();
3936
request.open('GET', `/api/search?shareId=${shareId_forLoad}`, true);
4037
request.onload = function () {
@@ -59,7 +56,6 @@ export function CbRoot( { isHome } ) {
5956
setFileInfo([]);
6057
loadFiles();
6158
}
62-
console.log("useEffect")
6359
}, [isHome]);
6460

6561
return (

0 commit comments

Comments
 (0)