Skip to content

Commit

Permalink
use hash for field options
Browse files Browse the repository at this point in the history
  • Loading branch information
omohokcoj committed Nov 12, 2023
1 parent a250225 commit 1066131
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 28 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def template_params
schema: [%i[attachment_uuid name]],
submitters: [%i[name uuid]],
fields: [[:uuid, :submitter_uuid, :name, :type, :required, :readonly, :default_value,
{ options: [], areas: [%i[x y w h cell_w attachment_uuid page]] }]]
{ options: [%i[value uuid]], areas: [%i[x y w h cell_w attachment_uuid option_uuid page]] }]]
)
end
end
Expand Down
22 changes: 11 additions & 11 deletions app/javascript/submission_form/form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@
{{ t('select_your_option') }}
</option>
<option
v-for="(option, index) in currentField.options"
:key="index"
:selected="values[currentField.uuid] == option"
:value="option"
v-for="option in currentField.options"
:key="option.uuid"
:selected="values[currentField.uuid] == option.value"
:value="option.value"
>
{{ option }}
{{ option.value }}
</option>
</select>
</div>
Expand All @@ -119,24 +119,24 @@
<div class="flex w-full">
<div class="space-y-3.5 mx-auto">
<div
v-for="(option, index) in currentField.options"
:key="index"
v-for="option in currentField.options"
:key="option.uuid"
>
<label
:for="currentField.uuid + option"
:for="option.uuid"
class="flex items-center space-x-3"
>
<input
:id="currentField.uuid + option"
:id="option.uuid"
v-model="values[currentField.uuid]"
type="radio"
class="base-radio !h-7 !w-7"
:name="`values[${currentField.uuid}]`"
:value="option"
:value="option.value"
:required="currentField.required"
>
<span class="text-xl">
{{ option }}
{{ option.value }}
</span>
</label>
</div>
Expand Down
14 changes: 7 additions & 7 deletions app/javascript/submission_form/multi_select_step.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@
<div class="flex w-full">
<div class="space-y-3.5 mx-auto">
<div
v-for="(option, index) in field.options"
:key="index"
v-for="option in field.options"
:key="option.uuid"
>
<label
:for="field.uuid + option"
:for="option.uuid"
class="flex items-center space-x-3"
>
<input
:id="field.uuid + option"
:id="option.uuid"
:ref="setInputRef"
type="checkbox"
:name="`values[${field.uuid}][]`"
:value="option"
:value="option.value"
class="base-checkbox !h-7 !w-7"
:checked="(modelValue || []).includes(option)"
:checked="(modelValue || []).includes(option.value)"
@change="onChange"
>
<span class="text-xl">
{{ option }}
{{ option.value }}
</span>
</label>
</div>
Expand Down
3 changes: 2 additions & 1 deletion app/javascript/template_builder/area.vue
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ import FieldSubmitter from './field_submitter'
import FieldType from './field_type'
import Field from './field'
import { IconX } from '@tabler/icons-vue'
import { v4 } from 'uuid'
export default {
name: 'FieldArea',
Expand Down Expand Up @@ -303,7 +304,7 @@ export default {
}
if (['select', 'multiple', 'radio'].includes(this.field.type)) {
this.field.options ||= ['']
this.field.options ||= [{ value: '', uuid: v4() }]
}
(this.field.areas || []).forEach((area) => {
Expand Down
4 changes: 2 additions & 2 deletions app/javascript/template_builder/builder.vue
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ export default {
}
if (['select', 'multiple', 'radio'].includes(type)) {
field.options = ['']
field.options = [{ value: '', uuid: v4() }]
}
this.drawField = field
Expand Down Expand Up @@ -592,7 +592,7 @@ export default {
}
if (['select', 'multiple', 'radio'].includes(field.type)) {
field.options = ['']
field.options = [{ value: '', uuid: v4() }]
}
const fieldArea = {
Expand Down
16 changes: 12 additions & 4 deletions app/javascript/template_builder/field.vue
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,19 @@
<div
v-if="field.options"
class="border-t border-base-300 mx-2 pt-2 space-y-1.5"
draggable="true"
@dragstart.prevent.stop
>
<div
v-for="(option, index) in field.options"
:key="index"
:key="option.uuid"
class="flex space-x-1.5 items-center"
>
<span class="text-sm w-3.5">
{{ index + 1 }}.
</span>
<input
v-model="field.options[index]"
v-model="option.value"
class="w-full input input-primary input-xs text-sm bg-transparent"
type="text"
required
Expand All @@ -220,7 +222,7 @@
<button
v-if="field.options"
class="text-center text-sm w-full pb-1"
@click="[field.options.push(''), save()]"
@click="addOption"
>
+ Add option
</button>
Expand All @@ -233,6 +235,7 @@
import Contenteditable from './contenteditable'
import FieldType from './field_type'
import { IconShape, IconNewSection, IconTrashX, IconCopy, IconSettings } from '@tabler/icons-vue'
import { v4 } from 'uuid'
export default {
name: 'TemplateField',
Expand Down Expand Up @@ -309,6 +312,11 @@ export default {
closeDropdown () {
document.activeElement.blur()
},
addOption () {
this.field.options.push({ value: '', uuid: v4() })
this.save()
},
maybeUpdateOptions () {
delete this.field.default_value
Expand All @@ -317,7 +325,7 @@ export default {
}
if (['radio', 'multiple', 'select'].includes(this.field.type)) {
this.field.options ||= ['']
this.field.options ||= [{ value: '', uuid: v4() }]
}
(this.field.areas || []).forEach((area) => {
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/template_builder/fields.vue
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ export default {
}
if (['select', 'multiple', 'radio'].includes(type)) {
field.options = ['']
field.options = [{ value: '', uuid: v4() }]
}
this.fields.push(field)
Expand Down
51 changes: 51 additions & 0 deletions db/migrate/20231112224432_update_field_options.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

class UpdateFieldOptions < ActiveRecord::Migration[7.0]
class MigrationTemplate < ApplicationRecord
self.table_name = 'templates'
end

class MigrationSubmission < ApplicationRecord
self.table_name = 'submissions'
end

# rubocop:disable Metrics
def up
MigrationTemplate.find_each do |template|
next if template.fields.blank?

template_fields = JSON.parse(template.fields)

new_fields = template_fields.deep_dup

new_fields.each do |field|
if field['options'].present? && !field['options'].first.is_a?(Hash)
field['options'] = field['options'].map { |o| { value: o || '', uuid: SecureRandom.uuid } }
end
end

template.update_columns(fields: new_fields.to_json) if template_fields != new_fields
end

MigrationSubmission.find_each do |submission|
next if submission.template_fields.blank?

template_fields = JSON.parse(submission.template_fields)

new_fields = template_fields.deep_dup

new_fields.each do |field|
if field['options'].present? && !field['options'].first.is_a?(Hash)
field['options'] = field['options'].map { |o| { value: o || '', uuid: SecureRandom.uuid } }
end
end

submission.update_columns(template_fields: new_fields.to_json) if template_fields != new_fields
end
end
# rubocop:enable Metrics

def down
nil
end
end
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2023_11_02_171817) do
ActiveRecord::Schema[7.0].define(version: 2023_11_12_224432) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down

0 comments on commit 1066131

Please sign in to comment.