-
Notifications
You must be signed in to change notification settings - Fork 73
Firestore document preview and basic-editing #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 28 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
a96ee8b
Dialog queue!
tjlav5 2c2ca14
Merge branch 'master' into firestore-clear-all
tjlav5 a4a087a
add tests
tjlav5 cd5ee37
add test to assert dialogs open
tjlav5 e0c7b6b
very basic field-preview w/ delete
tjlav5 4982977
remove accidental file
tjlav5 fb2f1d3
Merge branch 'master' into firestore-document-fields
tjlav5 1a58f25
shared state/reducer/dispatch within DocumentContext
tjlav5 fae560d
switch to reducer on serialize snapshot
tjlav5 7a40c3d
arrays are handled by lodash getter
tjlav5 54133d1
Merge branch 'master' into firestore-document-fields
tjlav5 ce1d0f4
editor uses the same context and reducer
tjlav5 781fff4
rough add to array/map
tjlav5 1dce0a9
better type narrowing
tjlav5 6433a43
type document actions
tjlav5 2192ab9
clean up comments
tjlav5 dc62e36
add tests
tjlav5 a676e8c
Merge branch 'master' into firestore-document-fields
tjlav5 3ee122d
basic tests
tjlav5 5e4fab7
PoC reducer with DocumentReference, specifically partial updates/dele…
tjlav5 e92c879
delineate DocumentPreview from DocumentEditor
tjlav5 5be3408
address comments
tjlav5 a94144d
revert dialogqueue
tjlav5 e500f42
add tests
tjlav5 c6411b2
switch to fieldpath
tjlav5 9877d76
Merge branch 'master' into firestore-document-fields
tjlav5 bfd9086
Merge branch 'master' into firestore-document-fields
tjlav5 78f135c
Merge branch 'firestore-document-fields' of github.com:firebase/fireb…
tjlav5 c68267d
switch lodash.last
tjlav5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 @@ | ||
| /** | ||
| * Copyright 2020 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import React from 'react'; | ||
| import { fireEvent, render } from '@testing-library/react'; | ||
| import { firestore } from 'firebase'; | ||
|
|
||
| import DocumentEditor from './index'; | ||
|
|
||
| it('renders an editable field', () => { | ||
| const onChange = jest.fn(); | ||
| const { getByPlaceholderText } = render( | ||
| <DocumentEditor value={{ hello: 'world' }} onChange={onChange} /> | ||
| ); | ||
| expect(getByPlaceholderText('Field').value).toBe('hello'); | ||
| expect(getByPlaceholderText('Type').value).toBe('string'); | ||
| expect(getByPlaceholderText('Value').value).toBe('"world"'); | ||
|
|
||
| fireEvent.change(getByPlaceholderText('Value'), { | ||
| target: { value: 'new' }, | ||
| }); | ||
|
|
||
| expect(getByPlaceholderText('Value').value).toBe('"new"'); | ||
| expect(onChange).toHaveBeenCalledWith({ hello: 'new' }); | ||
| }); | ||
|
|
||
| it('renders an editable field with children', () => { | ||
| const onChange = jest.fn(); | ||
|
|
||
| const { getByDisplayValue } = render( | ||
| <DocumentEditor | ||
| value={{ hello: { foo: ['bar', { spam: 'eggs' }] } }} | ||
| onChange={onChange} | ||
| /> | ||
| ); | ||
|
|
||
| fireEvent.change(getByDisplayValue('"eggs"'), { | ||
| target: { value: 'new' }, | ||
| }); | ||
|
|
||
| expect(onChange).toHaveBeenCalledWith({ | ||
| hello: { foo: ['bar', { spam: 'new' }] }, | ||
| }); | ||
| }); |
This file contains hidden or 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,104 @@ | ||
| /** | ||
| * Copyright 2020 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import React, { useEffect } from 'react'; | ||
| import { TextField } from '@rmwc/textfield'; | ||
|
|
||
| import * as actions from './actions'; | ||
| import { | ||
| useDocumentState, | ||
| useDocumentDispatch, | ||
| useFieldState, | ||
| DocumentProvider, | ||
| } from './store'; | ||
| import { FirestoreMap } from '../models'; | ||
| import { isMap, isArray, getLeafKey, getFieldType } from '../utils'; | ||
|
|
||
| /** Entry point for a Document/Field editor */ | ||
| const DocumentEditor: React.FC<{ | ||
| value: FirestoreMap; | ||
| onChange?: (value: FirestoreMap) => void; | ||
| }> = ({ value, onChange }) => { | ||
| return ( | ||
| <DocumentProvider value={value}> | ||
| <RootField onChange={onChange} /> | ||
| </DocumentProvider> | ||
| ); | ||
| }; | ||
|
|
||
| /** | ||
| * Special representation of a Document Root, where we don't want to show | ||
| * the implicit top-level map. | ||
| */ | ||
| const RootField: React.FC<{ | ||
| onChange?: (value: FirestoreMap) => void; | ||
| }> = ({ onChange }) => { | ||
| const state = useDocumentState(); | ||
|
|
||
| useEffect(() => { | ||
| onChange && onChange(state); | ||
| }, [onChange, state]); | ||
|
|
||
| return ( | ||
| <> | ||
| {Object.keys(state).map(field => ( | ||
| <NestedEditor key={field} path={[field]} /> | ||
| ))} | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| /** | ||
| * Field with call-to-actions for editing as well as rendering applicable child-nodes | ||
| */ | ||
| const NestedEditor: React.FC<{ path: string[] }> = ({ path }) => { | ||
| const state = useFieldState(path); | ||
| const dispatch = useDocumentDispatch()!; | ||
|
|
||
| let childEditors = null; | ||
| if (isMap(state)) { | ||
| childEditors = Object.keys(state).map(childLeaf => { | ||
| const childPath = [...path, childLeaf]; | ||
| return <NestedEditor key={childLeaf} path={childPath} />; | ||
| }); | ||
| } else if (isArray(state)) { | ||
| childEditors = state.map((value, index) => { | ||
| const childPath = [...path, `${index}`]; | ||
| return <NestedEditor key={index} path={childPath} />; | ||
| }); | ||
| } | ||
|
|
||
| function handleEditValue(e: React.FormEvent<HTMLInputElement>) { | ||
| dispatch(actions.updateField({ path, value: e.currentTarget.value })); | ||
| } | ||
|
|
||
| return ( | ||
| <> | ||
| <div style={{ display: 'flex' }}> | ||
| <TextField readOnly value={getLeafKey(path)} placeholder="Field" /> | ||
| <TextField readOnly value={getFieldType(state)} placeholder="Type" /> | ||
| <TextField | ||
| value={JSON.stringify(state)} | ||
| onChange={handleEditValue} | ||
| placeholder="Value" | ||
| /> | ||
| </div> | ||
| {childEditors && <div>{childEditors}</div>} | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| export default DocumentEditor; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.