resolved permission error from permissions coming back as string

This commit is contained in:
Carter 2024-11-19 17:32:37 -06:00
parent cf4e3fbe1f
commit aa56f05c25
4 changed files with 43 additions and 28 deletions

View file

@ -52,6 +52,10 @@ export default function Router(props: Props) {
{/* Page routes */} {/* Page routes */}
<Route index element={hello()} /> <Route index element={hello()} />
<Route path="/teams" element={<Teams/>} /> <Route path="/teams" element={<Teams/>} />
{/* <Route
path="/teams/:teamID"
element={<Team />}
/> */}
{/* <Route path="blogs" element={<Blogs />} /> {/* <Route path="blogs" element={<Blogs />} />
<Route path="contact" element={<Contact />} /> <Route path="contact" element={<Contact />} />
<Route path="*" element={<NoPage />} /> */} <Route path="*" element={<NoPage />} /> */}

View file

@ -120,7 +120,21 @@ export default function UserProvider(props: PropsWithChildren<any>) {
if (scope) partial += "?kind=" + scope.kind + "&key=" + scope.key; if (scope) partial += "?kind=" + scope.kind + "&key=" + scope.key;
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
get(pondURL(partial)) get(pondURL(partial))
.then((response: any) => resolve(response.data)) .then((response: any) => {
let data = response.data
if (Array.isArray(data.permissions)) {
data.permissions = data.permissions.map((perm: unknown) => {
const mappedPerm = pond.Permission[perm as keyof typeof pond.Permission];
return mappedPerm !== undefined ? mappedPerm : null; // Return null for invalid keys
}).filter(Boolean); // Optionally filter out any null values
} else {
console.error("data.permissions is not an array or does not exist");
data.permissions = [];
}
// data.permissions = data.permissions.map((perm: unknown) => pond.Permission[perm as unknown as keyof typeof pond.Permission]);
resolve(data)
})
.catch((error: any) => reject(error)); .catch((error: any) => reject(error));
}); });
}; };

View file

