Skip to content

Commit

Permalink
Implement posting note
Browse files Browse the repository at this point in the history
  • Loading branch information
pbzweihander committed Nov 29, 2023
1 parent ccbe583 commit 098dac5
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
55 changes: 47 additions & 8 deletions frontend/src/pages/login/LogInIndex.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,70 @@
import { SubmitHandler, useForm } from "react-hook-form";
import z from "zod";

import { CreatePost } from "../../dto";
import { useNotes, usePostNoteMutation } from "../../queries/note";

export function LogInIndexPage() {
const notes = new Array(40).fill("foobar"); // TODO: stub
const { data: notes } = useNotes();
const { register, handleSubmit, reset } =
useForm<z.infer<typeof CreatePost>>();
const {
mutate: postNote,
isLoading,
error,
} = usePostNoteMutation(() => {
reset();
});

const onSubmit: SubmitHandler<z.infer<typeof CreatePost>> = (data) => {
postNote(data);
};

return (
<div className="relative flex h-full w-full">
<div className="h-full w-full overflow-y-scroll">
{notes.map((note, i) => (
<div className="chat chat-start">
<div className="chat-bubble">
{note} {i}
</div>
{(notes ?? []).map((note) => (
<div className={`chat chat-${note.user != null ? "start" : "end"}`}>
{note.user && (
<div className="chat-header">
{note.user.name != null ? (
<span>
{note.user.name}
<span className="ml-2 text-gray-500">
@{note.user.handle}
</span>
</span>
) : (
<span>@{note.user.handle}</span>
)}
</div>
)}
<div className="chat-bubble">{note.text}</div>
</div>
))}
</div>
<form className="chat chat-end absolute bottom-4 right-4">
<form
className="chat chat-end absolute bottom-4 right-4"
onSubmit={handleSubmit(onSubmit)}
>
<input type="hidden" value="public" {...register("visibility")} />
<div className="chat-bubble chat-bubble-primary">
<textarea
className="textarea w-full"
className="textarea w-full text-base-content"
placeholder="Jot something..."
required
{...register("text")}
/>
</div>
<div className="chat-footer">
<input
type="submit"
className="btn btn-primary btn-sm mt-2"
value="Chirp!"
disabled={isLoading}
/>
</div>
{error && <div className="mt-5 text-error">{error.message}</div>}
</form>
</div>
);
Expand Down
23 changes: 23 additions & 0 deletions frontend/src/queries/note.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useQueryClient } from "react-query";
import z from "zod";

import { JsonMutationRet, useJsonQuery, useJsonMutation } from ".";
import { CreatePost, IdResponse, Post } from "../dto";

const NOTES_KEY = ["notes"];

export function useNotes() {
return useJsonQuery(z.array(Post), NOTES_KEY, "/api/post");
}

export function usePostNoteMutation(
onSuccess: () => void,
): JsonMutationRet<z.infer<typeof CreatePost>, typeof IdResponse> {
const queryClient = useQueryClient();
return useJsonMutation("POST", "/api/post", IdResponse, {
onSuccess: () => {
queryClient.invalidateQueries(NOTES_KEY);
onSuccess();
},
});
}

0 comments on commit 098dac5

Please sign in to comment.