feat(backoffice): add Toast

This commit is contained in:
Hafid Nur
2025-04-05 08:40:19 +07:00
parent 07f1fb0208
commit 5bdf8f5a4d
5 changed files with 36 additions and 17 deletions
+3 -6
View File
@@ -5,7 +5,7 @@ import {
usePostLogin, usePostLogin,
} from '@imphnen-frontend-service/service'; } from '@imphnen-frontend-service/service';
import { zodResolver } from '@hookform/resolvers/zod'; import { zodResolver } from '@hookform/resolvers/zod';
import { useNavigate } from 'react-router-dom'; import { toast } from 'sonner';
export const useLogin = () => { export const useLogin = () => {
const postLogin = usePostLogin(); const postLogin = usePostLogin();
@@ -13,14 +13,11 @@ export const useLogin = () => {
resolver: zodResolver(authLoginSchema), resolver: zodResolver(authLoginSchema),
mode: 'all', mode: 'all',
}); });
const navigate = useNavigate();
const onSubmit = form.handleSubmit((data) => { const onSubmit = form.handleSubmit((data) => {
postLogin.mutate(data, { postLogin.mutate(data, {
onSuccess: () => { onSuccess: () => toast.success("Login sukses"),
console.log('Success Login'); onError: (error) => toast.error(error.message),
navigate('/dashboard');
},
}); });
}); });
@@ -6,7 +6,7 @@ import { useItem, useConfirmItem } from '../_hook/use-item';
interface IModalAddItem { interface IModalAddItem {
isOpen: boolean; isOpen: boolean;
onClose: () => void; onClose: () => void;
handleAddItem?: () => void; handleAddItem?: () => Promise<boolean>;
currentStep?: number; currentStep?: number;
nextStep: () => void; nextStep: () => void;
prevStep: () => void; prevStep: () => void;
@@ -109,7 +109,7 @@ const StepOne = ({ nextStep }: IStepOneProps) => {
interface IStepTwoProps { interface IStepTwoProps {
onClose: () => void; onClose: () => void;
handleAddItem?: () => void; handleAddItem?: () => Promise<boolean>;
resetStep: () => void; resetStep: () => void;
} }
@@ -117,7 +117,11 @@ const StepTwo = ({ onClose, handleAddItem, resetStep }: IStepTwoProps) => {
const { onConfirm, onCancel } = useConfirmItem( const { onConfirm, onCancel } = useConfirmItem(
onClose, onClose,
resetStep, resetStep,
handleAddItem handleAddItem,
{
success: 'Item ditambahkan ke gacha item',
error: 'Item gagal ditambahkan ke gacha item',
}
); );
return ( return (
@@ -6,7 +6,7 @@ import { ControlledInputField } from '@imphnen-frontend-service/ui/organisms';
interface IModalUpdateItem { interface IModalUpdateItem {
isOpen: boolean; isOpen: boolean;
onClose: () => void; onClose: () => void;
handleUpdateItem?: () => void; handleUpdateItem?: () => Promise<boolean>;
currentStep?: number; currentStep?: number;
nextStep: () => void; nextStep: () => void;
prevStep: () => void; prevStep: () => void;
@@ -117,7 +117,7 @@ const StepOne = ({ nextStep }: IStepOneProps) => {
interface IStepTwoProps { interface IStepTwoProps {
onClose: () => void; onClose: () => void;
handleUpdateItem?: () => void; handleUpdateItem?: () => Promise<boolean>;
resetStep: () => void; resetStep: () => void;
} }
@@ -125,7 +125,11 @@ const StepTwo = ({ onClose, handleUpdateItem, resetStep }: IStepTwoProps) => {
const { onConfirm, onCancel } = useConfirmItem( const { onConfirm, onCancel } = useConfirmItem(
onClose, onClose,
resetStep, resetStep,
handleUpdateItem handleUpdateItem,
{
success: 'Item ditambahkan ke gacha item',
error: 'Item gagal ditambahkan ke gacha item',
}
); );
return ( return (
@@ -4,6 +4,7 @@ import {
gachaRollItemSchema, gachaRollItemSchema,
TGachaRollItem TGachaRollItem
} from '@imphnen-frontend-service/service'; } from '@imphnen-frontend-service/service';
import { toast } from 'sonner';
export const useItem = ( export const useItem = (
nextStep: () => void, nextStep: () => void,
@@ -29,12 +30,23 @@ export const useItem = (
export const useConfirmItem = ( export const useConfirmItem = (
onClose: () => void, onClose: () => void,
resetStep: () => void, resetStep: () => void,
actionFunction?: () => void, actionFunction?: () => Promise<boolean>,
messages?: {
success?: string;
error?: string;
}
) => { ) => {
const onConfirm = () => { const onConfirm = async () => {
actionFunction?.(); try {
onClose(); // const result = await actionFunction?.();
resetStep(); // result ? toast.success(messages?.success) : toast.error(messages?.error);
toast.success(messages?.success);
onClose();
resetStep();
} catch (error) {
console.log(error);
toast.error(messages?.error);
}
}; };
const onCancel = () => { const onCancel = () => {
+2
View File
@@ -8,6 +8,7 @@ import {
convertPagesToRoute, convertPagesToRoute,
QueryProvider, QueryProvider,
} from '@imphnen-frontend-service/utils'; } from '@imphnen-frontend-service/utils';
import { Toaster } from 'sonner';
import './index.css'; import './index.css';
const files = import.meta.glob('./app/**/*(page|layout).tsx'); const files = import.meta.glob('./app/**/*(page|layout).tsx');
@@ -34,6 +35,7 @@ if (!rootElement) throw new Error('Failed to find the root element');
createRoot(rootElement).render( createRoot(rootElement).render(
<StrictMode> <StrictMode>
<QueryProvider> <QueryProvider>
<Toaster position="top-right" />
<RouterProvider router={router} /> <RouterProvider router={router} />
</QueryProvider> </QueryProvider>
</StrictMode> </StrictMode>