handle genres
This commit is contained in:
parent
798eb499e4
commit
688c3ef6be
|
@ -39,6 +39,14 @@ export const getResponses = async () => {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
export const getGenres = async () => {
|
||||||
|
try {
|
||||||
|
const res = await API.get("genres")
|
||||||
|
return res
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
export const requestEdit = async (data,type) => {
|
export const requestEdit = async (data,type) => {
|
||||||
try {
|
try {
|
||||||
const res = await API.post(`/${type}/edit`,data)
|
const res = await API.post(`/${type}/edit`,data)
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
export default ({ label, value, onChange }) => {
|
export default ({ label, value, name, onChange }) => {
|
||||||
return (
|
return (
|
||||||
<label>
|
<label>
|
||||||
{label}
|
{label}
|
||||||
<input type="checkbox" checked={value} onChange={()=>{onChange(label)}} />
|
<input type="checkbox" name={name} checked={value} onChange={()=>{onChange(label)}} />
|
||||||
</label>
|
</label>
|
||||||
)
|
)
|
||||||
}
|
}
|
|
@ -1,16 +1,18 @@
|
||||||
|
import { isNull } from "lodash"
|
||||||
import Checkbox from "./Checkbox"
|
import Checkbox from "./Checkbox"
|
||||||
export default (props) => {
|
export default (props) => {
|
||||||
|
|
||||||
console.dir(props.options)
|
|
||||||
const optionsRendered = props.options?.map((e,i)=>{
|
const optionsRendered = props.options?.map((e,i)=>{
|
||||||
console.log(props.values)
|
if(e===null){return}
|
||||||
return <Checkbox
|
return <Checkbox
|
||||||
onChange={props.onChange}
|
onChange={props.onChange}
|
||||||
label={e}
|
label={e}
|
||||||
|
name={`${props.name}${i}`}
|
||||||
key={e+i}
|
key={e+i}
|
||||||
value={props?.values[e]}
|
value={props?.values[e]}
|
||||||
/>
|
/>
|
||||||
})
|
})
|
||||||
|
.filter(e=>{return e!==null})
|
||||||
|
|
||||||
return <fieldset>
|
return <fieldset>
|
||||||
<legend>{props.legend}</legend>
|
<legend>{props.legend}</legend>
|
||||||
|
|
|
@ -52,7 +52,8 @@ export default function Table(props) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return e
|
return e
|
||||||
}) : []
|
})
|
||||||
|
.filter(e=>{return e!==null}) : []
|
||||||
|
|
||||||
if (data.length === 0) { return <p>Nothing to see here...</p> }
|
if (data.length === 0) { return <p>Nothing to see here...</p> }
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import {getStories,getPublications,getSubmissions, getResponses} from './APIcalls.mjs'
|
import {getStories,getGenres,getPublications,getSubmissions, getResponses} from './APIcalls.mjs'
|
||||||
|
|
||||||
export async function publicationsLoader (){
|
export async function publicationsLoader (){
|
||||||
let publications = await getPublications()
|
let publications = await getPublications()
|
||||||
|
@ -24,3 +24,10 @@ export async function editSubmissionLoader(){
|
||||||
responses = responses.data
|
responses = responses.data
|
||||||
return { stories, publications, submissions, responses}
|
return { stories, publications, submissions, responses}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function editStoryLoader(){
|
||||||
|
const {stories} = await storiesLoader()
|
||||||
|
let genres = await getGenres()
|
||||||
|
genres=genres.data
|
||||||
|
return {stories,genres}
|
||||||
|
}
|
|
@ -7,7 +7,7 @@ import Story from './routes/story';
|
||||||
import Stories from './routes/stories';
|
import Stories from './routes/stories';
|
||||||
import Publication from './routes/publication';
|
import Publication from './routes/publication';
|
||||||
import Publications from './routes/publications';
|
import Publications from './routes/publications';
|
||||||
import { storiesLoader,publicationsLoader,submissionsLoader, editSubmissionLoader } from './loaders.mjs';
|
import { storiesLoader,publicationsLoader,submissionsLoader, editSubmissionLoader, editStoryLoader } from './loaders.mjs';
|
||||||
import EditStory, {action as editStoryAction } from './routes/editStory';
|
import EditStory, {action as editStoryAction } from './routes/editStory';
|
||||||
import EditPublication, {action as editPublicationAction} from './routes/editPublication';
|
import EditPublication, {action as editPublicationAction} from './routes/editPublication';
|
||||||
import CreateStory, {action as createStoryAction} from './routes/createStory';
|
import CreateStory, {action as createStoryAction} from './routes/createStory';
|
||||||
|
@ -70,7 +70,7 @@ const router = createBrowserRouter([
|
||||||
{
|
{
|
||||||
path:"/story/:storyId/edit",
|
path:"/story/:storyId/edit",
|
||||||
element: <EditStory/>,
|
element: <EditStory/>,
|
||||||
loader: storiesLoader,
|
loader: editStoryLoader,
|
||||||
action: editStoryAction
|
action: editStoryAction
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,25 +1,53 @@
|
||||||
import { Form, useLoaderData, useParams, redirect, useNavigate } from "react-router-dom";
|
import { Form, useLoaderData, useParams, redirect, useNavigate } from "react-router-dom";
|
||||||
import PageHeader from "../Components/PageHeader";
|
import PageHeader from "../Components/PageHeader";
|
||||||
import { requestEdit } from "../APIcalls.mjs";
|
import { requestEdit } from "../APIcalls.mjs";
|
||||||
|
import Checkboxes from "../Components/Checkboxes"
|
||||||
|
import { useState,useEffect } from "react";
|
||||||
|
import { forIn } from "lodash";
|
||||||
|
|
||||||
export async function action({request,params}){
|
export async function action({request,params}){
|
||||||
const formData = await request.formData()
|
const formData = await request.formData()
|
||||||
const data = Object.fromEntries(formData)
|
console.dir(formData)
|
||||||
|
let data = Object.fromEntries(formData)
|
||||||
data.id=parseInt(params.storyId)
|
data.id=parseInt(params.storyId)
|
||||||
|
data = packageGenres(data)
|
||||||
console.dir(data)
|
console.dir(data)
|
||||||
await requestEdit(data,'story')
|
await requestEdit(data,'story')
|
||||||
return redirect(`/story/${params.storyId}`)
|
return redirect(`/story/${params.storyId}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export default function EditStory(){
|
export default function EditStory(){
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
const { storyId } = useParams()
|
const { storyId } = useParams()
|
||||||
const { stories } = useLoaderData()
|
const { stories, genres } = useLoaderData()
|
||||||
const storyData = stories.find(row=>row.id==storyId)
|
const storyData = stories.find(row=>row.id==storyId)
|
||||||
|
const [toggledGenres,setToggledGenres]=useState({})
|
||||||
|
const handleToggle = (target) => {
|
||||||
|
setToggledGenres(prev => {
|
||||||
|
return {
|
||||||
|
...prev,
|
||||||
|
[target]: !prev[target]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
useEffect(()=>{
|
||||||
|
console.dir(toggledGenres)
|
||||||
|
},[toggledGenres])
|
||||||
return(
|
return(
|
||||||
<div id="page-container">
|
<div id="page-container">
|
||||||
<PageHeader super={`Story #${storyData.id}`} heading="EDIT" url="/story" id={storyId}/>
|
<PageHeader super={`Story #${storyData.id}`} heading="EDIT" url="/story" id={storyId}/>
|
||||||
|
@ -43,6 +71,15 @@ export default function EditStory(){
|
||||||
defaultValue={storyData.word_count}
|
defaultValue={storyData.word_count}
|
||||||
/>
|
/>
|
||||||
</label>
|
</label>
|
||||||
|
<label>
|
||||||
|
<Checkboxes
|
||||||
|
name="genres"
|
||||||
|
options={genres}
|
||||||
|
onChange={handleToggle}
|
||||||
|
values={genres}
|
||||||
|
legend="Genres:"
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
<div id="bottom-button-container">
|
<div id="bottom-button-container">
|
||||||
<button type="submit">SAVE</button>
|
<button type="submit">SAVE</button>
|
||||||
<button type="button" onClick={()=>{navigate("/stories")}}>CANCEL</button>
|
<button type="button" onClick={()=>{navigate("/stories")}}>CANCEL</button>
|
||||||
|
|
|
@ -1,15 +1,18 @@
|
||||||
import { useLoaderData } from "react-router-dom"
|
import { useLoaderData } from "react-router-dom"
|
||||||
import Table from "../Components/Table.jsx";
|
import Table from "../Components/Table.jsx";
|
||||||
import PageHeader from "../Components/PageHeader.jsx";
|
import PageHeader from "../Components/PageHeader.jsx";
|
||||||
import { getStories } from "../APIcalls.mjs";
|
|
||||||
import magGlass from "../assets/magnifying-glass.svg"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export default function Stories(){
|
export default function Stories(){
|
||||||
const { stories } = useLoaderData();
|
const { stories } = useLoaderData();
|
||||||
const filterList = [
|
const filterColumns = [
|
||||||
'submissions'
|
'submissions',
|
||||||
|
'deleted'
|
||||||
|
]
|
||||||
|
const filterRows = [
|
||||||
|
{column:'deleted',value:1}
|
||||||
]
|
]
|
||||||
const clickables = [
|
const clickables = [
|
||||||
['title',(row)=>{return `../../story/${row.id}`}]
|
['title',(row)=>{return `../../story/${row.id}`}]
|
||||||
|
@ -23,8 +26,10 @@ export default function Stories(){
|
||||||
<Table
|
<Table
|
||||||
data={stories}
|
data={stories}
|
||||||
filterColumns={filterColumns}
|
filterColumns={filterColumns}
|
||||||
|
filterRows={filterRows}
|
||||||
clickables={clickables}
|
clickables={clickables}
|
||||||
sortByDefault='title'
|
sortByDefault='title'
|
||||||
|
badges={badges}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue