fix: spit button

This commit is contained in:
Sabda Yagra 2025-07-23 22:21:11 +07:00
parent 0873cef3ce
commit 96a3fd91cf
1 changed files with 33 additions and 15 deletions

View File

@ -69,6 +69,7 @@ import { getCookiesDecrypt } from "@/lib/utils";
import { error } from "@/lib/swal"; import { error } from "@/lib/swal";
import Swal from "sweetalert2"; import Swal from "sweetalert2";
import withReactContent from "sweetalert2-react-content"; import withReactContent from "sweetalert2-react-content";
import { close, loading } from "@/config/swal";
// Types // Types
interface Category { interface Category {
@ -163,6 +164,7 @@ export default function FormConvertSPIT() {
const router = useRouter(); const router = useRouter();
const t = useTranslations("Form"); const t = useTranslations("Form");
const { id } = useParams() as { id: string }; const { id } = useParams() as { id: string };
const [isAlreadySaved, setIsAlreadySaved] = useState(false);
// Form state // Form state
const { const {
@ -198,35 +200,33 @@ export default function FormConvertSPIT() {
const [showRewriteEditor, setShowRewriteEditor] = useState(false); const [showRewriteEditor, setShowRewriteEditor] = useState(false);
const [isGeneratingRewrite, setIsGeneratingRewrite] = useState(false); const [isGeneratingRewrite, setIsGeneratingRewrite] = useState(false);
const [isLoadingRewrite, setIsLoadingRewrite] = useState(false); const [isLoadingRewrite, setIsLoadingRewrite] = useState(false);
// Media state
const [detailThumb, setDetailThumb] = useState<string[]>([]); const [detailThumb, setDetailThumb] = useState<string[]>([]);
const [thumbsSwiper, setThumbsSwiper] = useState<any>(null); const [thumbsSwiper, setThumbsSwiper] = useState<any>(null);
const [files, setFiles] = useState<FileType[]>([]); const [files, setFiles] = useState<FileType[]>([]);
const [filePlacements, setFilePlacements] = useState<string[][]>([]); const [filePlacements, setFilePlacements] = useState<string[][]>([]);
// Content rewrite state
const [articleIds, setArticleIds] = useState<string[]>([]); const [articleIds, setArticleIds] = useState<string[]>([]);
const [selectedArticleId, setSelectedArticleId] = useState<string | null>( const [selectedArticleId, setSelectedArticleId] = useState<string | null>(
null null
); );
const [articleBody, setArticleBody] = useState<string>(""); const [articleBody, setArticleBody] = useState<string>("");
// Form data state
const [tags, setTags] = useState<string[]>([]); const [tags, setTags] = useState<string[]>([]);
const [publishedFor, setPublishedFor] = useState<string[]>([]); const [publishedFor, setPublishedFor] = useState<string[]>([]);
const [inputRef] = useState(useRef<HTMLInputElement>(null)); const [inputRef] = useState(useRef<HTMLInputElement>(null));
// User permissions
const userLevelId = getCookiesDecrypt("ulie"); const userLevelId = getCookiesDecrypt("ulie");
const roleId = getCookiesDecrypt("urie"); const roleId = getCookiesDecrypt("urie");
const [isUserMabesApprover, setIsUserMabesApprover] = useState(false); const [isUserMabesApprover, setIsUserMabesApprover] = useState(false);
// Initialize component
useEffect(() => { useEffect(() => {
initializeComponent(); initializeComponent();
}, []); }, []);
// useEffect(() => {
// const savedFlag = localStorage.getItem(`spit_saved_${id}`);
// if (savedFlag === "true") {
// setIsAlreadySaved(true);
// }
// }, [id]);
useEffect(() => { useEffect(() => {
checkUserPermissions(); checkUserPermissions();
}, [userLevelId, roleId]); }, [userLevelId, roleId]);
@ -294,9 +294,12 @@ export default function FormConvertSPIT() {
}; };
const loadDetail = async () => { const loadDetail = async () => {
loading()
try { try {
const response = await detailSPIT(id); const response = await detailSPIT(id);
const details = response?.data?.data; const details = response?.data?.data;
setIsAlreadySaved(details?.isPublish ? true : false)
if (!details) { if (!details) {
throw new Error("Detail not found"); throw new Error("Detail not found");
@ -323,13 +326,11 @@ export default function FormConvertSPIT() {
details.contentRewriteDescription || "" details.contentRewriteDescription || ""
); );
// Set category
if (details.categoryId) { if (details.categoryId) {
setSelectedCategoryId(details.categoryId); setSelectedCategoryId(details.categoryId);
await loadTags(details.categoryId); await loadTags(details.categoryId);
} }
// Set tags
if (details.contentTag) { if (details.contentTag) {
const initialTags = details.contentTag const initialTags = details.contentTag
.split(",") .split(",")
@ -340,9 +341,9 @@ export default function FormConvertSPIT() {
console.error("Failed to load detail:", error); console.error("Failed to load detail:", error);
throw error; throw error;
} }
close()
}; };
// Event handlers
const handleCategoryChange = async (categoryId: string) => { const handleCategoryChange = async (categoryId: string) => {
const id = Number(categoryId); const id = Number(categoryId);
setSelectedCategoryId(id); setSelectedCategoryId(id);
@ -601,6 +602,9 @@ export default function FormConvertSPIT() {
await convertSPIT(requestData); await convertSPIT(requestData);
// localStorage.setItem(`spit_saved_${id}`, "true");
// setIsAlreadySaved(true);
MySwal.fire({ MySwal.fire({
title: "Success", title: "Success",
text: "Data saved successfully", text: "Data saved successfully",
@ -608,7 +612,8 @@ export default function FormConvertSPIT() {
confirmButtonColor: "#3085d6", confirmButtonColor: "#3085d6",
}).then(() => { }).then(() => {
// router.push("/in/contributor/content/spit"); // router.push("/in/contributor/content/spit");
router.replace(`${window.location.pathname}?id=${id}`); // router.replace(`${window.location.pathname}?id=${id}`);
loadDetail()
}); });
} catch (error) { } catch (error) {
console.error("Failed to save:", error); console.error("Failed to save:", error);
@ -1164,15 +1169,28 @@ export default function FormConvertSPIT() {
<Button <Button
type="submit" type="submit"
className="w-full" className="w-full"
disabled={isSubmitting || isSaving} disabled={isSubmitting || isSaving || isAlreadySaved}
> >
{isSubmitting || isSaving ? ( {isSubmitting || isSaving ? (
<Loader2 className="h-4 w-4 mr-2 animate-spin" /> <Loader2 className="h-4 w-4 mr-2 animate-spin" />
) : ( ) : (
<Save className="h-4 w-4 mr-2" /> <Save className="h-4 w-4 mr-2" />
)} )}
{isSubmitting || isSaving ? "Saving..." : "Save Changes"} {isAlreadySaved
? "Already Saved"
: isSubmitting || isSaving
? "Saving..."
: "Save Changes"}
</Button> </Button>
{isAlreadySaved && (
<Alert variant="soft">
<CheckCircle className="h-4 w-4 text-red-500" />
<AlertDescription className="text-red-500">
Konten sudah disimpan. Anda tidak dapat menyimpan ulang.
</AlertDescription>
</Alert>
)}
</CardContent> </CardContent>
</Card> </Card>
</div> </div>