Reef.debug(true); const store = new Reef.Store({ data: { hash: { board: null }, loading: false, boards: [], board: null, addPin: { active: false, boardId: "", newBoardName: null, imageUrl: "", previewReady: false, previewImageUrl: null, siteUrl: "", description: "", saveInProgress: false }, pinZoom: { active: false, pin: null, fullDescriptionOpen: false }, aboutModal: { active: false }, editBoard: { active: false, name: "" } }, getters: { isAddPinValid: (data) => { if ( data.addPin.boardId == "new"){ if ( !data.addPin.newBoardName ){ return false; } else if ( data.addPin.newBoardName.trim().length < 1 ){ return false; } } if ( !data.addPin.previewImageUrl ){ return false; } return true; } } }); // since we can't dynamically set setters/getters in Reef, // we'll create our own outside 'store' const actions = new Proxy(new function(){ const _actions = {}; this.add = (actionName, f) => { if ( _actions[actionName] ){ console.error(`action ${actionName} is already defined.`); } else { console.log(`Added action ${actionName}`); _actions[actionName] = f; } }; this.do = (actionName, target) => { console.log(_actions); if (!_actions[actionName]){ console.error(`action ${actionName} is not defined.`); } else { console.log(`running action ${actionName}`); _actions[actionName](store.data, target); } } set = () => { console.error("Use actions.do(name, function)."); } }, { get(target, name, receiver){ console.log("target"); console.log(target); console.log("name"); console.log(name); console.log("receiver"); console.log(receiver); return Reflect.get(target, name, receiver); }, set(target, name, receiver){ console.error("Direct modification of actions is not allowed. Use actions.do(name, function) instead."); } }); function getBoardIndexById(id){ let idx = -1; for ( let i = 0; i < store.data.boards.length; ++i ){ if ( store.data.boards[i].id == id ){ idx = i; } } return idx; } function getBoardById(id){ return store.data.boards[getBoardIndexById(id)]; } function getPinIndexById(id){ let idx = -1; for ( let i = 0; i < store.data.board.pins.length; ++i ){ if ( store.data.board.pins[i].id == id ){ idx = i; } } return idx; } function getPinById(id){ return store.data.board.pins[getPinIndexById(id)]; } // const actions = { // deletePin: async () => { // if ( confirm("Are you sure you want to delete this pin?") ){ // store.data.loading++; // let pinId = store.data.pinZoom.pin.id; // let idx = getPinIndexById(pin.id); // if ( idx >= 0 ){ // store.data.board.pins.splice(idx,1); // } // actions.closePinZoomModal(); // let res = await fetch(`/api/pins/${pinId}`, { // method: "DELETE" // }); // if ( res.status == 200 ){ // console.log(`deleted pin#${pinId}`); // } else { // console.error(`error deleting pin#${pinId}`); // } // store.data.loading--; // } // }, // } const app = new Reef("#app", { store: store, template: (data) => { return /*html*/`