Skip to content

Commit 30b00fe

Browse files
authored
(DOCSP-27002): @realm/react-ify Create and Delete Users page (#2567)
## Pull Request Info @realm/react-ify Create and Delete Users page ### Jira - https://jira.mongodb.org/browse/DOCSP-27002 ### Staged Changes - [Create & Delete Users (React Native)](https://docs-mongodbcom-staging.corp.mongodb.com/realm/docsworker-xlarge/DOCSP-27002/sdk/react-native/manage-users/create-and-delete-users/) ### Reminder Checklist If your PR modifies the docs, you might need to also update some corresponding pages. Check if completed or N/A. - [x] Create Jira ticket for corresponding docs-app-services update(s), if any - [x] Checked/updated Admin API - [x] Checked/updated CLI reference ### Review Guidelines [REVIEWING.md](https://github.com/mongodb/docs-realm/blob/master/REVIEWING.md)
1 parent 1fe5aaa commit 30b00fe

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// :snippet-start: delete-user
2+
import React from 'react';
3+
import {useApp, useUser} from '@realm/react';
4+
// :remove-start:
5+
import {UserProvider} from '@realm/react';
6+
import {Credentials} from 'realm';
7+
import {App} from 'realm';
8+
import {AppProvider} from '@realm/react';
9+
import {render, fireEvent, waitFor} from '@testing-library/react-native';
10+
import {View, Button} from 'react-native';
11+
12+
const APP_ID = 'example-testers-kvjdy';
13+
14+
function AppWrapper() {
15+
return (
16+
<View>
17+
<AppProvider id={APP_ID}>
18+
<MyApp />
19+
</AppProvider>
20+
</View>
21+
);
22+
}
23+
24+
function MyApp() {
25+
const [loggedIn, setLoggedIn] = React.useState(false);
26+
const app = useApp();
27+
28+
React.useEffect(() => {
29+
app.logIn(Credentials.anonymous()).then(user => user && setLoggedIn(true));
30+
}, []);
31+
// ...
32+
return loggedIn ? (
33+
<View>
34+
<UserProvider>
35+
<DeleteUser />
36+
</UserProvider>
37+
</View>
38+
) : null;
39+
}
40+
// :remove-end:
41+
42+
function DeleteUser() {
43+
const app = useApp();
44+
const user = useUser();
45+
46+
async function deleteUser() {
47+
// Delete the currently logged in user
48+
await app.deleteUser(user);
49+
expect(app.currentUser).toBeNull(); // :remove:
50+
}
51+
// ...
52+
// :remove-start:
53+
return <Button onPress={deleteUser} testID='test-delete-user' title='Test Me!' />;
54+
// :remove-end:
55+
}
56+
// :snippet-end:
57+
58+
afterEach(async () => await App.getApp(APP_ID).currentUser?.logOut());
59+
60+
test('delete user', async () => {
61+
const {getByTestId} = render(<AppWrapper />);
62+
const button = await waitFor(() => getByTestId('test-delete-user'));
63+
fireEvent.press(button);
64+
await waitFor(() => expect(App.getApp(APP_ID).currentUser).toBeNull());
65+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react';
2+
import {useApp, useUser} from '@realm/react';
3+
4+
function DeleteUser() {
5+
const app = useApp();
6+
const user = useUser();
7+
8+
async function deleteUser() {
9+
// Delete the currently logged in user
10+
await app.deleteUser(user);
11+
}
12+
// ...
13+
}

source/sdk/react-native/manage-users/create-and-delete-users.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ Call the :js-sdk:`App.deleteUser() <Realm.App.html#deleteUser>` on a user object
3434
the user's account from your Realm application. This deletes the account from
3535
the server in addition to clearing local data.
3636

37-
38-
.. literalinclude:: /examples/generated/node/authenticate.snippet.delete-user.js
39-
:language: javascript
37+
.. literalinclude:: /examples/generated/react-native/ts/delete-user.test.snippet.delete-user.tsx
38+
:language: typescript
4039

4140
To use your app in the future, the user must sign up for a new account.
4241
They can use the same credentials (depending on the authentication provider),

0 commit comments

Comments
 (0)