|
@@ -24,15 +24,68 @@
|
|
|
var surveyFormComponent = new Vue({
|
|
|
el: '#surveyForm',
|
|
|
data: {
|
|
|
- form: <?= json_encode(json_decode($survey->survey_data ?? '{}')) ?>
|
|
|
+ socketSessionID: "{{ time() }}",
|
|
|
+ surveyAccessKey: "{{ @$survey->access_key }}",
|
|
|
+
|
|
|
+ form: <?= json_encode(json_decode($survey->survey_data ?? '{}')) ?>,
|
|
|
+ socketIo: null,
|
|
|
},
|
|
|
methods: {
|
|
|
+ onFormFieldChange: function(){
|
|
|
+ var self = this;
|
|
|
+ self.socketIo = io("http://localhost:3333");
|
|
|
+ self.watchInputFields();
|
|
|
+ },
|
|
|
+ watchInputFields: function(){
|
|
|
+ var self = this;
|
|
|
+ var form = $('#surveyForm');
|
|
|
+ form.find('input, select, textarea').on('change', function(){
|
|
|
+ self.autosaveChanges();
|
|
|
+ });
|
|
|
+ },
|
|
|
+ autosaveChanges: function(){
|
|
|
+ var self = this;
|
|
|
+ var url = "{{ @$survey->access_key ? route('view-survey-form-auto-submit', @$survey->access_key) : '' }}";
|
|
|
+ $.post(url, { dataJson: JSON.stringify(self.form) }, function(response){
|
|
|
+ if(response.success){
|
|
|
+ self.socketIo.emit('SURVEY_DATA_CHANGED_BY_SESSION', {socketSessionID: self.socketSessionID, surveyAccessKey: self.surveyAccessKey});
|
|
|
+ }else{
|
|
|
+ toastr.error(response.message);
|
|
|
+ }
|
|
|
+ }, 'json');
|
|
|
+ },
|
|
|
+ onSurveyDataChange: function(){
|
|
|
+ var self = this;
|
|
|
+ self.socketIo.on('UPDATE_SURVEY_DATA_CHANGED_BY_SESSION', (data) => {
|
|
|
+ if(data.socketSessionID === self.socketSessionID) return;
|
|
|
+ if(data.surveyAccessKey !== self.surveyAccessKey) return;
|
|
|
+ self.updateUi();
|
|
|
+ });
|
|
|
+ },
|
|
|
+ updateUi: function(){
|
|
|
+ var self = this;
|
|
|
+ var url = "{{ @$survey->access_key ? route('view-survey-form-get-data', @$survey->access_key) : '' }}";
|
|
|
+ $.get(url, {}, function(response){
|
|
|
+ if(response.success){
|
|
|
+ const data = JSON.parse(response.data.survey_data);
|
|
|
+ self.form = data;
|
|
|
+ }
|
|
|
+ }, 'json');
|
|
|
+
|
|
|
+ },
|
|
|
init: function(){
|
|
|
- console.log('Default Data: ', this.form);
|
|
|
+ this.onFormFieldChange();
|
|
|
+ this.onSurveyDataChange();
|
|
|
}
|
|
|
},
|
|
|
mounted: function(){
|
|
|
this.init();
|
|
|
+ },
|
|
|
+ updated: function(){
|
|
|
+ var self = this;
|
|
|
+ self.$nextTick(function(){
|
|
|
+ self.watchInputFields();
|
|
|
+ });
|
|
|
}
|
|
|
});
|
|
|
</script>
|