Skip to content

Commit

Permalink
capture user settings changes in local state (#2192)
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwarz2030 authored Nov 30, 2023
1 parent f1d331e commit df6df06
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/components/SuperAdmin/MetricsData.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ const setProjectTab = () => {
]
}

const setUserTab = () => {
const setUserTab = (userChanges, setUserChanges) => {
return [
{
id: 'id',
Expand Down Expand Up @@ -291,7 +291,12 @@ const setUserTab = () => {
sortable: true,
Cell: (props) => {
return (
<SuperUserToggle initialValue={props.value} userId={props?.original?.id} />
<SuperUserToggle
initialValue={props.value}
userId={props?.original?.id}
userChanges={userChanges}
setUserChanges={setUserChanges}
/>
)
}
},
Expand Down
5 changes: 3 additions & 2 deletions src/components/SuperAdmin/MetricsTable.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { useState } from 'react'
import WithSortedChallenges from '../HOCs/WithSortedChallenges/WithSortedChallenges'
import WithMetricsSearchResults from './WithMetricsSearchResults'
import WithSortedProjects from './WithSortedProjects'
Expand All @@ -9,6 +9,7 @@ import { injectIntl } from 'react-intl'
import BusySpinner from '../BusySpinner/BusySpinner'

const MetricsTable = (props) => {
const [userChanges, setUserChanges] = useState({})
let data
const constructHeader = () => {
if (props.currentTab === 'challenges') {
Expand Down Expand Up @@ -50,7 +51,7 @@ const MetricsTable = (props) => {
superUser: Boolean(u.grants?.find(grant => grant.role === -1))
}))

return setUserTab()
return setUserTab(userChanges, setUserChanges)
}
}

Expand Down
11 changes: 10 additions & 1 deletion src/components/SuperAdmin/SuperUserToggle.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import React, { useState } from 'react'
import React, { useState, useEffect } from 'react'
import Endpoint from '../../services/Server/Endpoint'
import { defaultRoutes as api } from "../../services/Server/Server";
import WithCurrentUser from '../HOCs/WithCurrentUser/WithCurrentUser';

const SuperUserToggle = (props) => {
const [selection, setSelection] = useState(props.initialValue ? "super" : "basic")

useEffect(() => {
const localState = props.userChanges[props.userId]
setSelection(localState || props.initialValue ? "super" : "basic")
}, [props.userId])

const updateValue = async (e) => {
const value = e.target.value

Expand All @@ -16,6 +21,10 @@ const SuperUserToggle = (props) => {
await new Endpoint(api.superUser.deleteSuperUserGrant, { variables: { userId: props.userId } }).execute()
}

props.setUserChanges({
...props.userChanges,
[props.userId]: value
})
setSelection(value)
} catch (e) {
console.log(e)
Expand Down

0 comments on commit df6df06

Please sign in to comment.