2021-05-23 15:35:38 +00:00
|
|
|
import React, { useCallback, useContext } from "react"
|
2021-05-23 14:32:28 +00:00
|
|
|
import classNames from "classnames"
|
|
|
|
import BoxHeader from "../components/base/BoxHeader"
|
|
|
|
import ContextLanguage from "../contexts/ContextLanguage"
|
|
|
|
import Style from "./PageShare.module.css"
|
2021-05-23 15:35:38 +00:00
|
|
|
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"
|
2021-05-24 03:23:17 +00:00
|
|
|
import BoxAlert from "../components/base/BoxAlert"
|
2021-05-24 12:13:24 +00:00
|
|
|
import useStrings from "../hooks/useStrings"
|
2021-05-23 14:32:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
export default function PageShare({ className, ...props }) {
|
2021-05-24 12:13:24 +00:00
|
|
|
const strings = useStrings()
|
2021-05-24 01:42:30 +00:00
|
|
|
const { user: loggedUser } = useContext(ContextUser)
|
2021-05-23 15:35:38 +00:00
|
|
|
const { id } = useParams()
|
2021-05-24 01:42:30 +00:00
|
|
|
|
|
|
|
const {
|
|
|
|
resources: authorizations,
|
|
|
|
createResource: createAuthorization,
|
|
|
|
destroyResource: destroyAuthorization,
|
2021-05-24 03:23:17 +00:00
|
|
|
running: authBvRunning,
|
|
|
|
error: authBvError,
|
2021-05-24 01:42:30 +00:00
|
|
|
} = useBackendViewset(
|
|
|
|
`/api/v1/repositories/${id}/authorizations/`,
|
2021-05-23 15:35:38 +00:00
|
|
|
"email",
|
|
|
|
{
|
|
|
|
list: true,
|
2021-05-24 01:42:30 +00:00
|
|
|
create: true,
|
2021-05-23 15:35:38 +00:00
|
|
|
retrieve: false,
|
|
|
|
edit: false,
|
2021-05-24 01:42:30 +00:00
|
|
|
destroy: true,
|
2021-05-23 15:35:38 +00:00
|
|
|
command: false,
|
|
|
|
action: false,
|
|
|
|
}
|
|
|
|
)
|
2021-05-24 01:42:30 +00:00
|
|
|
|
|
|
|
const {
|
|
|
|
resources: users,
|
2021-05-24 03:23:17 +00:00
|
|
|
running: usersBvRunning,
|
|
|
|
error: usersBvError,
|
2021-05-24 01:42:30 +00:00
|
|
|
} = useBackendViewset(
|
|
|
|
"/api/v1/users/",
|
2021-05-23 15:35:38 +00:00
|
|
|
"email",
|
|
|
|
{
|
|
|
|
list: true,
|
2021-05-24 01:42:30 +00:00
|
|
|
create: false,
|
2021-05-23 15:35:38 +00:00
|
|
|
retrieve: false,
|
|
|
|
edit: false,
|
2021-05-24 01:42:30 +00:00
|
|
|
destroy: false,
|
2021-05-23 15:35:38 +00:00
|
|
|
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 (
|
|
|
|
<div className={classNames(Style.PageShare, className)} {...props}>
|
|
|
|
<BoxHeader className={Style.Header}>
|
|
|
|
{strings.repoShare}
|
|
|
|
</BoxHeader>
|
2021-05-23 15:35:38 +00:00
|
|
|
<BoxUserList
|
|
|
|
className={Style.UserList}
|
2021-05-23 15:56:25 +00:00
|
|
|
users={users.filter(user => user["email"] !== loggedUser["email"] && !authorizations.map(a => a.email).includes(user.email))}
|
2021-05-23 15:35:38 +00:00
|
|
|
shareWithUser={shareWith}
|
|
|
|
header={strings.availableUsers}
|
2021-05-23 15:56:25 +00:00
|
|
|
running={usersBvRunning && authBvRunning}
|
2021-05-23 15:35:38 +00:00
|
|
|
/>
|
|
|
|
<BoxUserList
|
|
|
|
className={Style.SharingWith}
|
2021-05-23 15:56:25 +00:00
|
|
|
users={users.filter(user => user["email"] === loggedUser["email"] || authorizations.map(a => a.email).includes(user.email))}
|
2021-05-23 15:35:38 +00:00
|
|
|
unshareWithUser={unshareWith}
|
|
|
|
header={strings.sharingWith}
|
2021-05-23 15:56:25 +00:00
|
|
|
running={usersBvRunning && authBvRunning}
|
2021-05-23 15:35:38 +00:00
|
|
|
/>
|
2021-05-24 03:23:17 +00:00
|
|
|
{authBvError ?
|
|
|
|
<BoxAlert color={"Red"} className={Style.Error}>{strings[authBvError?.data?.code ?? "errorUnknownError"]}</BoxAlert>
|
|
|
|
: null}
|
|
|
|
{usersBvError ?
|
|
|
|
<BoxAlert color={"Red"} className={Style.Error}>{strings[usersBvError?.data?.code ?? "errorUnknownError"]}</BoxAlert>
|
|
|
|
: null}
|
2021-05-23 14:32:28 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|