-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
observable collection
- Loading branch information
Showing
6 changed files
with
406 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** @jsx jsx */ | ||
import { jsx, Styled } from 'theme-ui'; | ||
import { useState, useEffect } from 'react'; | ||
import { useForm } from 'react-hook-form'; | ||
import { NextPage } from 'next'; | ||
|
||
import withAuthUser from '../utils/pageWrappers/withAuthUser'; | ||
import PageLayout from '../components/PageLayout'; | ||
import { PropsWithAuthUserInfo } from '../interfaces'; | ||
import { firestore, collectionData, functions } from '../utils/auth/initFirebase'; | ||
|
||
interface AddUrlProps extends PropsWithAuthUserInfo { | ||
data: Datum[]; | ||
} | ||
|
||
interface Datum { | ||
id: string; | ||
metaTitle: string; | ||
} | ||
|
||
const AddUrlPage: NextPage<{}> = (props): JSX.Element => { | ||
const [data, setData] = useState([]); | ||
const [loading, setLoading] = useState(false); | ||
const [error, setError] = useState(''); | ||
const { register, handleSubmit } = useForm(); | ||
|
||
const onSubmit = async (data): Promise<void> => { | ||
try { | ||
setLoading(true); | ||
const addURL = functions.httpsCallable('addURL'); | ||
await addURL(data); | ||
setLoading(false); | ||
} catch (error) { | ||
console.error(error); | ||
setError(error.message); | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
const query = firestore | ||
.collection('pages') | ||
.orderBy('created', 'desc') | ||
.limit(3); | ||
const subscription = collectionData(query, 'id').subscribe(setData); | ||
// Specify how to clean up after this effect: | ||
return function cleanup(): void { | ||
subscription.unsubscribe(); | ||
}; | ||
}, []); | ||
return ( | ||
<PageLayout> | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
{error ? <h3 className="error">{error}</h3> : null} | ||
<label htmlFor="url">URL</label> | ||
<input name="url" required ref={register} /> | ||
<button disabled={loading}>{loading ? 'Loading...' : 'Add URL'}</button> | ||
</form> | ||
{data.map( | ||
(page): JSX.Element => ( | ||
<Styled.h3 key={page.id}>{!page.metaTitle ? page.url : page.metaTitle}</Styled.h3> | ||
) | ||
)} | ||
</PageLayout> | ||
); | ||
}; | ||
|
||
export default withAuthUser(AddUrlPage); |
25 changes: 25 additions & 0 deletions
25
packages/poolbase-app/src/functions/handlers/pageCreateHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import * as functions from 'firebase-functions'; | ||
import { scrapeHTML } from '../processing'; | ||
import { firestore } from '../initFirebase'; | ||
|
||
export const pageCreateHandler = functions | ||
.region('europe-west1') | ||
.runWith({ | ||
timeoutSeconds: 540, | ||
memory: '1GB', | ||
}) | ||
.firestore.document('pages/{pageId}') | ||
.onCreate(async (snapshot, context) => { | ||
const page = snapshot.data(); | ||
try { | ||
if (typeof page !== 'undefined' && typeof page.url !== 'undefined') { | ||
const data = await scrapeHTML(page.url); | ||
firestore | ||
.collection('pages') | ||
.doc(context.params.pageId) | ||
.update(data); | ||
} | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.