Skip to content

Commit

Permalink
fix: Only allow coordinator to update config (#756) (#759)
Browse files Browse the repository at this point in the history
* chore: check if user is coordinator

* chore: create useGetOwnRole hook

* chore: useGetOwnRole hook in config screen

* chore: change role key
  • Loading branch information
ErikSin authored Sep 26, 2024
1 parent 63186f1 commit 20930d8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
12 changes: 12 additions & 0 deletions src/frontend/hooks/server/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const PROJECT_KEY = 'project';
export const PROJECT_MEMBERS_KEY = 'project_members';
export const ORIGINAL_VERSION_ID_TO_DEVICE_ID_KEY =
'originalVersionIdToDeviceId';
export const THIS_USERS_ROLE_KEY = 'my_role';

export function useProject(projectId?: string) {
const api = useApi();
Expand Down Expand Up @@ -147,3 +148,14 @@ export function useImportProjectConfig() {
},
});
}

export function useGetOwnRole() {
const {projectId, projectApi} = useActiveProject();

return useQuery({
queryKey: [THIS_USERS_ROLE_KEY, projectId],
queryFn: () => {
return projectApi.$getOwnRole();
},
});
}
18 changes: 12 additions & 6 deletions src/frontend/screens/Settings/Config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import * as React from 'react';
import {defineMessages, useIntl} from 'react-intl';
import {StyleSheet, View} from 'react-native';
import {Text} from '../../sharedComponents/Text';
import {useProjectSettings} from '../../hooks/server/projects';
import {useGetOwnRole, useProjectSettings} from '../../hooks/server/projects';
import {Loading} from '../../sharedComponents/Loading';
import {NativeNavigationComponent} from '../../sharedTypes/navigation';
import {COMAPEO_BLUE, MEDIUM_GREY} from '../../lib/styles';
import {Button} from '../../sharedComponents/Button';
import {UIActivityIndicator} from 'react-native-indicators';
import {ErrorBottomSheet} from '../../sharedComponents/ErrorBottomSheet';
import {useSelectFileAndImportConfig} from '../../hooks/useSelectFileAndImportConfig';
import {COORDINATOR_ROLE_ID, CREATOR_ROLE_ID} from '../../sharedTypes';

const m = defineMessages({
navTitle: {
Expand Down Expand Up @@ -37,9 +38,13 @@ const m = defineMessages({
export const Config: NativeNavigationComponent<'Config'> = ({navigation}) => {
const {formatMessage} = useIntl();
const {data, isPending} = useProjectSettings();

const deviceRole = useGetOwnRole();
const selectAndImportConfigMutation = useSelectFileAndImportConfig();

const isCoordinator =
deviceRole.data?.roleId === COORDINATOR_ROLE_ID ||
deviceRole.data?.roleId === CREATOR_ROLE_ID;

React.useEffect(() => {
// Prevent back navigation while project creation mutation is pending
const unsubscribe = navigation.addListener('beforeRemove', event => {
Expand All @@ -56,6 +61,7 @@ export const Config: NativeNavigationComponent<'Config'> = ({navigation}) => {
}, [navigation, selectAndImportConfigMutation.isPending]);

async function importConfigFile() {
if (!isCoordinator) return;
selectAndImportConfigMutation.mutate();
}

Expand Down Expand Up @@ -83,7 +89,9 @@ export const Config: NativeNavigationComponent<'Config'> = ({navigation}) => {
<Text>{data.configMetadata.name}</Text>
</>
)}
{!selectAndImportConfigMutation.isPending ? (
{deviceRole.isPending || selectAndImportConfigMutation.isPending ? (
<UIActivityIndicator style={{marginTop: 20, flex: 0}} />
) : isCoordinator ? (
<Button
style={{marginTop: 20}}
fullWidth
Expand All @@ -93,9 +101,7 @@ export const Config: NativeNavigationComponent<'Config'> = ({navigation}) => {
{formatMessage(m.importConfig)}
</Text>
</Button>
) : (
<UIActivityIndicator style={{marginTop: 20, flex: 0}} />
)}
) : null}
<ErrorBottomSheet
error={selectAndImportConfigMutation.error}
clearError={() => {
Expand Down

0 comments on commit 20930d8

Please sign in to comment.