({ target }: InviteFormProps)
| 19 | * generated Subject after saving. |
| 20 | */ |
| 21 | export function InviteForm({ target }: InviteFormProps) { |
| 22 | const invite = useResource(null, { newResource: true }); |
| 23 | const store = useStore(); |
| 24 | const [err, setErr] = useState<Error>(null); |
| 25 | const [createdSubject, setCreatedSubject] = useState<string>(null); |
| 26 | |
| 27 | /** Stores the Invite, sends it to the server, shows the Subject to the User */ |
| 28 | async function createInvite() { |
| 29 | await invite.set(properties.isA, [urls.classes.invite], store); |
| 30 | await invite.set(properties.read, [urls.instances.publicAgent], store); |
| 31 | await invite.set(properties.invite.target, target.getSubject(), store); |
| 32 | invite.setSubject(store.createSubject('invite')); |
| 33 | try { |
| 34 | await invite.set( |
| 35 | properties.parent, |
| 36 | `${store.getServerUrl()}/invites`, |
| 37 | store, |
| 38 | ); |
| 39 | await invite.save(store); |
| 40 | navigator.clipboard.writeText(invite.getSubject()); |
| 41 | toast.success('Copied to clipboard'); |
| 42 | setCreatedSubject(invite.getSubject()); |
| 43 | } catch (e) { |
| 44 | setErr(e); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | if (invite.new) { |
| 49 | return ( |
| 50 | <Card> |
| 51 | <ResourceField |
| 52 | propertyURL={urls.properties.invite.usagesLeft} |
| 53 | resource={invite} |
| 54 | /> |
| 55 | <ResourceField |
| 56 | propertyURL={urls.properties.description} |
| 57 | resource={invite} |
| 58 | /> |
| 59 | <ResourceField |
| 60 | propertyURL={urls.properties.invite.write} |
| 61 | resource={invite} |
| 62 | /> |
| 63 | <Button onClick={createInvite}>Create Invite</Button> |
| 64 | {err && ( |
| 65 | <p> |
| 66 | <ErrorLook>{err.message}</ErrorLook> |
| 67 | </p> |
| 68 | )} |
| 69 | </Card> |
| 70 | ); |
| 71 | } else |
| 72 | return ( |
| 73 | <Card> |
| 74 | <p>Invite created and copied to clipboard! Send it to your buddy:</p> |
| 75 | <CodeBlock content={createdSubject} data-test='invite-code' /> |
| 76 | </Card> |
| 77 | ); |
| 78 | } |
nothing calls this directly
no test coverage detected