Skip to content

Commit de002b6

Browse files
authored
Merge pull request #116 from CivicDataLab/fix-file-structure
fix resource file structure
2 parents eeafc1c + 74e95c0 commit de002b6

File tree

5 files changed

+92
-171
lines changed

5 files changed

+92
-171
lines changed

app/[locale]/dashboard/organization/[organizationId]/dataset/[id]/edit/components/EditResource.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import React from 'react';
2222

2323
import { Icons } from '@/components/icons';
2424
import { GraphQL } from '@/lib/api';
25-
import { createResourceFilesDoc } from './DistributionList';
25+
import { createResourceFilesDoc } from './ResourceDropzone';
2626
import { ResourceSchema } from './ResourceSchema';
2727

2828
interface TListItem {

app/[locale]/dashboard/organization/[organizationId]/dataset/[id]/edit/components/DistributionList.tsx renamed to app/[locale]/dashboard/organization/[organizationId]/dataset/[id]/edit/components/ResourceDropzone.tsx

Lines changed: 2 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,20 @@
11
import React from 'react';
2-
import Link from 'next/link';
32
import { useParams, useSearchParams } from 'next/navigation';
4-
import router from 'next/router';
53
import { graphql } from '@/gql';
64
import { CreateFileResourceInput } from '@/gql/generated/graphql';
75
import { useMutation, useQuery } from '@tanstack/react-query';
86
import {
9-
parseAsBoolean,
107
parseAsString,
118
useQueryState,
129
} from 'next-usequerystate';
1310
import {
1411
Button,
15-
ButtonGroup,
16-
Combobox,
17-
Divider,
1812
DropZone,
19-
Icon,
20-
Spinner,
2113
Text,
2214
} from 'opub-ui';
2315

2416
import { GraphQL } from '@/lib/api';
2517
import { bytesToSize } from '@/lib/utils';
26-
import { Icons } from '@/components/icons';
27-
import { LinkButton } from '@/components/Link';
28-
import { EditDistribution } from './EditDistribution';
29-
import { EditResource } from './EditResource';
30-
import { ResourceListView } from './ResourceListView';
31-
32-
export const getReourceDoc: any = graphql(`
33-
query getResources($filters: DatasetFilter) {
34-
datasets(filters: $filters) {
35-
resources {
36-
id
37-
dataset {
38-
pk
39-
}
40-
schema {
41-
id
42-
fieldName
43-
format
44-
description
45-
}
46-
type
47-
name
48-
description
49-
created
50-
fileDetails {
51-
id
52-
resource {
53-
pk
54-
}
55-
file {
56-
name
57-
path
58-
url
59-
}
60-
size
61-
created
62-
modified
63-
}
64-
}
65-
}
66-
}
67-
`);
68-
69-
export function DistributionList({
70-
setPage,
71-
}: {
72-
setPage: (page: 'list' | 'create') => void;
73-
setEditId: (id: string) => void;
74-
}) {
75-
const params = useParams();
76-
77-
const {
78-
data,
79-
isLoading,
80-
refetch,
81-
}: { data: any; isLoading: boolean; refetch: any } = useQuery(
82-
[`fetch_resources_${params.id}`],
83-
() => GraphQL(getReourceDoc, { filters: { id: params.id } }),
84-
{
85-
refetchOnMount: true,
86-
refetchOnReconnect: true,
87-
}
88-
);
89-
90-
const searchParams = useSearchParams();
91-
const resourceId = searchParams.get('id');
92-
93-
return (
94-
<div>
95-
{isLoading ? (
96-
<div className="flex h-[70vh] w-full items-center justify-center">
97-
<Spinner size={40} />
98-
</div>
99-
) : (
100-
<>
101-
{data && data.datasets[0].resources.length > 0 ? (
102-
<>
103-
{resourceId ? (
104-
<EditResource
105-
reload={refetch}
106-
data={data.datasets[0].resources}
107-
/>
108-
) : (
109-
<ResourceListView
110-
refetch={refetch}
111-
data={data.datasets[0].resources}
112-
/>
113-
)}
114-
</>
115-
) : (
116-
<div className="py-4">
117-
<NoList setPage={setPage} reload={refetch} />
118-
</div>
119-
)}
120-
</>
121-
)}
122-
</div>
123-
);
124-
}
12518

12619
export const createResourceFilesDoc: any = graphql(`
12720
mutation readFiles($fileResourceInput: CreateFileResourceInput!) {
@@ -134,12 +27,10 @@ export const createResourceFilesDoc: any = graphql(`
13427
}
13528
`);
13629

137-
const NoList = ({
138-
setPage,
30+
export const ResourceDropzone = ({
13931
reload,
14032
}: {
141-
setPage: (page: 'list' | 'create') => void;
142-
reload: any;
33+
reload: () => void;
14334
}) => {
14435
const fileTypes = ['PDF', 'CSV', 'XLS', 'XLSX', 'TXT'];
14536
const params = useParams();

app/[locale]/dashboard/organization/[organizationId]/dataset/[id]/edit/components/ResourceListView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
import { GraphQL } from '@/lib/api';
2727
import { formatDate } from '@/lib/utils';
2828
import { Icons } from '@/components/icons';
29-
import { createResourceFilesDoc } from './DistributionList';
29+
import { createResourceFilesDoc } from './ResourceDropzone';
3030

3131
type FilteredRow = {
3232
name_of_resource: string;

app/[locale]/dashboard/organization/[organizationId]/dataset/[id]/edit/resources/loading.tsx

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 88 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,105 @@
11
'use client';
22

33
import React from 'react';
4+
import { useSearchParams } from 'next/navigation';
45
import { graphql } from '@/gql';
5-
// import { usePRouter } from '@/hooks/use-prouter';
6+
67
import { useQuery } from '@tanstack/react-query';
8+
import { Spinner,Text } from 'opub-ui';
79

810
import { GraphQL } from '@/lib/api';
9-
import { LinkButton } from '@/components/Link';
10-
import { DistributionList } from '../components/DistributionList';
11-
import { EditDistribution } from '../components/EditDistribution';
11+
import { EditResource } from '../components/EditResource';
12+
import { ResourceDropzone } from '../components/ResourceDropzone';
13+
import { ResourceListView } from '../components/ResourceListView';
1214

13-
// const datasetDistributionQueryDoc = graphql(`
14-
// query datasetDistributionQuery($dataset_id: Int) {
15-
// dataset(dataset_id: $dataset_id) {
16-
// id
17-
// title
18-
// resource_set {
19-
// id
20-
// title
21-
// description
22-
// file_details {
23-
// resource {
24-
// id
25-
// title
26-
// description
27-
// }
28-
// format
29-
// file
30-
// remote_url
31-
// source_file_name
32-
// }
33-
// }
34-
// }
35-
// }
36-
// `);
15+
export const getReourceDoc = graphql(`
16+
query getResources($filters: DatasetFilter) {
17+
datasets(filters: $filters) {
18+
resources {
19+
id
20+
dataset {
21+
pk
22+
}
23+
schema {
24+
id
25+
fieldName
26+
format
27+
description
28+
}
29+
type
30+
name
31+
description
32+
created
33+
fileDetails {
34+
id
35+
resource {
36+
pk
37+
}
38+
file {
39+
name
40+
path
41+
url
42+
}
43+
size
44+
created
45+
modified
46+
}
47+
}
48+
}
49+
}
50+
`);
3751

3852
export function DistibutionPage({ params }: { params: { id: string } }) {
39-
const [editId, setEditId] = React.useState<string | null>(null);
40-
const [page, setPage] = React.useState<'list' | 'create'>('list');
41-
// const router = usePRouter();
42-
const submitRef = React.useRef<HTMLButtonElement>(null);
4353

44-
// const { data } = useQuery([`dataset_distribution_${params.id}`], () =>
45-
// GraphQL(datasetDistributionQueryDoc, {
46-
// dataset_id: Number(params.id),
47-
// })
48-
// );
54+
const { data, isLoading, refetch } = useQuery(
55+
[`fetch_resources_${params.id}`],
56+
() => GraphQL(getReourceDoc, { filters: { id: params.id } }),
57+
{
58+
refetchOnMount: true,
59+
refetchOnReconnect: true,
60+
}
61+
);
62+
63+
const searchParams = useSearchParams();
64+
const resourceId = searchParams.get('id');
4965

5066
return (
5167
<>
52-
{!editId && page === 'list' ? (
53-
<div>
54-
<DistributionList setPage={setPage} setEditId={setEditId} />
55-
</div>
56-
) : (
57-
// <EditDistribution
58-
// setPage={setPage}
59-
// submitRef={submitRef}
60-
// id={params.id}
61-
// defaultVal={{
62-
// id: params.id,
63-
// resources: data?.dataset?.resource_set || [],
64-
// }}
65-
// />
66-
<>Edit Distribution</>
67-
)}
68+
<div>
69+
{isLoading ? (
70+
<div className="flex min-h-full w-full items-center justify-center">
71+
<Spinner size={40} />
72+
</div>
73+
) : (
74+
<>
75+
{data && data.datasets[0].resources.length > 0 ? (
76+
<>
77+
{resourceId ? (
78+
<EditResource
79+
reload={refetch}
80+
data={data.datasets[0].resources}
81+
/>
82+
) : (
83+
<ResourceListView
84+
refetch={refetch}
85+
data={data.datasets[0].resources}
86+
/>
87+
)}
88+
</>
89+
) : data && data.datasets[0].resources.length === 0 ? (
90+
<div className="py-4">
91+
<ResourceDropzone reload={refetch} />
92+
</div>
93+
) : (
94+
<div className="flex h-[70vh] w-full items-center justify-center">
95+
<Text variant="headingLg">
96+
Please refresh this page
97+
</Text>
98+
</div>
99+
)}
100+
</>
101+
)}
102+
</div>
68103
</>
69104
);
70105
}

0 commit comments

Comments
 (0)