-
Notifications
You must be signed in to change notification settings - Fork 41
feat: toggle publish unpublish documents #421
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
DennisAlund
merged 7 commits into
milestone/3-publishing
from
issue/380-toggle-publish-unpublish-documents
Aug 2, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d6e317a
add publish button
muzanella11 c292dd4
refactor crud tanam document
muzanella11 3fb39a7
remove unused
muzanella11 c9a2c85
add toggle publish document
muzanella11 d5d4305
Updating model's status implementation
DennisAlund 443045e
Updating document hook and toggle component
DennisAlund 0424873
Removing unused import
DennisAlund File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {Button} from "@/components/Button"; | ||
import {useTanamDocument} from "@/hooks/useTanamDocuments"; | ||
import {useParams} from "next/navigation"; | ||
|
||
export function TogglePublishDocument() { | ||
const {documentId} = useParams<{documentId: string}>() ?? {}; | ||
const {data: document, changeStatus} = useTanamDocument(documentId); | ||
|
||
async function onTogglePublishDocument() { | ||
if (!document) { | ||
return; | ||
} | ||
|
||
// Toggle the status of the document | ||
return changeStatus(document.status === "published" ? "unpublished" : "published"); | ||
} | ||
|
||
return ( | ||
documentId && ( | ||
<> | ||
<Button | ||
title={document?.status === "published" ? "Unpublish" : "Publish"} | ||
onClick={onTogglePublishDocument} | ||
style="outline" | ||
color="primary" | ||
/> | ||
</> | ||
) | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,10 @@ | ||
"use client"; | ||
import {TanamDocumentClient} from "@/models/TanamDocumentClient"; | ||
import {UserNotification} from "@/models/UserNotification"; | ||
import {firestore} from "@/plugins/firebase"; | ||
import {ITanamDocument} from "@functions/models/TanamDocument"; | ||
import { | ||
Timestamp, | ||
collection, | ||
doc, | ||
onSnapshot, | ||
query, | ||
serverTimestamp, | ||
setDoc, | ||
updateDoc, | ||
where, | ||
} from "firebase/firestore"; | ||
import {TanamPublishStatus} from "@functions/models/TanamDocument"; | ||
import {collection, doc, onSnapshot, query, serverTimestamp, setDoc, updateDoc, where} from "firebase/firestore"; | ||
import {useEffect, useState} from "react"; | ||
import {UserNotification} from "@/models/UserNotification"; | ||
|
||
interface UseTanamDocumentsResult { | ||
data: TanamDocumentClient[]; | ||
|
@@ -59,6 +50,7 @@ export function useTanamDocuments(documentTypeId?: string): UseTanamDocumentsRes | |
|
||
interface UseTanamDocumentResult { | ||
data: TanamDocumentClient | null; | ||
changeStatus: (status: TanamPublishStatus) => Promise<void>; | ||
error: UserNotification | null; | ||
} | ||
|
||
|
@@ -92,67 +84,66 @@ export function useTanamDocument(documentId?: string): UseTanamDocumentResult { | |
return () => unsubscribe(); | ||
}, [documentId]); | ||
|
||
return {data, error}; | ||
} | ||
|
||
export function useCrudTanamDocument(documentId?: string) { | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [error, setError] = useState<UserNotification | null>(null); | ||
/** | ||
* Method to publish or unpublish a document | ||
* | ||
* @param {TanamPublishStatus} status Flag to publish or unpublish the document | ||
* @return {Promise<void>} Promise | ||
*/ | ||
async function changeStatus(status: TanamPublishStatus): Promise<void> { | ||
if (!documentId) { | ||
setError(new UserNotification("error", "Missing parameter", "Document id parameter is missing")); | ||
return; | ||
} | ||
|
||
async function update(data: Partial<ITanamDocument<Timestamp>>): Promise<void> { | ||
setIsLoading(true); | ||
try { | ||
if (!documentId) { | ||
setError(new UserNotification("error", "Missing parameter", "Document id parameter is missing")); | ||
return; | ||
} | ||
|
||
const typeRef = doc(firestore, "tanam-documents", documentId); | ||
await updateDoc(typeRef, {...data, updatedAt: serverTimestamp()}); | ||
await updateDoc(typeRef, { | ||
publishedAt: status === "published" ? serverTimestamp() : null, | ||
status, | ||
} as Partial<TanamDocumentClient>); | ||
} catch (err) { | ||
setError( | ||
new UserNotification( | ||
"error", | ||
"UserNotification updating document", | ||
"An error occurred while updating the document", | ||
), | ||
new UserNotification("error", "Error publishing document", "An error occurred while publishing the document"), | ||
); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
} | ||
return {update, isLoading, error}; | ||
|
||
return {data, changeStatus, error}; | ||
} | ||
|
||
export function useCreateTanamDocument(documentType?: string) { | ||
const [isLoading, setIsLoading] = useState(false); | ||
export function useCrudTanamDocument() { | ||
const [error, setError] = useState<UserNotification | null>(null); | ||
|
||
async function create() { | ||
setIsLoading(true); | ||
async function create(documentType?: string) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perfect. This is good. |
||
try { | ||
if (!documentType) { | ||
setError(new UserNotification("error", "Missing parameter", "Document id parameter is missing")); | ||
setError(new UserNotification("error", "Missing parameter", "Document type 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); | ||
const tanamDocument = new TanamDocumentClient(docId, {data: {}, documentType}); | ||
await setDoc(docRef, tanamDocument.toJson()); | ||
return docId; | ||
} catch (err) { | ||
setError( | ||
new UserNotification( | ||
"error", | ||
"UserNotification creating document", | ||
"An error occurred while creating the document", | ||
), | ||
new UserNotification("error", "Error creating document", "An error occurred while creating the document"), | ||
); | ||
} | ||
} | ||
|
||
async function update(document: TanamDocumentClient): Promise<void> { | ||
try { | ||
const typeRef = doc(firestore, "tanam-documents", document.id); | ||
await updateDoc(typeRef, document.toJson()); | ||
} catch (err) { | ||
setError( | ||
new UserNotification("error", "Error updating document", "An error occurred while updating the document"), | ||
); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
} | ||
|
||
return {create, isLoading, error}; | ||
return {error, create, update}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need this empty
<>
. Please try remove it to confirm.