-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #292 from Decatur-Robotics/fix-slack-notifications
Fix Slack notifications
- Loading branch information
Showing
20 changed files
with
257 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,58 @@ | ||
import { useRouter } from "next/router"; | ||
import { useState } from "react"; | ||
import Modal, { hideModal, showModal } from "./Modal"; | ||
import ClientApi from "@/lib/api/ClientApi"; | ||
|
||
export default function AddToSlack() { | ||
const host = window ? window.location.host : "4026.org"; | ||
const SLACK_WEBHOOK_INSTALL_URL = "https://my.slack.com/services/new/incoming-webhook/"; | ||
|
||
const api = new ClientApi(); | ||
|
||
function AddToSlackModal({ teamId }: { teamId: string }) { | ||
const [webhookUrl, setWebhookUrl] = useState<string>(); | ||
const [errorMsg, setErrorMsg] = useState<string>(""); | ||
|
||
function save() { | ||
if (!webhookUrl) { | ||
setErrorMsg("Webhook URL is required"); | ||
return; | ||
} | ||
|
||
api.setSlackWebhook(teamId, webhookUrl) | ||
.catch((err) => setErrorMsg(err.message)) | ||
.then(() => hideModal("add-to-slack")); | ||
} | ||
|
||
return ( | ||
<a className="link text-accent" href={` | ||
https://slack.com/oauth/v2/authorize | ||
?client_id=${process.env.NEXT_PUBLIC_SLACK_CLIENT_ID} | ||
&scope=chat:write,commands&user_scope=email,openid | ||
&redirect_uri=https://${host}/api/addSlackBot | ||
`}> | ||
Add Gearbox to Slack | ||
</a> | ||
<Modal id="add-to-slack" showCloseButton={false} className="flex flex-col gap-4"> | ||
<h2 className="text-xl">Add Gearbox to Slack</h2> | ||
<div> | ||
Install the Incoming Webhooks app here:{" "} | ||
<a href={SLACK_WEBHOOK_INSTALL_URL} target="_blank" className="link link-accent">{SLACK_WEBHOOK_INSTALL_URL}</a> | ||
</div> | ||
<div> | ||
Enter the Webhook URL here: | ||
<input | ||
value={webhookUrl} | ||
onChange={(e) => setWebhookUrl(e.target.value)} | ||
type="url" | ||
placeholder="Webhook URL" | ||
className="input input-bordered ml-2" | ||
/> | ||
</div> | ||
<div className="flex gap-4 w-full justify-end"> | ||
<button className="btn btn-primary" onClick={save}>Save</button> | ||
<button className="btn" onClick={() => hideModal("add-to-slack")}>Cancel</button> | ||
</div> | ||
{ errorMsg && <div className="text-red-500">{errorMsg}</div> } | ||
</Modal> | ||
); | ||
} | ||
|
||
export default function AddToSlack({ edit, teamId }: { edit: boolean, teamId: string }) { | ||
return (<> | ||
<button className="btn btn-accent btn-sm" onClick={() => showModal("add-to-slack")}> | ||
{ edit ? "Change Slack Webhook" : "Add Gearbox to Slack" } | ||
</button> | ||
<AddToSlackModal teamId={teamId} /> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React from "react"; | ||
|
||
export default function Modal({ id, children, className, showCloseButton }: { | ||
id: string, | ||
children: React.ReactNode, | ||
className?: string, | ||
showCloseButton?: boolean | ||
}) { | ||
return <dialog id={id} className="modal w-screen h-screen"> | ||
<div className={`modal-box ${className}`}> | ||
{children} | ||
{ (showCloseButton === undefined || showCloseButton) && | ||
<div className="modal-action"> | ||
<form method="dialog"> | ||
{/* if there is a button in form, it will close the modal */} | ||
<button className="btn">Close</button> | ||
</form> | ||
</div> | ||
} | ||
</div> | ||
<form method="dialog" className="modal-backdrop"> | ||
<button>Close</button> | ||
</form> | ||
</dialog> | ||
} | ||
|
||
export function showModal(id: string) { | ||
(document.getElementById(id) as HTMLDialogElement | undefined)?.showModal(); | ||
} | ||
|
||
export function hideModal(id: string) { | ||
(document.getElementById(id) as HTMLDialogElement | undefined)?.close(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export interface SlackInterface { | ||
sendMsg(webhookUrl: string, msg: string): Promise<any>; | ||
} | ||
|
||
export default class SlackClient implements SlackInterface { | ||
sendMsg(webhookUrl: string, msg: string) { | ||
return fetch(webhookUrl, { | ||
method: "POST", | ||
body: JSON.stringify({ text: msg }), | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.