Skip to content

Commit

Permalink
edit groups - works!
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur committed Aug 31, 2024
1 parent ff84196 commit 5beef0b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 38 deletions.
27 changes: 23 additions & 4 deletions ui2/src/features/api/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {createApi, fetchBaseQuery} from "@reduxjs/toolkit/query/react"
import {getBaseURL} from "@/utils"
import {PAGINATION_DEFAULT_ITEMS_PER_PAGES} from "@/cconstants"

import type {Group, Paginated} from "@/types"
import type {Group, GroupUpdate, Paginated} from "@/types"
import type {RootState} from "@/app/types"

type GetGroupsArgs = {
Expand Down Expand Up @@ -47,18 +47,37 @@ export const apiSlice = createApi({
reducerPath: "api",
baseQuery: baseQuery,
keepUnusedDataFor: 60,
tagTypes: ["Group"],
endpoints: builder => ({
getGroups: builder.query<Paginated<Group>, GetGroupsArgs | void>({
query: ({
page_number = 1,
page_size = PAGINATION_DEFAULT_ITEMS_PER_PAGES
}: GetGroupsArgs) =>
`/groups/?page_number=${page_number}&page_size=${page_size}`
`/groups/?page_number=${page_number}&page_size=${page_size}`,
providesTags: (
result = {page_number: 1, page_size: 1, num_pages: 1, items: []},
error,
arg
) => [
"Group",
...result.items.map(({id}) => ({type: "Group", id}) as const)
]
}),
getGroup: builder.query<Group, string>({
query: groupID => `/groups/${groupID}`
query: groupID => `/groups/${groupID}`,
providesTags: (result, error, arg) => [{type: "Group", id: arg}]
}),
editGroup: builder.mutation<Group, GroupUpdate>({
query: group => ({
url: `groups/${group.id}`,
method: "PATCH",
body: group
}),
invalidatesTags: (result, error, arg) => [{type: "Group", id: arg.id}]
})
})
})

export const {useGetGroupsQuery, useGetGroupQuery} = apiSlice
export const {useGetGroupsQuery, useGetGroupQuery, useEditGroupMutation} =
apiSlice
28 changes: 0 additions & 28 deletions ui2/src/features/groups/components/EditButton.tsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,18 @@
import {useDispatch, useSelector} from "react-redux"
import {Button} from "@mantine/core"
import {IconEdit} from "@tabler/icons-react"

import {openModal} from "@/components/modals/Generic"

import {useGetGroupQuery} from "@/features/api/slice"

import EditGroupModal from "./EditGroupModal"
import type {GroupDetails} from "@/types"

interface Args {
groupId: string
}

export default function EditButton({groupId}: Args) {
const dispatch = useDispatch()
const {data, isLoading} = useGetGroupQuery(groupId)

const missingGroupDetails = (groupId: string) => {
if (!data) {
return true
}

if (data.id != groupId) {
return true
}

return false
}

const onClick = () => {
// if (groupId && missingGroupDetails(data)) {
//dispatch(fetchGroupDetails(groupId!))
//}

openModal<any, {groupId: string}>(EditGroupModal, {
groupId: groupId!
})
.then((group: GroupDetails) => {
//dispatch(updateGroupDetails(group))
})
.catch(() => {})
}

if (!groupId) {
Expand Down
19 changes: 13 additions & 6 deletions ui2/src/features/groups/components/EditGroupModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import {
TextInput,
Table,
Checkbox,
Tooltip
Tooltip,
Loader
} from "@mantine/core"

import {updateGroup} from "@/features/groups/slice"
//import {selectGroupDetails} from "@/slices/groupDetails"
import {RootState} from "@/app/types"
import type {GroupDetails, SliceState} from "@/types"
import {useGetGroupQuery} from "@/features/api/slice"
import {useGetGroupQuery, useEditGroupMutation} from "@/features/api/slice"

function initialScopesDict(initialScopes: string[]): Record<string, boolean> {
let scopes: Record<string, boolean> = {
Expand All @@ -36,8 +37,9 @@ type Args = {
}

export default function EditGroupModal({groupId, onOK, onCancel}: Args) {
const dispatch = useDispatch()
const {data, isLoading} = useGetGroupQuery(groupId)
const [updateGroup, {isLoading: isLoadingGroupUpdate}] =
useEditGroupMutation()
const [show, setShow] = useState<boolean>(true)
const [name, setName] = useState<string>("")
const [scopes, setScopes] = useState<Record<string, boolean>>({})
Expand All @@ -55,7 +57,7 @@ export default function EditGroupModal({groupId, onOK, onCancel}: Args) {
scopes: Object.keys(scopes),
name: name!
}
//await dispatch(updateGroup(updatedData))
await updateGroup(updatedData)
onOK(updatedData)
setShow(false)
}
Expand Down Expand Up @@ -116,7 +118,7 @@ export default function EditGroupModal({groupId, onOK, onCancel}: Args) {
return (
<Modal title={"Edit Group"} opened={show} size="lg" onClose={onClose}>
<LoadingOverlay
visible={data == null || status == "loading"}
visible={data == null || isLoading}
zIndex={1000}
overlayProps={{radius: "sm", blur: 2}}
/>
Expand Down Expand Up @@ -506,7 +508,12 @@ export default function EditGroupModal({groupId, onOK, onCancel}: Args) {
<Button variant="default" onClick={onClose}>
Cancel
</Button>
<Button onClick={onSubmit}>Submit</Button>
<Group>
{isLoadingGroupUpdate && <Loader size="sm" />}
<Button disabled={isLoadingGroupUpdate} onClick={onSubmit}>
Update Group
</Button>
</Group>
</Group>
</Modal>
)
Expand Down
2 changes: 2 additions & 0 deletions ui2/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ export type GroupDetails = {
scopes: Array<string>
}

export type GroupUpdate = Pick<Group, "id" | "name" | "scopes">

export type FileItemStatus = "uploading" | "success" | "failure"

export type FileItemType = {
Expand Down

0 comments on commit 5beef0b

Please sign in to comment.