import FormGroup from "@/Components/FormGroup"; import InputError from "@/Components/InputError"; import InputLabel from "@/Components/InputLabel"; import TextInput from "@/Components/TextInput"; import Authenticated from "@/Layouts/AuthenticatedLayout"; import { PageProps } from "@/types"; import { Head, Link, useForm } from "@inertiajs/react"; import { Book, Box, CheckCircle, FileText, Paperclip, Plus, PlusCircle, XCircle, XOctagon } from "react-feather"; import 'codemirror/lib/codemirror.css'; import 'codemirror/mode/javascript/javascript'; import PrimaryButton from "@/Components/PrimaryButton"; import Instructions from "./InstructionsForm"; import Modal from "@/Components/Modal"; import ModalButton from "@/Components/ModalButton"; import { FormEventHandler, useEffect, useState } from "react"; import AddCategoryModal from "../SubjectCategory/AddModal"; import axios from "axios"; import SuccessButton from "@/Components/SuccessButton"; import ErrorButton from "@/Components/ErrorButton"; import dayjs from "dayjs"; interface Category { id: number; subject_title: string; description?: string; } export default function Create({ auth }: PageProps) { const thisUser = auth.user; const { data, setData, processing, errors } = useForm({ pageTitle: '', category: '', introduction:'', }); const [isAddCategoryModalOpen, setIsAddCategoryModalOpen] = useState(false); const handleChange = (e: React.ChangeEvent) => { const selectedValue = e.target.value; setData('category', selectedValue); if (selectedValue === "0") { setIsAddCategoryModalOpen(true); } } const [status, setStatus] = useState(''); const [feedback, setFeedback] = useState(''); const [isSuccessModalOpen, setIsSuccessModalOpen] = useState(false); const [isErrorModalOpen, setIsErrorModalOpen] = useState(false); const [categories, setCategories] = useState([]); const fetchCategories = async () => { try { const response = await axios.get(route('categories.index')); setCategories(response.data); } catch (e) { setStatus('Function Failure'); setFeedback('Failed to fetch categories'); setIsErrorModalOpen(true); } }; useEffect(() => { if (thisUser.role_id !== 1) { setStatus('Warning'); setFeedback('You are unauthorized to access this page'); setIsErrorModalOpen(true); } else { fetchCategories(); } }, [thisUser.role_id]); const handleCloseSuccessModal = () => { setIsSuccessModalOpen(false); window.location.href = '/dashboard'; } const handleCloseErrorModal = () => { setIsErrorModalOpen(false); window.location.href = '/dashboard'; }; const [instructions, setInstructions] = useState<{ id: number; steps: any[]; frameworkID: string; }[]>([]); const [nextId, setNextId] = useState(0); const handleAddInstruction = () => { setInstructions([...instructions, { id: nextId, steps: [], frameworkID: '' }]); setNextId(nextId + 1); }; const handleRemoveInstruction = (id: number) => { setInstructions(prevInstructions => prevInstructions.filter(instruction => instruction.id !== id) ); }; const handleUpdateInstruction = (id: number, steps: any[], frameworkID: string) => { setInstructions(prevInstructions => prevInstructions.map(instruction => instruction.id === id ? { ...instruction, steps, frameworkID } : instruction ) ); }; const submit: FormEventHandler = async (e) => { e.preventDefault(); if (data.category==="0" || data.category==="") { alert('Please choose a valid category.') return } try { const response = await axios.post(route('page.add'), data); const pageID = response.data.page.id; await Promise.all(instructions.map(async (instruction) => { if (instruction.frameworkID === "0" || instruction.frameworkID === "") { alert('Please choose a valid framework for each instruction.'); return; } const timestamp = dayjs().format('YYYYMMDDHHmmss'); const jsonString = JSON.stringify({ steps: instruction.steps }); const blob = new Blob([jsonString], { type: 'application/json' }); const file = new File([blob], `${timestamp}.json`, { type: 'application/json' }); const formData = new FormData(); formData.append('file', file); formData.append('frameworkID', instruction.frameworkID); formData.append('pageID', pageID.toString()); await axios.post(route('instruction.add'), formData, { headers: { 'Content-Type': 'multipart/form-data', }, }); })); setStatus('Success'); setFeedback(response.data.message); setIsSuccessModalOpen(true); } catch (error) { setStatus('Error') if (axios.isAxiosError(error)) { setFeedback(error.response?.data.message || 'An error occurred'); } else { setFeedback('An unexpected error occurred'); } setIsErrorModalOpen(true); } } const renderIfAdmin = () => { if (thisUser.role_id === 1) { return (

Create Repository Page

setData('pageTitle', e.target.value)} />