Skip to content

Commit

Permalink
Merge pull request #372 from revelrylabs/channels_list_limit
Browse files Browse the repository at this point in the history
Refactored Disable Modal to Use Plain Text Input
  • Loading branch information
bford002 authored Dec 13, 2023
2 parents fdf7460 + 39a6468 commit 1d49d51
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 54 deletions.
17 changes: 17 additions & 0 deletions lib/slax_web/websocket_listener.ex
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ defmodule SlaxWeb.WebsocketListener do
response = Jason.encode!(%{envelope_id: envelope_id})
:gun.ws_send(pid, stream_ref, {:text, response})

:error ->
response = determine_error_response(payload, envelope_id)

:gun.ws_send(pid, stream_ref, {:text, response})

_ ->
nil
end
Expand Down Expand Up @@ -154,4 +159,16 @@ defmodule SlaxWeb.WebsocketListener do
defp determine_payload(%{"view" => %{"callback_id" => "enable_view"}} = payload) do
Disable.handle_payload(payload)
end

defp determine_error_response(%{"view" => %{"callback_id" => "disable_view"}}, envelope_id) do
Jason.encode!(%{
envelope_id: envelope_id,
payload: %{
response_action: "errors",
errors: %{
channel_input: "This channel does not exist"
}
}
})
end
end
87 changes: 49 additions & 38 deletions lib/slax_web/websockets/disable.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,10 @@ defmodule SlaxWeb.Disable do
"callback_id" => "slax_disable"
} = payload
) do
Task.start_link(fn ->
trigger_id = payload["trigger_id"]

slack_channels =
Enum.map(Slack.get_channels(%{trigger_id: trigger_id}), fn channel ->
%{channel_id: channel["id"], name: channel["name"], disabled: false}
end)

all_channels = Channels.get_all() ++ slack_channels

enabled_channels =
all_channels
|> Enum.uniq_by(& &1.channel_id)
|> Enum.reject(& &1.disabled)
|> Enum.sort_by(& &1.name)

view = build_disable_view(enabled_channels)
Slack.open_modal(%{trigger_id: trigger_id, view: view})
end)
trigger_id = payload["trigger_id"]

:ok
view = build_disable_view()
Slack.open_modal(%{trigger_id: trigger_id, view: view})
end

def handle_payload(
Expand All @@ -56,15 +39,27 @@ defmodule SlaxWeb.Disable do
}
} = payload
) do
trigger_id = payload["trigger_id"]
values = payload["view"]["state"]["values"]
%{channel_id: channel_id, name: name} = parse_state_values(Map.values(values))
%{name: name} = parse_state_values(Map.values(values))

Channels.create_or_update_channel(channel_id, %{
name: name,
disabled: true
})
valid_channel =
Enum.find(Slack.get_channels(%{trigger_id: trigger_id}), &(&1["name"] == name))

:ok
case valid_channel do
nil ->
:error

channel ->
channel_id = Map.get(channel, "id")

Channels.create_or_update_channel(channel_id, %{
name: name,
disabled: true
})

:ok
end
end

def handle_payload(
Expand Down Expand Up @@ -102,37 +97,53 @@ defmodule SlaxWeb.Disable do
}
end

defp build_disable_view(channels) do
defp parse_state_values([
%{
"channel_input" => %{
"value" => name
}
}
]) do
%{
name: name
}
end

defp build_disable_view() do
%{
type: "modal",
callback_id: "disable_view",
title: %{
type: "plain_text",
text: "Disable Slax",
emoji: true
},
submit: %{
type: "plain_text",
text: "Disable"
text: "Disable",
emoji: true
},
title: %{
close: %{
type: "plain_text",
text: "Disable Slax"
text: "Cancel",
emoji: true
},
blocks: [
%{
type: "input",
block_id: "channel_input",
element: %{
type: "static_select",
action_id: "channel_select",
type: "plain_text_input",
placeholder: %{
type: "plain_text",
text: "Select a Channel",
text: "Enter the name of a channel (excluding #)",
emoji: true
},
options:
Enum.map(channels, fn channel ->
%{text: %{type: "plain_text", text: channel.name}, value: channel.channel_id}
end)
action_id: "channel_input"
},
label: %{
type: "plain_text",
text: "Channels",
text: "Channel Name",
emoji: true
}
}
Expand Down
16 changes: 0 additions & 16 deletions test/slax_web/websockets/disable_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,6 @@ defmodule SlaxWeb.Disable.Test do
[channel_id: channel_id, values: values]
end

test "creates or updates a channel to be disabled", %{channel_id: channel_id, values: values} do
event = %{
"trigger_id" => "trigger_id",
"type" => "view_submission",
"view" => %{
"callback_id" => "disable_view",
"state" => %{
"values" => values
}
}
}

assert :ok == Disable.handle_payload(event)
assert Channels.get_by_channel_id(channel_id).disabled == true
end

test "creates or updates a channel to be enabled", %{channel_id: channel_id, values: values} do
event = %{
"trigger_id" => "trigger_id",
Expand Down

0 comments on commit 1d49d51

Please sign in to comment.