Skip to content
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

No way to update a document within a collection #120

Open
ryanwtyler opened this issue Dec 5, 2020 · 0 comments
Open

No way to update a document within a collection #120

ryanwtyler opened this issue Dec 5, 2020 · 0 comments

Comments

@ryanwtyler
Copy link

ryanwtyler commented Dec 5, 2020

I use FirestoreCollection to render a table of data. I want to allow the user to update any of the items within the collection but i have no access to the id of the document. All that FirestoreCollection exposes are the data.

return (
    <FirebaseAuthConsumer>
        {({ isSignedIn, user, providerId }) => {
            if (!isSignedIn)
                return (
                    <>
                        <div>not signed in</div>
                    </>
                )
            return (
                <>
                    <FirestoreCollection
                        path={`users/${user.uid}/workouts`}
                        orderBy={[{ field: 'date', type: 'desc' }]}
                    >
                        {({ value }) => {
                            if (!value) {
                                return (
                                    <>
                                        <div>loading...</div>
                                    </>
                                )
                            }

                            return (
                                <>
                                    <MaterialTable
                                        columns={[
                                            {
                                                title: 'Exercise',
                                                field: 'exercise', // accessor is the "key" in the data

                                                editComponent: (
                                                    tableData
                                                ) => (
                                                    <ComboBox
                                                        style={
                                                            comboBoxStyle
                                                        }
                                                        selectedKey={
                                                            tableData
                                                                .rowData
                                                                .exerciseId
                                                        }
                                                        label="Select Exercise"
                                                        allowFreeform
                                                        autoComplete="on"
                                                        options={items}
                                                        onChange={onChange}
                                                    />
                                                ),
                                            },
                                            {
                                                title: 'Reps',
                                                field: 'reps',
                                                validate: (rowData) =>
                                                    rowData.reps > 0,
                                            },
                                            {
                                                type: 'date',
                                                title: 'Date',
                                                field: 'date',
                                            },
                                            {
                                                title: 'exerciseId',
                                                field: 'exerciseId',
                                                hidden: true,
                                            },
                                           
                                        ]}
                                        editable={{
                                            onRowAdd: (newData) =>
                                                new Promise(
                                                    (resolve, reject) => {
                                                        setTimeout(() => {
                                                            console.log(
                                                                newData
                                                            )
                                                            FirestoreService.addWorkout(
                                                                {
                                                                    exerciseId: selectedKey,
                                                                    exercise: selectedExercise,
                                                                    date: formatDate(
                                                                        newData.date
                                                                    ),
                                                                    reps:
                                                                        newData.reps,
                                                                }
                                                            )
                                                            resolve(1)
                                                        }, 1000)
                                                    }
                                                ),
                                            onRowUpdate: (
                                                newData,
                                                oldData
                                            ) =>
                                                new Promise(
                                                    (resolve, reject) => {
                                                        setTimeout(() => {
                                                            console.log(
                                                                'newupd',
                                                                newData,
                                                                oldData
                                                            )

                                                            FirestoreService.updateWorkout(
                                                                {
                                                                    exerciseId: selectedKey,
                                                                    exercise: selectedExercise,
                                                                    date: formatDate(
                                                                        newData.date
                                                                    ),
                                                                    reps:
                                                                        newData.reps,
                                                                    id:
                                                                       **Here is where I need an ID**
                                                                }
                                                            )
                                                            resolve(1)
                                                        }, 1000)
                                                    }
                                                ),
                                        }}
                                        title="Workout Log"
                                        actions={[
                                            {
                                                icon: 'delete',
                                                tooltip: 'Delete Workout',
                                                onClick: (rowData) => {
                                                    FirestoreService.deleteWorkout(
                                                       
                                                      **_### Here is where I need and ID_**
                                                    )
                                                },
                                            },
                                        ]}
                                        data={value}
                                        options={{
                                            pageSize: 10,
                                            pageSizeOptions: [10, 25, 50],
                                            toolbar: true,
                                            paging: true,
                                            emptyRowsWhenPaging: false,
                                        }}
                                    ></MaterialTable>
                                </>
                            )
                        }}
                    </FirestoreCollection>
                </>
            )
        }}
    </FirebaseAuthConsumer>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant