Skip to content
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

Calendar API not working #420

Open
eliandoran opened this issue Sep 12, 2024 · 0 comments
Open

Calendar API not working #420

eliandoran opened this issue Sep 12, 2024 · 0 comments

Comments

@eliandoran
Copy link

eliandoran commented Sep 12, 2024

See snippet:

var quarterOfYear = require('dayjs/plugin/quarterOfYear');
var isoWeek = require('dayjs/plugin/isoWeek');
api.dayjs.extend(quarterOfYear);
api.dayjs.extend(isoWeek);

const calendarRootNote = api.getNoteWithLabel('calendarRoot');
if (!calendarRootNote) {
    console.log('calendarRootNote does not exist');
    return;
}

const dayNote = api.getNote(api.originEntity.noteId);
const noteDate = dayNote.getOwnedLabelValue('dateNote');
if (!noteDate) {
    console.log('Not a day note');
    return;
}

const noteYear = noteDate.split('-')[0];
const noteMonth = noteDate.split('-')[1];

const noteQuarter = 'Q' + api.dayjs(`${noteYear}-${noteMonth}`).quarter();
const noteWeek = 'W' + `0${api.dayjs(noteDate).isoWeek()}`.slice(-2);

const yearNote = api.getYearNote(noteYear);
const quarterNote = getNote(
    'quarterNote',
    `${noteYear}-${noteQuarter}`,
    'ZoQ5rqHgTj5x',
    yearNote.noteId
);
const quarterOrMonthNotes = yearNote.getChildNotes();
for (let i = 0; i < quarterOrMonthNotes.length; i++) {
    if (quarterOrMonthNotes[i].getAttribute('label', 'monthNote')) {
        moveNote(quarterOrMonthNotes[i], quarterNote.noteId, yearNote.noteId);
    }
}

const monthNote = api.getNoteWithLabel('monthNote', `${noteYear}-${noteMonth}`);
const weekNote = getNote('weekNote', `${noteYear}-${noteWeek}`, 'dr6Zu0nCmsNn', monthNote.noteId);
moveNote(dayNote, weekNote.noteId, monthNote.noteId);

const currentMonthNotes = weekNote.getParentNotes();
let cloneWeekNote = true;
for (let i = 0; i < currentMonthNotes.length; i++){
    console.log(currentMonthNotes[i])
    if (currentMonthNotes[i].getAttribute('label', 'monthNote') === `${noteYear}-${noteMonth}`){
        cloneWeekNote = false;
    }
}

if (cloneWeekNote){
    weekNote.cloneTo(monthNote.noteId);
}
if (!dayNote.title.startsWith(`${noteYear}-${noteMonth}`)){
    dayNote.title = `${noteYear}-${noteMonth}-` + dayNote.title;
}
if (!monthNote.title.startsWith(noteYear)){
    monthNote.title = `${noteYear}-` + monthNote.title;
}

getNote

module.exports = function (labelName, labelValue, templateId, newParentNoteId) {
    let note=api.getNoteWithLabel(labelName, labelValue);
    let calendarNote=api.getNoteWithLabel('calendarRoot');
    if(!note){
        note=api.createTextNote(calendarNote.noteId,labelValue,"").note;
        note.setRelation('template',templateId);
        note.setLabel(labelName,labelValue);
        note.setLabel('sorted');
        
        moveNote(note, newParentNoteId, calendarNote.noteId);
    }

    return note
}

moveNote

module.exports = function (movingNote, newParentNoteId, oldParentNoteId) {
    movingNote.cloneTo(newParentNoteId);
    api.ensureNoteIsAbsentFromParent(movingNote.noteId,oldParentNoteId);
}

image

It's creating a week note if not exist and move the day note to the weeknote. But now it can only create day note under month note, it either create new week note nor move day note to existing week note.

It runs on note attribute creation before, and I have tried to run on note creation. Both doesn't work now.

Originally posted by @JYC333 in #417 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant