Skip to content

Commit

Permalink
refactor: minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mgierada committed Jun 21, 2024
1 parent 0d44e7b commit 4763f1c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
7 changes: 5 additions & 2 deletions ui/src/app/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ const Home = async () => {
alertDialogDescription={`
This action cannot be undone.
This will reset the healthy counter and start the sick interval.`}
handleUpdate={async function s() {
handleUpdate={async function update() {
"use server";
return recordEvent("ohno");
}}
Expand All @@ -151,7 +151,10 @@ const Home = async () => {
alertDialogDescription={`
This action cannot be undone.
This will reset the sick counter and start the healthy interval.`}
// handleUpdate={recordEvent("fine")}
handleUpdate={async function update() {
"use server";
return recordEvent("fine");
}}
/>
</div>
</main>
Expand Down
19 changes: 9 additions & 10 deletions ui/src/utils/actions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"use server";
// "use server";

/**
* @typedef {Object } RecordEventApiResponse
Expand All @@ -10,32 +10,31 @@
* @param {string} endpoint - The endpoint to record the event to.
* @returns {Promise<void>} A promise that resolves when the event is successfully recorded.
*/
export const recordEvent = async (endpoint) => {
// "use server";
try {
const rootUrl = process.env.NEXT_PUBLIC_ROOT_API_URL;
const url = `${rootUrl}/${endpoint}`;
export const recordEvent = async (eventType) => {
const rootUrl = process.env.NEXT_PUBLIC_ROOT_API_URL;
const url = `${rootUrl}${eventType}`;

try {
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
});

console.log("response: ", response);

if (!response.ok) {
throw new Error("Failed to record event");
}

/** @type {RecordEventApiResponse} */
const data = await response.json();

if (!data || !data.message) {
throw new Error("Invalid response data");
}

return;
} catch (error) {
console.log(error);
throw new Error(`Error recording event: ${error.message}`);
throw new Error(error.message);
}
};

0 comments on commit 4763f1c

Please sign in to comment.