-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CORE-1967 WIP auto-populate LocalContexts metadata in templates
- Loading branch information
Showing
6 changed files
with
290 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
{ | ||
"localContextsAttrDefaultValue": "The \"{{projectTitle}}\" project has Labels and/or Notices applied through the Local Contexts Hub. For more information, refer to the project page: {{projectPage}}", | ||
"localContextsHubProjectURI": "Local Contexts Hub Project URI", | ||
"projectMoreInfoWithLink": "For more information on the <ExternalLink href=\"https://localcontexts.org\">Local Contexts</ExternalLink> project for this data, please visit the project's page: <ProjectLink>{{projectTitle}}</ProjectLink>" | ||
} |
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
217 changes: 217 additions & 0 deletions
217
src/components/metadata/templates/LocalContextsField.js
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,217 @@ | ||
/** | ||
* @author psarando | ||
*/ | ||
import React from "react"; | ||
|
||
import { useQuery } from "react-query"; | ||
|
||
import { useTranslation } from "i18n"; | ||
|
||
import LocalContextsLabelDisplay from "../LocalContextsLabelDisplay"; | ||
|
||
import FormTextField from "components/forms/FormTextField"; | ||
import { | ||
LocalContextsAttrs, | ||
parseProjectID, | ||
} from "components/models/metadata/LocalContexts"; | ||
|
||
import { | ||
LOCAL_CONTEXTS_QUERY_KEY, | ||
getLocalContextsProject, | ||
} from "serviceFacades/metadata"; | ||
|
||
import { Skeleton } from "@mui/material"; | ||
|
||
const findAVU = (avus, attr) => avus?.find((avu) => avu.attr === attr); | ||
|
||
const LocalContextsField = ({ | ||
avu, | ||
onUpdate, | ||
form: { setFieldValue, ...form }, | ||
field: { value, onChange, ...field }, | ||
...props | ||
}) => { | ||
const { t } = useTranslation("localcontexts"); | ||
|
||
const [rightsURIAVU, setRightsURIAVU] = React.useState( | ||
() => | ||
findAVU(avu.avus, LocalContextsAttrs.RIGHTS_URI) || { | ||
attr: LocalContextsAttrs.RIGHTS_URI, | ||
value: "", | ||
unit: "", | ||
} | ||
); | ||
|
||
const [projectHubURI, setProjectHubURI] = React.useState( | ||
rightsURIAVU.value | ||
); | ||
|
||
const [rightsIDSchemeURIAVU] = React.useState(() => { | ||
let rightsIDScheme = findAVU( | ||
avu.avus, | ||
LocalContextsAttrs.RIGHTS_ID_SCHEME | ||
); | ||
|
||
if ( | ||
rightsIDScheme?.value !== LocalContextsAttrs.RIGHTS_ID_SCHEME_VALUE | ||
) { | ||
rightsIDScheme = { | ||
attr: LocalContextsAttrs.RIGHTS_ID_SCHEME, | ||
value: LocalContextsAttrs.RIGHTS_ID_SCHEME_VALUE, | ||
unit: "", | ||
}; | ||
} | ||
|
||
return rightsIDScheme; | ||
}); | ||
|
||
const [schemeURIAVU] = React.useState(() => { | ||
let schemeURI = findAVU(avu.avus, LocalContextsAttrs.SCHEME_URI); | ||
|
||
if (schemeURI?.value !== LocalContextsAttrs.SCHEME_URI_VALUE) { | ||
schemeURI = { | ||
attr: LocalContextsAttrs.SCHEME_URI, | ||
value: LocalContextsAttrs.SCHEME_URI_VALUE, | ||
unit: "", | ||
}; | ||
} | ||
|
||
return schemeURI; | ||
}); | ||
|
||
const projectID = parseProjectID(projectHubURI); | ||
|
||
const { data: project, isFetching } = useQuery({ | ||
queryKey: [LOCAL_CONTEXTS_QUERY_KEY, projectID], | ||
queryFn: () => | ||
getLocalContextsProject({ | ||
projectID, | ||
}), | ||
enabled: URL.canParse(projectHubURI), | ||
onSuccess: (project) => { | ||
let newValue = avu.value || ""; | ||
|
||
const notices = | ||
project?.notice?.filter((label) => label?.name) || []; | ||
const bc_labels = | ||
project?.bc_labels?.filter((label) => label?.name) || []; | ||
const tk_labels = | ||
project?.tk_labels?.filter((label) => label?.name) || []; | ||
|
||
const labels = [...bc_labels, ...tk_labels]; | ||
|
||
const newLabels = [...notices, ...labels]; | ||
|
||
if (newLabels.length === 1) { | ||
newValue = newLabels[0].label_text || newLabels[0].default_text; | ||
} else { | ||
newValue = t("localContextsAttrDefaultValue", { | ||
projectTitle: project?.title, | ||
projectPage: project?.project_page, | ||
}); | ||
} | ||
|
||
let newAVUs = avu.avus || []; | ||
|
||
const currentLabels = newAVUs | ||
.filter( | ||
(childAVU) => childAVU.attr === LocalContextsAttrs.RIGHTS_ID | ||
) | ||
?.map((childAVU) => childAVU.value); | ||
|
||
const missingLabels = newLabels.filter( | ||
(label) => !currentLabels.includes(label.name) | ||
); | ||
|
||
if (missingLabels.length > 0) { | ||
newAVUs = newAVUs.filter( | ||
(childAVU) => | ||
childAVU.attr !== LocalContextsAttrs.RIGHTS_URI && | ||
childAVU.attr !== LocalContextsAttrs.RIGHTS_ID_SCHEME && | ||
childAVU.attr !== LocalContextsAttrs.SCHEME_URI && | ||
(childAVU.attr !== LocalContextsAttrs.RIGHTS_ID || | ||
newLabels.find( | ||
(label) => label.name === childAVU.value | ||
)) | ||
); | ||
|
||
newAVUs = [ | ||
...newAVUs, | ||
rightsURIAVU, | ||
schemeURIAVU, | ||
rightsIDSchemeURIAVU, | ||
...missingLabels.map((label) => ({ | ||
attr: LocalContextsAttrs.RIGHTS_ID, | ||
value: label.name, | ||
unit: "", | ||
})), | ||
]; | ||
} | ||
|
||
if (avu.value !== newValue || avu.avus !== newAVUs) { | ||
onUpdate({ ...avu, value: newValue, avus: newAVUs }); | ||
} | ||
}, | ||
onError: (error) => { | ||
console.log("Error fetching Local Contexts project.", { | ||
projectHubURI, | ||
error, | ||
}); | ||
}, | ||
}); | ||
|
||
const updateProjectHubURI = (uri) => { | ||
setProjectHubURI(uri); | ||
|
||
let newAVUs = avu.avus || []; | ||
|
||
if (rightsURIAVU.value !== uri) { | ||
const newRightsURIAVU = { | ||
attr: LocalContextsAttrs.RIGHTS_URI, | ||
value: uri, | ||
unit: "", | ||
}; | ||
setRightsURIAVU(newRightsURIAVU); | ||
|
||
newAVUs = newAVUs.filter( | ||
(childAVU) => | ||
childAVU.attr !== LocalContextsAttrs.RIGHTS_URI && | ||
childAVU.attr !== LocalContextsAttrs.RIGHTS_ID_SCHEME && | ||
childAVU.attr !== LocalContextsAttrs.SCHEME_URI | ||
); | ||
|
||
newAVUs = [ | ||
...newAVUs, | ||
newRightsURIAVU, | ||
schemeURIAVU, | ||
rightsIDSchemeURIAVU, | ||
]; | ||
|
||
onUpdate({ ...avu, avus: newAVUs }); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<FormTextField | ||
multiline | ||
form={form} | ||
field={field} | ||
value={projectHubURI} | ||
onChange={(event) => updateProjectHubURI(event?.target?.value)} | ||
{...props} | ||
label={t("localContextsHubProjectURI")} | ||
required | ||
/> | ||
{isFetching ? ( | ||
<Skeleton variant="rectangular"> | ||
<LocalContextsLabelDisplay project={project} /> | ||
</Skeleton> | ||
) : ( | ||
<LocalContextsLabelDisplay project={project} /> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export default LocalContextsField; |
Oops, something went wrong.