Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add discard option #42

Merged
merged 7 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions functions/create_draft/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ export const buildDraftBlocks = (
},
"value": "edit_message_overflow",
},
{
"text": {
"type": "plain_text",
"text": "Discard the draft message",
},
"value": "discard_message_overflow",
},
],
"action_id": "preview_overflow",
},
Expand Down
54 changes: 50 additions & 4 deletions functions/create_draft/interactivity_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import DraftDatastore from "../../datastores/drafts.ts";

export const openDraftEditView: BlockActionHandler<
typeof CreateDraftFunction.definition
> = async ({ body, action, client }) => {
if (action.selected_option.value == "edit_message_overflow") {
> = async ({ body, action, client, inputs }) => {
// If the user selects to edit the draft message
if (action.selected_option.value === "edit_message_overflow") {
const id = action.block_id;

// Get the draft
Expand All @@ -37,10 +38,11 @@ export const openDraftEditView: BlockActionHandler<
},
);

// If the GET datastore record operation fails, log the error and complete the function with an error
if (!putResp.ok) {
const draftGetErrorMsg =
`Error getting draft with id ${id}. Contact the app maintainers with the following - (Error detail: ${putResp.error})`;
console.log(draftGetErrorMsg);
console.error(draftGetErrorMsg);

await client.functions.completeError({
function_execution_id: body.function_data.execution_id,
Expand All @@ -65,14 +67,58 @@ export const openDraftEditView: BlockActionHandler<
if (!viewsOpenResp.ok) {
const draftEditModalErrorMsg =
`Error opening up the draft edit modal view. Contact the app maintainers with the following - (Error detail: ${viewsOpenResp.error}`;
console.log(draftEditModalErrorMsg);
console.error(draftEditModalErrorMsg);

await client.functions.completeError({
function_execution_id: body.function_data.execution_id,
error: draftEditModalErrorMsg,
});
}
}

// If the user selects to discard the draft message
if (action.selected_option.value === "discard_message_overflow") {
const id = action.block_id;
const message_ts = body.message?.ts || "";

try {
// Delete the draft from the 'drafts' Datstore
const deleteResp = await client.apps.datastore.delete({
datastore: DraftDatastore.name,
id: id,
});

// If the DELETE datastore record operation fails, throw an error
if (!deleteResp.ok) {
const deleteDraftErrorMsg =
`Error deleting draft with id ${id}. Contact the app maintainers with the following - (Error detail: ${deleteResp.error})`;
zimeg marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(deleteDraftErrorMsg);
}

// If message_ts isn't empty, Delete the draft message from the Draft Channel
if (message_ts) {
const updateResp = await client.chat.delete({
channel: inputs.channel,
ts: message_ts,
});

// If the DELETE message operation fails, throw an error
if (!updateResp.ok) {
const updateDraftPreviewErrorMsg =
`Error deleting the draft message: ${message_ts} in channel ${inputs.channel}. Contact the app maintainers with the following - (Error detail: ${updateResp.error})`;
throw new Error(updateDraftPreviewErrorMsg);
}
}
// If the DELETE datastore record or Slack Message operations fail, log the error and complete the function with an error
} catch (error) {
console.error(error);

await client.functions.completeError({
function_execution_id: body.function_data.execution_id,
error: "Error discarding draft",
});
}
}
};

export const saveDraftEditSubmission: ViewSubmissionHandler<
Expand Down