admin page

This commit is contained in:
Janis
2023-01-15 17:27:00 +01:00
parent a0598484b4
commit 65eab1f95e
6 changed files with 135 additions and 45 deletions

View File

@@ -0,0 +1,3 @@
export default async function Layout({ children }) {
return <div>{children}</div>;
}

View File

@@ -2,7 +2,7 @@
import React from "react";
import { useState, useRef } from "react";
import { useState, useRef, useEffect } from "react";
import styles from "../../../../styles/modules/AdminArticlesCreate.module.scss";
import { PostArticle } from "../../../../types/postData";
import Markdown from "../../../Markdown";
@@ -11,27 +11,28 @@ import "../../../../styles/inputs.scss";
import Select, { GroupBase, OptionsOrGroups } from "react-select";
import { apiUrl } from "../../../global";
import urlJoin from "url-join";
import { getUrlSafeString } from "../../../utils";
export default function AdminArticlesCreate() {
const [formData, setFormData] = useState<PostArticle>(null);
const [title, setTitle] = useState<string>("");
const [markdown, setMarkdown] = useState<string>("");
const [selectedCategory, setSelectedCategory] = useState<string>();
const [selectCategoriesOptions, setSelectCategoriesOptions] = useState<any>([]);
const titleRef = useRef<HTMLInputElement>(null);
const categorySelectRef = useRef(null);
const introductionRef = useRef<HTMLInputElement>(null);
const markdownTextAreaRef = useRef<HTMLTextAreaElement>(null);
const selectCategoriesOptions: any = [
{ value: "chocolate", label: "Chocolate" },
{ value: "strawberry", label: "Strawberry" },
{ value: "vanilla", label: "Vanilla" },
];
function handleFormChange() {
setMarkdown(markdownTextAreaRef.current.value);
setTitle(titleRef.current.value);
setFormData({
name: titleRef.current.value.replaceAll(" ", "%20"),
name: getUrlSafeString(titleRef.current.value),
title: titleRef.current.value,
introduction: "test intro",
introduction: introductionRef.current.value,
markdown: markdown,
categoryId: 2,
categoryId: Number(categorySelectRef?.current?.getValue()[0]?.value),
});
}
@@ -47,6 +48,27 @@ export default function AdminArticlesCreate() {
});
}
useEffect(() => {
const fetchCategoryOptions = async () => {
const result: Response = await fetch(urlJoin(apiUrl, `categories`), {
cache: "force-cache",
next: { revalidate: 60 * 1 },
});
console.log();
const categories = await result.json();
let newSelectCategoriesOptions = [];
categories?.forEach((c) => {
newSelectCategoriesOptions.push({ value: c.id, label: c.title });
});
setSelectCategoriesOptions(newSelectCategoriesOptions);
};
fetchCategoryOptions().catch((err) => {
console.log(err);
});
}, []);
return (
<div className={styles.adminArticlesCreate}>
<h1>Create a new article</h1>
@@ -62,13 +84,10 @@ export default function AdminArticlesCreate() {
<div className={styles.contentTableEditor}>contenttable</div>
<div className={styles.articleEditor}>
<Select
className="react-select-container"
classNamePrefix="react-select"
value={selectedCategory}
onChange={handleFormChange}
options={selectCategoriesOptions}
/>
<div className={styles.title}>
<label htmlFor="title">Title</label>
<div className={styles.titleInputs}>
<input
onChange={handleFormChange}
className={""}
@@ -77,13 +96,47 @@ export default function AdminArticlesCreate() {
placeholder="title"
ref={titleRef}
/>
<input
readOnly={true}
onChange={handleFormChange}
className={""}
type="text"
name="name"
value={getUrlSafeString(title)}
/>
</div>
</div>
<div className={styles.category}>
<label htmlFor="title">Category</label>
<Select
ref={categorySelectRef}
className="react-select-container"
classNamePrefix="react-select"
onChange={handleFormChange}
options={selectCategoriesOptions}
/>
</div>
<div className={styles.introduction}>
{" "}
<label htmlFor="title">Introduction</label>
<input
onChange={handleFormChange}
className={""}
type="text"
name="introduction"
placeholder="Introduction"
ref={introductionRef}
/>
</div>
<div className={styles.markdown}>
<label htmlFor="">Markdown Editor</label>
<div className={styles.markdownEditor}>
<textarea ref={markdownTextAreaRef} onChange={handleFormChange}></textarea>
<Markdown value={markdown} />
</div>
</div>
</div>
</div>
</div>
);
}

3
app/utils.tsx Normal file
View File

@@ -0,0 +1,3 @@
export function getUrlSafeString(value: string): string {
return encodeURIComponent(value.toLowerCase().replace(/[^a-z0-9 _-]+/gi, "-"));
}

View File

@@ -45,7 +45,7 @@ input {
transition: none;
&:hover {
border-color: rgba(95, 95, 95, 0.5);
border-color: rgba(116, 116, 116, 0.587);
}
}
@@ -58,7 +58,7 @@ input {
background-color: var(--color-background-secondary);
&:hover {
background-color: var(--bg-primary);
background-color: rgba(116, 116, 116, 0.587);
}
}

View File

@@ -8,11 +8,35 @@
margin: 0px auto;
max-width: 1800px;
padding: 0px 24px;
.articleEditor {
display: flex;
flex-direction: column;
row-gap: 25px;
& > div {
display: flex;
flex-direction: column;
label {
padding: 5px;
}
}
.title {
.titleInputs {
display: grid;
grid-template-columns: 1fr 0.5fr;
gap: 0px 5px;
}
}
.category {
}
.introduction {
}
.markdown {
.markdownEditor {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px 10px;
@@ -32,4 +56,5 @@
}
}
}
}
}

View File

@@ -76,3 +76,9 @@ a {
font-weight: bold;
font-size: 0.8em;
}
label {
font-weight: bold;
font-size: 0.9em;
letter-spacing: 2px;
}