Renders the data of some Resource
()
| 14 | |
| 15 | /** Renders the data of some Resource */ |
| 16 | function Data(): JSX.Element { |
| 17 | const [subject] = useCurrentSubject(); |
| 18 | const resource = useResource(subject); |
| 19 | const [textResponse, setTextResponse] = useState(null); |
| 20 | const [textResponseLoading, setTextResponseLoading] = useState(false); |
| 21 | const [err, setErr] = useState(null); |
| 22 | const { agent } = useSettings(); |
| 23 | const store = useStore(); |
| 24 | |
| 25 | if (resource.loading) { |
| 26 | return <ContainerNarrow>Loading {subject}...</ContainerNarrow>; |
| 27 | } |
| 28 | if (resource.error) { |
| 29 | return ( |
| 30 | <ContainerNarrow> |
| 31 | <ErrorLook>{resource.getError().message}</ErrorLook> |
| 32 | </ContainerNarrow> |
| 33 | ); |
| 34 | } |
| 35 | |
| 36 | async function fetchAs(contentType: string) { |
| 37 | let headers: HeadersInit = new Headers(); |
| 38 | headers.set('Accept', contentType); |
| 39 | if (agent) { |
| 40 | headers = await signRequest(subject, agent, headers); |
| 41 | } |
| 42 | setTextResponseLoading(true); |
| 43 | try { |
| 44 | const resp = await window.fetch(subject, { headers }); |
| 45 | const body = await resp.text(); |
| 46 | setTextResponseLoading(false); |
| 47 | setTextResponse(body); |
| 48 | setErr(null); |
| 49 | } catch (e) { |
| 50 | setTextResponseLoading(false); |
| 51 | setErr(e); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return ( |
| 56 | <ContainerNarrow about={subject}> |
| 57 | <h1>data view</h1> |
| 58 | <PropValRow columns> |
| 59 | <PropertyLabel title='The URL of the resource'>subject:</PropertyLabel> |
| 60 | <AtomicLink subject={subject}>{subject}</AtomicLink> |
| 61 | </PropValRow> |
| 62 | <AllProps resource={resource} editable columns /> |
| 63 | {resource.getCommitBuilder().hasUnsavedChanges() ? ( |
| 64 | <> |
| 65 | <h2>⚠️ contains uncommitted changes</h2> |
| 66 | <p>This means that (some) of your local changes are not yet saved.</p> |
| 67 | {resource.commitError && ( |
| 68 | <ErrMessage>{resource.commitError.message}</ErrMessage> |
| 69 | )} |
| 70 | <Button onClick={() => resource.save(store)}>save</Button> |
| 71 | </> |
| 72 | ) : null} |
| 73 | <div> |
nothing calls this directly
no test coverage detected