【发布时间】:2022-07-08 18:18:21
【问题描述】:
我正在尝试根据用户是否是 MS 组的成员来限制自定义 Web 部件属性窗格
现在我已经构建了图形 API 调用等......它只是返回 true 或 false。
在我的 API 请求中:-
public async checkMSGroup(){
let groupName = "WebPartAdmin";
return new Promise<void>((resolve: (ig: any) => any, reject: (error: any) => void): void => {
this.context.msGraphClientFactory.getClient()
.then((client: MSGraphClient): void => {
client
.api('/me/transitiveMemberOf')
.get((error, response: any, rawResponse?: any) => {
let msGroups = response.value;
msGroups.forEach(
(group) => {
//console.log(group.displayName);
if (group.displayName == groupName) {
resolve(true);
}
});
})
});
}).then(resp => {return resp});
}
接下来我要在promise中得到响应
在我拥有的 PropertiesPane 配置中
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
let webPartOptions: IPropertyPaneField<any>[];
const ig:any = this.checkMSGroup()
console.log(ig) // I can see the promise results
这是我要解决的问题
const [inGroup, setInGroup] = useState(false);
useEffect(() => {
ig.then(resp => {
setInGroup(resp);
console.log("pp"+resp)
})
}, []);
console.log("var"+inGroup)
正如您在图像上看到的那样,我可以看到承诺,我在 useEffect 等方面收到错误... 任何建议都会很好,或者如果有另一种方法可以将 inGroup 设置为承诺结果。
剩下的很简单,我只需要 inGroup 变量为真
if(inGroup==true){
webPartOptions = [
PropertyPaneTextField("title", {
label: "Title",
value: this.properties.title
}),
//Adds more property fields
}
}else{
webPartOptions = [
PropertyPaneLabel("title", {
text: "Contact Administrator"
})
]
}
return {
pages: [
{
groups: [
{
groupFields: webPartOptions
}
]
}
]
};
}
【问题讨论】: