-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We want to view when an account import is in progress on the balance page and in the settings page. For that we activate the realtime on the jobs when we are on one of these 2 pages otherwise we stop it. We also want to display a notification to the user whether it went correctly or not. There are several tests which are not added yet
- Loading branch information
Florian Pires
committed
Mar 8, 2021
1 parent
89458bc
commit 6e17b15
Showing
15 changed files
with
600 additions
and
160 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* eslint-disable react-hooks/exhaustive-deps */ | ||
import React, { | ||
createContext, | ||
useContext, | ||
useEffect, | ||
useMemo, | ||
useRef, | ||
useState | ||
} from 'react' | ||
import { JOBS_DOCTYPE } from '../doctypes' | ||
import CozyRealtime from 'cozy-realtime' | ||
import logger from 'cozy-logger' | ||
import isFunction from 'lodash/isFunction' | ||
|
||
const log = logger.namespace('import.context') | ||
|
||
export const JobsContext = createContext({}) | ||
|
||
export const useJobsContext = () => { | ||
return useContext(JobsContext) | ||
} | ||
|
||
// @TODO add tests for JobsContext | ||
/** Allows to subscribe to jobs and thus to know jobs in progress | ||
* | ||
* @param client | ||
* @param options | ||
* @returns jobsInProgress | ||
*/ | ||
const JobsProvider = ({ children, client, options }) => { | ||
const [jobsInProgress, setJobsInProgress] = useState([]) | ||
const realtimeStartedRef = useRef(false) | ||
const jobsInProgressRef = useRef([]) | ||
|
||
const realtime = useMemo(() => { | ||
return new CozyRealtime({ client }) | ||
}, [client]) | ||
|
||
const handleRealtime = data => { | ||
const { worker, state, message: msg } = data | ||
|
||
// hack: Using jobsInProgressRef because jobsInProgress (from state) | ||
// keep its default value | ||
// The ideal would be to use only jobsInProgress and do not use the ref | ||
const { current: currJobsInProgress } = jobsInProgressRef | ||
const index = currJobsInProgress.findIndex(a => a.account === msg.account) | ||
const exist = index !== -1 | ||
|
||
const { onSuccess, onError } = options | ||
let arr = [...currJobsInProgress] | ||
if (worker === 'konnector') { | ||
if (state === 'running' && !exist) { | ||
arr.push(msg) | ||
} else if (state === 'done' && exist) { | ||
arr.splice(index, 1) | ||
|
||
if (isFunction(onSuccess)) { | ||
onSuccess() | ||
} | ||
} else if (state === 'errored' && exist) { | ||
arr.splice(index, 1) | ||
if (isFunction(onError)) { | ||
onError() | ||
} | ||
} | ||
jobsInProgressRef.current = arr | ||
setJobsInProgress(jobsInProgressRef.current) | ||
} | ||
} | ||
|
||
const startJobsRealtime = () => { | ||
if (!realtimeStartedRef.current) { | ||
log('info', 'Start Jobs Realtime') | ||
realtimeStartedRef.current = true | ||
|
||
realtime.subscribe('created', JOBS_DOCTYPE, handleRealtime) | ||
realtime.subscribe('updated', JOBS_DOCTYPE, handleRealtime) | ||
realtime.subscribe('deleted', JOBS_DOCTYPE, handleRealtime) | ||
} | ||
} | ||
|
||
const stopJobsRealtime = () => { | ||
if (realtimeStartedRef.current) { | ||
log('info', 'Stop Jobs Realtime') | ||
realtimeStartedRef.current = false | ||
|
||
realtime.unsubscribe('created', JOBS_DOCTYPE, handleRealtime) | ||
realtime.unsubscribe('updated', JOBS_DOCTYPE, handleRealtime) | ||
realtime.unsubscribe('deleted', JOBS_DOCTYPE, handleRealtime) | ||
} | ||
} | ||
|
||
// @TODO useRealtime hook | ||
useEffect(() => { | ||
startJobsRealtime() | ||
return () => stopJobsRealtime() | ||
}, []) | ||
|
||
return ( | ||
<JobsContext.Provider | ||
value={{ | ||
jobsInProgress | ||
}} | ||
> | ||
{children} | ||
</JobsContext.Provider> | ||
) | ||
} | ||
|
||
export default JobsProvider | ||
|
||
/** | ||
* @function | ||
* @description HOC to provide import context as prop | ||
* | ||
* @param {Component} Component - wrapped component | ||
* @returns {Function} - Component that will receive import context as prop | ||
*/ | ||
export const withJobsInProgress = Component => { | ||
const Wrapped = props => { | ||
const { jobsInProgress = [] } = useJobsContext() | ||
return <Component {...props} jobsInProgress={jobsInProgress} /> | ||
} | ||
Wrapped.displayName = `withJobsInProgress(${Component.displayName || | ||
Component.name})` | ||
return Wrapped | ||
} |
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
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
Oops, something went wrong.