2023-09-13 12:26:38 +00:00
|
|
|
import { Form, useLoaderData, useParams, redirect, useNavigate } from "react-router-dom";
|
2023-09-11 10:03:49 +00:00
|
|
|
import PageHeader from "../Components/PageHeader";
|
|
|
|
import { requestEdit } from "../APIcalls.mjs";
|
2023-09-18 17:32:04 +00:00
|
|
|
import Checkboxes from "../Components/Checkboxes"
|
|
|
|
import { useState,useEffect } from "react";
|
|
|
|
import { forIn } from "lodash";
|
2023-09-11 10:03:49 +00:00
|
|
|
|
|
|
|
export async function action({request,params}){
|
|
|
|
const formData = await request.formData()
|
2023-09-18 17:32:04 +00:00
|
|
|
console.dir(formData)
|
|
|
|
let data = Object.fromEntries(formData)
|
2023-09-11 10:03:49 +00:00
|
|
|
data.id=parseInt(params.storyId)
|
2023-09-18 17:32:04 +00:00
|
|
|
data = packageGenres(data)
|
2023-09-11 10:03:49 +00:00
|
|
|
console.dir(data)
|
|
|
|
await requestEdit(data,'story')
|
|
|
|
return redirect(`/story/${params.storyId}`)
|
|
|
|
}
|
|
|
|
|
2023-09-18 17:32:04 +00:00
|
|
|
export const packageGenres = (data) => {
|
|
|
|
const array = []
|
|
|
|
for (const key in data) {
|
|
|
|
if(/genres/.test(key)){
|
|
|
|
array.push(key.slice(6))
|
|
|
|
delete data[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data.genres=array
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-11 10:03:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
export default function EditStory(){
|
2023-09-13 12:26:38 +00:00
|
|
|
const navigate = useNavigate()
|
2023-09-11 10:03:49 +00:00
|
|
|
const { storyId } = useParams()
|
2023-09-18 17:32:04 +00:00
|
|
|
const { stories, genres } = useLoaderData()
|
2023-09-11 10:03:49 +00:00
|
|
|
const storyData = stories.find(row=>row.id==storyId)
|
2023-09-18 17:32:04 +00:00
|
|
|
const [toggledGenres,setToggledGenres]=useState({})
|
|
|
|
const handleToggle = (target) => {
|
|
|
|
setToggledGenres(prev => {
|
|
|
|
return {
|
|
|
|
...prev,
|
|
|
|
[target]: !prev[target]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
useEffect(()=>{
|
|
|
|
console.dir(toggledGenres)
|
|
|
|
},[toggledGenres])
|
2023-09-11 10:03:49 +00:00
|
|
|
return(
|
2023-09-12 14:27:51 +00:00
|
|
|
<div id="page-container">
|
|
|
|
<PageHeader super={`Story #${storyData.id}`} heading="EDIT" url="/story" id={storyId}/>
|
2023-09-11 10:03:49 +00:00
|
|
|
<Form method="post" id="story-form">
|
2023-09-12 09:38:45 +00:00
|
|
|
<label> Title:
|
2023-09-11 10:03:49 +00:00
|
|
|
<input
|
|
|
|
placeholder='title'
|
|
|
|
aria-label="Title"
|
|
|
|
type="text"
|
|
|
|
name="title"
|
|
|
|
defaultValue={storyData.title}
|
|
|
|
/>
|
2023-09-12 09:38:45 +00:00
|
|
|
</label>
|
|
|
|
<label> Wordcount:
|
2023-09-11 10:03:49 +00:00
|
|
|
<input
|
|
|
|
placeholder='1000'
|
|
|
|
aria-label="Wordcount"
|
|
|
|
type="number"
|
|
|
|
step="1"
|
|
|
|
name="word_count"
|
2023-09-11 12:44:24 +00:00
|
|
|
defaultValue={storyData.word_count}
|
2023-09-11 10:03:49 +00:00
|
|
|
/>
|
2023-09-12 09:38:45 +00:00
|
|
|
</label>
|
2023-09-18 17:32:04 +00:00
|
|
|
<label>
|
|
|
|
<Checkboxes
|
|
|
|
name="genres"
|
|
|
|
options={genres}
|
|
|
|
onChange={handleToggle}
|
|
|
|
values={genres}
|
|
|
|
legend="Genres:"
|
|
|
|
/>
|
|
|
|
</label>
|
2023-09-12 09:38:45 +00:00
|
|
|
<div id="bottom-button-container">
|
2023-09-11 10:03:49 +00:00
|
|
|
<button type="submit">SAVE</button>
|
2023-09-13 12:26:38 +00:00
|
|
|
<button type="button" onClick={()=>{navigate("/stories")}}>CANCEL</button>
|
2023-09-12 09:38:45 +00:00
|
|
|
</div>
|
|
|
|
|
2023-09-11 10:03:49 +00:00
|
|
|
</Form>
|
|
|
|
|
2023-09-12 14:27:51 +00:00
|
|
|
</div>
|
2023-09-11 10:03:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
}
|