-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added AgentIdentifierType interface - Implemented Identifier edit and view page - Saving/loading Identifiers should work - The uriTemplate needs to have a $1 placeholder in the string
- Loading branch information
1 parent
8c88316
commit 69347a3
Showing
8 changed files
with
287 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
import { | ||
ApiClientContext, | ||
BackButton, | ||
ButtonBar, | ||
DinaForm, | ||
DinaFormOnSubmit, | ||
SubmitButton, | ||
TextField, | ||
useQuery, | ||
withResponse, | ||
MultilingualTitle | ||
} from "common-ui"; | ||
import { InputResource, PersistedResource } from "kitsu"; | ||
import { fromPairs, toPairs } from "lodash"; | ||
import { useRouter } from "next/router"; | ||
import { Fragment, useContext } from "react"; | ||
import { DinaMessage, useDinaIntl } from "../../intl/dina-ui-intl"; | ||
import { AgentIdentifierType } from "../../types/agent-api/resources/AgentIdentifierType"; | ||
import { Head, Nav, Footer } from "../../components"; | ||
import Link from "next/link"; | ||
|
||
interface IdentifierFormProps { | ||
fetchedIdentifierType?: AgentIdentifierType; | ||
onSaved: ( | ||
identifierType: PersistedResource<AgentIdentifierType> | ||
) => Promise<void>; | ||
} | ||
|
||
export default function IdentifierTypeEditPage() { | ||
const router = useRouter(); | ||
const { | ||
query: { id } | ||
} = router; | ||
const { formatMessage } = useDinaIntl(); | ||
|
||
async function goToViewPage( | ||
identifier: PersistedResource<AgentIdentifierType> | ||
) { | ||
await router.push(`/identifier/view?id=${identifier.id}`); | ||
} | ||
|
||
const title = id ? "editIdentifierTitle" : "addIdentifier"; | ||
|
||
const query = useQuery<AgentIdentifierType>( | ||
{ | ||
path: `agent-api/identifier-type/${id}` | ||
}, | ||
{ disabled: !id } | ||
); | ||
|
||
return ( | ||
<div> | ||
<Head title={formatMessage(title)} /> | ||
<Nav /> | ||
<main className="container-fluid"> | ||
<div> | ||
<h1 id="wb-cont"> | ||
<DinaMessage id={title} /> | ||
</h1> | ||
{id ? ( | ||
withResponse(query, ({ data }) => ( | ||
<IdentifierTypeForm | ||
fetchedIdentifierType={data} | ||
onSaved={goToViewPage} | ||
/> | ||
)) | ||
) : ( | ||
<IdentifierTypeForm onSaved={goToViewPage} /> | ||
)} | ||
</div> | ||
</main> | ||
<Footer /> | ||
</div> | ||
); | ||
} | ||
|
||
export function IdentifierTypeForm({ | ||
fetchedIdentifierType, | ||
onSaved | ||
}: IdentifierFormProps) { | ||
const { apiClient } = useContext(ApiClientContext); | ||
|
||
const initialValues: AgentIdentifierType = fetchedIdentifierType | ||
? { | ||
...fetchedIdentifierType, | ||
// Convert multilingualDescription to editable Dictionary format: | ||
multilingualTitle: fromPairs<string | undefined>( | ||
fetchedIdentifierType.multilingualTitle?.titles?.map( | ||
({ title, lang }) => [lang ?? "", title ?? ""] | ||
) | ||
) | ||
} | ||
: { type: "identifier-type" }; | ||
|
||
const onSubmit: DinaFormOnSubmit<AgentIdentifierType> = async ({ | ||
submittedValues | ||
}) => { | ||
const input: InputResource<AgentIdentifierType> = { | ||
...submittedValues, | ||
// Convert the editable format to the stored format: | ||
multilingualTitle: { | ||
titles: toPairs(submittedValues.multilingualTitle).map( | ||
([lang, title]) => ({ lang, title }) | ||
) | ||
} | ||
}; | ||
delete (input as any).type; | ||
const savedIdentifierType = await apiClient.axios.post( | ||
`agent-api/identifier-type`, | ||
{ | ||
data: { | ||
type: "identifier-type", | ||
attributes: { | ||
...input, | ||
...(input.uriTemplate && { uriTemplate: `${input.uriTemplate}$1` }) | ||
} | ||
} | ||
}, | ||
{ | ||
headers: { | ||
"Content-Type": "application/vnd.api+json" | ||
} | ||
} | ||
); | ||
await onSaved(savedIdentifierType?.data?.data); | ||
}; | ||
|
||
return ( | ||
<DinaForm<AgentIdentifierType> | ||
initialValues={initialValues} | ||
onSubmit={onSubmit} | ||
> | ||
<ButtonBar className="mb-4"> | ||
<div className="col-md-6 col-sm-12 mt-2"> | ||
<BackButton | ||
entityId={fetchedIdentifierType?.id} | ||
entityLink="/identifier" | ||
/> | ||
</div> | ||
<div className="col-md-6 col-sm-12 d-flex"> | ||
<SubmitButton className="ms-auto" /> | ||
</div> | ||
</ButtonBar> | ||
<IdentifierTypeFormLayout /> | ||
</DinaForm> | ||
); | ||
} | ||
|
||
export function IdentifierTypeFormLayout() { | ||
return ( | ||
<div> | ||
<div className="row"> | ||
<TextField className="col-md-6" name="name" /> | ||
<TextField className="col-md-6" name="term" /> | ||
</div> | ||
<div className="row"> | ||
<TextField | ||
className="col-md-6" | ||
name={"uriTemplate"} | ||
readOnlyRender={(value) => { | ||
try { | ||
const url = new URL(value); | ||
if (url.protocol === "http:" || url.protocol === "https:") { | ||
return ( | ||
<Fragment key={value}> | ||
<Link href={value} passHref={true}> | ||
<a>{value}</a> | ||
</Link> | ||
</Fragment> | ||
); | ||
} | ||
} catch (_) { | ||
return value; | ||
} | ||
}} | ||
/> | ||
</div> | ||
<MultilingualTitle /> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.