@ -159,7 +159,7 @@ export default function TeamActions(props: Props) {
label={label} label={label}
isDialogOpen={openState.removeSelf} isDialogOpen={openState.removeSelf}
closeDialogCallback={(removeSelf: boolean) => { closeDialogCallback={(removeSelf: boolean) => {
console.log(removeSelf) // console.log(removeSelf)
if (removeSelf) refreshCallback() if (removeSelf) refreshCallback()
setOpenState({ ...openState, removeSelf: false }) setOpenState({ ...openState, removeSelf: false })
}} }}
@ -168,7 +168,6 @@ export default function TeamActions(props: Props) {
); );
}; };
// console.log(permissions)
const canWrite = permissions.includes(pond.Permission.PERMISSION_WRITE); const canWrite = permissions.includes(pond.Permission.PERMISSION_WRITE);
return ( return (
<React.Fragment> <React.Fragment>

View file

@ -28,10 +28,10 @@ import PrevIcon from "@mui/icons-material/NavigateBefore";
import LastIcon from "@mui/icons-material/SkipNext"; import LastIcon from "@mui/icons-material/SkipNext";
import FirstIcon from "@mui/icons-material/SkipPrevious"; import FirstIcon from "@mui/icons-material/SkipPrevious";
import TeamSettings from "./TeamSettings"; import TeamSettings from "./TeamSettings";
import { Add, AddBox, AddOutlined } from "@mui/icons-material"; import { AddBox } from "@mui/icons-material";
import { cloneDeep } from "lodash"; import { cloneDeep } from "lodash";
import { getContextKeys, getContextTypes } from "pbHelpers/Context"; import { getContextKeys, getContextTypes } from "pbHelpers/Context";
import { makeStyles, styled } from "@mui/styles"; import { makeStyles } from "@mui/styles";
import TeamActions from "./TeamActions"; import TeamActions from "./TeamActions";
const useStyles = makeStyles((theme: Theme) => ({ const useStyles = makeStyles((theme: Theme) => ({
@ -76,9 +76,7 @@ const limits = [
} }
]; ];
interface Props {} export default function TeamList() {
export default function TeamList(props: Props) {
const classes = useStyles(); const classes = useStyles();
const [teams, setTeams] = useState<pond.Team[]>([]); const [teams, setTeams] = useState<pond.Team[]>([]);
const teamAPI = useTeamAPI(); const teamAPI = useTeamAPI();
@ -98,22 +96,6 @@ export default function TeamList(props: Props) {
const [userId, setUserId] = useState<string | undefined>(undefined); const [userId, setUserId] = useState<string | undefined>(undefined);
const [isUser, setIsUser] = useState<boolean>(true); const [isUser, setIsUser] = useState<boolean>(true);
// const ColorAdd = styled(Add)((theme: Theme) => ({
// color: theme.palette.getContrastText(theme.palette.primary.main),
// backgroundColor: theme.palette.primary.main,
// marginTop: 'auto',
// marginBottom: 'auto',
// borderRadius: '4px',
// '&:hover': {
// backgroundColor: theme.palette.primary.light,
// cursor: 'pointer',
// },
// '&:disabled': {
// backgroundColor: 'rgba(0,0,0,0)',
// color: theme.palette.getContrastText(theme.palette.primary.main),
// },
// }));
useEffect(() => { useEffect(() => {
if (as.includes("auth0")) setAsUser(as); if (as.includes("auth0")) setAsUser(as);
}, [as]); }, [as]);
@ -130,6 +112,13 @@ export default function TeamList(props: Props) {
setIsUser(userId === asUser); setIsUser(userId === asUser);
}, [userId, asUser]); }, [userId, asUser]);
// Convert string array to valid Permission[] array
// const convertToPermissions = (permissions: any[]): pond.Permission[] => {
// return permissions
// .map(p => p as any as pond.Permission) // Type assertion to treat strings as Permission
// .filter(p => Object.entries(pond.Permission).includes(p)); // Filter to ensure valid Permission values
// };
const loadTeams = useCallback(() => { const loadTeams = useCallback(() => {
setLoading(true); setLoading(true);
let newTeamPerms: pond.Permission[][] = []; let newTeamPerms: pond.Permission[][] = [];
@ -145,6 +134,16 @@ export default function TeamList(props: Props) {
userAPI userAPI
.getUser(u, teamScope(team.settings?.key)) .getUser(u, teamScope(team.settings?.key))
.then(resp => { .then(resp => {
// let permissions: pond.Permission[] = resp.permissions.map(perm => pond.Permission[perm as unknown as keyof typeof pond.Permission]);
// console.log(resp.permissions)
// console.log(typeof(resp.permissions))
resp.permissions.forEach(p => {
// console.log('Value:', p, 'Type:', typeof p);
});
// permissions.forEach(p => {
// console.log('Value:', p, 'Type:', typeof p);
// });
// console.log('Expected Value:', pond.Permission.PERMISSION_WRITE, 'Type:', typeof pond.Permission.PERMISSION_WRITE);
newTeamPerms[index] = resp.permissions; newTeamPerms[index] = resp.permissions;
newTeamPrefs[index] = resp.preferences; newTeamPrefs[index] = resp.preferences;
}) })
@ -183,7 +182,7 @@ export default function TeamList(props: Props) {
<ListItemAvatar> <ListItemAvatar>
<Avatar <Avatar
onClick={() => { onClick={() => {
if (team.settings) history("teams/" + team.settings.key); if (team.settings) history(team.settings.key);
}} }}
src={team.settings?.avatar} src={team.settings?.avatar}
alt="" alt=""
@ -195,7 +194,7 @@ export default function TeamList(props: Props) {
<div <div
className={classes.clickable} className={classes.clickable}
onClick={() => { onClick={() => {
if (team.settings) history("teams/" + team.settings.key); if (team.settings) history(team.settings.key);
}}> }}>
{team.settings?.name} {team.settings?.name}
</div> </div>
@ -219,7 +218,6 @@ export default function TeamList(props: Props) {
if (preferences) { if (preferences) {
let newTeamPrefs = cloneDeep(teamPrefs); let newTeamPrefs = cloneDeep(teamPrefs);
newTeamPrefs[index].notify = !newTeamPrefs[index].notify; newTeamPrefs[index].notify = !newTeamPrefs[index].notify;
console.log(newTeamPrefs[index]);
teamAPI teamAPI
.updatePreferences( .updatePreferences(
Team.create(team).key(), Team.create(team).key(),
@ -227,7 +225,7 @@ export default function TeamList(props: Props) {
getContextKeys(), getContextKeys(),
getContextTypes() getContextTypes()
) )
.then(resp => { .then(() => {
setTeamPrefs(newTeamPrefs); setTeamPrefs(newTeamPrefs);
}); });
// setTeamPrefs(newTeamPrefs) // setTeamPrefs(newTeamPrefs)