Adjust product state transitions

This commit is contained in:
2026-05-08 10:26:43 +07:00
parent 0aa5227943
commit 85406b0bb2
4 changed files with 8 additions and 17 deletions

View File

@ -1197,7 +1197,7 @@ function EditProductPageInner() {
}
}
function buildPayload(state?: "DRAFT" | "PUBLISHED") {
function buildPayload(state?: "DRAFT" | "REVIEW") {
if (!form) return null;
const resolvedState = state ?? "DRAFT";
const base = {
@ -1312,7 +1312,7 @@ function EditProductPageInner() {
setErrorLogCopied(false);
try {
const payload = buildPayload("PUBLISHED");
const payload = buildPayload("REVIEW");
const res = await fetch("/api/products/create", {
method: "POST",
@ -1343,7 +1343,7 @@ function EditProductPageInner() {
setErrorLogCopied(false);
try {
const payload = buildPayload();
const payload = buildPayload("REVIEW");
const res = await fetch(`/api/products/${params.productId}`, {
method: "PUT",
@ -1356,16 +1356,6 @@ function EditProductPageInner() {
throw new Error(result?.responseDesc || "Gagal menyimpan produk");
}
const reviewRes = await fetch(`/api/products/submit-review/${params.productId}`, {
method: "POST",
headers: { "x-auth-token": getToken() },
});
const reviewResult = await reviewRes.json();
if (!reviewRes.ok) {
setErrorLog({ request: { submitReview: params.productId }, response: reviewResult });
throw new Error(reviewResult?.responseDesc || "Gagal mengirim produk ke review");
}
setSaveSuccess(true);
setTimeout(() => router.back(), 1500);
} catch (err) {

View File

@ -97,7 +97,7 @@ export default function ProductReviewPage() {
async function handleSubmitForReview() {
setErrorLogCopied(false);
try {
await submit("PUBLISHED");
await submit("REVIEW");
router.push("/products/new/submitted");
} catch {
// error is set by the hook

View File

@ -36,12 +36,13 @@ export async function POST(
// isNew=false: PUT /product/accept/{id}
url = `${API_URL}/api/v1.0/product/accept/${productId}`;
method = isNew ? "POST" : "PUT";
body = { state: "PUBLISHED" };
} else {
// isNew=true: POST /product/reject/{id}
// isNew=false: PUT /product/reject/{id}
url = `${API_URL}/api/v1.0/product/reject/${productId}`;
method = isNew ? "POST" : "PUT";
if (reason) body = { reason };
body = reason ? { reason, state: "REJECTED" } : { state: "REJECTED" };
}
const res = await fetch(url, {

View File

@ -14,7 +14,7 @@ function toNumber(value: string) {
return Number.isFinite(parsed) ? parsed : 0;
}
export function buildProductPayload(draft: ProductDraftState, state: "DRAFT" | "PUBLISHED") {
export function buildProductPayload(draft: ProductDraftState, state: "DRAFT" | "REVIEW") {
return {
subCategory: draft.subCategoryId ? { id: draft.subCategoryId } : undefined,
name: draft.name,
@ -105,7 +105,7 @@ export function useProductSubmit() {
const [error, setError] = useState("");
const [errorLog, setErrorLog] = useState<{ request: unknown; response: unknown } | null>(null);
async function submit(state: "DRAFT" | "PUBLISHED"): Promise<void> {
async function submit(state: "DRAFT" | "REVIEW"): Promise<void> {
setSubmitting(true);
setError("");
setErrorLog(null);