Skip to content

Commit

Permalink
fix last UpdatedAt date
Browse files Browse the repository at this point in the history
  • Loading branch information
maheshj01 committed Dec 22, 2024
1 parent 2c28d95 commit 0e5f752
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/app/(main)/_components/Pastelog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ export default function Pastelog({ id }: { id?: string }) {

setLoading(true);
const log = new Log({
expiryDate: expiryDate,
expiryDate: expiryDate?.toDateString(),
data: content,
type: LogType.TEXT,
title: title,
createdDate: new Date(),
createdDate: new Date().toDateString(),
isExpired: false,
summary: '',
isPublic: false,
Expand Down Expand Up @@ -166,7 +166,7 @@ export default function Pastelog({ id }: { id?: string }) {
if (log) {
setTitle(log.title!);
setContent(log.data!);
setExpiryDate(log.expiryDate!);
setExpiryDate(new Date(log.expiryDate!));
notify(false, "Log imported successfully");
onImportClose();
Analytics.logEvent('import_pastelog', { id: id, action: 'click' });
Expand All @@ -193,7 +193,7 @@ export default function Pastelog({ id }: { id?: string }) {
if (log) {
setTitle(log.title!);
setContent(log.data!);
setExpiryDate(log.expiryDate!);
setExpiryDate(new Date(log.expiryDate!));
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/(main)/_components/PreviewPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const PreviewPage = ({ logId }: { logId: string }) => {
const handleOnEdit = async (hasUpdated: boolean) => {
setLoading(true);
if (hasUpdated) {
editedLog!.lastUpdatedAt = new Date().toISOString();
editedLog!.lastUpdatedAt = new Date().toUTCString();
await logService.updateLog(logId, editedLog!);
setpreviewLog(new Log({ ...editedLog! }));
} else {
Expand Down
31 changes: 24 additions & 7 deletions src/app/(main)/_models/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class Log implements ILog {
expiryDate = null,
data,
createdDate = new Date().toUTCString(),
lastUpdatedAt = new Date().toUTCString(),
lastUpdatedAt,
type,
isMarkDown,
title = '',
Expand All @@ -62,7 +62,7 @@ export class Log implements ILog {
id?: string
}) {
this.expiryDate = expiryDate;
this.lastUpdatedAt = createdDate;
this.lastUpdatedAt = lastUpdatedAt ? lastUpdatedAt : createdDate;
this.data = data;
this.title = title;
this.isExpired = isExpired;
Expand All @@ -77,11 +77,12 @@ export class Log implements ILog {

static fromFirestore(doc: QueryDocumentSnapshot<DocumentData, DocumentData>): Log {
const data = doc.data();
console.log("firestore", data);
return new Log({
expiryDate: data.expiryDate ? data.expiryDate : null,
lastUpdatedAt: data.lastUpdatedAt,
expiryDate: data.expiryDate ? (data.expiryDate.toDate ? data.expiryDate.toDate().toUTCString() : data.expiryDate) : null,
lastUpdatedAt: data.lastUpdatedAt ?? data.createdDate,
data: data.data,
createdDate: data.createdDate,
createdDate: data.createdDate ? (data.createdDate.toDate ? data.createdDate.toDate().toUTCString() : data.createdDate) : '',
type: data.type as LogType,
isMarkDown: data.isMarkDown,
title: data.title ? data.title : '',
Expand All @@ -90,8 +91,7 @@ export class Log implements ILog {
isPublic: data.isPublic,
isExpired: data.isExpired,
id: data.id ? data.id : doc.id
}
);
});
}

toFirestore(): any {
Expand All @@ -117,5 +117,22 @@ export class Log implements ILog {
}
return doc;
}

toJson(): any {
return {
expiryDate: this.expiryDate ? this.expiryDate : null,
lastUpdatedAt: this.lastUpdatedAt,
data: this.data,
createdDate: this.createdDate,
title: this.title ? this.title : '',
type: this.type,
summary: this.summary,
userId: this.userId,
isPublic: this.isPublic,
isMarkDown: this.isMarkDown,
isExpired: this.isExpired,
id: this.id
};
}
}
export default Log;
16 changes: 5 additions & 11 deletions src/app/(main)/_services/logService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,10 @@ class LogService {
if (docRef.id) {
// if user not loggedIn save to local
if (!log.userId) {
await this.saveLogToLocal({
await this.saveLogToLocal(new Log({
...log, id: docRef.id,
toFirestore: function () {
throw new Error('Function not implemented.');
}
});
},
));
}
return docRef.id!
} else {
Expand Down Expand Up @@ -124,19 +122,15 @@ class LogService {
async publishLogWithId(log: Log, id: string): Promise<string> {
const docRef = doc(this.logCollection, id);
await setDoc(docRef, log.toFirestore());
await this.saveLogToLocal({
await this.saveLogToLocal(new Log({
...log, id: docRef.id,
toFirestore: function () {
throw new Error('Function not implemented.');
}
});
}));
return docRef.id;
}

async updateLog(id: string, log: Log): Promise<void> {
const docRef = doc(this.logCollection, id);
const data = log.toFirestore();
console.log("updating data:", data);
// await this.saveLogToLocal(log);
// Remove any undefined fields
Object.keys(data).forEach(key => data[key] === undefined && delete data[key]);
Expand Down

0 comments on commit 0e5f752

Please sign in to comment.