Skip to content

Invalid Form Locking Behavior  #139

@jpatel2005

Description

@jpatel2005

When viewing any of the forms, refreshing the page will result in the page being falsely reported as locked.

Additionally, the lockProject code (see below) has an issue. Specifically, the project-already-locked error is not fully propagated up, so the return result is: { success: false, errorCode: "unknown" }.

/**
 * Lock a project for editing (set isLocked to true)
 */
const lockProject = async (projectName: string): Promise<Result> => {
    try {
        // first get the doc ID by projectName
        const q = query(collection(db, "projects"), where("projectName", "==", projectName));
        const querySnapshot = await getDocs(q);
        if (querySnapshot.empty) {
            return { success: false, errorCode: "project-not-found" };
        }

        const docRef = querySnapshot.docs[0].ref; // use actual doc ref

        await runTransaction(db, async (tx) => {
            const snap = await tx.get(docRef);
            const projectData = snap.data();

            if (projectData?.isLocked) {
                throw new Error("project-already-locked"); // <----- this error is never handled/propagated
            }

            tx.update(docRef, {
                isLocked: true,
                lastUpdated: serverTimestamp()
            });
        });

        return { success: true };
    } catch (error) {
        return handleFirebaseError(error);
    }
};

The locking process also seems quite buggy/problematic in general (e.g. race conditions are present). Rather than checking if the lock is free, then attempting to obtain a lock, it would be better to just attempt to to obtain the lock. If this fails due to the lock already being held, then the locked popup can be displayed, or else, the lock can be obtained and everything can proceed as normal.

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions