Skip to content

Commit

Permalink
feat: [Contracts] - approve contract
Browse files Browse the repository at this point in the history
  • Loading branch information
luciatugui committed Sep 25, 2024
1 parent fe0b3a9 commit 1f6bf24
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
4 changes: 4 additions & 0 deletions frontend/src/assets/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,10 @@
"success": "The contract has been successfully rejected.",
"error": "An error occurred while rejecting the contract."
}
},
"approve": {
"success": "The contract has been successfully approved.",
"error": "An error occurred while approving the contract."
}
},
"doc_templates": {
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/assets/locales/ro/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,10 @@
"success": "Contractul a fost respins cu succes.",
"error": "A apărut o eroare la respingerea contractului."
}
},
"approve": {
"success": "Contractul a fost aprobat cu succes.",
"error": "A apărut o eroare la aprobarea contractului."
}
},
"doc_templates": {
Expand Down
38 changes: 33 additions & 5 deletions frontend/src/components/ContractInfoContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import {
DocumentContractStatus,
} from '../common/enums/document-contract-status.enum';
import Button from './Button';
import { useApproveDocumentContractMutation } from '../services/document-contracts/document-contracts.service';
import Spinner from './Spinner';
import { useErrorToast, useSuccessToast } from '../hooks/useToast';
import { useQueryClient } from 'react-query';

export const ContractInfoContent = ({
contract,
Expand All @@ -27,6 +31,10 @@ export const ContractInfoContent = ({
}) => {
const { t } = useTranslation('document_contract');
const navigate = useNavigate();
const queryClient = useQueryClient();

const { mutate: approveDocumentContract, isLoading: isLoadingApproveDocumentContract } =
useApproveDocumentContractMutation();

const onVolunteerClick = () => {
if (contract) navigate(`/volunteers/${contract.volunteerId}`);
Expand All @@ -41,7 +49,21 @@ export const ContractInfoContent = ({
navigate(`/documents/templates/${contract.documentTemplateId}`);
};

//todo: reject contract
const handleApproveDocumentContract = () => {
approveDocumentContract(contract.documentId, {
onSuccess: () => {
useSuccessToast(t('approve.success'));
},
onError: () => {
useErrorToast(t('approve.error'));
},
onSettled: () => {
queryClient.invalidateQueries({ queryKey: ['document-contract', contract.documentId] });
queryClient.invalidateQueries({ queryKey: ['documents-contracts'] });
},
});
};

//todo: download contract

const approvedStatus = useMemo(() => {
Expand Down Expand Up @@ -184,11 +206,16 @@ export const ContractInfoContent = ({
<Button
label={t('contract.actions.send_to_signing')}
className="btn-primary"
onClick={() => {
//todo: trimite spre semnare
}}
onClick={handleApproveDocumentContract}
aria-label={`${t('contract.actions.sign')}`}
icon={<CheckIcon className="h-5 w-5" />}
icon={
isLoadingApproveDocumentContract ? (
<Spinner className="h-5 w-5 fill-black" />
) : (
<CheckIcon className="h-5 w-5" />
)
}
disabled={isLoadingApproveDocumentContract}
type="button"
/>
<Button
Expand All @@ -198,6 +225,7 @@ export const ContractInfoContent = ({
aria-label={`${t('contract.actions.reject')}`}
icon={<XMarkIcon className="h-5 w-5" />}
type="button"
disabled={isLoadingApproveDocumentContract}
/>
</footer>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export const signDocumentContract = (id: string, body: ISignDocumentContractBody
return API.patch(`/documents/contracts/${id}/sign`, body).then((res) => res.data);
};

export const approveDocumentContract = (id: string) => {
return API.patch(`/documents/contracts/${id}/approve`).then((res) => res.data);
};

export const rejectDocumentContract = (id: string, body: IRejectDocumentContractBody) => {
return API.patch(`/documents/contracts/${id}/reject`, body).then((res) => res.data);
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useMutation, useQuery } from 'react-query';
import {
addDocumentContract,
approveDocumentContract,
getDocumentContract,
getDocumentsContracts,
rejectDocumentContract,
Expand Down Expand Up @@ -84,6 +85,14 @@ export const useSignDocumentContractMutation = () => {
);
};

export const useApproveDocumentContractMutation = () => {
return useMutation((id: string) => approveDocumentContract(id), {
onError: (error) => {
console.log(`⛔️ ERROR IN APPROVE DOCUMENT CONTRACT ⛔️`, error);
},
});
};

export const useRejectcontractMutation = () => {
return useMutation(
({ id, payload }: { id: string; payload: IRejectDocumentContractBody }) =>
Expand Down

0 comments on commit 1f6bf24

Please sign in to comment.