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