Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Brecht De Rooms committed Apr 27, 2020
1 parent c5fad0a commit b122473
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 70 deletions.
2 changes: 1 addition & 1 deletion src/components/card.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const Card = props => {
<div className="fweet-card-text">
<p className="fweet-description"> {fweetAndMore.fweet.message} </p>
</div>
<div className="fweet-card-text refweet">
<div className="fweet-card-text refweet">
<div className="fweet-header">
<span className="fweet-name"> {fweetAndMore.original.user.name} </span>
<span className="fweet-alias"> @{fweetAndMore.original.user.alias} </span>
Expand Down
9 changes: 7 additions & 2 deletions src/components/fweeter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Uploader } from './uploader'
import Asset from './asset'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faPaperPlane } from '@fortawesome/free-solid-svg-icons'
import { toast } from 'react-toastify'

const Fweeter = props => {
const [fweet, setFweet] = useState('')
Expand All @@ -19,8 +20,12 @@ const Fweeter = props => {

const handleSubmit = event => {
event.preventDefault()

if (!fweet) return

if (!fweet) {
toast.warn('Please enter some text first :)')
return
}

props.handleCreateFweet(fweet, asset).then(e => {
setFweet('')
setAsset(null)
Expand Down
8 changes: 5 additions & 3 deletions src/components/nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { isFunction } from '../fauna/helpers/util'

const renderLogo = sessionContext => {
return (
<div className="fauna-logo">
<div key="link_logo" className="fauna-logo">
<Link className="logo-container" to="/">
<img alt="Fauna logo" src="/images/logo-fauna-white.svg" />
</Link>
Expand Down Expand Up @@ -53,13 +53,15 @@ const renderLink = (link, sessionContext) => {
if (link.handleClick) {
return (
<li onClick={event => handleLogout(event, sessionContext)} key={`nav-link-${link.label}`}>
<Link>{link.label}</Link>
<Link key={'link_' + link.label}>{link.label}</Link>
</li>
)
} else {
return (
<li key={`nav-link-${link.href}-${link.label}`}>
<Link to={link.href}>{link.label}</Link>
<Link key={'link_' + link.label} to={link.href}>
{link.label}
</Link>
</li>
)
}
Expand Down
16 changes: 1 addition & 15 deletions src/fauna/helpers/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,6 @@ const wrapPromiseError = (promise, entity) => {
})
}

const alias = (promise, tag, printData) => {
return promise
.then(data => {
// console.log('Success call:' + tag)
if (printData) console.log(data)
return data
})
.catch(error => {
console.error('Failed to execute: ' + tag)
console.error(error)
throw error
})
}

const handleSetupError = (promise, entity) => {
return promise
.then(data => {
Expand Down Expand Up @@ -64,4 +50,4 @@ const safeVerifyError = (error, keys) => {
}
return error
}
export { handlePromiseError, wrapPromiseError, alias, handleSetupError, safeVerifyError }
export { handlePromiseError, wrapPromiseError, handleSetupError, safeVerifyError }
14 changes: 10 additions & 4 deletions src/fauna/queries/auth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CreateOrUpdateFunction, CreateAccountUDF } from './../setup/functions'
import { setupProtectedResource, setupDatabaseAuthSpec, deleteAndCreateDatabase } from '../setup/database'
import { DeleteAllAccounts } from '../setup/accounts'

import { alias, wrapPromiseError } from './../helpers/errors'
import { handlePromiseError, wrapPromiseError } from './../helpers/errors'
import { register, login } from './auth'

// About this spec:
Expand All @@ -24,14 +24,20 @@ beforeAll(async () => {
const adminClient = new faunadb.Client({
secret: process.env.REACT_APP_TEST__ADMIN_KEY
})
const secret = await alias(deleteAndCreateDatabase(adminClient, 'auth-spec'), 'Creating temporary test database')
const secret = await handlePromiseError(
deleteAndCreateDatabase(adminClient, 'auth-spec'),
'Creating temporary test database'
)
client = new faunadb.Client({
secret: secret
})
// Setup the database for this test.
await alias(setupDatabaseAuthSpec(client), 'Setup Database')
await handlePromiseError(setupDatabaseAuthSpec(client), 'Setup Database')
// Set up a resource that we should only be able to access after logging in.
misterProtectedRef = await alias(setupProtectedResource(client), 'Setup a protected collection and entity')
misterProtectedRef = await handlePromiseError(
setupProtectedResource(client),
'Setup a protected collection and entity'
)
} catch (err) {
console.error(err)
}
Expand Down
26 changes: 16 additions & 10 deletions src/fauna/queries/fweets.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import faunadb from 'faunadb'

import { setupDatabase, deleteAndCreateDatabase } from '../setup/database'

import { alias } from './../helpers/errors'
import { handlePromiseError } from './../helpers/errors'
import { registerWithUser, login } from './auth'
import { getFweets, createFweet, createFweetWithoutUDF } from './fweets'
import { follow } from './followers'
Expand All @@ -29,34 +29,40 @@ beforeAll(async () => {
secret: process.env.REACT_APP_TEST__ADMIN_KEY
})
// Create the admin client for the new database to bootstrap things
const secret = await alias(
const secret = await handlePromiseError(
deleteAndCreateDatabase(adminClientParentDb, 'fweets-spec'),
'Creating temporary test database'
)
adminClient = new faunadb.Client({
secret: secret
})
// Setup the database for this test.
await alias(setupDatabase(adminClient), 'Setup Database')
await handlePromiseError(setupDatabase(adminClient), 'Setup Database')

// Create a client with a login key (getting privileges from 'memberships' in roles)
// We create a user directly as well
await alias(registerWithUser(adminClient, '[email protected]', 'testtest'), 'Register with User')
const res = await alias(login(adminClient, '[email protected]', 'testtest'), 'Login')
await handlePromiseError(registerWithUser(adminClient, '[email protected]', 'testtest'), 'Register with User')
const res = await handlePromiseError(login(adminClient, '[email protected]', 'testtest'), 'Login')
loggedInClient = new faunadb.Client({ secret: res.secret })
user1Ref = res.user.ref

await alias(registerWithUser(adminClient, '[email protected]', 'testtest'), 'Register with User')
const res2 = await alias(login(adminClient, '[email protected]', 'testtest'), 'Login')
await handlePromiseError(registerWithUser(adminClient, '[email protected]', 'testtest'), 'Register with User')
const res2 = await handlePromiseError(login(adminClient, '[email protected]', 'testtest'), 'Login')
loggedInClient2 = new faunadb.Client({ secret: res2.secret })

// Create a client with the bootstrap key (assuming the bootstrap role)
const key = await alias(adminClient.query(CreateKey({ role: Role('keyrole_calludfs') })), 'Creating Bootstrap Key')
const key = await handlePromiseError(
adminClient.query(CreateKey({ role: Role('keyrole_calludfs') })),
'Creating Bootstrap Key'
)
bootstrapClient = new faunadb.Client({ secret: key.secret })

user2Ref = res2.user.ref
await alias(createFweet(loggedInClient, 'Tweet user 1 #tag1', ['tag1']), 'Creating Fweet 1')
await alias(createFweet(loggedInClient2, 'Tweet user 2 #tag2 #tag3', ['tag2', 'tag3']), 'Creating Fweet 2')
await handlePromiseError(createFweet(loggedInClient, 'Tweet user 1 #tag1', ['tag1']), 'Creating Fweet 1')
await handlePromiseError(
createFweet(loggedInClient2, 'Tweet user 2 #tag2 #tag3', ['tag2', 'tag3']),
'Creating Fweet 2'
)

return
// Set up a resource that we should only be able to access after logging in.
Expand Down
6 changes: 3 additions & 3 deletions src/fauna/queries/rate-limiting.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DeleteAllRatelimiting } from '../setup/rate-limiting'
import { DeleteAllAccounts } from '../setup/accounts'
import { DeleteAllUsers } from '../setup/users'
import { registerWithUser, login } from './auth'
import { alias } from './../helpers/errors'
import { handlePromiseError } from './../helpers/errors'

// the rate-limiting function we are actually testing
import { AddRateLimiting } from './rate-limiting'
Expand Down Expand Up @@ -35,7 +35,7 @@ beforeAll(async () => {
adminClient = new faunadb.Client({
secret: process.env.REACT_APP_TEST__ADMIN_KEY
})
const secret = await alias(
const secret = await handlePromiseError(
deleteAndCreateDatabase(adminClient, 'ratelimiting-spec'),
'Creating temporary test database'
)
Expand All @@ -44,7 +44,7 @@ beforeAll(async () => {
secret: secret
})
// Setup the database for this test
await alias(setupDatabaseRateLimitingSpec(adminClient), 'Setup Database')
await handlePromiseError(setupDatabaseRateLimitingSpec(adminClient), 'Setup Database')
} catch (err) {
console.error(err)
}
Expand Down
13 changes: 8 additions & 5 deletions src/fauna/queries/search.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { DeleteAllRatelimiting } from '../setup/rate-limiting'
import { DeleteAllAccounts } from '../setup/accounts'
import { DeleteAllUsers } from '../setup/users'
import { registerWithUser, login } from './auth'
import { alias } from './../helpers/errors'
import { handlePromiseError } from './../helpers/errors'
import { searchPeopleAndTags } from './search'
import { CreateHashtags } from './hashtags'
// About this spec:
// --------------------
// This spec shows how the autocompletion search worls.
// A search that searches over multiple collections, in this case user aliass and tags (indexes can range over multiple collections)
// A search that searches over multiple collections, in this case user aliases and tags (indexes can range over multiple collections)
// It's based on bindings which is like a calculated value.
// That means that the value to search for is automatically transformed to the 'ngrams' that support the search

Expand Down Expand Up @@ -45,13 +45,16 @@ beforeAll(async () => {
adminClient = new faunadb.Client({
secret: process.env.REACT_APP_TEST__ADMIN_KEY
})
const secret = await alias(deleteAndCreateDatabase(adminClient, 'search-spec'), 'Creating temporary test database')
const secret = await handlePromiseError(
deleteAndCreateDatabase(adminClient, 'search-spec'),
'Creating temporary test database'
)
// Scope key to the new database
adminClient = new faunadb.Client({
secret: secret
})
// Setup the database for this test
await alias(setupDatabaseSearchSpec(adminClient), 'Setup Database')
await handlePromiseError(setupDatabaseSearchSpec(adminClient), 'Setup Database')
} catch (err) {
console.error(err)
}
Expand All @@ -78,7 +81,7 @@ beforeEach(async () => {
}
}, 1200000)

it('We can autocomplete tags and user aliass', function() {
it('We can autocomplete tags and user aliases', function() {
let loggedInClient = null

return (
Expand Down
31 changes: 17 additions & 14 deletions src/fauna/setup/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { createFweetStatsCollection } from './fweetstats'
import { createFollowerStatsCollection } from './followerstats'
import { createCommentsCollection } from './comments'

import { alias, handleSetupError } from '../helpers/errors'
import { handleSetupError } from '../helpers/errors'

const q = faunadb.query
const { Collection, CreateCollection, Create, If, Exists, Database, CreateDatabase, CreateKey, Delete, Do } = q
Expand All @@ -49,11 +49,11 @@ const { Collection, CreateCollection, Create, If, Exists, Database, CreateDataba
// Here we insert the lambdas into the database as User Defined Functions (UDF) or a sort of stored procedure.

async function deleteAndCreateDatabase(client, name) {
const database = await alias(
const database = await handleSetupError(
client.query(Do(If(Exists(Database(name)), Delete(Database(name)), false), CreateDatabase({ name: name }))),
'Deleting and recreate database'
)
const adminKey = await alias(
const adminKey = await handleSetupError(
client.query(CreateKey({ database: database.ref, role: 'admin' })),
'Create Admin key for db'
)
Expand Down Expand Up @@ -127,16 +127,19 @@ async function setupDatabaseRateLimitingSpec(client) {
}

async function setupDatabaseAuthSpec(client) {
await alias(createAccountCollection(client), 'Create Accounts Collection')
await alias(createUsersCollection(client), 'Create Users Collection')
await alias(createRateLimitingCollection(client), 'Create Rate Limiting Collection')
await handleSetupError(createAccountCollection(client), 'Create Accounts Collection')
await handleSetupError(createUsersCollection(client), 'Create Users Collection')
await handleSetupError(createRateLimitingCollection(client), 'Create Rate Limiting Collection')
await handleSetupError(createFollowerStatsCollection(client), 'followerstats collection')
await alias(client.query(CreateFnRoleLoginWithoutRateLimiting), 'Create Login Fn role (no rate limiting)')
await alias(client.query(CreateFnRoleRegisterWithoutRateLimiting), 'Create Register Fn role (no rate limiting)')
await alias(client.query(CreateLoginSimpleUDF), 'Create Login UDF')
await alias(client.query(CreateAccountUDF), 'Create Account UDF')
await alias(client.query(CreateBootstrapRoleSimple), 'Create Bootstrap Role')
await alias(client.query(CreatePowerlessRole), 'Create Powerless Role')
await handleSetupError(client.query(CreateFnRoleLoginWithoutRateLimiting), 'Create Login Fn role (no rate limiting)')
await handleSetupError(
client.query(CreateFnRoleRegisterWithoutRateLimiting),
'Create Register Fn role (no rate limiting)'
)
await handleSetupError(client.query(CreateLoginSimpleUDF), 'Create Login UDF')
await handleSetupError(client.query(CreateAccountUDF), 'Create Account UDF')
await handleSetupError(client.query(CreateBootstrapRoleSimple), 'Create Bootstrap Role')
await handleSetupError(client.query(CreatePowerlessRole), 'Create Powerless Role')
}

async function setupDatabaseSearchSpec(client) {
Expand All @@ -163,13 +166,13 @@ async function setupDatabaseSearchSpec(client) {

async function setupProtectedResource(client) {
let misterProtectedRef
await alias(
await handleSetupError(
client.query(
If(Exists(Collection('something_protected')), true, CreateCollection({ name: 'something_protected' }))
),
'Create protected collection'
)
await alias(
await handleSetupError(
client.query(Create(Collection('something_protected'), { data: { name: 'mister-protected' } })).then(res => {
misterProtectedRef = res.ref
}),
Expand Down
2 changes: 1 addition & 1 deletion src/fauna/setup/searching.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const CreateHashtagsByWordpartsWithBinding = CreateIndex({

// --- Step 3 ---
// We can index multiple collections !
// We want to search for user aliass as well, not only for tagS!
// We want to search for user aliases as well, not only for tagS!
// Since we can put multiple collections in one index, we can do that.
// In this case we decided to let users and accounts have the same property 'wordparts'.
// the users property has a different name, we can easily fix that with bindings as well.
Expand Down
27 changes: 18 additions & 9 deletions src/fauna/setup/users.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { alias } from '../helpers/errors'
import { handlePromiseError } from '../helpers/errors'
const faunadb = require('faunadb')
const q = faunadb.query
const { CreateCollection, CreateIndex, Collection, Exists, If, Index, Delete, Lambda, Paginate, Match, Var } = q
Expand Down Expand Up @@ -61,29 +61,38 @@ const DeleteAllUsers = If(
)

async function createUsersCollection(client) {
await alias(client.query(If(Exists(Collection('users')), true, CreateUsersCollection)), 'Creating users collection')
await alias(client.query(If(Exists(Index('all_users')), true, CreateIndexAllUsers)), 'Creating all_users index')
await alias(
await handlePromiseError(
client.query(If(Exists(Collection('users')), true, CreateUsersCollection)),
'Creating users collection'
)
await handlePromiseError(
client.query(If(Exists(Index('all_users')), true, CreateIndexAllUsers)),
'Creating all_users index'
)
await handlePromiseError(
client.query(If(Exists(Index('users_by_alias')), true, CreateUsersByAlias)),
'Creating users_by_alias index'
)
await alias(
await handlePromiseError(
client.query(If(Exists(Index('users_by_account')), true, CreateUsersByAccount)),
'Creating users_by_account index'
)
}

async function deleteUsersCollection(client) {
await alias(
await handlePromiseError(
client.query(If(Exists(Collection('users')), true, Delete(Collection('users')))),
'Delete users collection'
)
await alias(
await handlePromiseError(
client.query(If(Exists(Index('users_by_alias')), true, Delete(Index('users_by_alias')))),
'Delete users_by_alias index'
)
await alias(client.query(If(Exists(Index('all_users')), true, Delete(Index('all_users')))), 'Delete all_users index')
await alias(
await handlePromiseError(
client.query(If(Exists(Index('all_users')), true, Delete(Index('all_users')))),
'Delete all_users index'
)
await handlePromiseError(
client.query(If(Exists(Index('users_by_account')), true, Delete(Index('users_by_account')))),
'Delete users_by_account index'
)
Expand Down
5 changes: 2 additions & 3 deletions src/pages/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ const Home = () => {
.catch(err => {
console.log(err)
const rawError = safeVerifyError(err, ['requestResult', 'responseRaw'])
if (rawError.includes('Rate limiting')) {
if (rawError && rawError.includes('Rate limiting')) {
setState({ error: { message: 'Rate-limiting' }, fweets: [], loaded: true })
toast.warn('You are reloading too fast')
} else if (rawError.includes('permission denied')) {
console.log(err)
} else if (rawError && rawError.includes('permission denied')) {
setState({ error: { message: 'Permission denied!' }, fweets: [], loaded: true })
toast.error('No data permissions')
} else {
Expand Down

0 comments on commit b122473

Please sign in to comment.