diff --git a/react/package-lock.json b/react/package-lock.json index 2a170f40..f5c62209 100644 --- a/react/package-lock.json +++ b/react/package-lock.json @@ -15,6 +15,7 @@ "@reduxjs/toolkit": "^1.8.4", "@testing-library/react": "^13.4.0", "@types/leaflet.markercluster": "^1.5.1", + "axios": "^1.6.2", "eslint-config-prettier": "^8.5.0", "eslint-plugin-jsx-a11y": "^6.6.1", "eslint-plugin-prettier": "^4.2.1", @@ -24,6 +25,7 @@ "react": "^18.2.0", "react-dom": "^18.2.0", "react-leaflet": "^4.2.0", + "react-query": "^3.39.3", "react-redux": "^8.0.2", "react-router-dom": "^6.3.0" }, @@ -3321,8 +3323,7 @@ "node_modules/asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", - "dev": true + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" }, "node_modules/available-typed-arrays": { "version": "1.0.5", @@ -3343,6 +3344,16 @@ "node": ">=4" } }, + "node_modules/axios": { + "version": "1.6.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.2.tgz", + "integrity": "sha512-7i24Ri4pmDRfJTR7LDBhsOTtcm+9kjX5WiY1X3wIisx6G9So3pfMkEiU7emUBe46oceVImccTEM3k6C5dbVW8A==", + "dependencies": { + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, "node_modules/axobject-query": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-3.1.1.tgz", @@ -3491,6 +3502,14 @@ "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" }, + "node_modules/big-integer": { + "version": "1.6.52", + "resolved": "https://registry.npmjs.org/big-integer/-/big-integer-1.6.52.tgz", + "integrity": "sha512-QxD8cf2eVqJOOz63z6JIN9BzvVs/dlySa5HGSBH5xtR8dPteIRQnBxxKqkNTiT6jbDTF6jAfrd4oMcND9RGbQg==", + "engines": { + "node": ">=0.6" + } + }, "node_modules/brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", @@ -3512,6 +3531,21 @@ "node": ">=8" } }, + "node_modules/broadcast-channel": { + "version": "3.7.0", + "resolved": "https://registry.npmjs.org/broadcast-channel/-/broadcast-channel-3.7.0.tgz", + "integrity": "sha512-cIAKJXAxGJceNZGTZSBzMxzyOn72cVgPnKx4dc6LRjQgbaJUQqhy5rzL3zbMxkMWsGKkv2hSFkPRMEXfoMZ2Mg==", + "dependencies": { + "@babel/runtime": "^7.7.2", + "detect-node": "^2.1.0", + "js-sha3": "0.8.0", + "microseconds": "0.2.0", + "nano-time": "1.0.0", + "oblivious-set": "1.0.0", + "rimraf": "3.0.2", + "unload": "2.2.0" + } + }, "node_modules/browserslist": { "version": "4.21.5", "resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.21.5.tgz", @@ -3705,7 +3739,6 @@ "version": "1.0.8", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", - "dev": true, "dependencies": { "delayed-stream": "~1.0.0" }, @@ -3884,7 +3917,6 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", - "dev": true, "engines": { "node": ">=0.4.0" } @@ -3898,6 +3930,11 @@ "node": ">=8" } }, + "node_modules/detect-node": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/detect-node/-/detect-node-2.1.0.tgz", + "integrity": "sha512-T0NIuQpnTvFDATNuHN5roPwSBG83rFsuO+MXXH9/3N1eFbn4wcPjttvjMLEPWJ0RGUYgQE7cGgS3tNxbqCGM7g==" + }, "node_modules/diff-sequences": { "version": "29.4.3", "resolved": "https://registry.npmjs.org/diff-sequences/-/diff-sequences-29.4.3.tgz", @@ -5029,6 +5066,25 @@ "resolved": "https://registry.npmjs.org/flatted/-/flatted-3.2.7.tgz", "integrity": "sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==" }, + "node_modules/follow-redirects": { + "version": "1.15.3", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.3.tgz", + "integrity": "sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, "node_modules/for-each": { "version": "0.3.3", "resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz", @@ -5041,7 +5097,6 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", - "dev": true, "dependencies": { "asynckit": "^0.4.0", "combined-stream": "^1.0.8", @@ -6813,6 +6868,11 @@ "url": "https://opencollective.com/js-sdsl" } }, + "node_modules/js-sha3": { + "version": "0.8.0", + "resolved": "https://registry.npmjs.org/js-sha3/-/js-sha3-0.8.0.tgz", + "integrity": "sha512-gF1cRrHhIzNfToc802P800N8PpXS+evLLXfsVpowqmAFR9uwbi89WvXg2QspOmXL8QL86J4T1EpFu+yUkwJY3Q==" + }, "node_modules/js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", @@ -7100,6 +7160,15 @@ "tmpl": "1.0.5" } }, + "node_modules/match-sorter": { + "version": "6.3.1", + "resolved": "https://registry.npmjs.org/match-sorter/-/match-sorter-6.3.1.tgz", + "integrity": "sha512-mxybbo3pPNuA+ZuCUhm5bwNkXrJTbsk5VWbR5wiwz/GC6LIiegBGn2w3O08UG/jdbYLinw51fSQ5xNU1U3MgBw==", + "dependencies": { + "@babel/runtime": "^7.12.5", + "remove-accents": "0.4.2" + } + }, "node_modules/merge-stream": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz", @@ -7128,11 +7197,15 @@ "node": ">=8.6" } }, + "node_modules/microseconds": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/microseconds/-/microseconds-0.2.0.tgz", + "integrity": "sha512-n7DHHMjR1avBbSpsTBj6fmMGh2AGrifVV4e+WYc3Q9lO+xnSZ3NyhcBND3vzzatt05LFhoKFRxrIyklmLlUtyA==" + }, "node_modules/mime-db": { "version": "1.52.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", - "dev": true, "engines": { "node": ">= 0.6" } @@ -7141,7 +7214,6 @@ "version": "2.1.35", "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", - "dev": true, "dependencies": { "mime-db": "1.52.0" }, @@ -7174,6 +7246,14 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, + "node_modules/nano-time": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/nano-time/-/nano-time-1.0.0.tgz", + "integrity": "sha512-flnngywOoQ0lLQOTRNexn2gGSNuM9bKj9RZAWSzhQ+UJYaAFG9bac4DW9VHjUAzrOaIcajHybCTHe/bkvozQqA==", + "dependencies": { + "big-integer": "^1.6.16" + } + }, "node_modules/nanoid": { "version": "3.3.4", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.4.tgz", @@ -7351,6 +7431,11 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/oblivious-set": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/oblivious-set/-/oblivious-set-1.0.0.tgz", + "integrity": "sha512-z+pI07qxo4c2CulUHCDf9lcqDlMSo72N/4rLUpRXf6fu+q8vjt8y0xS+Tlf8NTJDdTXHbdeO1n3MlbctwEoXZw==" + }, "node_modules/once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", @@ -7707,6 +7792,11 @@ "integrity": "sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==", "dev": true }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "node_modules/psl": { "version": "1.9.0", "resolved": "https://registry.npmjs.org/psl/-/psl-1.9.0.tgz", @@ -7803,6 +7893,31 @@ "react-dom": "^18.0.0" } }, + "node_modules/react-query": { + "version": "3.39.3", + "resolved": "https://registry.npmjs.org/react-query/-/react-query-3.39.3.tgz", + "integrity": "sha512-nLfLz7GiohKTJDuT4us4X3h/8unOh+00MLb2yJoGTPjxKs2bc1iDhkNx2bd5MKklXnOD3NrVZ+J2UXujA5In4g==", + "dependencies": { + "@babel/runtime": "^7.5.5", + "broadcast-channel": "^3.4.1", + "match-sorter": "^6.0.2" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/tannerlinsley" + }, + "peerDependencies": { + "react": "^16.8.0 || ^17.0.0 || ^18.0.0" + }, + "peerDependenciesMeta": { + "react-dom": { + "optional": true + }, + "react-native": { + "optional": true + } + } + }, "node_modules/react-redux": { "version": "8.0.5", "resolved": "https://registry.npmjs.org/react-redux/-/react-redux-8.0.5.tgz", @@ -7981,6 +8096,11 @@ "jsesc": "bin/jsesc" } }, + "node_modules/remove-accents": { + "version": "0.4.2", + "resolved": "https://registry.npmjs.org/remove-accents/-/remove-accents-0.4.2.tgz", + "integrity": "sha512-7pXIJqJOq5tFgG1A2Zxti3Ht8jJF337m4sowbuHsW30ZnkQFnDzy9qBNhgzX8ZLW4+UBcXiiR7SwR6pokHsxiA==" + }, "node_modules/require-directory": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", @@ -8729,6 +8849,15 @@ "node": ">= 4.0.0" } }, + "node_modules/unload": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/unload/-/unload-2.2.0.tgz", + "integrity": "sha512-B60uB5TNBLtN6/LsgAf3udH9saB5p7gqJwcFfbOEZ8BcBHnGwCf6G/TGiEqkRAxX7zAFIUtzdrXQSdL3Q/wqNA==", + "dependencies": { + "@babel/runtime": "^7.6.2", + "detect-node": "^2.0.4" + } + }, "node_modules/update-browserslist-db": { "version": "1.0.10", "resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.0.10.tgz", diff --git a/react/package.json b/react/package.json index 80095147..7ea759f6 100644 --- a/react/package.json +++ b/react/package.json @@ -25,6 +25,7 @@ "@reduxjs/toolkit": "^1.8.4", "@testing-library/react": "^13.4.0", "@types/leaflet.markercluster": "^1.5.1", + "axios": "^1.6.2", "eslint-config-prettier": "^8.5.0", "eslint-plugin-jsx-a11y": "^6.6.1", "eslint-plugin-prettier": "^4.2.1", @@ -34,6 +35,7 @@ "react": "^18.2.0", "react-dom": "^18.2.0", "react-leaflet": "^4.2.0", + "react-query": "^3.39.3", "react-redux": "^8.0.2", "react-router-dom": "^6.3.0" }, diff --git a/react/src/hooks/index.ts b/react/src/hooks/index.ts new file mode 100644 index 00000000..62a23442 --- /dev/null +++ b/react/src/hooks/index.ts @@ -0,0 +1 @@ +export { default as useProjects } from './projects/useProjects'; diff --git a/react/src/hooks/projects/useProjects.ts b/react/src/hooks/projects/useProjects.ts new file mode 100644 index 00000000..2e9a8715 --- /dev/null +++ b/react/src/hooks/projects/useProjects.ts @@ -0,0 +1,14 @@ +import { UseQueryResult } from 'react-query'; +import { Project } from '../../types'; +import { useGet } from '../../requests'; + +const useProjects = (): UseQueryResult => { + const query = useGet({ + endpoint: '/projects/', + key: ['projects'], + baseUrl: 'https://agave.designsafe-ci.org/geo/v2', + }); + return query; +}; + +export default useProjects; diff --git a/react/src/index.tsx b/react/src/index.tsx index 5979437b..4a1da182 100644 --- a/react/src/index.tsx +++ b/react/src/index.tsx @@ -1,14 +1,19 @@ import React from 'react'; import ReactDOM from 'react-dom/client'; +import { QueryClient, QueryClientProvider } from 'react-query'; import App from './AppRouter'; import './index.css'; import store from './redux/store'; import { Provider } from 'react-redux'; +const queryClient = new QueryClient(); + ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render( - + + + ); diff --git a/react/src/pages/Login/Login.test.tsx b/react/src/pages/Login/Login.test.tsx index da05aa26..5bac8d82 100644 --- a/react/src/pages/Login/Login.test.tsx +++ b/react/src/pages/Login/Login.test.tsx @@ -3,13 +3,17 @@ import { render, waitFor } from '@testing-library/react'; import Login from './Login'; import { Provider } from 'react-redux'; import store from '../../redux/store'; +import { QueryClientProvider } from 'react-query'; +import { testQueryClient } from '../../testUtil'; import { MemoryRouter } from 'react-router'; test('renders login', async () => { const { getByText } = render( - + + + ); diff --git a/react/src/pages/MainMenu/MainMenu.test.tsx b/react/src/pages/MainMenu/MainMenu.test.tsx index cf38b508..8f373c71 100644 --- a/react/src/pages/MainMenu/MainMenu.test.tsx +++ b/react/src/pages/MainMenu/MainMenu.test.tsx @@ -1,13 +1,17 @@ import React from 'react'; import { render } from '@testing-library/react'; import MainMenu from './MainMenu'; +import { QueryClientProvider } from 'react-query'; +import { testQueryClient } from '../../testUtil'; import { Provider } from 'react-redux'; import store from '../../redux/store'; test('renders menu', () => { const { getByText } = render( - + + + ); expect(getByText(/Main Menu/)).toBeDefined(); diff --git a/react/src/pages/MainMenu/MainMenu.tsx b/react/src/pages/MainMenu/MainMenu.tsx index f1216a57..bfb59bc8 100644 --- a/react/src/pages/MainMenu/MainMenu.tsx +++ b/react/src/pages/MainMenu/MainMenu.tsx @@ -1,13 +1,14 @@ import React from 'react'; -import { - useGetGeoapiProjectsQuery, - useGetGeoapiUserInfoQuery, -} from '../../redux/api/geoapi'; +import { useProjects } from '../../hooks'; function MainMenu() { - useGetGeoapiProjectsQuery(); - useGetGeoapiUserInfoQuery(); - return

