chore: Improve relationship loading, include policies (#6321)

Use model where available in usePolicy
This commit is contained in:
Tom Moor
2023-12-28 12:51:33 -04:00
committed by GitHub
parent bd7d5c338d
commit 79764b1e64
16 changed files with 37 additions and 24 deletions

View File

@@ -11,7 +11,6 @@ import useStores from "./useStores";
*/
export default function usePolicy(entity?: string | Model | null) {
const { policies } = useStores();
const triggered = React.useRef(false);
const entityId = entity
? typeof entity === "string"
? entity
@@ -20,12 +19,9 @@ export default function usePolicy(entity?: string | Model | null) {
React.useEffect(() => {
if (entity && typeof entity !== "string") {
// The policy for this model is missing and we haven't tried to fetch it
// yet, go ahead and do that now. The force flag is needed otherwise the
// network request will be skipped due to the model existing in the store
if (!policies.get(entity.id) && !triggered.current) {
triggered.current = true;
void entity.store.fetch(entity.id, { force: true });
// The policy for this model is missing, reload relationships for this model.
if (!policies.get(entity.id)) {
void entity.loadRelations();
}
}
}, [policies, entity]);