-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7453d3a
commit a500fad
Showing
7 changed files
with
363 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,251 @@ | ||
import { FC, useCallback, useEffect, useState } from 'react'; | ||
import axios from 'axios'; | ||
|
||
import { cn } from '@/lib/utils'; | ||
import { socket } from '@/lib/socket'; | ||
import { env } from '@/env.mjs'; | ||
import { formatPercentage } from '@/utils/number-format'; | ||
|
||
const STEPS_NAMES = { | ||
VALIDATING: 'Validating', | ||
'DATA SOURCING': 'Data Sourcing', | ||
GEOCODING: 'Geocoding', | ||
'IMPACT CALCULATION': 'Impact Calculation', | ||
} as const; | ||
|
||
type ProgressTask = { | ||
kind: 'DATA_IMPORT_PROGRESS'; | ||
data: { | ||
step: 'VALIDATING' | 'DATA SOURCING' | 'IMPACT CALCULATION' | 'GEOCODING'; | ||
status: 'running' | 'idle' | 'completed'; | ||
progress: number; | ||
}[]; | ||
}; | ||
|
||
const SAMPLE_DATA: ProgressTask[] = [ | ||
{ | ||
kind: 'DATA_IMPORT_PROGRESS', | ||
data: [ | ||
{ | ||
step: 'VALIDATING', | ||
status: 'running', | ||
progress: 0, | ||
}, | ||
{ | ||
step: 'DATA SOURCING', | ||
status: 'idle', | ||
progress: 0, | ||
}, | ||
{ | ||
step: 'GEOCODING', | ||
status: 'idle', | ||
progress: 0, | ||
}, | ||
{ | ||
step: 'IMPACT CALCULATION', | ||
status: 'idle', | ||
progress: 0, | ||
}, | ||
], | ||
}, | ||
{ | ||
kind: 'DATA_IMPORT_PROGRESS', | ||
data: [ | ||
{ | ||
step: 'VALIDATING', | ||
status: 'running', | ||
progress: 10, | ||
}, | ||
{ | ||
step: 'DATA SOURCING', | ||
status: 'idle', | ||
progress: 0, | ||
}, | ||
{ | ||
step: 'GEOCODING', | ||
status: 'idle', | ||
progress: 0, | ||
}, | ||
{ | ||
step: 'IMPACT CALCULATION', | ||
status: 'idle', | ||
progress: 0, | ||
}, | ||
], | ||
}, | ||
{ | ||
kind: 'DATA_IMPORT_PROGRESS', | ||
data: [ | ||
{ | ||
step: 'VALIDATING', | ||
status: 'running', | ||
progress: 20, | ||
}, | ||
{ | ||
step: 'DATA SOURCING', | ||
status: 'idle', | ||
progress: 0, | ||
}, | ||
{ | ||
step: 'GEOCODING', | ||
status: 'idle', | ||
progress: 0, | ||
}, | ||
{ | ||
step: 'IMPACT CALCULATION', | ||
status: 'idle', | ||
progress: 0, | ||
}, | ||
], | ||
}, | ||
{ | ||
kind: 'DATA_IMPORT_PROGRESS', | ||
data: [ | ||
{ | ||
step: 'VALIDATING', | ||
status: 'completed', | ||
progress: 100, | ||
}, | ||
{ | ||
step: 'DATA SOURCING', | ||
status: 'running', | ||
progress: 23, | ||
}, | ||
{ | ||
step: 'GEOCODING', | ||
status: 'idle', | ||
progress: 0, | ||
}, | ||
{ | ||
step: 'IMPACT CALCULATION', | ||
status: 'idle', | ||
progress: 0, | ||
}, | ||
], | ||
}, | ||
{ | ||
kind: 'DATA_IMPORT_PROGRESS', | ||
data: [ | ||
{ | ||
step: 'VALIDATING', | ||
status: 'completed', | ||
progress: 100, | ||
}, | ||
{ | ||
step: 'DATA SOURCING', | ||
status: 'completed', | ||
progress: 100, | ||
}, | ||
{ | ||
step: 'GEOCODING', | ||
status: 'running', | ||
progress: 34, | ||
}, | ||
{ | ||
step: 'IMPACT CALCULATION', | ||
status: 'idle', | ||
progress: 0, | ||
}, | ||
], | ||
}, | ||
{ | ||
kind: 'DATA_IMPORT_PROGRESS', | ||
data: [ | ||
{ | ||
step: 'VALIDATING', | ||
status: 'completed', | ||
progress: 100, | ||
}, | ||
{ | ||
step: 'DATA SOURCING', | ||
status: 'completed', | ||
progress: 100, | ||
}, | ||
{ | ||
step: 'GEOCODING', | ||
status: 'completed', | ||
progress: 100, | ||
}, | ||
{ | ||
step: 'IMPACT CALCULATION', | ||
status: 'completed', | ||
progress: 100, | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
export const UploadTracker: FC = () => { | ||
const [tasksProgress, setTaskProgress] = useState<ProgressTask>(SAMPLE_DATA[0]); | ||
// ! THIS IS FOR TESTING.REMOVE | ||
const [testIndex, setTestIndex] = useState(0); | ||
|
||
// ! THIS IS FOR TESTING.REMOVE | ||
const triggerSocket = useCallback(() => { | ||
axios.get(env.NEXT_PUBLIC_API_URL + '/tasks/progress'); | ||
}, []); | ||
|
||
useEffect(() => { | ||
socket.connect(); | ||
|
||
return () => { | ||
socket.disconnect(); | ||
}; | ||
}, []); | ||
|
||
useEffect(() => { | ||
function setTasksProgress(tasks: ProgressTask) { | ||
setTaskProgress(tasks); | ||
} | ||
|
||
socket.on('DATA_IMPORT_PROGRESS', setTasksProgress); | ||
|
||
return () => { | ||
socket.off('DATA_IMPORT_PROGRESS', setTasksProgress); | ||
}; | ||
}, []); | ||
|
||
// ! THIS IS FOR TESTING.REMOVE | ||
useEffect(() => { | ||
function setTestProgress() { | ||
setTaskProgress(SAMPLE_DATA[testIndex]); | ||
|
||
setTestIndex((prev) => (prev + 1 >= SAMPLE_DATA.length ? 0 : prev + 1)); | ||
} | ||
|
||
const intervalId = window.setInterval(setTestProgress, 1500); | ||
|
||
return () => { | ||
window.clearInterval(intervalId); | ||
}; | ||
}, [tasksProgress, testIndex]); | ||
|
||
return ( | ||
<div className="w-full rounded-3xl bg-white px-8 py-10"> | ||
{/* // ! THIS IS FOR TESTING.REMOVE */} | ||
<button type="button" onClick={triggerSocket} className="mb-8"> | ||
Trigger Socket | ||
</button> | ||
<div className="grid grid-cols-4 gap-1"> | ||
{tasksProgress?.data?.map(({ step, status, progress }) => ( | ||
<div className="flex flex-col items-start" key={step}> | ||
<span | ||
className={cn('text-base text-gray-400 transition-colors', { | ||
'text-gray-900': ['running', 'completed'].includes(status), | ||
})} | ||
> | ||
{STEPS_NAMES[step]} | ||
</span> | ||
{status !== 'idle' && ( | ||
<span className="text-sm text-gray-500">{`Progress: ${formatPercentage( | ||
progress / 100, | ||
)}`}</span> | ||
)} | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default UploadTracker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { io } from 'socket.io-client'; | ||
|
||
import { env } from '@/env.mjs'; | ||
|
||
export const socket = io(env.NEXT_PUBLIC_API_URL, { | ||
autoConnect: false, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.