Skip to content

Commit

Permalink
Unique INCS from dropdown menu
Browse files Browse the repository at this point in the history
  • Loading branch information
Sinan-96 committed Sep 19, 2024
1 parent 258f55e commit 511242b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 59 deletions.
11 changes: 6 additions & 5 deletions app/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ client
.then(() => console.log("Connected to the database"))
.catch((err) => console.error("Connection error", err.stack));

//Currently this relies on the text to be unique. This is not optimal but couldnt find another way to do it TODO: could be done better
export function addOrUpdateInc(user_name, text, category) {
export function addOrUpdateInc(user_name, text, category, dropdown_id) {
return client.query(
`INSERT INTO incs (user_name, text, category)
VALUES ($1, $2, $3)`,
[user_name, text, category],
`INSERT INTO incs (user_name, text, category, dropdown_id)
VALUES ($1, $2, $3, $4)
ON CONFLICT (dropdown_id)
DO UPDATE SET user_name = EXCLUDED.user_name, text = EXCLUDED.text, category = EXCLUDED.category`,
[user_name, text, category, dropdown_id],
);
}

Expand Down
56 changes: 2 additions & 54 deletions app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ app.action(/category_select-.*/, async ({ body, ack, say }) => {
console.log("text", text);
// Respond to the user's selection
const selectedCategory = body.actions[0].selected_option.text.text;
const dropdown_id = body.actions[0].action_id;
await say(`You selected: ${selectedCategory}`);
addOrUpdateInc(body.user.username, text, selectedCategory);
addOrUpdateInc(body.user.username, text, selectedCategory, dropdown_id);
});

app.command("/inc_stats", async ({ ack, body, client }) => {
Expand Down Expand Up @@ -157,59 +158,6 @@ app.command("/inc_stats", async ({ ack, body, client }) => {
}
});

app.event("app_home_opened", async ({ event, client }) => {
try {
const dbResponse = await getIncs();
/* view.publish is the method that your app uses to push a view to the Home tab */
await client.views.publish({
/* the user that opened your app's app home */
user_id: event.user,

/* the view object that appears in the app home*/
view: {
type: "home",
callback_id: "home_view",

/* body of the view */
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: "*Welcome to your _App's Home tab_* :tada:",
},
},
{
type: "divider",
},
{
type: "section",
text: {
type: "mrkdwn",
text: dbResponse.rows
.map((row) => `${row.text} (${row.category})`)
.join("\n"),
},
},
{
type: "actions",
elements: [
{
type: "button",
text: {
type: "plain_text",
text: "Click me!",
},
},
],
},
],
},
});
} catch (error) {
console.error(error);
}
});

(async () => {
await app.start(process.env.PORT || 3000);
Expand Down

0 comments on commit 511242b

Please sign in to comment.