Skip to content

Commit

Permalink
fix: Removed group visualization dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
josebui committed Oct 17, 2023
1 parent 7d0d2ed commit badf3b1
Show file tree
Hide file tree
Showing 15 changed files with 135 additions and 77 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"slate-history": "^0.93.0",
"slate-hyperscript": "^0.77.0",
"slate-react": "^0.99.0",
"terraso-client-shared": "github:techmatters/terraso-client-shared#f211e372131f71d40f66b5e3a77f629db879d837",
"terraso-client-shared": "github:techmatters/terraso-client-shared#6cc1a24717090875f8d1822986529eeb6237c746",
"use-debounce": "^9.0.4",
"uuid": "^9.0.0",
"web-vitals": "^3.5.0",
Expand Down
14 changes: 9 additions & 5 deletions src/group/groupFragments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ export const groupsListFields = /* GraphQL */ `
}
`;

export const dataEntries = /* GraphQL */ `
fragment dataEntries on GroupNode {
dataEntries(resourceType_In: $resourceTypes) {
export const groupDataEntries = /* GraphQL */ `
fragment groupDataEntries on GroupNode {
sharedResources(source_DataEntry_ResourceType_In: $resourceTypes) {
edges {
node {
...dataEntry
...dataEntryVisualizations
source {
... on DataEntryNode {
...dataEntry
...dataEntryVisualizations
}
}
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/group/groupService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import { extractDataEntries } from 'sharedData/sharedDataUtils';

import type { Group } from './groupSlice';

import { SHARED_DATA_ACCEPTED_EXTENSIONS } from 'config';

export const fetchGroupToUpdate = (slug: string) => {
const query = graphql(`
query groupToUpdate($slug: String!) {
Expand All @@ -48,21 +50,25 @@ export const fetchGroupToUpdate = (slug: string) => {

export const fetchGroupToView = async (slug: string) => {
const query = graphql(`
query groupToView($slug: String!) {
query groupToView($slug: String!, $resourceTypes: [String]!) {
groups(slug: $slug) {
edges {
node {
...groupFields
...groupMembersInfo
...groupMembersPending
...accountMembership
...groupDataEntries
}
}
}
}
`);
return terrasoApi
.requestGraphQL(query, { slug })
.requestGraphQL(query, {
slug,
resourceTypes: SHARED_DATA_ACCEPTED_EXTENSIONS,
})
.then(resp => resp.groups?.edges.at(0)?.node || Promise.reject('not_found'))
.then(group => ({
..._.omit(['memberships', 'membershipsCount'], group),
Expand Down
3 changes: 1 addition & 2 deletions src/landscape/components/LandscapeSharedDataVisualization.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ const LandscapeSharedDataVisualization = () => {
}

return (
<GroupContextProvider group={landscape.defaultGroup} owner={landscape}>
<GroupContextProvider owner={landscape}>
<VisualizationWrapper
configSlug={configSlug}
groupSlug={landscape.defaultGroup.slug}
onDeleted={() => navigate(`/landscapes/${landscapeSlug}`)}
/>
</GroupContextProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,12 @@ test('LandscapeSharedDataVisualization: Display visualization', async () => {
expect(domElement.querySelector('p').textContent).toEqual(
'Custom Label: val11'
);

// Validate fetch input
const fetchCall = terrasoApi.requestGraphQL.mock.calls[3];
expect(fetchCall[1]).toStrictEqual({
configSlug: 'config-slug',
ownerSlug: 'jose-landscape-deafult-test-4',
ownerType: 'landscape',
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const LandscapeSharedDataVisualizationConfig = () => {

return (
<PageContainer>
<GroupContextProvider group={landscape.defaultGroup} owner={landscape}>
<GroupContextProvider owner={landscape}>
<VisualizationConfigForm
onCompleteSuccess={onCompleteSuccess}
onCancel={onCancel}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,33 +255,25 @@ test('LandscapeSharedDataVisualizationConfig: Create visualization', async () =>
},
});
}
if (trimmedQuery.startsWith('query group')) {
if (trimmedQuery.startsWith('query dataEntries')) {
return Promise.resolve({
groups: {
dataEntries: {
edges: [
{
node: {
dataEntries: {
edges: [
{
node: {
createdAt: '2022-05-17T23:32:50.606587+00:00',
createdBy: {
id: 'dc695d00-d6b4-45b2-ab8d-f48206d998da',
lastName: 'Buitrón',
firstName: 'José',
},
description: '',
id: 'f00c5564-cf93-471a-94c2-b930cbb0a4f8',
name: 'File 1',
resourceType: 'text/csv',
size: 3565,
url: 'https://file-url',
visualizations: { edges: [] },
},
},
],
createdAt: '2022-05-17T23:32:50.606587+00:00',
createdBy: {
id: 'dc695d00-d6b4-45b2-ab8d-f48206d998da',
lastName: 'Buitrón',
firstName: 'José',
},
description: '',
id: 'f00c5564-cf93-471a-94c2-b930cbb0a4f8',
name: 'File 1',
resourceType: 'text/csv',
size: 3565,
url: 'https://file-url',
visualizations: { edges: [] },
},
},
],
Expand Down Expand Up @@ -327,6 +319,14 @@ test('LandscapeSharedDataVisualizationConfig: Create visualization', async () =>
await testAnnotateStep();
await testPreviewStep(map, events);

// Fetch data entries validation
const fetchCall = terrasoApi.requestGraphQL.mock.calls[3];
expect(fetchCall[1]).toStrictEqual({
resourceTypes: ['csv', 'xls', 'xlsx'],
slug: 'landscape-slug',
type: 'landscape',
});

// Save
await act(async () =>
fireEvent.click(screen.getByRole('button', { name: 'Publish' }))
Expand All @@ -335,7 +335,8 @@ test('LandscapeSharedDataVisualizationConfig: Create visualization', async () =>
const saveCall = terrasoApi.requestGraphQL.mock.calls[4];
expect(saveCall[1]).toStrictEqual({
input: {
groupId: '6a625efb-4ec8-45e8-ad6a-eb052cc3fe65',
ownerId: 'e9a65bef-4ef1-4058-bba3-fc73b53eb779',
ownerType: 'landscape',
title: 'Test Title',
configuration: JSON.stringify({
datasetConfig: {
Expand Down
10 changes: 8 additions & 2 deletions src/landscape/landscapeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import {
extractPartnership,
} from './landscapeUtils';

import { SHARED_DATA_ACCEPTED_EXTENSIONS } from 'config';

const cleanLandscape = landscape =>
_.flow(
_.pick([
Expand Down Expand Up @@ -143,21 +145,25 @@ const getDefaultGroup = landscape => {

export const fetchLandscapeToView = slug => {
const query = graphql(`
query landscapesToView($slug: String!) {
query landscapesToView($slug: String!, $resourceTypes: [String]!) {
landscapes(slug: $slug) {
edges {
node {
...landscapeFields
...landscapePartnershipField
...defaultGroupWithMembersSample
...landscapeDataEntries
areaPolygon
}
}
}
}
`);
return terrasoApi
.requestGraphQL(query, { slug })
.requestGraphQL(query, {
slug,
resourceTypes: SHARED_DATA_ACCEPTED_EXTENSIONS,
})
.then(_.get('landscapes.edges[0].node'))
.then(landscape => landscape || Promise.reject('not_found'))
.then(landscape => ({
Expand Down
3 changes: 2 additions & 1 deletion src/permissions/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ const isAllowedToDeleteSharedData = ({ resource, user }) => {
};

const isAllowedToDeleteVisualization = ({
resource: { group, visualizationConfig },
resource: { owner, visualizationConfig },
user,
}) => {
const group = owner.defaultGroup || owner;
const isManager = hasRole({ group, role: ROLE_MANAGER });
const isOwner =
_.get('createdBy.id', visualizationConfig) === _.get('id', user);
Expand Down
56 changes: 40 additions & 16 deletions src/sharedData/sharedDataService.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import _ from 'lodash/fp';
import * as terrasoApi from 'terraso-client-shared/terrasoApi/api';
import { graphql } from 'terrasoApi/shared/graphqlSchema';

import { extractDataEntries, extractDataEntry } from './sharedDataUtils';
import { extractDataEntry } from './sharedDataUtils';

import { SHARED_DATA_ACCEPTED_EXTENSIONS } from 'config';

Expand Down Expand Up @@ -122,31 +122,45 @@ export const updateSharedData = ({ dataEntry }) => {
.then(extractDataEntry);
};

export const fetchGroupSharedData = ({
slug,
export const fetchSharedData = ({
targetSlug,
targetType,
resourceTypes = ALL_RESOURCE_TYPES,
}) => {
const query = graphql(`
query group($slug: String!, $resourceTypes: [String]!) {
groups(slug: $slug) {
query dataEntries(
$slug: String!
$type: String!
$resourceTypes: [String]!
) {
dataEntries(
sharedResources_Target_Slug: $slug
sharedResources_TargetContentType: $type
resourceType_In: $resourceTypes
) {
edges {
node {
...dataEntries
...dataEntry
...dataEntryVisualizations
}
}
}
}
`);
return terrasoApi
.requestGraphQL(query, { slug, resourceTypes })
.then(_.get('groups.edges[0].node'))
.then(group => group || Promise.reject('not_found'))
.then(group => extractDataEntries(group));
.requestGraphQL(query, {
slug: targetSlug,
type: targetType,
resourceTypes,
})
.then(_.get('dataEntries.edges'))
.then(edges => edges.map(edge => extractDataEntry(edge.node)));
};

export const addVisualizationConfig = ({
title,
group,
ownerId,
ownerType,
selectedFile,
visualizationConfig,
}) => {
Expand All @@ -171,7 +185,8 @@ export const addVisualizationConfig = ({
title,
configuration,
dataEntryId: selectedFile.id,
groupId: group.id,
ownerId,
ownerType,
},
})
.then(response => ({
Expand All @@ -195,11 +210,20 @@ export const deleteVisualizationConfig = config => {
});
};

export const fetchVisualizationConfig = ({ groupSlug, configSlug }) => {
export const fetchVisualizationConfig = ({
ownerSlug,
ownerType,
configSlug,
}) => {
const query = graphql(`
query fetchVisualizationConfig($groupSlug: String!, $configSlug: String!) {
query fetchVisualizationConfig(
$ownerSlug: String!
$ownerType: String!
$configSlug: String!
) {
visualizationConfigs(
dataEntry_SharedResources_Target_Slug: $groupSlug
dataEntry_SharedResources_Target_Slug: $ownerSlug
dataEntry_SharedResources_TargetContentType: $ownerType
slug: $configSlug
) {
edges {
Expand All @@ -214,7 +238,7 @@ export const fetchVisualizationConfig = ({ groupSlug, configSlug }) => {
}
`);
return terrasoApi
.requestGraphQL(query, { groupSlug, configSlug })
.requestGraphQL(query, { ownerSlug, ownerType, configSlug })
.then(_.get('visualizationConfigs.edges[0].node'))
.then(
visualizationConfig => visualizationConfig || Promise.reject('not_found')
Expand Down
10 changes: 5 additions & 5 deletions src/sharedData/sharedDataSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ export const updateSharedData = createAsyncThunk(
params: { name: dataEntry.name },
})
);
export const fetchGroupSharedData = createAsyncThunk(
'sharedData/fetchGroupSharedData',
sharedDataService.fetchGroupSharedData
export const fetchSharedData = createAsyncThunk(
'sharedData/fetchSharedData',
sharedDataService.fetchSharedData
);

export const addVisualizationConfig = createAsyncThunk(
Expand Down Expand Up @@ -226,15 +226,15 @@ const sharedDataSlice = createSlice({
)
);

builder.addCase(fetchGroupSharedData.pending, (state, action) => ({
builder.addCase(fetchSharedData.pending, (state, action) => ({
...state,
list: {
fetching: true,
data: null,
},
}));

builder.addCase(fetchGroupSharedData.fulfilled, (state, action) => ({
builder.addCase(fetchSharedData.fulfilled, (state, action) => ({
...state,
list: {
fetching: false,
Expand Down
Loading

0 comments on commit badf3b1

Please sign in to comment.