Skip to content

Commit

Permalink
feat(ui): loading states for POST request, redirection to newly creat…
Browse files Browse the repository at this point in the history
…ed entities
  • Loading branch information
yg-lim committed Aug 9, 2024
1 parent 545e1e3 commit b1f9af7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 33 deletions.
39 changes: 21 additions & 18 deletions ui/src/components/ChatbotConfiguration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { LongContextReorderField } from "./form_fields/LongContextReorderField";
import { PromptField } from "./form_fields/PromptField";

import { ServerKnowledgeBaseConfig } from "../services/knowledge-base-service";
import { RotateCw } from "lucide-react";

interface ChatbotConfigurationProps {
chatbot: ServerPipelineConfig;
Expand All @@ -34,7 +35,7 @@ export function ChatbotConfiguration({
chatbot,
knowledgeBases,
}: ChatbotConfigurationProps) {
const mutation = useMutation({
const { mutate, isPending } = useMutation({
mutationFn: (data: ClientPipelineConfig) =>
chatbotService.updateChatbot(chatbot.id, data),
});
Expand All @@ -49,28 +50,30 @@ export function ChatbotConfiguration({
}
}, [chatbot, form]);

const handleSubmit = form.handleSubmit(
(data) => {
mutation.mutate(data);
},
(errors) => {
console.error("Form submission failed. Errors:", errors);
}
);
const handleSubmit = form.handleSubmit((data) => {
mutate(data);
});

return (
<Card className="h-full flex flex-col px-6">
<header className="flex justify-between items-baseline">
<Typography variant="h4">Configuration</Typography>
<Button
type="submit"
onClick={(e) => {
e.preventDefault();
handleSubmit();
}}
>
Save
</Button>
{isPending ? (
<Button disabled>
<RotateCw className="w-4 h-4 mr-2 animate-spin" />
Saving...
</Button>
) : (
<Button
type="submit"
onClick={(e) => {
e.preventDefault();
handleSubmit();
}}
>
Save
</Button>
)}
</header>
<Form {...form}>
<form className="space-y-8 mb-8">
Expand Down
23 changes: 20 additions & 3 deletions ui/src/components/ModalKnowledgeBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ import {
ClientKnowledgeBaseConfig,
clientKnowledgeBaseConfigSchema,
knowledgeBaseService,
ServerKnowledgeBaseConfig,
} from "@/services/knowledge-base-service";
import { zodResolver } from "@hookform/resolvers/zod";
import { RefreshCw } from "lucide-react";
import { useLocation } from "wouter";

interface ModalKnowledgeBaseProps {
setModalVisible: Dispatch<SetStateAction<boolean>>;
Expand All @@ -51,9 +54,16 @@ export function ModalKnowledgeBase({
},
});

const mutation = useMutation({
const [, setLocation] = useLocation();

const { mutate, isPending } = useMutation({
mutationFn: (data: ClientKnowledgeBaseConfig) =>
knowledgeBaseService.createKnowledgeBase(data),

onSuccess: (data: ServerKnowledgeBaseConfig) => {
setModalVisible(false);
setLocation(`/knowledge-bases/${data.id}`);
},
});

function handleCancelClick() {
Expand All @@ -64,7 +74,7 @@ export function ModalKnowledgeBase({
(data) => {
console.log(form.getValues());
console.log(data);
mutation.mutate(data);
mutate(data);
},
(error) => {
console.log(error);
Expand Down Expand Up @@ -411,7 +421,14 @@ export function ModalKnowledgeBase({
<Button variant="outline" onClick={handleCancelClick}>
Cancel
</Button>
<Button type="submit">Create</Button>
{isPending ? (
<Button disabled>
<RefreshCw className="mr-2 h-4 w-4 animate-spin" />
Creating...
</Button>
) : (
<Button type="submit">Create</Button>
)}
</div>
</form>
</Form>
Expand Down
31 changes: 19 additions & 12 deletions ui/src/components/pages/PageCreateChatbot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ErrorMessageWithReload } from "../ErrorMessageWithReload";
import { chatbotService } from "../../services/chatbot-service";
import { useMutation } from "@tanstack/react-query";

import { ArrowLeftIcon } from "lucide-react";
import { ArrowLeftIcon, RotateCw } from "lucide-react";
import { useLocation } from "wouter";

import { useQuery } from "@tanstack/react-query";
Expand Down Expand Up @@ -54,7 +54,7 @@ export function PageCreateChatbot() {
queryFn: knowledgeBaseService.fetchKnowledgeBases,
});

const mutation = useMutation({
const { mutate, isPending } = useMutation({
mutationFn: (data: z.infer<typeof clientPipelineConfigSchema>) =>
chatbotService.createChatbot(data),
onSuccess: (data) => {
Expand All @@ -65,7 +65,7 @@ export function PageCreateChatbot() {
function handleFormSubmit(
values: z.infer<typeof clientPipelineConfigSchema>
) {
mutation.mutate(values);
mutate(values);
}

function handleBackClick() {
Expand Down Expand Up @@ -127,15 +127,22 @@ export function PageCreateChatbot() {
<LongContextReorderField control={form.control} />
<PromptField control={form.control} />
<div className="flex justify-end">
<Button
type="submit"
onClick={(e) => {
e.preventDefault();
form.handleSubmit(handleFormSubmit)();
}}
>
Create
</Button>
{isPending ? (
<Button disabled className="gap-2">
<RotateCw className="h-5 w-5 animate-spin" />
Creating...
</Button>
) : (
<Button
type="submit"
onClick={(e) => {
e.preventDefault();
form.handleSubmit(handleFormSubmit)();
}}
>
Create
</Button>
)}
</div>
</form>
</Form>
Expand Down

0 comments on commit b1f9af7

Please sign in to comment.