Skip to content

Commit

Permalink
#112: update sync with backend of confirm and cancel for user
Browse files Browse the repository at this point in the history
  • Loading branch information
an2508374 committed Jan 13, 2024
1 parent a3136d9 commit 192eb9c
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 62 deletions.
16 changes: 16 additions & 0 deletions SwiftParcel.API.Gateway/src/SwiftParcel.API.Gateway/ntrada.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,22 @@ modules:
auth: true
bind:
- orderId:{orderId}

- upstream: /{orderId}/confirm
method: POST
use: downstream
downstream: orders-service/orders/{orderId}/confirm
auth: true
bind:
- orderId:{orderId}

- upstream: /{orderId}/cancel
method: DELETE
use: downstream
downstream: orders-service/orders/{orderId}/cancel
auth: true
bind:
- orderId:{orderId}

services:
orders-service:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import React from "react";
import dateFromUTCToLocal from "../../parsing/dateFromUTCToLocal";
import formatOfferStatus from "../../parsing/formatOfferStatus";
import { approvePendingOffer, cancelPendingOffer } from "../../../utils/api";
import { confirmOrder, cancelOrder } from "../../../utils/api";

interface OrderDetailsModalProps {
show: boolean;
Expand Down Expand Up @@ -66,7 +66,7 @@ import { approvePendingOffer, cancelPendingOffer } from "../../../utils/api";
</div>
);

