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
|
||||
await dbSync;
|
||||
|
||||
@ -6,6 +6,7 @@ import { Attributes, where } from "@sequelize/core";
|
||||
import { cookies } from 'next/headers';
|
||||
import { getCookieAuth, userIsAdmin } from '../actions';
|
||||
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;
|
||||
if(! await userIsAdmin()) return {error:"Unauthorized, not fetching Posts."}
|
||||
const posts = await Post.findAll({include: {association: Post.associations.buckets, include: Bucket.associations.attachments}},);
|
||||
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>[]>> {
|
||||
await dbSync;
|
||||
if(! await userIsAdmin()) return {error:"Unauthorized, not updating Post."}
|
||||
|
||||
@ -1,14 +1,16 @@
|
||||
import { ActionResult } from "@/app/lib/actions/ActionResult";
|
||||
import { handleActionResult } from "@/app/lib/actions/clientActionHandler";
|
||||
import { Post, Project, Bucket, PostBucket, Attachment } from "@/model/Models";
|
||||
import { PostAttributesWithBuckets } from "@/model/Post";
|
||||
import { Attributes } from "@sequelize/core";
|
||||
import { UUID } from "crypto";
|
||||
import { ChangeEventHandler, MouseEventHandler, useLayoutEffect, useRef, useState } from "react";
|
||||
import { Accordion, AccordionBody, AccordionHeader, AccordionItem } from "react-bootstrap";
|
||||
|
||||
export type PostTableCallbacks = {
|
||||
savePost: (p:Partial<Attributes<Post>>)=>any;
|
||||
savePost: (p:Partial<PostAttributesWithBuckets>)=>void;
|
||||
closeEditor: ()=>any;
|
||||
uploadAttachment : ()=>any;
|
||||
}
|
||||
type PostEditorBucket = Partial<Attributes<Bucket>> & {
|
||||
attachments:Partial<Attributes<Attachment>>[]
|
||||
@ -20,7 +22,7 @@ export type PostEditorPost = Attributes<Post> & {
|
||||
|
||||
export type EditorProps = {
|
||||
open:boolean;
|
||||
post:PostEditorPost;
|
||||
post:Partial<PostAttributesWithBuckets>;
|
||||
projects?:Attributes<Project>[];
|
||||
callbacks:PostTableCallbacks;
|
||||
}
|
||||
@ -79,8 +81,9 @@ export default function PostEditor(props:EditorProps){
|
||||
</thead>
|
||||
<tbody>
|
||||
{
|
||||
(()=>{
|
||||
let bucketMap:Map<UUID,PostEditorBucket> = new Map(props.post.buckets.map((b)=>[b.id as UUID,b]));
|
||||
props.post.buckets
|
||||
? (()=>{
|
||||
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)];
|
||||
return bucketList.map((e)=>{
|
||||
return <>
|
||||
@ -90,16 +93,28 @@ export default function PostEditor(props:EditorProps){
|
||||
<AccordionBody>
|
||||
|
||||
<ul>
|
||||
<li><button className='btn btn-success'>Upload new attachment</button></li>
|
||||
{ bucketMap.get(e as UUID)?.attachments.map((attachment)=><li key={`listItem-file-${attachment.filename}`}>{attachment.filename}</li>) }
|
||||
<li><input type="file" className='btn btn-success' onChange={(e)=>{
|
||||
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>
|
||||
</AccordionBody>
|
||||
</AccordionItem>
|
||||
</Accordion></tr></>
|
||||
})
|
||||
})()
|
||||
: <></>
|
||||
}
|
||||
|
||||
</tbody>
|
||||
</table>
|
||||
<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 { handleActionResult } from "@/app/lib/actions/clientActionHandler";
|
||||
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 = {
|
||||
deletePost: (id: number) => Promise<ActionResult<boolean>>
|
||||
getPosts: () => Promise<ActionResult<PostEditorPost[]>>
|
||||
getPosts: () => Promise<ActionResult<PostAttributesWithBuckets[]>>
|
||||
getProjects: () => Promise<ActionResult<Attributes<Project>[]>>
|
||||
savePost: (data: Partial<Attributes<Post>>) => Promise<ActionResult<Attributes<Post>[]>>
|
||||
// uploadAttachment: ()=> Promise<ActionResult<any>>
|
||||
}
|
||||
|
||||
type Props = {
|
||||
children?: ReactNode;
|
||||
headings: Array<string>;
|
||||
data: PostEditorPost[];
|
||||
data: Partial<PostAttributesWithBuckets>[];
|
||||
projects: Attributes<Project>[];
|
||||
actions: Actions;
|
||||
}
|
||||
@ -33,7 +35,7 @@ export default function PostTable(props: Props) {
|
||||
})
|
||||
}
|
||||
|
||||
function showEditor(entry: PostEditorPost): void {
|
||||
function showEditor(entry: Partial<PostAttributesWithBuckets>): void {
|
||||
setEditor({
|
||||
open: true,
|
||||
post: entry
|
||||
@ -49,7 +51,8 @@ export default function PostTable(props: Props) {
|
||||
const [projects, setProjects] = useState(props.projects);
|
||||
|
||||
|
||||
function deletePost(entry: PostEditorPost) {
|
||||
function deletePost(entry: Partial<PostAttributesWithBuckets>) {
|
||||
if(! entry.id) return;
|
||||
props.actions?.deletePost(entry.id)
|
||||
.then(actionResult => {
|
||||
const result = handleActionResult(actionResult);
|
||||
@ -73,7 +76,7 @@ export default function PostTable(props: Props) {
|
||||
})
|
||||
}
|
||||
|
||||
function savePost(e: Partial<PostEditorPost>) {
|
||||
function savePost(e: Partial<PostAttributesWithBuckets>) {
|
||||
props.actions.savePost({
|
||||
id: e.id,
|
||||
content: e.content,
|
||||
@ -81,21 +84,20 @@ export default function PostTable(props: Props) {
|
||||
project_id: e.project_id
|
||||
})
|
||||
.then(res => handleActionResult(res))
|
||||
.then(getPosts)
|
||||
.then(getPostsWithBuckets)
|
||||
.then(res => {
|
||||
1
|
||||
if (!res.error) setPosts(res.result as PostEditorPost[]);
|
||||
if (!res.error && res.result) setPosts(res.result);
|
||||
})
|
||||
closeEditor();
|
||||
};
|
||||
|
||||
return <>
|
||||
<TableGen headings={props.headings}>
|
||||
{posts.map((d: PostEditorPost) => <>
|
||||
{posts.map((d: Partial<PostAttributesWithBuckets>) => <>
|
||||
<tr key={d.id}>
|
||||
<th key={`row${d.id}`} scope="row">{d.id}</th>
|
||||
<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'>{
|
||||
aifa((projects.find((e) => {
|
||||
return (e.id == d.project_id)
|
||||
@ -108,7 +110,7 @@ export default function PostTable(props: Props) {
|
||||
</tr>
|
||||
{
|
||||
(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 TableGen from "../../../client/TableGen";
|
||||
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 { Bucket, Project, Post, dbSync, Attachment } from "@/model/Models";
|
||||
import { tryCreateAttachment } from "@/app/api/attachment/route";
|
||||
|
||||
type Props = {
|
||||
children?:ReactNode
|
||||
@ -29,9 +30,10 @@ export default async function PostView(props:Props){
|
||||
]
|
||||
const actions = {
|
||||
deletePost: deletePost,
|
||||
getPosts:getPosts, getProjects:
|
||||
getProjects,
|
||||
savePost:updatePost
|
||||
getPosts:getPostsWithBuckets,
|
||||
getProjects:getProjects,
|
||||
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))));
|
||||
|
||||
@ -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 { 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 { 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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user