Skip to content

Commit

Permalink
feat: change save button behaviour in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
comoser committed Sep 27, 2023
1 parent c5337af commit 41e4372
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/web-ui/app/admin/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default function SuperAdminLogin() {
variant: 'destructive',
});
},
onSuccess: (room) => {
onSuccess: () => {
router.push('/admin/manage/users');
},
});
Expand Down
2 changes: 1 addition & 1 deletion packages/web-ui/components/admin/add-ai-models-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function AddAiModelsForm() {
variant: 'destructive',
});
},
onSuccess: (room) => {
onSuccess: () => {
setOpen(false);
toast({
title: 'AI Model added successfully!',
Expand Down
2 changes: 1 addition & 1 deletion packages/web-ui/components/admin/update-roles-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function UpdateRolesForm({
variant: 'destructive',
});
},
onSuccess: (room) => {
onSuccess: () => {
setOpen(false);
toast({
title: 'Roles updated successfully!',
Expand Down
21 changes: 19 additions & 2 deletions packages/web-ui/components/update-room-form.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use client';

import { useMemo, useState } from 'react';
import { useRouter } from 'next/navigation';
import { updateRoom } from '@/api/requests/rooms/update-room';
import { RoomResponseDto } from '@/contract/rooms/room.response.dto.d';
Expand Down Expand Up @@ -27,10 +28,13 @@ import { useToast } from '@/components/ui/use-toast';
interface UpdateRoomFormProps {
room: RoomResponseDto;
}

export function UpdateRoomForm({ room }: UpdateRoomFormProps) {
const router = useRouter();
const { toast } = useToast();
const { getToken } = useAuth();
const [originalName, setOriginalName] = useState(room.name);
const [originalPrivacy, setOriginalPrivacy] = useState(room.isPrivate);
const { mutate: updateRoomMutationReq, isLoading } = useMutation({
mutationFn: async (values: UpdateRoomSettingsRequestDto) =>
updateRoom(room.id, values, await getToken()),
Expand All @@ -40,10 +44,12 @@ export function UpdateRoomForm({ room }: UpdateRoomFormProps) {
variant: 'destructive',
});
},
onSuccess: () => {
onSuccess: (room) => {
toast({
title: 'Room updated successfully!',
});
setOriginalName(room.name);
setOriginalPrivacy(room.isPrivate);
router.refresh();
},
});
Expand All @@ -54,6 +60,13 @@ export function UpdateRoomForm({ room }: UpdateRoomFormProps) {
isPrivate: room.isPrivate,
},
});
const isFormChanged = useMemo(() => {
const formValues = form.getValues();
return (
originalName !== formValues.name ||
originalPrivacy !== formValues.isPrivate
);
}, [originalName, originalPrivacy, form.getValues()]);

async function onSubmit(values: UpdateRoomSettingsRequestDto) {
try {
Expand Down Expand Up @@ -96,7 +109,11 @@ export function UpdateRoomForm({ room }: UpdateRoomFormProps) {
/>
</FormControl>
<div className="flex w-full justify-end">
<Button disabled={isLoading} variant="success" type="submit">
<Button
disabled={isLoading || !isFormChanged}
variant="success"
type="submit"
>
Save
</Button>
</div>
Expand Down

0 comments on commit 41e4372

Please sign in to comment.