-
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.
Merge remote-tracking branch 'origin/feat/massCategorization'
- Loading branch information
Showing
18 changed files
with
591 additions
and
175 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import React, { | ||
createContext, | ||
useState, | ||
useCallback, | ||
useEffect, | ||
useContext, | ||
useMemo | ||
} from 'react' | ||
|
||
import flag from 'cozy-flags' | ||
|
||
export const SelectionContext = createContext() | ||
|
||
export const useSelectionContext = () => { | ||
return useContext(SelectionContext) | ||
} | ||
|
||
const SelectionProvider = ({ children }) => { | ||
const [isSelectionModeActive, setIsSelectionModeActive] = useState(false) | ||
const [selected, setSelected] = useState([]) | ||
const isSelectionModeEnabled = flag('banks.selectionMode.enabled') | ||
|
||
const activateSelectionMode = useCallback( | ||
() => isSelectionModeEnabled && setIsSelectionModeActive(true), | ||
[isSelectionModeEnabled] | ||
) | ||
|
||
const deactivateSelectionMode = () => setIsSelectionModeActive(false) | ||
|
||
const addToSelection = useCallback( | ||
item => { | ||
setSelected(v => [...v, item]) | ||
}, | ||
[setSelected] | ||
) | ||
|
||
const removeFromSelection = useCallback( | ||
item => { | ||
setSelected(selected.filter(e => e._id !== item._id)) | ||
}, | ||
[selected] | ||
) | ||
|
||
const emptySelection = useCallback(() => setSelected([]), [setSelected]) | ||
|
||
const isSelected = useCallback(item => selected.includes(item), [selected]) | ||
|
||
useEffect(() => { | ||
if (isSelectionModeActive && selected.length === 0) { | ||
deactivateSelectionMode() | ||
} | ||
if (!isSelectionModeActive && selected.length > 0) { | ||
activateSelectionMode() | ||
} | ||
}, [selected, activateSelectionMode, isSelectionModeActive]) | ||
|
||
const value = useMemo( | ||
() => ({ | ||
isSelectionModeActive, | ||
selected, | ||
addToSelection, | ||
isSelected, | ||
emptySelection, | ||
removeFromSelection, | ||
isSelectionModeEnabled | ||
}), | ||
[ | ||
addToSelection, | ||
emptySelection, | ||
isSelected, | ||
isSelectionModeActive, | ||
isSelectionModeEnabled, | ||
removeFromSelection, | ||
selected | ||
] | ||
) | ||
|
||
return ( | ||
<SelectionContext.Provider value={value}> | ||
{children} | ||
</SelectionContext.Provider> | ||
) | ||
} | ||
|
||
export default SelectionProvider |
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,63 @@ | ||
import React, { useCallback, useMemo } from 'react' | ||
|
||
import UISelectionBar from 'cozy-ui/transpiled/react/SelectionBar' | ||
import Alerter from 'cozy-ui/transpiled/react/Alerter' | ||
import { useI18n } from 'cozy-ui/transpiled/react/I18n' | ||
|
||
import { useSelectionContext } from 'ducks/context/SelectionContext' | ||
import { makeSelectionBarActions } from 'ducks/selection/helpers' | ||
import { useTransactionCategoryModal } from 'ducks/transactions/TransactionRow' | ||
|
||
const SelectionBar = () => { | ||
const { | ||
isSelectionModeEnabled, | ||
isSelectionModeActive, | ||
selected, | ||
emptySelection | ||
} = useSelectionContext() | ||
|
||
const { t } = useI18n() | ||
|
||
const beforeUpdate = useCallback(() => { | ||
emptySelection() | ||
}, [emptySelection]) | ||
|
||
const afterUpdates = () => { | ||
Alerter.success( | ||
t('Categorization.success', { | ||
smart_count: selected.length | ||
}) | ||
) | ||
} | ||
|
||
const [ | ||
showTransactionCategoryModal, | ||
, | ||
transactionCategoryModal | ||
] = useTransactionCategoryModal({ | ||
transactions: selected, | ||
beforeUpdate, | ||
afterUpdates | ||
}) | ||
|
||
const actions = useMemo( | ||
() => makeSelectionBarActions(showTransactionCategoryModal), | ||
[showTransactionCategoryModal] | ||
) | ||
|
||
if (!isSelectionModeEnabled) return null | ||
return ( | ||
<> | ||
{isSelectionModeActive && ( | ||
<UISelectionBar | ||
actions={actions} | ||
selected={selected} | ||
hideSelectionBar={emptySelection} | ||
/> | ||
)} | ||
{transactionCategoryModal} | ||
</> | ||
) | ||
} | ||
|
||
export default SelectionBar |
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,11 @@ | ||
import GraphCircleIcon from 'cozy-ui/transpiled/react/Icons/GraphCircle' | ||
|
||
export const makeSelectionBarActions = showTransactionCategoryModal => { | ||
return { | ||
categorize: { | ||
action: () => showTransactionCategoryModal(), | ||
displayCondition: selected => selected.length > 0, | ||
icon: GraphCircleIcon | ||
} | ||
} | ||
} |
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.