1
0
mirror of https://github.com/chinchang/web-maker.git synced 2025-07-18 12:31:12 +02:00

add routing

This commit is contained in:
Kushagra Gour
2024-02-28 09:35:36 +05:30
parent 73df9ccf5b
commit de56ee3e1f
5 changed files with 38 additions and 12 deletions

View File

@@ -147,7 +147,7 @@ export function MainHeader(props) {
} }
class="main-header__avatar-img" class="main-header__avatar-img"
/> />
{props.user && props.user.isPro ? <ProBadge /> : null} {props.user?.isPro ? <ProBadge /> : null}
</HStack> </HStack>
</Button> </Button>
)} )}

View File

@@ -245,6 +245,10 @@ export default class App extends Component {
this.setCurrentItem(this.state.currentItem).then(() => { this.setCurrentItem(this.state.currentItem).then(() => {
this.refreshEditor(); this.refreshEditor();
}); });
} else if (this.props.itemId) {
window.db.fetchItem(this.props.itemId).then(item => {
this.setCurrentItem(item).then(() => this.refreshEditor());
});
} else if (result.preserveLastCode && lastCode) { } else if (result.preserveLastCode && lastCode) {
this.setState({ unsavedEditCount: 0 }); this.setState({ unsavedEditCount: 0 });
log('Load last unsaved item', lastCode); log('Load last unsaved item', lastCode);
@@ -570,6 +574,7 @@ export default class App extends Component {
} }
componentDidMount() { componentDidMount() {
console.log('itemId', this.props.itemId);
function setBodySize() { function setBodySize() {
document.body.style.height = `${window.innerHeight}px`; document.body.style.height = `${window.innerHeight}px`;
} }

View File

@@ -59,7 +59,7 @@ import { log } from './utils';
return firestoreInstance return firestoreInstance
.enablePersistence({ experimentalTabSynchronization: true }) .enablePersistence({ experimentalTabSynchronization: true })
.then(function() { .then(function () {
// Initialize Cloud Firestore through firebase // Initialize Cloud Firestore through firebase
db = firebase.firestore(); db = firebase.firestore();
// const settings = { // const settings = {
@@ -69,7 +69,7 @@ import { log } from './utils';
log('firebase db ready', db); log('firebase db ready', db);
resolve(db); resolve(db);
}) })
.catch(function(err) { .catch(function (err) {
reject(err.code); reject(err.code);
if (err.code === 'failed-precondition') { if (err.code === 'failed-precondition') {
// Multiple tabs open, persistence can only be enabled // Multiple tabs open, persistence can only be enabled
@@ -113,7 +113,7 @@ import { log } from './utils';
{ {
lastSeenVersion: version lastSeenVersion: version
}, },
function() {} function () {}
); );
if (window.user) { if (window.user) {
const remoteDb = await getDb(); const remoteDb = await getDb();
@@ -142,6 +142,18 @@ import { log } from './utils';
}); });
} }
async function fetchItem(itemId) {
const remoteDb = await getDb();
return remoteDb
.doc(`items/${itemId}`)
.get()
.then(doc => {
if (!doc.exists) return {};
const data = doc.data();
return data;
});
}
// Fetch user settings. // Fetch user settings.
// This isn't hitting the remote db because remote settings // This isn't hitting the remote db because remote settings
// get fetch asynchronously (in user/) and update the envioronment. // get fetch asynchronously (in user/) and update the envioronment.
@@ -161,6 +173,7 @@ import { log } from './utils';
getUserLastSeenVersion, getUserLastSeenVersion,
setUserLastSeenVersion, setUserLastSeenVersion,
getSettings, getSettings,
fetchItem,
local: dbLocalAlias, local: dbLocalAlias,
sync: dbSyncAlias sync: dbSyncAlias
}; };

View File

@@ -1,3 +1,4 @@
import Router from 'preact-router';
import App from './components/app.jsx'; import App from './components/app.jsx';
import './lib/codemirror/lib/codemirror.css'; import './lib/codemirror/lib/codemirror.css';
@@ -8,4 +9,11 @@ import './lib/hint.min.css';
import './lib/inlet.css'; import './lib/inlet.css';
import './style.css'; import './style.css';
export default App; export default function () {
return (
<Router>
<App path="/" />
<App path="/creation/:itemId" />
</Router>
);
}

View File

@@ -1,5 +1,5 @@
import { deferred } from './deferred'; import { deferred } from './deferred';
import { log } from 'util'; import { log } from './utils';
import firebase from 'firebase/app'; import firebase from 'firebase/app';
export const itemService = { export const itemService = {
@@ -52,15 +52,15 @@ export const itemService = {
.collection('items') .collection('items')
.where('createdBy', '==', window.user.uid) .where('createdBy', '==', window.user.uid)
.onSnapshot( .onSnapshot(
function(querySnapshot) { function (querySnapshot) {
querySnapshot.forEach(function(doc) { querySnapshot.forEach(function (doc) {
items.push(doc.data()); items.push(doc.data());
}); });
log('Items fetched in ', Date.now() - t, 'ms'); log('Items fetched in ', Date.now() - t, 'ms');
d.resolve(items); d.resolve(items);
}, },
function() { function () {
d.resolve([]); d.resolve([]);
} }
); );
@@ -147,7 +147,7 @@ export const itemService = {
{ {
items: {} items: {}
}, },
function(result) { function (result) {
/* eslint-disable guard-for-in */ /* eslint-disable guard-for-in */
for (var id in items) { for (var id in items) {
result.items[id] = true; result.items[id] = true;
@@ -205,7 +205,7 @@ export const itemService = {
{ {
items: {} items: {}
}, },
function(result) { function (result) {
result.items[itemId] = true; result.items[itemId] = true;
window.db.local.set({ window.db.local.set({
items: result.items items: result.items
@@ -235,7 +235,7 @@ export const itemService = {
{ {
items: {} items: {}
}, },
function(result) { function (result) {
delete result.items[itemId]; delete result.items[itemId];
window.db.local.set({ window.db.local.set({
items: result.items items: result.items