✨ Migrate to TanStack Query (React Query) and TanStack Router (#637)
This commit is contained in:
@@ -2,11 +2,11 @@ import React from 'react';
|
||||
|
||||
import { Button, Checkbox, Flex, FormControl, FormErrorMessage, FormLabel, Input, Modal, ModalBody, ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalOverlay } from '@chakra-ui/react';
|
||||
import { SubmitHandler, useForm } from 'react-hook-form';
|
||||
import { useMutation, useQueryClient } from 'react-query';
|
||||
|
||||
import { UserCreate } from '../../client';
|
||||
import useCustomToast from '../../hooks/useCustomToast';
|
||||
import { useUsersStore } from '../../store/users-store';
|
||||
import { UserCreate, UsersService } from '../../client';
|
||||
import { ApiError } from '../../client/core/ApiError';
|
||||
import useCustomToast from '../../hooks/useCustomToast';
|
||||
|
||||
interface AddUserProps {
|
||||
isOpen: boolean;
|
||||
@@ -19,6 +19,7 @@ interface UserCreateForm extends UserCreate {
|
||||
}
|
||||
|
||||
const AddUser: React.FC<AddUserProps> = ({ isOpen, onClose }) => {
|
||||
const queryClient = useQueryClient();
|
||||
const showToast = useCustomToast();
|
||||
const { register, handleSubmit, reset, getValues, formState: { errors, isSubmitting } } = useForm<UserCreateForm>({
|
||||
mode: 'onBlur',
|
||||
@@ -32,18 +33,28 @@ const AddUser: React.FC<AddUserProps> = ({ isOpen, onClose }) => {
|
||||
is_active: false
|
||||
}
|
||||
});
|
||||
const { addUser } = useUsersStore();
|
||||
|
||||
const onSubmit: SubmitHandler<UserCreateForm> = async (data) => {
|
||||
try {
|
||||
await addUser(data);
|
||||
const addUser = async (data: UserCreate) => {
|
||||
await UsersService.createUser({ requestBody: data })
|
||||
}
|
||||
|
||||
const mutation = useMutation(addUser, {
|
||||
onSuccess: () => {
|
||||
showToast('Success!', 'User created successfully.', 'success');
|
||||
reset();
|
||||
onClose();
|
||||
} catch (err) {
|
||||
const errDetail = (err as ApiError).body.detail;
|
||||
},
|
||||
onError: (err: ApiError) => {
|
||||
const errDetail = err.body.detail;
|
||||
showToast('Something went wrong.', `${errDetail}`, 'error');
|
||||
},
|
||||
onSettled: () => {
|
||||
queryClient.invalidateQueries('users');
|
||||
}
|
||||
});
|
||||
|
||||
const onSubmit: SubmitHandler<UserCreateForm> = (data) => {
|
||||
mutation.mutate(data);
|
||||
}
|
||||
|
||||
return (
|
||||
|
@@ -2,13 +2,13 @@ import React from 'react';
|
||||
|
||||
import { Button, Checkbox, Flex, FormControl, FormErrorMessage, FormLabel, Input, Modal, ModalBody, ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalOverlay } from '@chakra-ui/react';
|
||||
import { SubmitHandler, useForm } from 'react-hook-form';
|
||||
import { useMutation, useQueryClient } from 'react-query';
|
||||
|
||||
import { ApiError, UserUpdate } from '../../client';
|
||||
import { ApiError, UserOut, UserUpdate, UsersService } from '../../client';
|
||||
import useCustomToast from '../../hooks/useCustomToast';
|
||||
import { useUsersStore } from '../../store/users-store';
|
||||
|
||||
interface EditUserProps {
|
||||
user_id: number;
|
||||
user: UserOut;
|
||||
isOpen: boolean;
|
||||
onClose: () => void;
|
||||
}
|
||||
@@ -17,37 +17,39 @@ interface UserUpdateForm extends UserUpdate {
|
||||
confirm_password: string;
|
||||
}
|
||||
|
||||
const EditUser: React.FC<EditUserProps> = ({ user_id, isOpen, onClose }) => {
|
||||
const EditUser: React.FC<EditUserProps> = ({ user, isOpen, onClose }) => {
|
||||
const queryClient = useQueryClient();
|
||||
const showToast = useCustomToast();
|
||||
const { editUser, users } = useUsersStore();
|
||||
const currentUser = users.find((user) => user.id === user_id);
|
||||
const { register, handleSubmit, reset, getValues, formState: { errors, isSubmitting } } = useForm<UserUpdateForm>({
|
||||
|
||||
const { register, handleSubmit, reset, getValues, formState: { errors, isSubmitting, isDirty } } = useForm<UserUpdateForm>({
|
||||
mode: 'onBlur',
|
||||
criteriaMode: 'all',
|
||||
defaultValues: {
|
||||
email: currentUser?.email,
|
||||
full_name: currentUser?.full_name,
|
||||
password: '',
|
||||
confirm_password: '',
|
||||
is_superuser: currentUser?.is_superuser,
|
||||
is_active: currentUser?.is_active
|
||||
defaultValues: user
|
||||
});
|
||||
|
||||
const updateUser = async (data: UserUpdateForm) => {
|
||||
await UsersService.updateUser({ userId: user.id, requestBody: data });
|
||||
}
|
||||
|
||||
const mutation = useMutation(updateUser, {
|
||||
onSuccess: () => {
|
||||
showToast('Success!', 'User updated successfully.', 'success');
|
||||
onClose();
|
||||
},
|
||||
onError: (err: ApiError) => {
|
||||
const errDetail = err.body.detail;
|
||||
showToast('Something went wrong.', `${errDetail}`, 'error');
|
||||
},
|
||||
onSettled: () => {
|
||||
queryClient.invalidateQueries('users');
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
const onSubmit: SubmitHandler<UserUpdateForm> = async (data) => {
|
||||
try {
|
||||
if (data.password === '') {
|
||||
delete data.password;
|
||||
}
|
||||
await editUser(user_id, data);
|
||||
showToast('Success!', 'User updated successfully.', 'success');
|
||||
reset();
|
||||
onClose();
|
||||
} catch (err) {
|
||||
const errDetail = (err as ApiError).body.detail;
|
||||
showToast('Something went wrong.', `${errDetail}`, 'error');
|
||||
if (data.password === '') {
|
||||
delete data.password;
|
||||
}
|
||||
mutation.mutate(data)
|
||||
}
|
||||
|
||||
const onCancel = () => {
|
||||
@@ -70,12 +72,12 @@ const EditUser: React.FC<EditUserProps> = ({ user_id, isOpen, onClose }) => {
|
||||
<ModalBody pb={6}>
|
||||
<FormControl isInvalid={!!errors.email}>
|
||||
<FormLabel htmlFor='email'>Email</FormLabel>
|
||||
<Input id='email' {...register('email', { pattern: { value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i, message: 'Invalid email address' } })} placeholder='Email' type='email' />
|
||||
<Input id='email' {...register('email', { required: 'Email is required', pattern: { value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i, message: 'Invalid email address' } })} placeholder='Email' type='email' />
|
||||
{errors.email && <FormErrorMessage>{errors.email.message}</FormErrorMessage>}
|
||||
</FormControl>
|
||||
<FormControl mt={4}>
|
||||
<FormLabel htmlFor='name'>Full name</FormLabel>
|
||||
<Input id="name" {...register('full_name')} type='text' />
|
||||
<Input id='name' {...register('full_name')} type='text' />
|
||||
</FormControl>
|
||||
<FormControl mt={4} isInvalid={!!errors.password}>
|
||||
<FormLabel htmlFor='password'>Set Password</FormLabel>
|
||||
@@ -100,7 +102,7 @@ const EditUser: React.FC<EditUserProps> = ({ user_id, isOpen, onClose }) => {
|
||||
</ModalBody>
|
||||
|
||||
<ModalFooter gap={3}>
|
||||
<Button bg='ui.main' color='white' _hover={{ opacity: 0.8 }} type='submit' isLoading={isSubmitting}>
|
||||
<Button bg='ui.main' color='white' _hover={{ opacity: 0.8 }} type='submit' isLoading={isSubmitting} isDisabled={!isDirty}>
|
||||
Save
|
||||
</Button>
|
||||
<Button onClick={onCancel}>Cancel</Button>
|
||||
|
@@ -7,15 +7,16 @@ import { FiEdit, FiTrash } from 'react-icons/fi';
|
||||
import EditUser from '../Admin/EditUser';
|
||||
import EditItem from '../Items/EditItem';
|
||||
import Delete from './DeleteAlert';
|
||||
import { ItemOut, UserOut } from '../../client';
|
||||
|
||||
|
||||
interface ActionsMenuProps {
|
||||
type: string;
|
||||
id: number;
|
||||
value: ItemOut | UserOut;
|
||||
disabled?: boolean;
|
||||
}
|
||||
|
||||
const ActionsMenu: React.FC<ActionsMenuProps> = ({ type, id, disabled }) => {
|
||||
const ActionsMenu: React.FC<ActionsMenuProps> = ({ type, value, disabled }) => {
|
||||
const editUserModal = useDisclosure();
|
||||
const deleteModal = useDisclosure();
|
||||
|
||||
@@ -29,10 +30,10 @@ const ActionsMenu: React.FC<ActionsMenuProps> = ({ type, id, disabled }) => {
|
||||
<MenuItem onClick={deleteModal.onOpen} icon={<FiTrash fontSize='16px' />} color='ui.danger'>Delete {type}</MenuItem>
|
||||
</MenuList>
|
||||
{
|
||||
type === 'User' ? <EditUser user_id={id} isOpen={editUserModal.isOpen} onClose={editUserModal.onClose} />
|
||||
: <EditItem id={id} isOpen={editUserModal.isOpen} onClose={editUserModal.onClose} />
|
||||
type === 'User' ? <EditUser user={value as UserOut} isOpen={editUserModal.isOpen} onClose={editUserModal.onClose} />
|
||||
: <EditItem item={value as ItemOut} isOpen={editUserModal.isOpen} onClose={editUserModal.onClose} />
|
||||
}
|
||||
<Delete type={type} id={id} isOpen={deleteModal.isOpen} onClose={deleteModal.onClose} />
|
||||
<Delete type={type} id={value.id} isOpen={deleteModal.isOpen} onClose={deleteModal.onClose} />
|
||||
</Menu>
|
||||
</>
|
||||
);
|
||||
|
@@ -1,11 +1,11 @@
|
||||
import React, { useState } from 'react';
|
||||
import React from 'react';
|
||||
|
||||
import { AlertDialog, AlertDialogBody, AlertDialogContent, AlertDialogFooter, AlertDialogHeader, AlertDialogOverlay, Button } from '@chakra-ui/react';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { useMutation, useQueryClient } from 'react-query';
|
||||
|
||||
import { ItemsService, UsersService } from '../../client';
|
||||
import useCustomToast from '../../hooks/useCustomToast';
|
||||
import { useItemsStore } from '../../store/items-store';
|
||||
import { useUsersStore } from '../../store/users-store';
|
||||
|
||||
interface DeleteProps {
|
||||
type: string;
|
||||
@@ -15,20 +15,36 @@ interface DeleteProps {
|
||||
}
|
||||
|
||||
const Delete: React.FC<DeleteProps> = ({ type, id, isOpen, onClose }) => {
|
||||
const queryClient = useQueryClient();
|
||||
const showToast = useCustomToast();
|
||||
const cancelRef = React.useRef<HTMLButtonElement | null>(null);
|
||||
const { handleSubmit, formState: {isSubmitting} } = useForm();
|
||||
const { deleteItem } = useItemsStore();
|
||||
const { deleteUser } = useUsersStore();
|
||||
const { handleSubmit, formState: { isSubmitting } } = useForm();
|
||||
|
||||
const onSubmit = async () => {
|
||||
try {
|
||||
type === 'Item' ? await deleteItem(id) : await deleteUser(id);
|
||||
const deleteEntity = async (id: number) => {
|
||||
if (type === 'Item') {
|
||||
await ItemsService.deleteItem({ id: id });
|
||||
} else if (type === 'User') {
|
||||
await UsersService.deleteUser({ userId: id });
|
||||
} else {
|
||||
throw new Error(`Unexpected type: ${type}`);
|
||||
}
|
||||
}
|
||||
|
||||
const mutation = useMutation(deleteEntity, {
|
||||
onSuccess: () => {
|
||||
showToast('Success', `The ${type.toLowerCase()} was deleted successfully.`, 'success');
|
||||
onClose();
|
||||
} catch (err) {
|
||||
},
|
||||
onError: () => {
|
||||
showToast('An error occurred.', `An error occurred while deleting the ${type.toLowerCase()}.`, 'error');
|
||||
},
|
||||
onSettled: () => {
|
||||
queryClient.invalidateQueries(type === 'Item' ? 'items' : 'users');
|
||||
}
|
||||
})
|
||||
|
||||
const onSubmit = async () => {
|
||||
mutation.mutate(id);
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -37,11 +53,11 @@ const Delete: React.FC<DeleteProps> = ({ type, id, isOpen, onClose }) => {
|
||||
isOpen={isOpen}
|
||||
onClose={onClose}
|
||||
leastDestructiveRef={cancelRef}
|
||||
size={{ base: "sm", md: "md" }}
|
||||
size={{ base: 'sm', md: 'md' }}
|
||||
isCentered
|
||||
>
|
||||
<AlertDialogOverlay>
|
||||
<AlertDialogContent as="form" onSubmit={handleSubmit(onSubmit)}>
|
||||
<AlertDialogContent as='form' onSubmit={handleSubmit(onSubmit)}>
|
||||
<AlertDialogHeader>
|
||||
Delete {type}
|
||||
</AlertDialogHeader>
|
||||
@@ -52,7 +68,7 @@ const Delete: React.FC<DeleteProps> = ({ type, id, isOpen, onClose }) => {
|
||||
</AlertDialogBody>
|
||||
|
||||
<AlertDialogFooter gap={3}>
|
||||
<Button bg="ui.danger" color="white" _hover={{ opacity: 0.8 }} type="submit" isLoading={isSubmitting}>
|
||||
<Button bg='ui.danger' color='white' _hover={{ opacity: 0.8 }} type='submit' isLoading={isSubmitting}>
|
||||
Delete
|
||||
</Button>
|
||||
<Button ref={cancelRef} onClick={onClose} isDisabled={isSubmitting}>
|
||||
|
@@ -1,7 +1,7 @@
|
||||
import React from 'react';
|
||||
|
||||
import { Button, Flex, Icon, Input, InputGroup, InputLeftElement, useDisclosure } from '@chakra-ui/react';
|
||||
import { FaPlus, FaSearch } from "react-icons/fa";
|
||||
import { Button, Flex, Icon, useDisclosure } from '@chakra-ui/react';
|
||||
import { FaPlus } from 'react-icons/fa';
|
||||
|
||||
import AddUser from '../Admin/AddUser';
|
||||
import AddItem from '../Items/AddItem';
|
||||
@@ -17,13 +17,14 @@ const Navbar: React.FC<NavbarProps> = ({ type }) => {
|
||||
return (
|
||||
<>
|
||||
<Flex py={8} gap={4}>
|
||||
<InputGroup w={{ base: "100%", md: "auto" }}>
|
||||
<InputLeftElement pointerEvents="none">
|
||||
<Icon as={FaSearch} color="gray.400" />
|
||||
{/* TODO: Complete search functionality */}
|
||||
{/* <InputGroup w={{ base: '100%', md: 'auto' }}>
|
||||
<InputLeftElement pointerEvents='none'>
|
||||
<Icon as={FaSearch} color='gray.400' />
|
||||
</InputLeftElement>
|
||||
<Input type="text" placeholder="Search" fontSize={{ base: "sm", md: "inherit" }} borderRadius="8px" />
|
||||
</InputGroup>
|
||||
<Button bg="ui.main" color="white" _hover={{ opacity: 0.8 }} gap={1} fontSize={{ base: "sm", md: "inherit" }} onClick={type === "User" ? addUserModal.onOpen : addItemModal.onOpen}>
|
||||
<Input type='text' placeholder='Search' fontSize={{ base: 'sm', md: 'inherit' }} borderRadius='8px' />
|
||||
</InputGroup> */}
|
||||
<Button bg='ui.main' color='white' _hover={{ opacity: 0.8 }} gap={1} fontSize={{ base: 'sm', md: 'inherit' }} onClick={type === 'User' ? addUserModal.onOpen : addItemModal.onOpen}>
|
||||
<Icon as={FaPlus} /> Add {type}
|
||||
</Button>
|
||||
<AddUser isOpen={addUserModal.isOpen} onClose={addUserModal.onClose} />
|
||||
|
22
new-frontend/src/components/Common/NotFound.tsx
Normal file
22
new-frontend/src/components/Common/NotFound.tsx
Normal file
@@ -0,0 +1,22 @@
|
||||
import { Button, Container, Text } from '@chakra-ui/react';
|
||||
import { Link } from '@tanstack/react-router';
|
||||
|
||||
const NotFound: React.FC = () => {
|
||||
|
||||
return (
|
||||
<>
|
||||
<Container h='100vh'
|
||||
alignItems='stretch'
|
||||
justifyContent='center' textAlign='center' maxW='sm' centerContent>
|
||||
<Text fontSize='8xl' color='ui.main' fontWeight='bold' lineHeight='1' mb={4}>404</Text>
|
||||
<Text fontSize='md'>Oops!</Text>
|
||||
<Text fontSize='md'>Page not found.</Text>
|
||||
<Button as={Link} to='/' color='ui.main' borderColor='ui.main' variant='outline' mt={4}>Go back</Button>
|
||||
</Container>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export default NotFound;
|
||||
|
||||
|
@@ -2,18 +2,20 @@ import React from 'react';
|
||||
|
||||
import { Box, Drawer, DrawerBody, DrawerCloseButton, DrawerContent, DrawerOverlay, Flex, IconButton, Image, Text, useColorModeValue, useDisclosure } from '@chakra-ui/react';
|
||||
import { FiLogOut, FiMenu } from 'react-icons/fi';
|
||||
import { useQueryClient } from 'react-query';
|
||||
|
||||
import Logo from '../../assets/images/fastapi-logo.svg';
|
||||
import { UserOut } from '../../client';
|
||||
import useAuth from '../../hooks/useAuth';
|
||||
import { useUserStore } from '../../store/user-store';
|
||||
import SidebarItems from './SidebarItems';
|
||||
|
||||
const Sidebar: React.FC = () => {
|
||||
const queryClient = useQueryClient();
|
||||
const bgColor = useColorModeValue('white', '#1a202c');
|
||||
const textColor = useColorModeValue('gray', 'white');
|
||||
const secBgColor = useColorModeValue('ui.secondary', '#252d3d');
|
||||
const currentUser = queryClient.getQueryData<UserOut>('currentUser');
|
||||
const { isOpen, onOpen, onClose } = useDisclosure();
|
||||
const { user } = useUserStore();
|
||||
const { logout } = useAuth();
|
||||
|
||||
const handleLogout = async () => {
|
||||
@@ -40,8 +42,8 @@ const Sidebar: React.FC = () => {
|
||||
</Flex>
|
||||
</Box>
|
||||
{
|
||||
user?.email &&
|
||||
<Text color={textColor} noOfLines={2} fontSize='sm' p={2}>Logged in as: {user.email}</Text>
|
||||
currentUser?.email &&
|
||||
<Text color={textColor} noOfLines={2} fontSize='sm' p={2}>Logged in as: {currentUser.email}</Text>
|
||||
}
|
||||
</Flex>
|
||||
</DrawerBody>
|
||||
@@ -56,8 +58,8 @@ const Sidebar: React.FC = () => {
|
||||
<SidebarItems />
|
||||
</Box>
|
||||
{
|
||||
user?.email &&
|
||||
<Text color={textColor} noOfLines={2} fontSize='sm' p={2} maxW='180px'>Logged in as: {user.email}</Text>
|
||||
currentUser?.email &&
|
||||
<Text color={textColor} noOfLines={2} fontSize='sm' p={2} maxW='180px'>Logged in as: {currentUser.email}</Text>
|
||||
}
|
||||
</Flex>
|
||||
</Box>
|
||||
|
@@ -2,14 +2,15 @@ import React from 'react';
|
||||
|
||||
import { Box, Flex, Icon, Text, useColorModeValue } from '@chakra-ui/react';
|
||||
import { FiBriefcase, FiHome, FiSettings, FiUsers } from 'react-icons/fi';
|
||||
import { Link, useLocation } from 'react-router-dom';
|
||||
import { Link } from '@tanstack/react-router';
|
||||
import { useQueryClient } from 'react-query';
|
||||
|
||||
import { useUserStore } from '../../store/user-store';
|
||||
import { UserOut } from '../../client';
|
||||
|
||||
const items = [
|
||||
{ icon: FiHome, title: 'Dashboard', path: "/" },
|
||||
{ icon: FiBriefcase, title: 'Items', path: "/items" },
|
||||
{ icon: FiSettings, title: 'User Settings', path: "/settings" },
|
||||
{ icon: FiHome, title: 'Dashboard', path: '/' },
|
||||
{ icon: FiBriefcase, title: 'Items', path: '/items' },
|
||||
{ icon: FiSettings, title: 'User Settings', path: '/settings' },
|
||||
];
|
||||
|
||||
interface SidebarItemsProps {
|
||||
@@ -17,28 +18,31 @@ interface SidebarItemsProps {
|
||||
}
|
||||
|
||||
const SidebarItems: React.FC<SidebarItemsProps> = ({ onClose }) => {
|
||||
const textColor = useColorModeValue("ui.main", "#E2E8F0");
|
||||
const bgActive = useColorModeValue("#E2E8F0", "#4A5568");
|
||||
const location = useLocation();
|
||||
const { user } = useUserStore();
|
||||
const queryClient = useQueryClient();
|
||||
const textColor = useColorModeValue('ui.main', '#E2E8F0');
|
||||
const bgActive = useColorModeValue('#E2E8F0', '#4A5568');
|
||||
const currentUser = queryClient.getQueryData<UserOut>('currentUser');
|
||||
|
||||
const finalItems = user?.is_superuser ? [...items, { icon: FiUsers, title: 'Admin', path: "/admin" }] : items;
|
||||
|
||||
const finalItems = currentUser?.is_superuser ? [...items, { icon: FiUsers, title: 'Admin', path: '/admin' }] : items;
|
||||
|
||||
const listItems = finalItems.map((item) => (
|
||||
<Flex
|
||||
as={Link}
|
||||
to={item.path}
|
||||
w="100%"
|
||||
w='100%'
|
||||
p={2}
|
||||
key={item.title}
|
||||
style={location.pathname === item.path ? {
|
||||
background: bgActive,
|
||||
borderRadius: "12px",
|
||||
} : {}}
|
||||
activeProps={{
|
||||
style: {
|
||||
background: bgActive,
|
||||
borderRadius: '12px',
|
||||
},
|
||||
}}
|
||||
color={textColor}
|
||||
onClick={onClose}
|
||||
>
|
||||
<Icon as={item.icon} alignSelf="center" />
|
||||
<Icon as={item.icon} alignSelf='center' />
|
||||
<Text ml={2}>{item.title}</Text>
|
||||
</Flex>
|
||||
));
|
||||
|
@@ -3,9 +3,9 @@ import React from 'react';
|
||||
import { Box, IconButton, Menu, MenuButton, MenuItem, MenuList } from '@chakra-ui/react';
|
||||
import { FaUserAstronaut } from 'react-icons/fa';
|
||||
import { FiLogOut, FiUser } from 'react-icons/fi';
|
||||
import { Link } from 'react-router-dom';
|
||||
|
||||
import useAuth from '../../hooks/useAuth';
|
||||
import { Link } from '@tanstack/react-router';
|
||||
|
||||
const UserMenu: React.FC = () => {
|
||||
const { logout } = useAuth();
|
||||
|
@@ -2,10 +2,10 @@ import React from 'react';
|
||||
|
||||
import { Button, FormControl, FormErrorMessage, FormLabel, Input, Modal, ModalBody, ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalOverlay } from '@chakra-ui/react';
|
||||
import { SubmitHandler, useForm } from 'react-hook-form';
|
||||
import { useMutation, useQueryClient } from 'react-query';
|
||||
|
||||
import { ApiError, ItemCreate } from '../../client';
|
||||
import { ApiError, ItemCreate, ItemsService } from '../../client';
|
||||
import useCustomToast from '../../hooks/useCustomToast';
|
||||
import { useItemsStore } from '../../store/items-store';
|
||||
|
||||
interface AddItemProps {
|
||||
isOpen: boolean;
|
||||
@@ -13,6 +13,7 @@ interface AddItemProps {
|
||||
}
|
||||
|
||||
const AddItem: React.FC<AddItemProps> = ({ isOpen, onClose }) => {
|
||||
const queryClient = useQueryClient();
|
||||
const showToast = useCustomToast();
|
||||
const { register, handleSubmit, reset, formState: { errors, isSubmitting } } = useForm<ItemCreate>({
|
||||
mode: 'onBlur',
|
||||
@@ -22,19 +23,29 @@ const AddItem: React.FC<AddItemProps> = ({ isOpen, onClose }) => {
|
||||
description: '',
|
||||
},
|
||||
});
|
||||
const { addItem } = useItemsStore();
|
||||
|
||||
const onSubmit: SubmitHandler<ItemCreate> = async (data) => {
|
||||
try {
|
||||
await addItem(data);
|
||||
const addItem = async (data: ItemCreate) => {
|
||||
await ItemsService.createItem({ requestBody: data })
|
||||
}
|
||||
|
||||
const mutation = useMutation(addItem, {
|
||||
onSuccess: () => {
|
||||
showToast('Success!', 'Item created successfully.', 'success');
|
||||
reset();
|
||||
onClose();
|
||||
} catch (err) {
|
||||
const errDetail = (err as ApiError).body.detail;
|
||||
},
|
||||
onError: (err: ApiError) => {
|
||||
const errDetail = err.body.detail;
|
||||
showToast('Something went wrong.', `${errDetail}`, 'error');
|
||||
},
|
||||
onSettled: () => {
|
||||
queryClient.invalidateQueries('items');
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
const onSubmit: SubmitHandler<ItemCreate> = (data) => {
|
||||
mutation.mutate(data);
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
|
@@ -1,34 +1,47 @@
|
||||
import React from 'react';
|
||||
|
||||
import { Button, FormControl, FormLabel, Input, Modal, ModalBody, ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalOverlay } from '@chakra-ui/react';
|
||||
import { Button, FormControl, FormErrorMessage, FormLabel, Input, Modal, ModalBody, ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalOverlay } from '@chakra-ui/react';
|
||||
import { SubmitHandler, useForm } from 'react-hook-form';
|
||||
|
||||
import { ApiError, ItemUpdate } from '../../client';
|
||||
import { useMutation, useQueryClient } from 'react-query';
|
||||
import { ApiError, ItemOut, ItemUpdate, ItemsService } from '../../client';
|
||||
import useCustomToast from '../../hooks/useCustomToast';
|
||||
import { useItemsStore } from '../../store/items-store';
|
||||
|
||||
interface EditItemProps {
|
||||
id: number;
|
||||
item: ItemOut;
|
||||
isOpen: boolean;
|
||||
onClose: () => void;
|
||||
}
|
||||
|
||||
const EditItem: React.FC<EditItemProps> = ({ id, isOpen, onClose }) => {
|
||||
const EditItem: React.FC<EditItemProps> = ({ item, isOpen, onClose }) => {
|
||||
const queryClient = useQueryClient();
|
||||
const showToast = useCustomToast();
|
||||
const { editItem, items } = useItemsStore();
|
||||
const currentItem = items.find((item) => item.id === id);
|
||||
const { register, handleSubmit, reset, formState: { isSubmitting }, } = useForm<ItemUpdate>({ defaultValues: { title: currentItem?.title, description: currentItem?.description } });
|
||||
const { register, handleSubmit, reset, formState: { isSubmitting, errors, isDirty } } = useForm<ItemUpdate>({
|
||||
mode: 'onBlur',
|
||||
criteriaMode: 'all',
|
||||
defaultValues: item
|
||||
});
|
||||
|
||||
const updateItem = async (data: ItemUpdate) => {
|
||||
await ItemsService.updateItem({ id: item.id, requestBody: data });
|
||||
}
|
||||
|
||||
const mutation = useMutation(updateItem, {
|
||||
onSuccess: () => {
|
||||
showToast('Success!', 'Item updated successfully.', 'success');
|
||||
onClose();
|
||||
},
|
||||
onError: (err: ApiError) => {
|
||||
const errDetail = err.body.detail;
|
||||
showToast('Something went wrong.', `${errDetail}`, 'error');
|
||||
},
|
||||
onSettled: () => {
|
||||
queryClient.invalidateQueries('items');
|
||||
}
|
||||
});
|
||||
|
||||
const onSubmit: SubmitHandler<ItemUpdate> = async (data) => {
|
||||
try {
|
||||
await editItem(id, data);
|
||||
showToast('Success!', 'Item updated successfully.', 'success');
|
||||
reset();
|
||||
onClose();
|
||||
} catch (err) {
|
||||
const errDetail = (err as ApiError).body.detail;
|
||||
showToast('Something went wrong.', `${errDetail}`, 'error');
|
||||
}
|
||||
mutation.mutate(data)
|
||||
}
|
||||
|
||||
const onCancel = () => {
|
||||
@@ -49,9 +62,10 @@ const EditItem: React.FC<EditItemProps> = ({ id, isOpen, onClose }) => {
|
||||
<ModalHeader>Edit Item</ModalHeader>
|
||||
<ModalCloseButton />
|
||||
<ModalBody pb={6}>
|
||||
<FormControl>
|
||||
<FormControl isInvalid={!!errors.title}>
|
||||
<FormLabel htmlFor='title'>Title</FormLabel>
|
||||
<Input id='title' {...register('title')} type='text' />
|
||||
<Input id='title' {...register('title', { required: 'Title is required' })} type='text' />
|
||||
{errors.title && <FormErrorMessage>{errors.title.message}</FormErrorMessage>}
|
||||
</FormControl>
|
||||
<FormControl mt={4}>
|
||||
<FormLabel htmlFor='description'>Description</FormLabel>
|
||||
@@ -59,7 +73,7 @@ const EditItem: React.FC<EditItemProps> = ({ id, isOpen, onClose }) => {
|
||||
</FormControl>
|
||||
</ModalBody>
|
||||
<ModalFooter gap={3}>
|
||||
<Button bg='ui.main' color='white' _hover={{ opacity: 0.8 }} type='submit' isLoading={isSubmitting}>
|
||||
<Button bg='ui.main' color='white' _hover={{ opacity: 0.8 }} type='submit' isLoading={isSubmitting} isDisabled={!isDirty}>
|
||||
Save
|
||||
</Button>
|
||||
<Button onClick={onCancel}>Cancel</Button>
|
||||
|
@@ -1,10 +1,11 @@
|
||||
import React from 'react';
|
||||
|
||||
import { Box, Button, Container, FormControl, FormLabel, Heading, Input, useColorModeValue } from '@chakra-ui/react';
|
||||
import { Box, Button, Container, FormControl, FormErrorMessage, FormLabel, Heading, Input, useColorModeValue } from '@chakra-ui/react';
|
||||
import { SubmitHandler, useForm } from 'react-hook-form';
|
||||
import { ApiError, UpdatePassword } from '../../client';
|
||||
import { useMutation } from 'react-query';
|
||||
|
||||
import { ApiError, UpdatePassword, UsersService } from '../../client';
|
||||
import useCustomToast from '../../hooks/useCustomToast';
|
||||
import { useUserStore } from '../../store/user-store';
|
||||
|
||||
interface UpdatePasswordForm extends UpdatePassword {
|
||||
confirm_password: string;
|
||||
@@ -13,19 +14,28 @@ interface UpdatePasswordForm extends UpdatePassword {
|
||||
const ChangePassword: React.FC = () => {
|
||||
const color = useColorModeValue('gray.700', 'white');
|
||||
const showToast = useCustomToast();
|
||||
const { register, handleSubmit, reset, formState: { isSubmitting } } = useForm<UpdatePasswordForm>();
|
||||
const { editPassword } = useUserStore();
|
||||
const { register, handleSubmit, reset, getValues, formState: { errors, isSubmitting } } = useForm<UpdatePasswordForm>({
|
||||
mode: 'onBlur',
|
||||
criteriaMode: 'all'
|
||||
});
|
||||
|
||||
const onSubmit: SubmitHandler<UpdatePasswordForm> = async (data) => {
|
||||
try {
|
||||
await editPassword(data);
|
||||
const UpdatePassword = async (data: UpdatePassword) => {
|
||||
await UsersService.updatePasswordMe({ requestBody: data })
|
||||
}
|
||||
|
||||
const mutation = useMutation(UpdatePassword, {
|
||||
onSuccess: () => {
|
||||
showToast('Success!', 'Password updated.', 'success');
|
||||
reset();
|
||||
} catch (err) {
|
||||
const errDetail = (err as ApiError).body.detail;
|
||||
},
|
||||
onError: (err: ApiError) => {
|
||||
const errDetail = err.body.detail;
|
||||
showToast('Something went wrong.', `${errDetail}`, 'error');
|
||||
}
|
||||
})
|
||||
|
||||
const onSubmit: SubmitHandler<UpdatePasswordForm> = async (data) => {
|
||||
mutation.mutate(data);
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -35,17 +45,23 @@ const ChangePassword: React.FC = () => {
|
||||
Change Password
|
||||
</Heading>
|
||||
<Box w={{ 'sm': 'full', 'md': '50%' }}>
|
||||
<FormControl>
|
||||
<FormLabel color={color} htmlFor='currentPassword'>Current password</FormLabel>
|
||||
<Input id='currentPassword' {...register('current_password')} placeholder='••••••••' type='password' />
|
||||
<FormControl isRequired isInvalid={!!errors.current_password}>
|
||||
<FormLabel color={color} htmlFor='current_password'>Current password</FormLabel>
|
||||
<Input id='current_password' {...register('current_password', { required: 'Password is required', minLength: { value: 8, message: 'Password must be at least 8 characters' } })} placeholder='Password' type='password' />
|
||||
{errors.current_password && <FormErrorMessage>{errors.current_password.message}</FormErrorMessage>}
|
||||
</FormControl>
|
||||
<FormControl mt={4}>
|
||||
<FormLabel color={color} htmlFor='newPassword'>New password</FormLabel>
|
||||
<Input id='newPassword' {...register('new_password')} placeholder='••••••••' type='password' />
|
||||
<FormControl mt={4} isRequired isInvalid={!!errors.new_password}>
|
||||
<FormLabel htmlFor='password'>Set Password</FormLabel>
|
||||
<Input id='password' {...register('new_password', { required: 'Password is required', minLength: { value: 8, message: 'Password must be at least 8 characters' } })} placeholder='Password' type='password' />
|
||||
{errors.new_password && <FormErrorMessage>{errors.new_password.message}</FormErrorMessage>}
|
||||
</FormControl>
|
||||
<FormControl mt={4}>
|
||||
<FormLabel color={color} htmlFor='confirmPassword'>Confirm new password</FormLabel>
|
||||
<Input id='confirmPassword' {...register('confirm_password')} placeholder='••••••••' type='password' />
|
||||
<FormControl mt={4} isRequired isInvalid={!!errors.confirm_password}>
|
||||
<FormLabel htmlFor='confirm_password'>Confirm Password</FormLabel>
|
||||
<Input id='confirm_password' {...register('confirm_password', {
|
||||
required: 'Please confirm your password',
|
||||
validate: value => value === getValues().new_password || 'The passwords do not match'
|
||||
})} placeholder='Password' type='password' />
|
||||
{errors.confirm_password && <FormErrorMessage>{errors.confirm_password.message}</FormErrorMessage>}
|
||||
</FormControl>
|
||||
<Button bg='ui.main' color='white' _hover={{ opacity: 0.8 }} mt={4} type='submit' isLoading={isSubmitting}>
|
||||
Save
|
||||
|
@@ -2,10 +2,11 @@ import React from 'react';
|
||||
|
||||
import { AlertDialog, AlertDialogBody, AlertDialogContent, AlertDialogFooter, AlertDialogHeader, AlertDialogOverlay, Button } from '@chakra-ui/react';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { ApiError } from '../../client';
|
||||
import { useMutation, useQueryClient } from 'react-query';
|
||||
|
||||
import { ApiError, UserOut, UsersService } from '../../client';
|
||||
import useAuth from '../../hooks/useAuth';
|
||||
import useCustomToast from '../../hooks/useCustomToast';
|
||||
import { useUserStore } from '../../store/user-store';
|
||||
|
||||
interface DeleteProps {
|
||||
isOpen: boolean;
|
||||
@@ -13,22 +14,35 @@ interface DeleteProps {
|
||||
}
|
||||
|
||||
const DeleteConfirmation: React.FC<DeleteProps> = ({ isOpen, onClose }) => {
|
||||
const queryClient = useQueryClient();
|
||||
const showToast = useCustomToast();
|
||||
const cancelRef = React.useRef<HTMLButtonElement | null>(null);
|
||||
const { handleSubmit, formState: { isSubmitting } } = useForm();
|
||||
const { user, deleteUser } = useUserStore();
|
||||
const currentUser = queryClient.getQueryData<UserOut>('currentUser');
|
||||
const { logout } = useAuth();
|
||||
|
||||
const onSubmit = async () => {
|
||||
try {
|
||||
await deleteUser(user!.id);
|
||||
const deleteCurrentUser = async (id: number) => {
|
||||
await UsersService.deleteUser({ userId: id });
|
||||
}
|
||||
|
||||
const mutation = useMutation(deleteCurrentUser, {
|
||||
onSuccess: () => {
|
||||
showToast('Success', 'Your account has been successfully deleted.', 'success');
|
||||
logout();
|
||||
onClose();
|
||||
showToast('Success', 'Your account has been successfully deleted.', 'success');
|
||||
} catch (err) {
|
||||
const errDetail = (err as ApiError).body.detail;
|
||||
},
|
||||
onError: (err: ApiError) => {
|
||||
const errDetail = err.body.detail;
|
||||
showToast('Something went wrong.', `${errDetail}`, 'error');
|
||||
},
|
||||
onSettled: () => {
|
||||
queryClient.invalidateQueries('currentUser');
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
const onSubmit = async () => {
|
||||
mutation.mutate(currentUser!.id);
|
||||
}
|
||||
|
||||
return (
|
||||
|
@@ -1,34 +1,50 @@
|
||||
import React, { useState } from 'react';
|
||||
|
||||
import { Box, Button, Container, Flex, FormControl, FormLabel, Heading, Input, Text, useColorModeValue } from '@chakra-ui/react';
|
||||
|
||||
import { Box, Button, Container, Flex, FormControl, FormErrorMessage, FormLabel, Heading, Input, Text, useColorModeValue } from '@chakra-ui/react';
|
||||
import { SubmitHandler, useForm } from 'react-hook-form';
|
||||
import { ApiError, UserOut, UserUpdateMe } from '../../client';
|
||||
import { useMutation, useQueryClient } from 'react-query';
|
||||
|
||||
import { ApiError, UserOut, UserUpdateMe, UsersService } from '../../client';
|
||||
import useAuth from '../../hooks/useAuth';
|
||||
import useCustomToast from '../../hooks/useCustomToast';
|
||||
import { useUserStore } from '../../store/user-store';
|
||||
import { useUsersStore } from '../../store/users-store';
|
||||
|
||||
const UserInformation: React.FC = () => {
|
||||
const queryClient = useQueryClient();
|
||||
const color = useColorModeValue('gray.700', 'white');
|
||||
const showToast = useCustomToast();
|
||||
const [editMode, setEditMode] = useState(false);
|
||||
const { register, handleSubmit, reset, formState: { isSubmitting } } = useForm<UserOut>();
|
||||
const { user, editUser } = useUserStore();
|
||||
const { getUsers } = useUsersStore();
|
||||
const { user: currentUser } = useAuth();
|
||||
const { register, handleSubmit, reset, formState: { isSubmitting, errors, isDirty } } = useForm<UserOut>({
|
||||
mode: 'onBlur', criteriaMode: 'all', defaultValues: {
|
||||
full_name: currentUser?.full_name,
|
||||
email: currentUser?.email
|
||||
}
|
||||
})
|
||||
|
||||
const toggleEditMode = () => {
|
||||
setEditMode(!editMode);
|
||||
};
|
||||
|
||||
const onSubmit: SubmitHandler<UserUpdateMe> = async (data) => {
|
||||
try {
|
||||
await editUser(data);
|
||||
await getUsers()
|
||||
const updateInfo = async (data: UserUpdateMe) => {
|
||||
await UsersService.updateUserMe({ requestBody: data })
|
||||
}
|
||||
|
||||
const mutation = useMutation(updateInfo, {
|
||||
onSuccess: () => {
|
||||
showToast('Success!', 'User updated successfully.', 'success');
|
||||
} catch (err) {
|
||||
const errDetail = (err as ApiError).body.detail;
|
||||
},
|
||||
onError: (err: ApiError) => {
|
||||
const errDetail = err.body.detail;
|
||||
showToast('Something went wrong.', `${errDetail}`, 'error');
|
||||
},
|
||||
onSettled: () => {
|
||||
queryClient.invalidateQueries('users');
|
||||
queryClient.invalidateQueries('currentUser');
|
||||
}
|
||||
});
|
||||
|
||||
const onSubmit: SubmitHandler<UserUpdateMe> = async (data) => {
|
||||
mutation.mutate(data)
|
||||
}
|
||||
|
||||
const onCancel = () => {
|
||||
@@ -47,21 +63,22 @@ const UserInformation: React.FC = () => {
|
||||
<FormLabel color={color} htmlFor='name'>Full name</FormLabel>
|
||||
{
|
||||
editMode ?
|
||||
<Input id='name' {...register('full_name')} defaultValue={user?.full_name} type='text' size='md' /> :
|
||||
<Input id='name' {...register('full_name', { maxLength: 30 })} type='text' size='md' /> :
|
||||
<Text size='md' py={2}>
|
||||
{user?.full_name || 'N/A'}
|
||||
{currentUser?.full_name || 'N/A'}
|
||||
</Text>
|
||||
}
|
||||
</FormControl>
|
||||
<FormControl mt={4}>
|
||||
<FormControl mt={4} isInvalid={!!errors.email}>
|
||||
<FormLabel color={color} htmlFor='email'>Email</FormLabel>
|
||||
{
|
||||
editMode ?
|
||||
<Input id='email' {...register('email')} defaultValue={user?.email} type='text' size='md' /> :
|
||||
<Input id='email' {...register('email', { required: 'Email is required', pattern: { value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i, message: 'Invalid email address' } })} type='text' size='md' /> :
|
||||
<Text size='md' py={2}>
|
||||
{user?.email || 'N/A'}
|
||||
{currentUser!.email}
|
||||
</Text>
|
||||
}
|
||||
{errors.email && <FormErrorMessage>{errors.email.message}</FormErrorMessage>}
|
||||
</FormControl>
|
||||
<Flex mt={4} gap={3}>
|
||||
<Button
|
||||
@@ -71,6 +88,7 @@ const UserInformation: React.FC = () => {
|
||||
onClick={toggleEditMode}
|
||||
type={editMode ? 'button' : 'submit'}
|
||||
isLoading={editMode ? isSubmitting : false}
|
||||
isDisabled={editMode ? !isDirty : false}
|
||||
>
|
||||
{editMode ? 'Save' : 'Edit'}
|
||||
</Button>
|
||||
|
Reference in New Issue
Block a user