Skip to content

Commit

Permalink
Merge pull request #19 from techx/eddie/reduce_post_requests
Browse files Browse the repository at this point in the history
reduce post requests when clicking through emails
  • Loading branch information
azliu0 authored Apr 27, 2024
2 parents 189302f + 200b26b commit b255088
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
10 changes: 10 additions & 0 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@tiptap/pm": "^2.1.12",
"@tiptap/react": "^2.1.12",
"@tiptap/starter-kit": "^2.1.12",
"@types/react-icons": "^3.0.0",
"node-fetch": "^3.3.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
24 changes: 20 additions & 4 deletions client/src/routes/inbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ export default function InboxPage() {

const [response, setResponse] = useState<Response | undefined>(undefined);

const [storedResponses, setStoredResponses] = useState<{
[key: number]: Response;
}>({});

const viewport = useRef<HTMLDivElement>(null);

const editor = useEditor(
Expand Down Expand Up @@ -153,11 +157,20 @@ export default function InboxPage() {
};

const getResponse = () => {
// Checks if response is already stored
const currEmailID =
activeThread.emailList[activeThread.emailList.length - 1].id;
if (storedResponses[currEmailID]) {
const oldResponse = storedResponses[currEmailID];
setResponse(oldResponse);
setContent(oldResponse.content.replace("\n", "<br/>"));
return;
}

// Otherwise fetches response from server
const formData = new FormData();
formData.append(
"id",
activeThread.emailList[activeThread.emailList.length - 1].id.toString(),
);
formData.append("id", currEmailID.toString());

fetch(`/api/emails/get_response`, {
method: "POST",
body: formData,
Expand All @@ -171,6 +184,9 @@ export default function InboxPage() {
});
})
.then((data) => {
setStoredResponses((oldResponses) => {
return { ...oldResponses, [currEmailID]: data };
});
setResponse(data);
setContent(data.content.replaceAll("\n", "<br/>"));
});
Expand Down

0 comments on commit b255088

Please sign in to comment.