Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions spec/advice/add/happyPathSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ describe('Advice QA - Add', () => {
model.locationKey('location')
model.tags('tag-a , tag-b, tag-c ')
model.sortPosition(123)

model.parentScenarioKey('parent-scenario')
model.parentScenarios([{ key: 'parent-scenario', name: 'Parent scenario' }, { key: 'parent-scenario2', name: 'Parent scenario 2' }])
model.save()
})

Expand All @@ -74,7 +75,8 @@ describe('Advice QA - Add', () => {
body: 'body',
locationKey: 'location',
tags: ['tag-a', 'tag-b', 'tag-c'],
sortPosition: 123
sortPosition: 123,
parentScenario: { key: 'parent-scenario', name: 'Parent scenario' }
}
expect(payload).toEqual(expected)
})
Expand Down
3 changes: 2 additions & 1 deletion src/js/api-endpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ module.exports = {
users: p('/v1/users'),
verifiedUsers: p('/v1/verified-users'),
volunteerCategories: p('/v1/volunteer-categories'),
volunteers: p('/v1/volunteer-enquiries')
volunteers: p('/v1/volunteer-enquiries'),
parentScenarios: p('/v1/parent-scenarios')
}
24 changes: 23 additions & 1 deletion src/js/models/advice/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const ajax = require('../../ajax')
const auth = require('../../auth')
const browser = require('../../browser')
const endpoints = require('../../api-endpoints')
const htmlEncode = require('htmlencode')

const BaseViewModel = require('../BaseViewModel')

Expand All @@ -17,6 +18,8 @@ const Model = function () {
self.sortPosition = ko.observable()
self.locationKey = ko.observable()
self.locations = ko.observableArray()
self.parentScenarios= ko.observableArray([])
self.parentScenarioKey = ko.observable()

self.save = function () {
browser.loading()
Expand All @@ -27,7 +30,8 @@ const Model = function () {
? self.tags().split(',').map((t) => t.trim())
: [],
locationKey: self.locationKey(),
sortPosition: self.sortPosition()
sortPosition: self.sortPosition(),
parentScenario: self.parentScenarios().find((ps) => ps.key === self.parentScenarioKey())
}
ajax
.post(endpoints.faqs, payload)
Expand All @@ -45,6 +49,24 @@ const Model = function () {

self.init = function () {
self.locations(auth.getLocationsForUser([{ id: 'general', name: 'General Advice' }]))
self.getParentScenarios()
}

self.getParentScenarios = () => {
ajax
.get(`${endpoints.parentScenarios}`)
.then((result) => {
self.parentScenarios(result.data
.map(p => {
return {
key: p.key,
name: htmlEncode.htmlDecode(p.name)
}
})
)
}, () => {
self.handleServerError()
})
}
}

Expand Down
37 changes: 33 additions & 4 deletions src/js/models/advice/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const ajax = require('../../ajax')
const auth = require('../../auth')
const browser = require('../../browser')
const querystring = require('../../get-url-parameter')
const endpoints = require('../../api-endpoints')

const BaseViewModel = require('../BaseViewModel')

Expand All @@ -18,6 +19,8 @@ const Model = function () {
self.sortPosition = ko.observable()
self.locationKey = ko.observable()
self.locations = ko.observableArray()
self.parentScenarios= ko.observableArray([])
self.parentScenarioKey = ko.observable()

self.save = function () {
browser.loading()
Expand All @@ -28,7 +31,8 @@ const Model = function () {
? self.tags().split(',').map((t) => t.trim())
: [],
locationKey: self.locationKey(),
sortPosition: self.sortPosition()
sortPosition: self.sortPosition(),
parentScenario: self.parentScenarios().find((ps) => ps.key === self.parentScenarioKey())
}
ajax
.put(self.endpointBuilder.faqs(querystring.parameter('id')).build(), payload)
Expand All @@ -46,23 +50,48 @@ const Model = function () {

self.init = function () {
self.locations(auth.getLocationsForUser([{ id: 'general', name: 'General Advice' }]))

browser.loading()
self.getParentScenarios()

// We generate this for retrieving the not cached item
let syntaxSugar = new Date().getTime()

ajax
.get(self.endpointBuilder.faqs(querystring.parameter('id')).build())
.get(self.endpointBuilder.faqs(querystring.parameter('id')).build() + `?unique=${syntaxSugar}`)
.then((result) => {
self.title(htmlencode.htmlDecode(result.data.title))
self.body(htmlencode.htmlDecode(result.data.body))
self.locationKey(result.data.locationKey)
self.tags(result.data.tags.join(', '))
self.sortPosition(result.data.sortPosition)

if (result.data.parentScenario !== null) {
self.parentScenarioKey(result.data.parentScenario.key)
}
else {
self.parentScenarioKey(null)
}
browser.loaded()
}, (err) => {
self.handleError(err)
})
}

self.getParentScenarios = () => {
ajax
.get(`${endpoints.parentScenarios}`)
.then((result) => {
self.parentScenarios(result.data
.map(p => {
return {
key: p.key,
name: htmlencode.htmlDecode(p.name)
}
})
)
}, () => {
self.handleServerError()
})
}
}

Model.prototype = new BaseViewModel()
Expand Down
8 changes: 8 additions & 0 deletions src/pages/advice/add/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ nosubnav: true
optionsText: function (item) { return item.name },
optionsCaption: '-- please select --'" class="form__input"></select>
</dd>
<dt class="key-value-pair__key">Parent Scenario</dt>
<dd class="key-value-pair__value">
<select data-bind="options: parentScenarios,
value: parentScenarioKey,
optionsValue: function (item) { return item.key },
optionsText: function (item) { return item.name },
optionsCaption: '-- please select --'" class="form__input"></select>
</dd>
<dt class="key-value-pair__key">Body</dt>
<dd class="key-value-pair__value">
<textarea data-bind="textInput: body" class="form__input" rows="20"></textarea>
Expand Down
10 changes: 9 additions & 1 deletion src/pages/advice/edit/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ nosubnav: true
<div class="block block--highlight">
<div class="container block__inner">
<header>
<h1>Add Location Advice Q&amp;A Content</h1>
<h1>Edit Location Advice Q&amp;A Content</h1>
<a class="btn btn--info" href="/advice">
<span class="btn__text">Back to Listing</span>
</a>
Expand All @@ -36,6 +36,14 @@ nosubnav: true
optionsText: function (item) { return item.name },
optionsCaption: '-- please select --'" class="form__input"></select>
</dd>
<dt class="key-value-pair__key">Parent Scenario</dt>
<dd class="key-value-pair__value">
<select data-bind="options: parentScenarios,
value: parentScenarioKey,
optionsValue: function (item) { return item.key },
optionsText: function (item) { return item.name },
optionsCaption: '-- please select --'" class="form__input"></select>
</dd>
<dt class="key-value-pair__key">Body</dt>
<dd class="key-value-pair__value">
<textarea data-bind="textInput: body" class="form__input" rows="20"></textarea>
Expand Down