-
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 a7edc37
Showing
7 changed files
with
197 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,84 @@ | ||
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_DATA: 'Validating', | ||
IMPORTING_DATA: 'Importing Data', | ||
GEOCODING: 'Geocoding', | ||
CALCULATING_IMPACT: 'Calculating Impact', | ||
} as const; | ||
|
||
type ProgressTask = { | ||
kind: 'DATA_IMPORT_PROGRESS'; | ||
data: Record< | ||
keyof typeof STEPS_NAMES, | ||
{ | ||
status: 'processing' | 'idle' | 'completed'; | ||
progress: number; | ||
} | ||
>; | ||
}; | ||
|
||
export const UploadTracker: FC = () => { | ||
const [tasksProgress, setTaskProgress] = useState<ProgressTask['data']>(undefined); | ||
|
||
// ! THIS IS FOR TESTING.REMOVE | ||
const triggerSocket = useCallback(() => { | ||
axios.get(env.NEXT_PUBLIC_API_URL + '/api/v1/import/sockets/start?time=60'); | ||
}, []); | ||
|
||
useEffect(() => { | ||
socket.connect(); | ||
|
||
return () => { | ||
socket.disconnect(); | ||
}; | ||
}, []); | ||
|
||
useEffect(() => { | ||
function setTasksProgress(tasks: ProgressTask) { | ||
console.log({ tasks }); | ||
setTaskProgress(tasks?.data); | ||
} | ||
|
||
socket.on('DATA_IMPORT_PROGRESS', setTasksProgress); | ||
|
||
return () => { | ||
socket.off('DATA_IMPORT_PROGRESS', setTasksProgress); | ||
}; | ||
}, []); | ||
|
||
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"> | ||
{Object.keys(tasksProgress || {})?.map((key: keyof typeof STEPS_NAMES) => ( | ||
<div className="flex flex-col items-start" key={key}> | ||
<span | ||
className={cn('text-base text-gray-400 transition-colors', { | ||
'text-gray-900': ['processing', 'completed'].includes(tasksProgress[key]?.status), | ||
})} | ||
> | ||
{STEPS_NAMES[key]} | ||
</span> | ||
{tasksProgress[key]?.status !== 'idle' && ( | ||
<span className="text-sm text-gray-500">{`Progress: ${formatPercentage( | ||
tasksProgress[key]?.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,8 @@ | ||
import { io } from 'socket.io-client'; | ||
|
||
import { env } from '@/env.mjs'; | ||
|
||
export const socket = io(env.NEXT_PUBLIC_API_URL, { | ||
autoConnect: false, | ||
transports: ['websocket'], | ||
}); |
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