diff --git a/components/Feature/VulnerabilitiesGrid/ResourceCard/index.js b/components/Feature/VulnerabilitiesGrid/ResourceCard/index.js index 8bef64fc..b4d864e9 100644 --- a/components/Feature/VulnerabilitiesGrid/ResourceCard/index.js +++ b/components/Feature/VulnerabilitiesGrid/ResourceCard/index.js @@ -1,9 +1,12 @@ import css from './index.module.scss'; import SummaryList from 'components/Form/SummaryList'; +import { useState } from 'react'; const HIDDEN_TAGS = ['Delivery', 'Collection', 'Food'] const ResourceCard = ({ + id, + updateSelectedResources, name, description, websites, @@ -19,6 +22,7 @@ const ResourceCard = ({ notes, distance, matches, + customerId, ...others }) => { const trimLength = (s, length) => s.length > length ? s.substring(0, length) + "..." : s @@ -27,7 +31,21 @@ const ResourceCard = ({ const websiteElement = websites && websites.length > 0 && websites.map(website => ({websites[0]})) const distributionElement = tags.filter(t => HIDDEN_TAGS.includes(t)).join(", ") const tagsElement = tags.filter(t => !HIDDEN_TAGS.includes(t)).map(item=> ({trimLength(item, 20)})) - + const snapshot = (customerId != undefined) ? true : false + const updateResource = () =>{ + updateSelectedResources({ + name:name, + description: description, + address:address, + telephone: telephone, + email:email, + referralContact: referralContact, + selfReferral: selfReferral, + openingTimes: openingTimes, + websites:websites, + notes:notes + }) + } return (
@@ -42,11 +60,19 @@ const ResourceCard = ({
- View more information + View more information - + { snapshot && + ( +
+ updateResource()} type="checkbox" value={name}/> + +
) + }
); diff --git a/components/Feature/VulnerabilitiesGrid/index.js b/components/Feature/VulnerabilitiesGrid/index.js index a1c55c79..7c1c0670 100644 --- a/components/Feature/VulnerabilitiesGrid/index.js +++ b/components/Feature/VulnerabilitiesGrid/index.js @@ -32,7 +32,7 @@ function createLookup() { return lookup; } -const VulnerabilitiesGrid = ({ resources, onUpdate, residentCoordinates, genericPostcode, onError }) => { +const VulnerabilitiesGrid = ({ resources, onUpdate, residentCoordinates, genericPostcode, onError , updateSelectedResources, customerId}) => { const [grid, setGrid] = useState({ assets: {}, @@ -43,6 +43,9 @@ const VulnerabilitiesGrid = ({ resources, onUpdate, residentCoordinates, generic const [expandedGroups, setExpandedGroups] = useState({}); const [residentData, setResidentData] = useState(null); + const updateSummaryResource = resourceName => { + updateSelectedResources(resourceName) + } const updateResidentData = (result) => { if(result){ setResidentData(result); @@ -319,6 +322,8 @@ const VulnerabilitiesGrid = ({ resources, onUpdate, residentCoordinates, generic key={resource.id} data-testid={`resource-${resource.id}`} {...resource} + updateSelectedResources = {updateSummaryResource} + customerId={customerId} /> ); })} diff --git a/cypress/integration/features/editSnapshot.spec.js b/cypress/integration/features/editSnapshot.spec.js index 4ee9f5c1..158d4da7 100644 --- a/cypress/integration/features/editSnapshot.spec.js +++ b/cypress/integration/features/editSnapshot.spec.js @@ -20,6 +20,39 @@ context('Edit snapshot', () => { cy.task('deleteSnapshot', '1'); }); + + it("Adds resources to the summary list", () => { + cy.visit(`/snapshots/1`); + cy.get('[data-testid=accordion-item]').eq(0).click(); + cy.get('[data-testid=food-needs-v-halal-checkbox]').click(); + + cy.get('[data-testid=resource-rec2FkHGEn9BiiXvW] > h3').eq(0) + .should('contain', 'Made Up Kitchen') + cy.get('#summary-recisR36NAVBna3N4').click() + cy.get('#input-recisR36NAVBna3N4').check() + cy.get('[data-testid=finish-and-save-button]').click(); + + cy.get('[data-testid=resources-summary]') + .should('contain', 'Resources') + .and('contain', 'Shirdi Sai Baba Temple'); + }) + + it("Adds and removes resources to the summary list", () => { + cy.visit(`/snapshots/1`); + cy.get('[data-testid=accordion-item]').eq(0).click(); + cy.get('[data-testid=food-needs-v-halal-checkbox]').click(); + + cy.get('[data-testid=resource-rec2FkHGEn9BiiXvW] > h3').eq(0) + .should('contain', 'Made Up Kitchen') + cy.get('#summary-recisR36NAVBna3N4').click() + cy.get('#input-recisR36NAVBna3N4').check() + cy.get('[data-testid=finish-and-save-button]').click(); + + cy.get('[data-testid=resources-summary]') + .should('contain', 'Resources') + .and('contain', 'Shirdi Sai Baba Temple'); + }) + describe('Edit snapshot', () => { it('Displays editable snapshot if there are no assets, vulnerabilites and notes added', () => { cy.visit(`/snapshots/1`); diff --git a/package-lock.json b/package-lock.json index fdcc8879..9a9da646 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4427,6 +4427,12 @@ "resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-1.0.0.tgz", "integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU=" }, + "assertion-error": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz", + "integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==", + "dev": true + }, "assign-symbols": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/assign-symbols/-/assign-symbols-1.0.0.tgz", @@ -5696,6 +5702,20 @@ "url-to-options": "^1.0.1" } }, + "chai": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/chai/-/chai-4.2.0.tgz", + "integrity": "sha512-XQU3bhBukrOsQCuwZndwGcCVQHyZi53fQ6Ys1Fym7E4olpIqqZZhhoFJoaKVvV17lWQoXYwgWN2nF5crA8J2jw==", + "dev": true, + "requires": { + "assertion-error": "^1.1.0", + "check-error": "^1.0.2", + "deep-eql": "^3.0.1", + "get-func-name": "^2.0.0", + "pathval": "^1.1.0", + "type-detect": "^4.0.5" + } + }, "chalk": { "version": "2.4.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", @@ -5722,6 +5742,12 @@ "integrity": "sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==", "dev": true }, + "check-error": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/check-error/-/check-error-1.0.2.tgz", + "integrity": "sha1-V00xLt2Iu13YkS6Sht1sCu1KrII=", + "dev": true + }, "check-more-types": { "version": "2.24.0", "resolved": "https://registry.npmjs.org/check-more-types/-/check-more-types-2.24.0.tgz", @@ -6879,67 +6905,6 @@ "integrity": "sha512-hX48+r5n7Ns7CHkn601Ag0JiCG1vby5+g7QhlP8X+mkiVYpTLpXAPiiaKFj9QTTCdZSI5+0UqwIxA+ShTsr5tA==", "dev": true }, - "cypress-plugin-retries": { - "version": "1.5.2", - "resolved": "https://registry.npmjs.org/cypress-plugin-retries/-/cypress-plugin-retries-1.5.2.tgz", - "integrity": "sha512-o1xVIGtv4WvNVxoVJ2X08eAuvditPHrePRzHqhwwHbMKu3C2rtxCdanRCZdO5fjh8ww+q4v4V0e9GmysbOvu3A==", - "dev": true, - "requires": { - "chalk": "^3.0.0" - }, - "dependencies": { - "ansi-styles": { - "version": "4.2.1", - "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.2.1.tgz", - "integrity": "sha512-9VGjrMsG1vePxcSweQsN20KY/c4zN0h9fLjqAbwbPfahM3t+NL+M9HC8xeXG2I8pX5NoamTGNuomEUFI7fcUjA==", - "dev": true, - "requires": { - "@types/color-name": "^1.1.1", - "color-convert": "^2.0.1" - } - }, - "chalk": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/chalk/-/chalk-3.0.0.tgz", - "integrity": "sha512-4D3B6Wf41KOYRFdszmDqMCGq5VV/uMAB273JILmO+3jAlh8X4qDtdtgCR3fxtbLEMzSx22QdhnDcJvu2u1fVwg==", - "dev": true, - "requires": { - "ansi-styles": "^4.1.0", - "supports-color": "^7.1.0" - } - }, - "color-convert": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", - "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", - "dev": true, - "requires": { - "color-name": "~1.1.4" - } - }, - "color-name": { - "version": "1.1.4", - "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", - "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", - "dev": true - }, - "has-flag": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", - "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", - "dev": true - }, - "supports-color": { - "version": "7.1.0", - "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.1.0.tgz", - "integrity": "sha512-oRSIpR8pxT1Wr2FquTNnGet79b3BWljqOuoW/h4oBhxJ/HUbX5nX6JSruTkvXDCFMwDPvsaTTbvMLKZWSy0R5g==", - "dev": true, - "requires": { - "has-flag": "^4.0.0" - } - } - } - }, "d": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/d/-/d-1.0.1.tgz", @@ -7163,6 +7128,15 @@ } } }, + "deep-eql": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-3.0.1.tgz", + "integrity": "sha512-+QeIQyN5ZuO+3Uk5DYh6/1eKO0m0YmJFGNmFHGACpf1ClL1nmlV/p4gNgbl2pJGxgXb4faqo6UE+M5ACEMyVcw==", + "dev": true, + "requires": { + "type-detect": "^4.0.0" + } + }, "deep-extend": { "version": "0.6.0", "resolved": "https://registry.npmjs.org/deep-extend/-/deep-extend-0.6.0.tgz", @@ -9329,6 +9303,12 @@ "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==", "dev": true }, + "get-func-name": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/get-func-name/-/get-func-name-2.0.0.tgz", + "integrity": "sha1-6td0q+5y4gQJQzoGY2YCPdaIekE=", + "dev": true + }, "get-package-type": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/get-package-type/-/get-package-type-0.1.0.tgz", @@ -14099,6 +14079,12 @@ "integrity": "sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==", "dev": true }, + "pathval": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/pathval/-/pathval-1.1.0.tgz", + "integrity": "sha1-uULm1L3mUwBe9rcTYd74cn0GReA=", + "dev": true + }, "pause-stream": { "version": "0.0.11", "resolved": "https://registry.npmjs.org/pause-stream/-/pause-stream-0.0.11.tgz", diff --git a/package.json b/package.json index 4c3fbd02..5ab0ba6b 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "babel-core": "^6.26.3", "babel-eslint": "^10.1.0", "babel-jest": "^25.5.1", + "chai": "^4.2.0", "cypress": "^4.5.0", "cypress-axe": "^0.8.1", "dynamodb-admin": "^4.0.0", diff --git a/pages/snapshots/[id].js b/pages/snapshots/[id].js index 202391ae..31a6ea90 100644 --- a/pages/snapshots/[id].js +++ b/pages/snapshots/[id].js @@ -1,4 +1,4 @@ -import { useState } from 'react'; +import { useState, useEffect } from 'react'; import useSnapshot from 'lib/api/utils/useSnapshot'; import { requestSnapshot, requestResources } from 'lib/api'; import HttpStatusError from 'lib/api/domain/HttpStatusError'; @@ -23,8 +23,8 @@ const SnapshotSummary = ({ resources, initialSnapshot, token }) => { const [editSnapshot, setEditSnapshot] = useState( snapshot.assets.length === 0 && - snapshot.vulnerabilities.length === 0 && - !snapshot.notes + snapshot.vulnerabilities.length === 0 && + !snapshot.notes ); const [hasValue, setHasValue] = useState(false); @@ -33,23 +33,58 @@ const SnapshotSummary = ({ resources, initialSnapshot, token }) => { snapshot.vulnerabilities = selected.vulnerabilities; setHasValue( snapshot.assets.length > 0 || - snapshot.vulnerabilities.length > 0 || - snapshot.notes + snapshot.vulnerabilities.length > 0 || + snapshot.notes ); }; + const [selectedResources, setResources] = useState([]); + + const updateSummaryResource = updatedResource => { + let updatedResources = selectedResources + if(updatedResources.some(resource => resource.name === updatedResource.name)){ + let resourcesRemoved = [] + updatedResources.forEach((resource) =>{ + if(resource.name != updatedResource.name){ + resourcesRemoved.push(resource) + } + }) + updatedResources = resourcesRemoved + }else{ + Object.keys(updatedResource).forEach(key => { + if (updatedResource[key] === undefined || updatedResource[key] === '') { + delete updatedResource[key]; + } + }) + let summary = [] + Object.keys(updatedResource).forEach(key => { + summary.push(updatedResource[key]) + }) + updatedResource.summary = summary.join(', ') + updatedResources.push(updatedResource) + } + setResources(updatedResources) + setHasValue(updatedResources.length > 0); + } + const handleError = errorMsg => console.log(errorMsg); - const handleError = (errorMsg) => console.log(errorMsg) - const updateNotes = notes => { snapshot.notes = notes; setHasValue( snapshot.assets.length > 0 || - snapshot.vulnerabilities.length > 0 || - snapshot.notes + snapshot.vulnerabilities.length > 0 || + snapshot.notes ); }; - const { dob, firstName, lastName, postcode, assets, vulnerabilities, notes } = snapshot; + const { + dob, + firstName, + lastName, + postcode, + assets, + vulnerabilities, + notes + } = snapshot; let customerId = snapshot.systemIds?.[0]; const residentCoordinates = geoCoordinates(postcode); const INH_URL = process.env.INH_URL @@ -87,6 +122,8 @@ const SnapshotSummary = ({ resources, initialSnapshot, token }) => { onUpdate={updateSelected} resources={resources} residentCoordinates={residentCoordinates} + updateSelectedResources={updateSummaryResource} + customerId={customerId} />