Skip to content

Instantly share code, notes, and snippets.

@alexmkio
Created November 18, 2025 21:29
Show Gist options
  • Select an option

  • Save alexmkio/2c6d249c27128a6c61218afc3f511846 to your computer and use it in GitHub Desktop.

Select an option

Save alexmkio/2c6d249c27128a6c61218afc3f511846 to your computer and use it in GitHub Desktop.
import { useMemo, useState } from 'react';
import { UserFormState } from '@components/settings/account-management/UserCard';
import { ApiError } from '@generated/api/core/ApiError';
import { AddClientNoteRequestModel } from '@generated/api/models/AddClientNoteRequestModel';
import { useCreateNote } from './mutation/useCreateNote';
export type TypeAddNote = {
setNewNoteFormState: () => void;
showNoteSuccess: boolean;
closeNoteModal: boolean;
closeNewNoteModal: () => void;
keepEditingNote: () => void;
addNoteOnSubmit: (note: AddClientNoteRequestModel) => Promise<void>;
resetNewNoteState: () => void;
noteFormFieldErrors: ApiError;
};
export const useAddNote = (clientId: string) => {
const [newNoteFormState, setNewNoteFormState] = useState({
isDirty: false,
isSubmitSuccessful: false,
} as UserFormState);
const [showNoteSuccess, setShowNoteSuccess] = useState(false);
useMemo(() => {
if (newNoteFormState.isSubmitSuccessful) {
setShowNoteSuccess(true);
}
}, [newNoteFormState.isSubmitSuccessful, setShowNoteSuccess]);
const [closeNoteModal, setCloseNoteModal] = useState(true);
const closeNewNoteModal = (e: React.MouseEvent<HTMLButtonElement>) => {
if (newNoteFormState.isDirty && closeNoteModal) {
e.preventDefault();
setCloseNoteModal(!closeNoteModal);
} else resetNewNoteState();
};
const keepEditingNote = (e: React.MouseEvent<HTMLButtonElement>) => {
e.preventDefault();
setCloseNoteModal(true);
};
const [requestNewNoteSent, setRequestNewNoteSent] = useState(false);
const newNote = useCreateNote(clientId);
const addNoteOnSubmit = async (note: AddClientNoteRequestModel) => {
await newNote.mutateAsync(note, {
onError: () => {
setRequestNewNoteSent(false);
},
onSuccess: () => {
setRequestNewNoteSent(!requestNewNoteSent);
},
});
setRequestNewNoteSent(!requestNewNoteSent);
};
const resetNewNoteState = () => {
setCloseNoteModal(true);
setRequestNewNoteSent(false);
setShowNoteSuccess(false);
};
return {
setNewNoteFormState,
showNoteSuccess,
closeNoteModal,
closeNewNoteModal,
keepEditingNote,
addNoteOnSubmit,
resetNewNoteState,
} as TypeAddNote;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment