这是indexloc提供的服务,不要输入任何密码
Skip to content

Issue/366 create a new document type #386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions functions/src/models/TanamDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export interface ITanamDocument<TimestampType> {
documentType: string;
revision?: number;
publishedAt?: TimestampType;
createdAt: TimestampType;
updatedAt: TimestampType;
createdAt?: TimestampType;
updatedAt?: TimestampType;
}

export abstract class TanamDocument<TimestampType, FieldValueType> {
Expand All @@ -29,8 +29,8 @@ export abstract class TanamDocument<TimestampType, FieldValueType> {
public documentType: string;
public publishedAt?: TimestampType;
public revision: number;
public readonly createdAt: TimestampType;
public readonly updatedAt: TimestampType;
public readonly createdAt?: TimestampType;
public readonly updatedAt?: TimestampType;

abstract get status(): TanamPublishStatus;
protected abstract getServerTimestamp(): FieldValueType;
Expand Down
12 changes: 10 additions & 2 deletions hosting/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions hosting/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"codecheck": "npm run prettier:fix && npm run lint:fix"
},
"dependencies": {
"@iconify-json/line-md": "^1.1.37",
"@tiptap/pm": "^2.4.0",
"@tiptap/react": "^2.4.0",
"@tiptap/starter-kit": "^2.4.0",
Expand Down
18 changes: 17 additions & 1 deletion hosting/src/app/(protected)/content/[documentTypeId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {useTanamDocuments} from "@/hooks/useTanamDocuments";
import {Suspense, useEffect, useState} from "react";
import {useParams} from "next/navigation";
import {UserNotification} from "@/models/UserNotification";
import {Button} from "@/components/Button";

export default function DocumentTypeDocumentsPage() {
const {documentTypeId} = useParams<{documentTypeId: string}>() ?? {};
Expand All @@ -19,10 +20,25 @@ export default function DocumentTypeDocumentsPage() {
setNotification(docsError);
}, [docsError]);

const addNewDocument = async () => {};

return (
<>
<Suspense fallback={<Loader />}>
{documentType ? <PageHeader pageName={documentType.titlePlural.translated} /> : <Loader />}
{documentType ? (
<div className="flex items-center gap-4">
<PageHeader pageName={documentType.titlePlural.translated} />{" "}
<div className="mb-6">
<Button
onClick={addNewDocument}
title={`Add New ${documentType.titleSingular.translated}`}
style="rounded"
/>
</div>
</div>
) : (
<Loader />
)}
</Suspense>
{notification && (
<Notification type={notification.type} title={notification.title} message={notification.message} />
Expand Down
32 changes: 28 additions & 4 deletions hosting/src/app/(protected)/content/article/page.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,51 @@
"use client";
import {Button} from "@/components/Button";
import {DocumentTypeGenericList} from "@/components/DocumentType/DocumentTypeGenericList";
import Loader from "@/components/common/Loader";
import Notification from "@/components/common/Notification";
import PageHeader from "@/components/common/PageHeader";
import {useTanamDocumentType} from "@/hooks/useTanamDocumentTypes";
import {useTanamDocuments} from "@/hooks/useTanamDocuments";
import {useCreateTanamDocument, useTanamDocuments} from "@/hooks/useTanamDocuments";
import {UserNotification} from "@/models/UserNotification";
import {useRouter} from "next/navigation";
import {Suspense, useEffect, useState} from "react";

export default function DocumentTypeDocumentsPage() {
const {data: documentType} = useTanamDocumentType("article");
const {create, error: writeError} = useCreateTanamDocument(documentType?.id);
const {data: documents, error: docsError} = useTanamDocuments("article");
const [notification, setNotification] = useState<UserNotification | null>(null);
const router = useRouter();

useEffect(() => {
setNotification(docsError);
}, [docsError]);
setNotification(docsError || writeError);
}, [docsError, writeError]);

const addNewArticle = async () => {
const id = await create();

router.push(`article/${id}`);
};

return (
<>
<Suspense fallback={<Loader />}>
{documentType ? <PageHeader pageName={documentType.titlePlural.translated} /> : <Loader />}
{documentType ? (
<div className="flex items-center gap-4">
<PageHeader pageName={documentType.titlePlural.translated} />
<div className="mb-6">
<Button
title={`Add New ${documentType.titleSingular.translated}`}
onClick={addNewArticle}
style="rounded"
/>
</div>
</div>
) : (
<Loader />
)}
</Suspense>

{notification && (
<Notification type={notification.type} title={notification.title} message={notification.message} />
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function DocumentTypeGenericList({documents, documentType}: TableOverview
<p className="font-medium text-black dark:text-white">{document.data[documentType.titleField] as string}</p>
</Link>,
<p key={`${key}-${document.id}-date`} className="text-black dark:text-white">
{document.createdAt.toDate().toUTCString()}
{document.createdAt?.toDate().toUTCString()}
</p>,

<TableRowLabel
Expand Down
46 changes: 44 additions & 2 deletions hosting/src/hooks/useTanamDocuments.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import {TanamDocumentClient} from "@/models/TanamDocumentClient";
import {firestore} from "@/plugins/firebase";
import {ITanamDocument} from "@functions/models/TanamDocument";
import {Timestamp, collection, doc, onSnapshot, query, serverTimestamp, updateDoc, where} from "firebase/firestore";
import {
Timestamp,
collection,
doc,
onSnapshot,
query,
serverTimestamp,
setDoc,
updateDoc,
where,
} from "firebase/firestore";
import {useEffect, useState} from "react";
import {UserNotification} from "@/models/UserNotification";

Expand Down Expand Up @@ -111,6 +121,38 @@ export function useCrudTanamDocument(documentId?: string) {
setIsLoading(false);
}
}

return {update, isLoading, error};
}

export function useCreateTanamDocument(documentType?: string) {
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<UserNotification | null>(null);

async function create() {
setIsLoading(true);
try {
if (!documentType) {
setError(new UserNotification("error", "Missing parameter", "Document id parameter is missing"));
return;
}
const docRef = doc(collection(firestore, "tanam-documents"));
const docId = docRef.id;

const tanamDocument = new TanamDocumentClient(docId, {data: {}, documentType}).toJson();
await setDoc(docRef, tanamDocument);
return docId;
} catch (err) {
setError(
new UserNotification(
"error",
"UserNotification creating document",
"An error occurred while creating the document",
),
);
} finally {
setIsLoading(false);
}
}

return {create, isLoading, error};
}
2 changes: 1 addition & 1 deletion hosting/tailwind.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ const config: Config = {
plugins: [
require("@tailwindcss/typography"),
iconsPlugin({
collections: getIconCollections(["ic", "ri"]),
collections: getIconCollections(["ic", "ri", "line-md"]),
}),
],
};
Expand Down