Skip to content

Commit

Permalink
feat(procedures): Add counters for completed/total documents in Overview
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricmessiant authored and Cédric Messiant committed Jul 1, 2019
1 parent c8f9a68 commit 4a0bd23
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 deletions.
16 changes: 15 additions & 1 deletion packages/cozy-procedures/src/components/Overview.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,22 @@ class Overview extends React.Component {
}
}

/**
* Total count of documents required by the template
*
* @return {number}
*/
getDocumentsTotal = () => {
const template = creditApplicationTemplate
return Object.values(template.documents).reduce(
(acc, { count }) => acc + count,
0
)
}

render() {
const {
documentsCompleted,
personalDataFieldsCompleted,
personalDataFieldsTotal,
data,
Expand Down Expand Up @@ -183,7 +197,7 @@ class Overview extends React.Component {
<SubTitle className="u-mb-1">{t('overview.documents')}</SubTitle>
<Button
label={t('overview.complete')}
extraRight={'0/0'}
extraRight={`${documentsCompleted}/${this.getDocumentsTotal()}`}
onClick={() => this.navigateTo('documents')}
theme="ghost"
extension="full"
Expand Down
6 changes: 5 additions & 1 deletion packages/cozy-procedures/src/containers/Overview.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import {
} from '../redux/personalDataSlice'
import { getSlice as getAmount } from '../redux/amountSlice'
import { getSlice as getDuration } from '../redux/durationSlice'
import { getFiles } from '../redux/documentsDataSlice'
import {
getCompletedDocumentsCount,
getFiles
} from '../redux/documentsDataSlice'

export const mapStateToProps = state => ({
documentsCompleted: getCompletedDocumentsCount(state),
personalDataFieldsCompleted: getCompletedFields(state),
personalDataFieldsTotal: getTotalFields(state),
data: {
Expand Down
15 changes: 12 additions & 3 deletions packages/cozy-procedures/src/redux/documentsDataSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,18 @@ const documentsSlice = createSlice({
}
}
})

const getData = state => get(state, [documentsSlice.slice, 'data'], {})

const getCompletedCount = state =>
Object.values(getData(state)).reduce(
(acc, { files }) => acc + files.length,
0
)

const selectors = {
getFiles: state => get(state, [documentsSlice.slice, 'data'], {}),
getFiles: getData,
getCompletedDocumentsCount: getCompletedCount,
getInitiated: state =>
get(state, [documentsSlice.slice, 'ui', ['initiated']], {})
}
Expand Down Expand Up @@ -98,7 +108,6 @@ export function fetchDocument(client, documentTemplate) {

const files = await AdministrativeProcedure.getFilesByRules(docWithRules)

//const files = {}
if (files.data) {
dispatch(
fetchDocumentSuccess({
Expand All @@ -125,5 +134,5 @@ export function fetchDocument(client, documentTemplate) {
}
}

export const { getFiles, getInitiated } = selectors
export const { getCompletedDocumentsCount, getFiles, getInitiated } = selectors
export default reducer
40 changes: 40 additions & 0 deletions packages/cozy-procedures/src/redux/documentsDataSlice.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { getCompletedDocumentsCount } from './documentsDataSlice'

describe('documentsDataSlice', () => {
describe('getCompletedDocumentsCount', () => {
it('should return the number of completed documents', () => {
const state = {
documents: {
data: {
address_certificate: {
files: [
{
id: 'baac72edf28acadd',
name: 'facture_edf.pdf',
trashed: false
},
{
id: '94bbda9a86b52c76',
name: 'facture_fibre.pdf',
trashed: false
}
]
},
bank_identity: { files: [] },
identity_document: {
files: [
{
id: '979469aa60434433',
name: 'carte_identite.png',
trashed: false
}
]
}
}
}
}
const result = getCompletedDocumentsCount(state)
expect(result).toEqual(3)
})
})
})

0 comments on commit 4a0bd23

Please sign in to comment.