1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-26 23:14:19 +00:00
pds-2021-g2-nest/nest_frontend/routes/PageShare.js

105 lines
3.3 KiB
JavaScript
Raw Normal View History

import React, { useCallback, useContext } from "react"
2021-05-23 14:32:28 +00:00
import BoxHeader from "../components/base/BoxHeader"
import BoxUserList from "../components/interactive/BoxUserList"
import useBackendViewset from "../hooks/useBackendViewset"
import { useParams } from "react-router"
2021-05-23 15:56:25 +00:00
import ContextUser from "../contexts/ContextUser"
import useStrings from "../hooks/useStrings"
2021-05-25 02:06:14 +00:00
import PageWithHeader from "../components/base/layout/PageWithHeader"
import BodyHorizontalHalves from "../components/base/layout/BodyHorizontalHalves"
import AlertError from "../components/interactive/AlertError"
2021-05-23 14:32:28 +00:00
2021-05-25 02:09:47 +00:00
export default function PageShare() {
const strings = useStrings()
2021-05-24 01:42:30 +00:00
const { user: loggedUser } = useContext(ContextUser)
const { id } = useParams()
2021-05-24 01:42:30 +00:00
const {
resources: authorizations,
createResource: createAuthorization,
destroyResource: destroyAuthorization,
running: authBvRunning,
error: authBvError,
2021-05-24 01:42:30 +00:00
} = useBackendViewset(
`/api/v1/repositories/${id}/authorizations/`,
"email",
{
list: true,
2021-05-24 01:42:30 +00:00
create: true,
retrieve: false,
edit: false,
2021-05-24 01:42:30 +00:00
destroy: true,
command: false,
action: false,
}
)
2021-05-24 01:42:30 +00:00
const {
resources: users,
running: usersBvRunning,
error: usersBvError,
2021-05-24 01:42:30 +00:00
} = useBackendViewset(
"/api/v1/users/",
"email",
{
list: true,
2021-05-24 01:42:30 +00:00
create: false,
retrieve: false,
edit: false,
2021-05-24 01:42:30 +00:00
destroy: false,
command: false,
action: false,
}
)
const shareWith = useCallback(
user => {
console.info("Authorizing ", user, " ...")
createAuthorization({rid: id, email: user.email})
},
[createAuthorization, id]
)
const unshareWith = useCallback(
user => {
console.info("Deauthorizing ", user, " ...")
destroyAuthorization(user.email)
},
[destroyAuthorization, id]
)
2021-05-23 14:32:28 +00:00
return (
2021-05-25 02:06:14 +00:00
<PageWithHeader
header={
<BoxHeader>
{strings.repoShare}
</BoxHeader>
}
>
<BodyHorizontalHalves
upper={
<BoxUserList
users={users.filter(user => user["email"] !== loggedUser["email"] && !authorizations.map(a => a.email).includes(user.email))}
shareWithUser={shareWith}
header={strings.availableUsers}
running={usersBvRunning || authBvRunning}
/>
}
lower={<>
<BoxUserList
users={users.filter(user => user["email"] === loggedUser["email"] || authorizations.map(a => a.email).includes(user.email))}
unshareWithUser={unshareWith}
header={strings.sharingWith}
running={usersBvRunning || authBvRunning}
/>
</>}
error={<>
{authBvError ? <AlertError error={authBvError}/> : null}
{usersBvError ? <AlertError error={usersBvError}/> : null}
</>}
/>
2021-05-25 02:06:14 +00:00
</PageWithHeader>
2021-05-23 14:32:28 +00:00
)
}