-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from DEFRA/581
[581] generic summary controller
- Loading branch information
Showing
15 changed files
with
137 additions
and
109 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
src/server/common/controller/summary-page-controller/SummaryPageController.js
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 SummaryPage from '../../model/page/summary-page/SummaryPageModel.js' */ | ||
/** @import { SectionModel } from "~/src/server/common/model/section/section-model/index.js" */ | ||
/** @import { Server, ServerRegisterPluginObject } from '@hapi/hapi' */ | ||
|
||
export class SummaryPageController { | ||
/** @type {string} */ | ||
indexView = 'common/controller/summary-page-controller/index.njk' | ||
|
||
/** @type {string} */ | ||
pageTitle | ||
|
||
/** @type {string} */ | ||
heading | ||
|
||
/** | ||
* @param {SummaryPage} page | ||
*/ | ||
constructor(page) { | ||
this.page = page | ||
} | ||
|
||
get urlPath() { | ||
return `/${this.page.sectionKey}/check-answers` | ||
} | ||
|
||
/** @type {SectionModel} */ | ||
Model | ||
|
||
/** @returns {ServerRegisterPluginObject<void>} */ | ||
plugin() { | ||
return { | ||
plugin: { | ||
name: `${this.page.sectionKey}-check-answers`, | ||
|
||
/** @param {Server} server */ | ||
register: (server) => { | ||
server.route([ | ||
{ | ||
method: 'GET', | ||
path: this.urlPath, | ||
handler: this.getHandler.bind(this) | ||
}, | ||
{ | ||
method: 'POST', | ||
path: this.urlPath, | ||
handler: this.postHandler.bind(this) | ||
} | ||
]) | ||
} | ||
} | ||
} | ||
} | ||
|
||
getHandler(req, res) { | ||
const section = this.page.sectionFactory(req.yar.get('origin')) | ||
|
||
const { isValid, firstInvalidPage } = section.validate() | ||
if (!isValid) { | ||
return res.redirect( | ||
`${firstInvalidPage?.urlPath}?redirect_uri=/${this.page.sectionKey}/check-answers` | ||
) | ||
} | ||
|
||
const items = section.pages.map((visitedPage) => ({ | ||
key: visitedPage.question, | ||
value: section[visitedPage.questionKey].html, | ||
url: `${visitedPage.urlPath}?redirect_uri=/${this.page.sectionKey}/check-answers`, | ||
visuallyHiddenKey: visitedPage.question, | ||
attributes: { | ||
'data-testid': `${visitedPage.questionKey}-change-link` | ||
} | ||
})) | ||
|
||
return res.view(this.indexView, { | ||
pageTitle: this.page.pageTitle, | ||
heading: this.page.heading, | ||
originSummary: items | ||
}) | ||
} | ||
|
||
postHandler(_req, res) { | ||
return res.redirect('/task-list') | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
export class Page { | ||
/** @type {string} */ | ||
urlPath | ||
|
||
/** @type {string} */ | ||
sectionKey | ||
} |
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
16 changes: 16 additions & 0 deletions
16
src/server/common/model/page/summary-page/SummaryPageModel.js
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,16 @@ | ||
import { Page } from '../page-model.js' | ||
|
||
/** @import { SectionModel } from '~/src/server/common/model/section/section-model/index.js' */ | ||
|
||
class SummaryPage extends Page { | ||
/** @type {(_data) => SectionModel} */ | ||
sectionFactory | ||
|
||
/** @type {string} */ | ||
heading | ||
|
||
/** @type {string} */ | ||
pageTitle | ||
} | ||
|
||
export default SummaryPage |
Empty file.
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 |
---|---|---|
@@ -1,3 +1 @@ | ||
import { SectionModelUpdated } from './section-model-updated.js' | ||
|
||
export const SectionModel = SectionModelUpdated | ||
export { SectionModel } from './section-model-updated.js' |
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 was deleted.
Oops, something went wrong.
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.