Fixed Type Issues
This commit is contained in:
parent
39f3c02e7c
commit
92bdb03836
@ -66,7 +66,7 @@ async function addToExistingBucket(bucketid: number): Promise<Bucket> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async function tryCreateAttachment(request: Request) {
|
export async function tryCreateAttachment(request: Request) {
|
||||||
|
|
||||||
// Make sure the DB is ready
|
// Make sure the DB is ready
|
||||||
await dbSync;
|
await dbSync;
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import { Attributes, where } from "@sequelize/core";
|
|||||||
import { cookies } from 'next/headers';
|
import { cookies } from 'next/headers';
|
||||||
import { getCookieAuth, userIsAdmin } from '../actions';
|
import { getCookieAuth, userIsAdmin } from '../actions';
|
||||||
import { ActionResult } from '../ActionResult';
|
import { ActionResult } from '../ActionResult';
|
||||||
|
import { PostAttributesWithBuckets } from '@/model/Post';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -20,13 +21,23 @@ export async function deletePost(postID: number): Promise<ActionResult<boolean>>
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export async function getPosts(): Promise<ActionResult<Attributes<Post>[]>> {
|
export async function getPostsWithBuckets(): Promise<ActionResult<PostAttributesWithBuckets[]>> {
|
||||||
await dbSync;
|
await dbSync;
|
||||||
if(! await userIsAdmin()) return {error:"Unauthorized, not fetching Posts."}
|
if(! await userIsAdmin()) return {error:"Unauthorized, not fetching Posts."}
|
||||||
const posts = await Post.findAll({include: {association: Post.associations.buckets, include: Bucket.associations.attachments}},);
|
const posts = await Post.findAll({include: {association: Post.associations.buckets, include: Bucket.associations.attachments}},);
|
||||||
return {result:JSON.parse(JSON.stringify(posts))};
|
return {result:JSON.parse(JSON.stringify(posts))};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
export async function getPosts(): Promise<ActionResult<Attributes<Post>[]>> {
|
||||||
|
await dbSync;
|
||||||
|
if(! await userIsAdmin()) return {error:"Unauthorized, not fetching Posts."}
|
||||||
|
const posts = await Post.findAll();
|
||||||
|
return {result:JSON.parse(JSON.stringify(posts))};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export async function updatePost(postAttributes: Partial<Attributes<Post>>): Promise<ActionResult<Attributes<Post>[]>> {
|
export async function updatePost(postAttributes: Partial<Attributes<Post>>): Promise<ActionResult<Attributes<Post>[]>> {
|
||||||
await dbSync;
|
await dbSync;
|
||||||
if(! await userIsAdmin()) return {error:"Unauthorized, not updating Post."}
|
if(! await userIsAdmin()) return {error:"Unauthorized, not updating Post."}
|
||||||
|
|||||||
@ -1,14 +1,16 @@
|
|||||||
import { ActionResult } from "@/app/lib/actions/ActionResult";
|
import { ActionResult } from "@/app/lib/actions/ActionResult";
|
||||||
import { handleActionResult } from "@/app/lib/actions/clientActionHandler";
|
import { handleActionResult } from "@/app/lib/actions/clientActionHandler";
|
||||||
import { Post, Project, Bucket, PostBucket, Attachment } from "@/model/Models";
|
import { Post, Project, Bucket, PostBucket, Attachment } from "@/model/Models";
|
||||||
|
import { PostAttributesWithBuckets } from "@/model/Post";
|
||||||
import { Attributes } from "@sequelize/core";
|
import { Attributes } from "@sequelize/core";
|
||||||
import { UUID } from "crypto";
|
import { UUID } from "crypto";
|
||||||
import { ChangeEventHandler, MouseEventHandler, useLayoutEffect, useRef, useState } from "react";
|
import { ChangeEventHandler, MouseEventHandler, useLayoutEffect, useRef, useState } from "react";
|
||||||
import { Accordion, AccordionBody, AccordionHeader, AccordionItem } from "react-bootstrap";
|
import { Accordion, AccordionBody, AccordionHeader, AccordionItem } from "react-bootstrap";
|
||||||
|
|
||||||
export type PostTableCallbacks = {
|
export type PostTableCallbacks = {
|
||||||
savePost: (p:Partial<Attributes<Post>>)=>any;
|
savePost: (p:Partial<PostAttributesWithBuckets>)=>void;
|
||||||
closeEditor: ()=>any;
|
closeEditor: ()=>any;
|
||||||
|
uploadAttachment : ()=>any;
|
||||||
}
|
}
|
||||||
type PostEditorBucket = Partial<Attributes<Bucket>> & {
|
type PostEditorBucket = Partial<Attributes<Bucket>> & {
|
||||||
attachments:Partial<Attributes<Attachment>>[]
|
attachments:Partial<Attributes<Attachment>>[]
|
||||||
@ -20,7 +22,7 @@ export type PostEditorPost = Attributes<Post> & {
|
|||||||
|
|
||||||
export type EditorProps = {
|
export type EditorProps = {
|
||||||
open:boolean;
|
open:boolean;
|
||||||
post:PostEditorPost;
|
post:Partial<PostAttributesWithBuckets>;
|
||||||
projects?:Attributes<Project>[];
|
projects?:Attributes<Project>[];
|
||||||
callbacks:PostTableCallbacks;
|
callbacks:PostTableCallbacks;
|
||||||
}
|
}
|
||||||
@ -79,8 +81,9 @@ export default function PostEditor(props:EditorProps){
|
|||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{
|
{
|
||||||
(()=>{
|
props.post.buckets
|
||||||
let bucketMap:Map<UUID,PostEditorBucket> = new Map(props.post.buckets.map((b)=>[b.id as UUID,b]));
|
? (()=>{
|
||||||
|
let bucketMap:Map<UUID,Attributes<Bucket>> = new Map(props.post.buckets.map((b)=>[b.id as UUID,b]));
|
||||||
let bucketList = [...props.post.buckets.map((b)=>b.id)];
|
let bucketList = [...props.post.buckets.map((b)=>b.id)];
|
||||||
return bucketList.map((e)=>{
|
return bucketList.map((e)=>{
|
||||||
return <>
|
return <>
|
||||||
@ -90,16 +93,28 @@ export default function PostEditor(props:EditorProps){
|
|||||||
<AccordionBody>
|
<AccordionBody>
|
||||||
|
|
||||||
<ul>
|
<ul>
|
||||||
<li><button className='btn btn-success'>Upload new attachment</button></li>
|
<li><input type="file" className='btn btn-success' onChange={(e)=>{
|
||||||
{ bucketMap.get(e as UUID)?.attachments.map((attachment)=><li key={`listItem-file-${attachment.filename}`}>{attachment.filename}</li>) }
|
for (let index = 0; index < ((e.target.files as FileList).length as number); index++) {
|
||||||
|
const element = (e.target.files as FileList)[index];
|
||||||
|
const fReader = new FileReader()
|
||||||
|
fReader.readAsDataURL(element)
|
||||||
|
fReader.onloadend = (event)=>{
|
||||||
|
console.log(event.target?.result);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}}/></li>
|
||||||
|
{(()=>{
|
||||||
|
const bucket = bucketMap.get(e as UUID)
|
||||||
|
return bucket && bucket.attachments ? bucket.attachments.map((attachment)=><li key={`listItem-file-${attachment.filename}`}>{attachment.filename}</li>) : <></>
|
||||||
|
})()}
|
||||||
</ul>
|
</ul>
|
||||||
</AccordionBody>
|
</AccordionBody>
|
||||||
</AccordionItem>
|
</AccordionItem>
|
||||||
</Accordion></tr></>
|
</Accordion></tr></>
|
||||||
})
|
})
|
||||||
})()
|
})()
|
||||||
|
: <></>
|
||||||
}
|
}
|
||||||
|
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
<button type="button" className="m-2 btn btn-primary">Preview</button>
|
<button type="button" className="m-2 btn btn-primary">Preview</button>
|
||||||
|
|||||||
@ -8,19 +8,21 @@ import toast from "react-hot-toast"
|
|||||||
import { ActionResult } from "@/app/lib/actions/ActionResult";
|
import { ActionResult } from "@/app/lib/actions/ActionResult";
|
||||||
import { handleActionResult } from "@/app/lib/actions/clientActionHandler";
|
import { handleActionResult } from "@/app/lib/actions/clientActionHandler";
|
||||||
import PostEditor, { EditorProps, PostEditorPost } from "./PostEditor";
|
import PostEditor, { EditorProps, PostEditorPost } from "./PostEditor";
|
||||||
import { getPosts } from "@/app/lib/actions/entityManagement/postActions";
|
import { getPostsWithBuckets } from "@/app/lib/actions/entityManagement/postActions";
|
||||||
|
import { PostAttributesWithBuckets } from "@/model/Post";
|
||||||
|
|
||||||
type Actions = {
|
type Actions = {
|
||||||
deletePost: (id: number) => Promise<ActionResult<boolean>>
|
deletePost: (id: number) => Promise<ActionResult<boolean>>
|
||||||
getPosts: () => Promise<ActionResult<PostEditorPost[]>>
|
getPosts: () => Promise<ActionResult<PostAttributesWithBuckets[]>>
|
||||||
getProjects: () => Promise<ActionResult<Attributes<Project>[]>>
|
getProjects: () => Promise<ActionResult<Attributes<Project>[]>>
|
||||||
savePost: (data: Partial<Attributes<Post>>) => Promise<ActionResult<Attributes<Post>[]>>
|
savePost: (data: Partial<Attributes<Post>>) => Promise<ActionResult<Attributes<Post>[]>>
|
||||||
|
// uploadAttachment: ()=> Promise<ActionResult<any>>
|
||||||
}
|
}
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
children?: ReactNode;
|
children?: ReactNode;
|
||||||
headings: Array<string>;
|
headings: Array<string>;
|
||||||
data: PostEditorPost[];
|
data: Partial<PostAttributesWithBuckets>[];
|
||||||
projects: Attributes<Project>[];
|
projects: Attributes<Project>[];
|
||||||
actions: Actions;
|
actions: Actions;
|
||||||
}
|
}
|
||||||
@ -33,7 +35,7 @@ export default function PostTable(props: Props) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function showEditor(entry: PostEditorPost): void {
|
function showEditor(entry: Partial<PostAttributesWithBuckets>): void {
|
||||||
setEditor({
|
setEditor({
|
||||||
open: true,
|
open: true,
|
||||||
post: entry
|
post: entry
|
||||||
@ -49,15 +51,16 @@ export default function PostTable(props: Props) {
|
|||||||
const [projects, setProjects] = useState(props.projects);
|
const [projects, setProjects] = useState(props.projects);
|
||||||
|
|
||||||
|
|
||||||
function deletePost(entry: PostEditorPost) {
|
function deletePost(entry: Partial<PostAttributesWithBuckets>) {
|
||||||
|
if(! entry.id) return;
|
||||||
props.actions?.deletePost(entry.id)
|
props.actions?.deletePost(entry.id)
|
||||||
.then(actionResult => {
|
.then(actionResult => {
|
||||||
const result = handleActionResult(actionResult);
|
const result = handleActionResult(actionResult);
|
||||||
if (!result) return;
|
if (!result) return;
|
||||||
posts.splice(posts.indexOf(entry), 1);
|
posts.splice(posts.indexOf(entry), 1);
|
||||||
setPosts([...posts])
|
setPosts([...posts])
|
||||||
toast.success('Removed Post:' + entry.id);
|
toast.success('Removed Post:' + entry.id);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const aifa = (a: ReactNode, b: ReactNode) => a ? a : b
|
const aifa = (a: ReactNode, b: ReactNode) => a ? a : b
|
||||||
@ -73,29 +76,28 @@ export default function PostTable(props: Props) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function savePost(e: Partial<PostEditorPost>) {
|
function savePost(e: Partial<PostAttributesWithBuckets>) {
|
||||||
props.actions.savePost({
|
props.actions.savePost({
|
||||||
id: e.id,
|
id: e.id,
|
||||||
content: e.content,
|
content: e.content,
|
||||||
title: e.title,
|
title: e.title,
|
||||||
project_id: e.project_id
|
project_id: e.project_id
|
||||||
})
|
})
|
||||||
.then(res => handleActionResult(res))
|
.then(res => handleActionResult(res))
|
||||||
.then(getPosts)
|
.then(getPostsWithBuckets)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
1
|
if (!res.error && res.result) setPosts(res.result);
|
||||||
if (!res.error) setPosts(res.result as PostEditorPost[]);
|
})
|
||||||
})
|
|
||||||
closeEditor();
|
closeEditor();
|
||||||
};
|
};
|
||||||
|
|
||||||
return <>
|
return <>
|
||||||
<TableGen headings={props.headings}>
|
<TableGen headings={props.headings}>
|
||||||
{posts.map((d: PostEditorPost) => <>
|
{posts.map((d: Partial<PostAttributesWithBuckets>) => <>
|
||||||
<tr key={d.id}>
|
<tr key={d.id}>
|
||||||
<th key={`row${d.id}`} scope="row">{d.id}</th>
|
<th key={`row${d.id}`} scope="row">{d.id}</th>
|
||||||
<td key='titlefield'>{d.title}</td>
|
<td key='titlefield'>{d.title}</td>
|
||||||
<td key='contentfield'>{d.content.length < 255 ? d.content : `${d.content.substring(0, 255)}...`}</td>
|
<td key='contentfield'>{d.content? (d.content.length < 255 ? d.content : `${d.content.substring(0, 255)}...`) : "No Content Found"}</td>
|
||||||
<td key='project'>{
|
<td key='project'>{
|
||||||
aifa((projects.find((e) => {
|
aifa((projects.find((e) => {
|
||||||
return (e.id == d.project_id)
|
return (e.id == d.project_id)
|
||||||
@ -108,7 +110,7 @@ export default function PostTable(props: Props) {
|
|||||||
</tr>
|
</tr>
|
||||||
{
|
{
|
||||||
(editor.open && editor.post && editor.post.id == d.id)
|
(editor.open && editor.post && editor.post.id == d.id)
|
||||||
?<tr key={'activeEditor'}><th scope="row" colSpan={props.headings.length}><PostEditor callbacks={{ savePost: savePost, closeEditor: closeEditor }} open={editor.open} post={editor.post} projects={projects} ></PostEditor></th></tr>
|
?<tr key={'activeEditor'}><th scope="row" colSpan={props.headings.length}><PostEditor callbacks={{ savePost: savePost, closeEditor: closeEditor, uploadAttachment: ()=>{} }} open={editor.open} post={editor.post} projects={projects} ></PostEditor></th></tr>
|
||||||
: ""
|
: ""
|
||||||
}
|
}
|
||||||
</>)}
|
</>)}
|
||||||
|
|||||||
@ -5,9 +5,10 @@ import { constructAPIUrl } from "@/util/Utils";
|
|||||||
import { ReactNode, useEffect } from "react";
|
import { ReactNode, useEffect } from "react";
|
||||||
import TableGen from "../../../client/TableGen";
|
import TableGen from "../../../client/TableGen";
|
||||||
import PostTable from "@/components/client/admin/PostTable";
|
import PostTable from "@/components/client/admin/PostTable";
|
||||||
import { deletePost, getPosts, updatePost } from "@/app/lib/actions/entityManagement/postActions";
|
import { deletePost, getPostsWithBuckets, updatePost } from "@/app/lib/actions/entityManagement/postActions";
|
||||||
import { getProjects } from "@/app/lib/actions/entityManagement/projectActions";
|
import { getProjects } from "@/app/lib/actions/entityManagement/projectActions";
|
||||||
import { Bucket, Project, Post, dbSync, Attachment } from "@/model/Models";
|
import { Bucket, Project, Post, dbSync, Attachment } from "@/model/Models";
|
||||||
|
import { tryCreateAttachment } from "@/app/api/attachment/route";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
children?:ReactNode
|
children?:ReactNode
|
||||||
@ -29,9 +30,10 @@ export default async function PostView(props:Props){
|
|||||||
]
|
]
|
||||||
const actions = {
|
const actions = {
|
||||||
deletePost: deletePost,
|
deletePost: deletePost,
|
||||||
getPosts:getPosts, getProjects:
|
getPosts:getPostsWithBuckets,
|
||||||
getProjects,
|
getProjects:getProjects,
|
||||||
savePost:updatePost
|
savePost:updatePost,
|
||||||
|
// uploadAttachment:tryCreateAttachment
|
||||||
};
|
};
|
||||||
|
|
||||||
const posts:Post[] = await Post.findAll({include: {model: Bucket, include: {model: Attachment}}}).then(posts=>posts.map((e)=>JSON.parse(JSON.stringify(e))));
|
const posts:Post[] = await Post.findAll({include: {model: Bucket, include: {model: Attachment}}}).then(posts=>posts.map((e)=>JSON.parse(JSON.stringify(e))));
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { Association, BelongsToGetAssociationMixin, BelongsToManyAddAssociationMixin, BelongsToManyAssociation, BelongsToManyCreateAssociationMixin, BelongsToManyGetAssociationsMixin, BelongsToManyRemoveAssociationMixin, BelongsToManySetAssociationsMixin, CreationOptional, DataTypes, ForeignKey, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize } from "@sequelize/core";import { User } from "./User";
|
import { Association, Attributes, BelongsToGetAssociationMixin, BelongsToManyAddAssociationMixin, BelongsToManyAssociation, BelongsToManyCreateAssociationMixin, BelongsToManyGetAssociationsMixin, BelongsToManyRemoveAssociationMixin, BelongsToManySetAssociationsMixin, CreationOptional, DataTypes, ForeignKey, InferAttributes, InferCreationAttributes, Model, NonAttribute, Sequelize } from "@sequelize/core";import { User } from "./User";
|
||||||
|
|
||||||
import { SqliteDialect } from '@sequelize/sqlite3';
|
import { SqliteDialect } from '@sequelize/sqlite3';
|
||||||
import { Attribute, AutoIncrement, BelongsTo, BelongsToMany, CreatedAt, Default, HasMany, NotNull, PrimaryKey, Unique, UpdatedAt } from "@sequelize/core/decorators-legacy";
|
import { Attribute, AutoIncrement, BelongsTo, BelongsToMany, CreatedAt, Default, HasMany, NotNull, PrimaryKey, Unique, UpdatedAt } from "@sequelize/core/decorators-legacy";
|
||||||
@ -9,7 +9,15 @@ import { Attachment } from "./Attachment";
|
|||||||
import { Bucket } from "./Bucket";
|
import { Bucket } from "./Bucket";
|
||||||
import { UUID } from "crypto";
|
import { UUID } from "crypto";
|
||||||
|
|
||||||
export class Post extends Model<InferAttributes<Post>, InferCreationAttributes<Post>> {
|
type PostAttributes = InferAttributes<Post>;
|
||||||
|
|
||||||
|
type PostCreationAttributes = InferCreationAttributes<Post>;
|
||||||
|
|
||||||
|
export type PostAttributesWithBuckets = PostAttributes & {
|
||||||
|
buckets:Attributes<Bucket>[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Post extends Model<PostAttributes, PostCreationAttributes> {
|
||||||
|
|
||||||
|
|
||||||
// Attributes
|
// Attributes
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user