const StatusDetailsSection = ({ detailsData }) => (
const StatusDetailsSection = ({ detailsData, confirm, cancel, finalized, refresh }) => (
<div>
<LabelsWithBorder
idA="status"
Expand All @@ -75,12 +75,27 @@ import { approvePendingOffer, cancelPendingOffer } from "../../../utils/api";
valueB={formatOfferStatus(detailsData.order.status)}
/>
{ detailsData.order.status === "approved" ?
<LabelsWithBorder
idA="request-valid-to"
valueA="Request valid to:"
idB="request-valid-to-value"
valueB={formatDate(detailsData.order.requestValidTo)}
/>
<div>
<LabelsWithBorder
idA="request-valid-to"
valueA="Request valid to:"
idB="request-valid-to-value"
valueB={formatDate(detailsData.order.requestValidTo)}
/>

{ (new Date() < new Date(detailsData.order.requestValidTo)) ? (
<div className="mb-4 border-b border-gray-200 pb-1 grid grid-cols-1 md:grid-cols-2 gap-4">
<Button onClick={() => confirm()}>Confirm</Button>
<Button onClick={() => cancel()}>Cancel</Button>
</div>
) : null }

{ finalized ? (
<div className="mb-4 pb-1 grid grid-cols-1 md:grid-cols-1 gap-4">
<Button onClick={() => refresh()}>Refresh page</Button>
</div>
) : null }
</div>
: null }
{ detailsData.order.status === "cancelled" ?
<LabelsWithBorder
Expand Down Expand Up @@ -186,8 +201,6 @@ import { approvePendingOffer, cancelPendingOffer } from "../../../utils/api";
export function OrderDetailsModal(props: OrderDetailsModalProps) {
const close = () => {
setReason("");
setAccepted(false);
setRejected(false);
setFinalized(false);
props.setShow(false);
};
Expand All @@ -197,19 +210,17 @@ import { approvePendingOffer, cancelPendingOffer } from "../../../utils/api";
close();
};

const [accepted, setAccepted] = React.useState<any>(false);
const [rejected, setRejected] = React.useState<any>(false);
const [finalized, setFinalized] = React.useState<any>(false);

const [reason, setReason] = React.useState<any>("");

const accept = () => {
approvePendingOffer(props.order.id);
const confirm = () => {
confirmOrder(props.order.id, props.order.company);
setFinalized(true);
};

const reject = (reason: string) => {
cancelPendingOffer(props.order.id, reason);
const cancel = () => {
cancelOrder(props.order.id, props.order.company);
setFinalized(true);
};

Expand Down Expand Up @@ -239,6 +250,10 @@ import { approvePendingOffer, cancelPendingOffer } from "../../../utils/api";
<SectionTitle title="Status info" />
<StatusDetailsSection
detailsData={props}
confirm={confirm}
cancel={cancel}
finalized={finalized}
refresh={refresh}
/>

<SectionTitle title="Buyer info" />
Expand All @@ -252,40 +267,6 @@ import { approvePendingOffer, cancelPendingOffer } from "../../../utils/api";
detailsData={props}
/>

<div className="mb-4 border-b border-gray-200 pb-1 grid grid-cols-1 md:grid-cols-2 gap-4">
<Button onClick={() => {setAccepted(true); setRejected(false);}}>Accept</Button>
<Button onClick={() => {setAccepted(false); setRejected(true);}}>Reject</Button>
</div>

{ (accepted && !finalized) ? (
<div className="mb-4 pb-1 grid grid-cols-1 md:grid-cols-1 gap-4">
<Button onClick={() => accept()}>Confirm acceptation</Button>
</div>
) : null }

{ (rejected && !finalized) ? (
<div className="mb-4 pb-1 grid grid-cols-1 md:grid-cols-1 gap-4">
<Label htmlFor="reason-of-rejection" className="mb-2 block text-sm font-medium text-gray-700">
Input reason of rejection:
</Label>
<TextInput
id="reason-of-rejection"
type="string"
lang="en"
value={reason}
onChange={(e) => setReason(e.target.value)}
className={`border-gray-300 focus:ring-blue-500 focus:border-blue-500 block w-full shadow-sm sm:text-sm rounded-md`}
/>
<Button onClick={() => reject(reason)}>Confirm rejection</Button>
</div>
) : null }

{ finalized ? (
<div className="mb-4 pb-1 grid grid-cols-1 md:grid-cols-1 gap-4">
<Button onClick={() => refresh()}>Refresh page</Button>
</div>
) : null }

<div style={{ marginBottom: '40px' }}></div>

</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ export default function CreateInquiry() {
})

.catch((err) => {
setError(err?.response?.data?.message || "Something went wrong!");
setError(err?.response?.data?.reason || "Something went wrong!");
})
.finally(() => {
setInquiryLoading(false);
Expand Down
18 changes: 9 additions & 9 deletions SwiftParcel.Web/frontend/src/pages/offers/offersUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,34 @@ export default function Offers() {
const [userData, setUserData] = React.useState(null);

React.useEffect(() => {
getOffers(location.state.parcelId)
getCustomerData(getUserIdFromStorage())
.then((res) => {
if (res.status === 200) {
setData(res?.data);
setUserData(res?.data);
} else {
throw new Error();
}
})
.catch((err) => {
setData(null);
setUserData(null);
})
.finally(() => {
setLoadingOffers(false);
});
}, [page]);

React.useEffect(() => {
getCustomerData(getUserIdFromStorage())
getOffers(location.state.parcelId)
.then((res) => {
if (res.status === 200) {
setUserData(res?.data);
setData(res?.data);
} else {
throw new Error();
}
})
.catch((err) => {
setUserData(null);
setData(null);
})
.finally(() => {
setLoadingOffers(false);
});
}, [page]);

const onPageChange = (page: number) => {
Expand Down
4 changes: 2 additions & 2 deletions SwiftParcel.Web/frontend/src/pages/register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ export default function Register() {
.then((res) => {
setCustomerId(res);
setSuccess(
res?.data?.message || "Registration successful! Please complete data referring to you below."
"Registration successful! Please complete data referring to you below."
);
})
.catch((err) => {
setError(err?.response?.data?.message || "Something went wrong during registration!");
setError(err?.response?.data?.reason || "Something went wrong during registration!");
})
.finally(() => {
setRegisterLoading(false);
Expand Down
67 changes: 67 additions & 0 deletions SwiftParcel.Web/frontend/src/utils/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,73 @@ export const cancelPendingOffer = async (orderId : string, reason: string) => {
}
};

export const confirmOrder = async (
orderId: string,
company: string
) => {
try {

const payload = {
OrderId: orderId,
Company: company
};

console.log("Request payload:", payload);

console.log("JSON being sent:", JSON.parse(JSON.stringify(payload)));

const response = await api.post(`/orders/${orderId}/confirm`, JSON.parse(JSON.stringify(payload)), {
headers: {
//'Authorization': `${userInfo.accessToken}`,
'Content-Type': 'application/json'
}
})

return response.data;

} catch (orderError) {
if (axios.isAxiosError(orderError) && orderError.response) {
console.error('Error status:', orderError.response.status);
console.error('Error data:', orderError.response.data);
console.error('Error during order confirmation:', orderError.message);
} else {
console.error('Error during order confirmation:', orderError);
}
throw orderError;
}
};

export const cancelOrder = async (
orderId: string,
company: string
) => {
try {

const payload = {
OrderId: orderId,
Company: company
};

console.log("Request payload:", payload);

console.log("JSON being sent:", JSON.parse(JSON.stringify(payload)));

const response = await api.delete(`/orders/${orderId}/cancel`, JSON.parse(JSON.stringify(payload)))

return response.data;

} catch (orderError) {
if (axios.isAxiosError(orderError) && orderError.response) {
console.error('Error status:', orderError.response.status);
console.error('Error data:', orderError.response.data);
console.error('Error during order cancellation:', orderError.message);
} else {
console.error('Error during order cancellation:', orderError);
}
throw orderError;
}
};

export const getCustomerData = async (customerId: string) => {
try {
const response = await api.get(`/customers/${customerId}`, { headers: getAuthHeader() });
Expand Down

0 comments on commit 192eb9c

Please sign in to comment.