Main Menu

; + const { data } = useProjects(); + return ( + <> +

Main Menu

+
You have {data?.length} projects.
+ + ); } export default MainMenu; diff --git a/react/src/requests.ts b/react/src/requests.ts new file mode 100644 index 00000000..7ad90c64 --- /dev/null +++ b/react/src/requests.ts @@ -0,0 +1,37 @@ +import axios from 'axios'; +import store from './redux/store'; +import { AxiosError } from 'axios'; +import { useQuery, UseQueryOptions, QueryKey } from 'react-query'; + +type UseGetParams = { + endpoint: string; + key: QueryKey; + options?: Omit< + UseQueryOptions, + 'queryKey' | 'queryFn' + >; + baseUrl: string; +}; + +export function useGet({ + endpoint, + key, + options = {}, + baseUrl, +}: UseGetParams) { + const client = axios; + const state = store.getState(); + const token = state.auth.token?.token; + // change to prod const { baseUrl } = useConfig(); + const getUtil = async () => { + const request = await client.get( + `${baseUrl}${endpoint}`, + + { + headers: { Authorization: `Bearer ${token}` }, + } + ); + return request.data; + }; + return useQuery(key, () => getUtil(), options); +} diff --git a/react/src/testUtil.ts b/react/src/testUtil.ts new file mode 100644 index 00000000..4c522729 --- /dev/null +++ b/react/src/testUtil.ts @@ -0,0 +1,5 @@ +import { QueryClient } from 'react-query'; + +export const testQueryClient = new QueryClient({ + defaultOptions: { queries: { retry: false, cacheTime: Infinity } }, +}); diff --git a/react/src/types/index.ts b/react/src/types/index.ts index 789ea821..a7280fb1 100644 --- a/react/src/types/index.ts +++ b/react/src/types/index.ts @@ -5,4 +5,5 @@ export type { FeatureClass, FeatureCollection, } from './feature'; +export type { Project } from './projects'; export type { AuthState, AuthenticatedUser, AuthToken } from './auth'; diff --git a/react/src/types/projects.ts b/react/src/types/projects.ts new file mode 100644 index 00000000..14fca1fe --- /dev/null +++ b/react/src/types/projects.ts @@ -0,0 +1,14 @@ +export interface Project { + description: string; + id?: number; + name: string; + title?: string; + uuid?: string; + public?: boolean; + system_file?: string; + system_id?: string; + system_path?: string; + deletable?: boolean; + streetview_instances?: any; +} +export class Project implements Project {}