Skip to content

Commit

Permalink
Forms (#17)
Browse files Browse the repository at this point in the history
* dispatch completion event when attempting to mount a form that was completed previously

* remove the associated element

* update stored data in localStorage

* update readme for forms payload

* 1.8.2

* update tests
  • Loading branch information
rockwellll authored Oct 28, 2024
1 parent fe3dbea commit 99ad68a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 18 deletions.
2 changes: 1 addition & 1 deletion __tests__/models/form_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('markAsCompleted', () => {
const emit = jest.spyOn(Hellotext.eventEmitter, 'dispatch')

form.markAsCompleted()
expect(emit).toHaveBeenCalledWith('form:completed', { id: 1 })
expect(emit).toHaveBeenCalled()
})
})

Expand Down
2 changes: 1 addition & 1 deletion dist/hellotext.js

Large diffs are not rendered by default.

14 changes: 9 additions & 5 deletions docs/forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,16 @@ Hellotext.on('form:completed', (form) => {
})

{
state: 'completed',
completedAt: 1730114734999, // Timestamp when the form was completed
id: "xxxxx", // Id of the form that has been completed
first_name: "Billy",
last_name: "Butcher",
email: "[email protected]",
phone: "+1234567890",
property_by_id[xxxxx]: "value"
data: {
first_name: "Billy",
last_name: "Butcher",
email: "[email protected]",
phone: "+1234567890",
property_by_id[xxxxx]: "value"
}
}
```

Expand Down
19 changes: 12 additions & 7 deletions lib/models/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ var Form = /*#__PURE__*/function () {
ifCompleted = true
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
if (ifCompleted && this.hasBeenCompleted) {
return;
var _this$element;
(_this$element = this.element) === null || _this$element === void 0 ? void 0 : _this$element.remove();
return _hellotext.default.eventEmitter.dispatch('form:completed', _objectSpread({
id: this.id
}, JSON.parse(localStorage.getItem("hello-form-".concat(this.id)))));
}
var firstStep = this.data.steps[0];
this.buildHeader(firstStep.header);
Expand Down Expand Up @@ -112,13 +116,14 @@ var Form = /*#__PURE__*/function () {
}, {
key: "markAsCompleted",
value: function markAsCompleted(data) {
localStorage.setItem("hello-form-".concat(this.id), JSON.stringify({
var payload = {
state: 'completed',
data
}));
_hellotext.default.eventEmitter.dispatch('form:completed', _objectSpread({
id: this.id
}, data));
id: this.id,
data,
completedAt: new Date().getTime()
};
localStorage.setItem("hello-form-".concat(this.id), JSON.stringify(payload));
_hellotext.default.eventEmitter.dispatch('form:completed', payload);
}
}, {
key: "hasBeenCompleted",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hellotext/hellotext",
"version": "1.8.1",
"version": "1.8.2",
"description": "Hellotext JavaScript Client",
"source": "src/index.js",
"main": "lib/index.js",
Expand Down
17 changes: 14 additions & 3 deletions src/models/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ class Form {

async mount({ ifCompleted = true } = {}) {
if(ifCompleted && this.hasBeenCompleted) {
return
this.element?.remove()

return Hellotext.eventEmitter.dispatch('form:completed', {
id: this.id,
...JSON.parse(localStorage.getItem(`hello-form-${this.id}`)),
})
}

const firstStep = this.data.steps[0]
Expand Down Expand Up @@ -91,9 +96,15 @@ class Form {
}

markAsCompleted(data) {
localStorage.setItem(`hello-form-${this.id}`, JSON.stringify({ state: 'completed', data }))
const payload = {
state: 'completed',
id: this.id,
data,
completedAt: new Date().getTime(),
}

Hellotext.eventEmitter.dispatch('form:completed', { id: this.id, ...data })
localStorage.setItem(`hello-form-${this.id}`, JSON.stringify(payload))
Hellotext.eventEmitter.dispatch('form:completed', payload)
}

get hasBeenCompleted() {
Expand Down

0 comments on commit 99ad68a

Please sign in to comment.