-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d258a6d
commit 25f400b
Showing
5 changed files
with
169 additions
and
19 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
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,19 @@ | ||
import Realm, { BSON } from "realm"; | ||
|
||
export class Item extends Realm.Object<Item> { | ||
_id!: BSON.ObjectId; | ||
isComplete!: boolean; | ||
summary!: string; | ||
owner_id!: string; | ||
|
||
static schema: Realm.ObjectSchema = { | ||
name: "Item", | ||
primaryKey: "_id", | ||
properties: { | ||
_id: { type: "objectId", default: () => new BSON.ObjectId() }, | ||
isComplete: { type: "bool", default: false }, | ||
summary: "string", | ||
owner_id: "string", | ||
}, | ||
}; | ||
} |
57 changes: 57 additions & 0 deletions
57
examples/frameworks/react-test-app/source/pages/LoginPage.tsx
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,57 @@ | ||
import { useState } from "react"; | ||
import { useAuth, useEmailPasswordAuth } from "@realm/react"; | ||
|
||
export const LoginPage = () => { | ||
const { logInWithAnonymous } = useAuth(); | ||
const { logIn, register } = useEmailPasswordAuth(); | ||
|
||
const [email, setEmail] = useState(""); | ||
const [password, setPassword] = useState(""); | ||
|
||
const loginAnonymous = () => { | ||
logInWithAnonymous(); | ||
}; | ||
|
||
const loginEmailPassword = () => { | ||
logIn({ email, password }); | ||
}; | ||
|
||
const registerUser = () => { | ||
register({ email, password }); | ||
|
||
setTimeout(() => { | ||
loginEmailPassword(); | ||
}, 500); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<div> | ||
<p>Create a new account or log into an existing account</p> | ||
<input | ||
onChange={(e) => setEmail(e.target.value)} | ||
value={email} | ||
placeholder="Email..." | ||
/> | ||
<input | ||
onChange={(e) => setPassword(e.target.value)} | ||
value={password} | ||
placeholder="Password..." | ||
/> | ||
|
||
<div> | ||
<button onClick={registerUser}>Create account</button> | ||
<button onClick={loginEmailPassword}>Log in</button> | ||
</div> | ||
</div> | ||
|
||
<div> | ||
<p> | ||
Log in to continue. The button below will log you in with an anonymous | ||
App Services user. | ||
</p> | ||
<button onClick={loginAnonymous}>Log in</button> | ||
</div> | ||
</div> | ||
); | ||
}; |
43 changes: 43 additions & 0 deletions
43
examples/frameworks/react-test-app/source/pages/TodoPage.tsx
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,43 @@ | ||
import { useState } from "react"; | ||
import { useQuery, useRealm, useUser } from "@realm/react"; | ||
import { Item } from "../models"; | ||
|
||
// TODO: This is working, but not re-rendering when new item is created. | ||
|
||
export const TodoPage = () => { | ||
const realm = useRealm(); | ||
const user = useUser(); | ||
const todoItems = useQuery(Item); | ||
|
||
const [summary, setSummary] = useState(""); | ||
|
||
const addTodo = () => { | ||
realm.write(() => { | ||
realm.create(Item, { summary: summary, owner_id: user.id }); | ||
}); | ||
}; | ||
|
||
// TODO: Add ability to complete/delete todos | ||
|
||
return ( | ||
<div> | ||
<input | ||
onChange={(e) => setSummary(e.target.value)} | ||
value={summary} | ||
placeholder="Pet the cat" | ||
/> | ||
|
||
<button onClick={addTodo}>Add todo item</button> | ||
|
||
{todoItems.length ? ( | ||
<ul> | ||
{todoItems.map((todo) => ( | ||
<li>{todo.summary}</li> | ||
))} | ||
</ul> | ||
) : ( | ||
<p>Nothing to do</p> | ||
)} | ||
</div> | ||
); | ||
}; |