Adjust product state transitions
This commit is contained in:
@ -1197,7 +1197,7 @@ function EditProductPageInner() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildPayload(state?: "DRAFT" | "PUBLISHED") {
|
function buildPayload(state?: "DRAFT" | "REVIEW") {
|
||||||
if (!form) return null;
|
if (!form) return null;
|
||||||
const resolvedState = state ?? "DRAFT";
|
const resolvedState = state ?? "DRAFT";
|
||||||
const base = {
|
const base = {
|
||||||
@ -1312,7 +1312,7 @@ function EditProductPageInner() {
|
|||||||
setErrorLogCopied(false);
|
setErrorLogCopied(false);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const payload = buildPayload("PUBLISHED");
|
const payload = buildPayload("REVIEW");
|
||||||
|
|
||||||
const res = await fetch("/api/products/create", {
|
const res = await fetch("/api/products/create", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
@ -1343,7 +1343,7 @@ function EditProductPageInner() {
|
|||||||
setErrorLogCopied(false);
|
setErrorLogCopied(false);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const payload = buildPayload();
|
const payload = buildPayload("REVIEW");
|
||||||
|
|
||||||
const res = await fetch(`/api/products/${params.productId}`, {
|
const res = await fetch(`/api/products/${params.productId}`, {
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
@ -1356,16 +1356,6 @@ function EditProductPageInner() {
|
|||||||
throw new Error(result?.responseDesc || "Gagal menyimpan produk");
|
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);
|
setSaveSuccess(true);
|
||||||
setTimeout(() => router.back(), 1500);
|
setTimeout(() => router.back(), 1500);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@ -97,7 +97,7 @@ export default function ProductReviewPage() {
|
|||||||
async function handleSubmitForReview() {
|
async function handleSubmitForReview() {
|
||||||
setErrorLogCopied(false);
|
setErrorLogCopied(false);
|
||||||
try {
|
try {
|
||||||
await submit("PUBLISHED");
|
await submit("REVIEW");
|
||||||
router.push("/products/new/submitted");
|
router.push("/products/new/submitted");
|
||||||
} catch {
|
} catch {
|
||||||
// error is set by the hook
|
// error is set by the hook
|
||||||
|
|||||||
@ -36,12 +36,13 @@ export async function POST(
|
|||||||
// isNew=false: PUT /product/accept/{id}
|
// isNew=false: PUT /product/accept/{id}
|
||||||
url = `${API_URL}/api/v1.0/product/accept/${productId}`;
|
url = `${API_URL}/api/v1.0/product/accept/${productId}`;
|
||||||
method = isNew ? "POST" : "PUT";
|
method = isNew ? "POST" : "PUT";
|
||||||
|
body = { state: "PUBLISHED" };
|
||||||
} else {
|
} else {
|
||||||
// isNew=true: POST /product/reject/{id}
|
// isNew=true: POST /product/reject/{id}
|
||||||
// isNew=false: PUT /product/reject/{id}
|
// isNew=false: PUT /product/reject/{id}
|
||||||
url = `${API_URL}/api/v1.0/product/reject/${productId}`;
|
url = `${API_URL}/api/v1.0/product/reject/${productId}`;
|
||||||
method = isNew ? "POST" : "PUT";
|
method = isNew ? "POST" : "PUT";
|
||||||
if (reason) body = { reason };
|
body = reason ? { reason, state: "REJECTED" } : { state: "REJECTED" };
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = await fetch(url, {
|
const res = await fetch(url, {
|
||||||
|
|||||||
@ -14,7 +14,7 @@ function toNumber(value: string) {
|
|||||||
return Number.isFinite(parsed) ? parsed : 0;
|
return Number.isFinite(parsed) ? parsed : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function buildProductPayload(draft: ProductDraftState, state: "DRAFT" | "PUBLISHED") {
|
export function buildProductPayload(draft: ProductDraftState, state: "DRAFT" | "REVIEW") {
|
||||||
return {
|
return {
|
||||||
subCategory: draft.subCategoryId ? { id: draft.subCategoryId } : undefined,
|
subCategory: draft.subCategoryId ? { id: draft.subCategoryId } : undefined,
|
||||||
name: draft.name,
|
name: draft.name,
|
||||||
@ -105,7 +105,7 @@ export function useProductSubmit() {
|
|||||||
const [error, setError] = useState("");
|
const [error, setError] = useState("");
|
||||||
const [errorLog, setErrorLog] = useState<{ request: unknown; response: unknown } | null>(null);
|
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);
|
setSubmitting(true);
|
||||||
setError("");
|
setError("");
|
||||||
setErrorLog(null);
|
setErrorLog(null);
|
||||||
|
|||||||
Reference in New Issue
Block a user