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 React from "react";
import { useState, useRef } from "react"; import { useState, useRef, useEffect } from "react";
import styles from "../../../../styles/modules/AdminArticlesCreate.module.scss"; import styles from "../../../../styles/modules/AdminArticlesCreate.module.scss";
import { PostArticle } from "../../../../types/postData"; import { PostArticle } from "../../../../types/postData";
import Markdown from "../../../Markdown"; import Markdown from "../../../Markdown";
@@ -11,27 +11,28 @@ import "../../../../styles/inputs.scss";
import Select, { GroupBase, OptionsOrGroups } from "react-select"; import Select, { GroupBase, OptionsOrGroups } from "react-select";
import { apiUrl } from "../../../global"; import { apiUrl } from "../../../global";
import urlJoin from "url-join"; import urlJoin from "url-join";
import { getUrlSafeString } from "../../../utils";
export default function AdminArticlesCreate() { export default function AdminArticlesCreate() {
const [formData, setFormData] = useState<PostArticle>(null); const [formData, setFormData] = useState<PostArticle>(null);
const [title, setTitle] = useState<string>("");
const [markdown, setMarkdown] = useState<string>(""); const [markdown, setMarkdown] = useState<string>("");
const [selectedCategory, setSelectedCategory] = useState<string>(); const [selectCategoriesOptions, setSelectCategoriesOptions] = useState<any>([]);
const titleRef = useRef<HTMLInputElement>(null); const titleRef = useRef<HTMLInputElement>(null);
const categorySelectRef = useRef(null);
const introductionRef = useRef<HTMLInputElement>(null);
const markdownTextAreaRef = useRef<HTMLTextAreaElement>(null); const markdownTextAreaRef = useRef<HTMLTextAreaElement>(null);
const selectCategoriesOptions: any = [
{ value: "chocolate", label: "Chocolate" },
{ value: "strawberry", label: "Strawberry" },
{ value: "vanilla", label: "Vanilla" },
];
function handleFormChange() { function handleFormChange() {
setMarkdown(markdownTextAreaRef.current.value); setMarkdown(markdownTextAreaRef.current.value);
setTitle(titleRef.current.value);
setFormData({ setFormData({
name: titleRef.current.value.replaceAll(" ", "%20"), name: getUrlSafeString(titleRef.current.value),
title: titleRef.current.value, title: titleRef.current.value,
introduction: "test intro", introduction: introductionRef.current.value,
markdown: markdown, 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 ( return (
<div className={styles.adminArticlesCreate}> <div className={styles.adminArticlesCreate}>
<h1>Create a new article</h1> <h1>Create a new article</h1>
@@ -62,25 +84,56 @@ export default function AdminArticlesCreate() {
<div className={styles.contentTableEditor}>contenttable</div> <div className={styles.contentTableEditor}>contenttable</div>
<div className={styles.articleEditor}> <div className={styles.articleEditor}>
<Select <div className={styles.title}>
className="react-select-container" <label htmlFor="title">Title</label>
classNamePrefix="react-select"
value={selectedCategory}
onChange={handleFormChange}
options={selectCategoriesOptions}
/>
<input
onChange={handleFormChange}
className={""}
type="text"
name="title"
placeholder="title"
ref={titleRef}
/>
<div className={styles.titleInputs}>
<input
onChange={handleFormChange}
className={""}
type="text"
name="title"
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}> <div className={styles.markdown}>
<textarea ref={markdownTextAreaRef} onChange={handleFormChange}></textarea> <label htmlFor="">Markdown Editor</label>
<Markdown value={markdown} /> <div className={styles.markdownEditor}>
<textarea ref={markdownTextAreaRef} onChange={handleFormChange}></textarea>
<Markdown value={markdown} />
</div>
</div> </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; transition: none;
&:hover { &: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); background-color: var(--color-background-secondary);
&:hover { &:hover {
background-color: var(--bg-primary); background-color: rgba(116, 116, 116, 0.587);
} }
} }

View File

@@ -8,26 +8,51 @@
margin: 0px auto; margin: 0px auto;
max-width: 1800px; max-width: 1800px;
padding: 0px 24px; padding: 0px 24px;
.articleEditor { .articleEditor {
display: flex; display: flex;
flex-direction: column; 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 { .markdown {
display: grid; .markdownEditor {
grid-template-columns: 1fr 1fr; display: grid;
gap: 10px 10px; grid-template-columns: 1fr 1fr;
textarea { gap: 10px 10px;
color: var(--font-color); textarea {
border: 2px solid rgba(59, 59, 59, 0.434); color: var(--font-color);
background-color: transparent; border: 2px solid rgba(59, 59, 59, 0.434);
min-height: 700px; background-color: transparent;
resize: none; min-height: 700px;
display: block; resize: none;
border-radius: 0px; display: block;
outline: 0; border-radius: 0px;
resize: vertical; outline: 0;
font-family: inherit; resize: vertical;
font-size: inherit; font-family: inherit;
font-size: inherit;
}
} }
} }
} }

View File

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