diff --git a/src/grom/component/checkbox.gleam b/src/grom/component/checkbox.gleam new file mode 100644 index 0000000..079ef58 --- /dev/null +++ b/src/grom/component/checkbox.gleam @@ -0,0 +1,34 @@ +import gleam/json.{type Json} +import gleam/list +import gleam/option.{type Option, None, Some} + +pub type Checkbox { + Checkbox( + id: Option(Int), + custom_id: String, + /// Defaults to False. + is_default_selected: Bool, + ) +} + +@internal +pub fn to_json(checkbox: Checkbox) -> Json { + let id = case checkbox.id { + Some(id) -> [#("id", json.int(id))] + None -> [] + } + + let custom_id = [#("custom_id", json.string(checkbox.custom_id))] + + let is_default_selected = [ + #("default", json.bool(checkbox.is_default_selected)), + ] + + [id, custom_id, is_default_selected] + |> list.flatten + |> json.object +} + +pub fn new(custom_id custom_id: String) -> Checkbox { + Checkbox(None, custom_id, False) +} diff --git a/src/grom/component/label.gleam b/src/grom/component/label.gleam index 727069a..62e313f 100644 --- a/src/grom/component/label.gleam +++ b/src/grom/component/label.gleam @@ -1,8 +1,8 @@ -import gleam/dynamic/decode import gleam/json.{type Json} import gleam/list import gleam/option.{type Option, None, Some} import grom/component/channel_select.{type ChannelSelect} +import grom/component/checkbox.{type Checkbox} import grom/component/checkbox_group.{type CheckboxGroup} import grom/component/file_upload.{type FileUpload} import grom/component/mentionable_select.{type MentionableSelect} @@ -33,36 +33,7 @@ pub type Component { FileUpload(FileUpload) RadioGroup(RadioGroup) CheckboxGroup(CheckboxGroup) -} - -// DECODERS -------------------------------------------------------------------- - -@internal -pub fn decoder() -> decode.Decoder(Label) { - use id <- decode.optional_field("id", None, decode.optional(decode.int)) - use label <- decode.field("label", decode.string) - use description <- decode.optional_field( - "description", - None, - decode.optional(decode.string), - ) - use component <- decode.field("component", component_decoder()) - decode.success(Label(id:, label:, description:, component:)) -} - -@internal -pub fn component_decoder() -> decode.Decoder(Component) { - use type_ <- decode.field("type", decode.int) - case type_ { - 3 -> decode.map(string_select.decoder(), StringSelect) - 4 -> decode.map(text_input.decoder(), TextInput) - 5 -> decode.map(user_select.decoder(), UserSelect) - 6 -> decode.map(role_select.decoder(), RoleSelect) - 7 -> decode.map(mentionable_select.decoder(), MentionableSelect) - 8 -> decode.map(channel_select.decoder(), ChannelSelect) - 19 -> decode.map(file_upload.decoder(), FileUpload) - _ -> decode.failure(FileUpload(file_upload.new("")), "Component") - } + Checkbox(Checkbox) } // ENCODERS -------------------------------------------------------------------- @@ -101,6 +72,7 @@ pub fn component_to_json(component: Component) -> Json { FileUpload(file_upload) -> file_upload.to_json(file_upload) RadioGroup(radio_group) -> radio_group.to_json(radio_group) CheckboxGroup(checkbox_group) -> checkbox_group.to_json(checkbox_group) + Checkbox(checkbox) -> checkbox.to_json(checkbox) } } diff --git a/src/grom/interaction.gleam b/src/grom/interaction.gleam index 96f5b3f..fc98c7b 100644 --- a/src/grom/interaction.gleam +++ b/src/grom/interaction.gleam @@ -119,6 +119,11 @@ pub type SubmittedLabelComponent { LabelFileUploadSubmitted(FileUploadSubmission) LabelRadioGroupSubmitted(RadioGroupSubmission) LabelCheckboxGroupSubmitted(CheckboxGroupSubmission) + LabelCheckboxSubmitted(CheckboxSubmission) +} + +pub type CheckboxSubmission { + CheckboxSubmission(id: Int, custom_id: String, is_selected: Bool) } pub type CheckboxGroupSubmission { @@ -648,6 +653,14 @@ pub fn label_submission_decoder() -> decode.Decoder(LabelSubmission) { decode.success(LabelSubmission(id:, component:)) } +fn checkbox_submission_decoder() -> decode.Decoder(CheckboxSubmission) { + use id <- decode.field("id", decode.int) + use custom_id <- decode.field("custom_id", decode.string) + use is_selected <- decode.field("value", decode.bool) + + decode.success(CheckboxSubmission(id, custom_id, is_selected)) +} + @internal pub fn submitted_label_component_decoder() -> decode.Decoder( SubmittedLabelComponent, @@ -676,6 +689,7 @@ pub fn submitted_label_component_decoder() -> decode.Decoder( checkbox_group_submission_decoder(), LabelCheckboxGroupSubmitted, ) + 23 -> decode.map(checkbox_submission_decoder(), LabelCheckboxSubmitted) _ -> decode.failure( LabelStringSelectSubmitted(StringSelectExecution("", [], None)),