-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
20a4cba
commit e734cda
Showing
8 changed files
with
270 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { useQuery, QueryObserverOptions } from 'react-query'; | ||
|
||
import { useSession } from 'next-auth/react'; | ||
|
||
import { CostSurface } from 'types/api/cost-surface'; | ||
import { Project } from 'types/api/project'; | ||
|
||
import { API } from 'services/api'; | ||
|
||
export function useProjectCostSurfaces<T = CostSurface[]>( | ||
pid: Project['id'], | ||
params: { search?: string; sort?: string; filters?: Record<string, unknown> } = {}, | ||
queryOptions: QueryObserverOptions<CostSurface[], Error, T> = {} | ||
) { | ||
const { data: session } = useSession(); | ||
|
||
const mockData: CostSurface[] = [ | ||
{ | ||
id: 'Cost Surface Rwanda A', | ||
name: 'Cost Surface Rwanda A', | ||
scenarioUsageCount: 3, | ||
}, | ||
{ | ||
id: 'Cost Surface Rwanda B', | ||
name: 'Cost Surface Rwanda B', | ||
scenarioUsageCount: 0, | ||
}, | ||
{ | ||
id: 'Cost Surface Rwanda C', | ||
name: 'Cost Surface Rwanda C', | ||
scenarioUsageCount: 0, | ||
}, | ||
]; | ||
|
||
return useQuery({ | ||
queryKey: ['cost-surfaces', pid], | ||
queryFn: async () => | ||
API.request<CostSurface[]>({ | ||
method: 'GET', | ||
url: `/projects/${pid}/cost-surfaces`, | ||
headers: { | ||
Authorization: `Bearer ${session.accessToken}`, | ||
}, | ||
params, | ||
}).then(({ data }) => mockData), | ||
enabled: Boolean(pid), | ||
...queryOptions, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { AxiosRequestConfig } from 'axios'; | ||
|
||
export interface UseWDPACategoriesProps { | ||
adminAreaId?: string; | ||
customAreaId?: string; | ||
scenarioId: string[] | string; | ||
} | ||
|
||
export interface UseSaveScenarioProtectedAreasProps { | ||
requestConfig?: AxiosRequestConfig; | ||
} | ||
|
||
export interface SaveScenarioProtectedAreasProps { | ||
data: unknown; | ||
id: string[] | string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 131 additions & 1 deletion
132
app/layout/project/sidebar/project/inventory-panel/cost-surface/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,135 @@ | ||
import { useState, useCallback, useEffect, ChangeEvent } from 'react'; | ||
|
||
import { useRouter } from 'next/router'; | ||
|
||
import { useAppDispatch, useAppSelector } from 'store/hooks'; | ||
import { setSelectedCostSurfaces as setVisibleCostSurfaces } from 'store/slices/projects/[id]'; | ||
|
||
import { useProjectCostSurfaces } from 'hooks/cost-surface'; | ||
|
||
import ActionsMenu from 'layout/project/sidebar/project/inventory-panel/features/actions-menu'; | ||
import FeaturesBulkActionMenu from 'layout/project/sidebar/project/inventory-panel/features/bulk-action-menu'; | ||
import { CostSurface } from 'types/api/cost-surface'; | ||
|
||
import InventoryTable, { type DataItem } from '../components/inventory-table'; | ||
|
||
const COST_SURFACE_TABLE_COLUMNS = { | ||
name: 'Name', | ||
}; | ||
|
||
const InventoryPanelCostSurface = ({ noData: noDataMessage }: { noData: string }): JSX.Element => { | ||
return <div className="flex h-[200px] items-center justify-center">{noDataMessage}</div>; | ||
const dispatch = useAppDispatch(); | ||
const { selectedCostSurfaces: visibleCostSurfaces, search } = useAppSelector( | ||
(state) => state['/projects/[id]'] | ||
); | ||
|
||
const { query } = useRouter(); | ||
const { pid } = query as { pid: string }; | ||
|
||
const [selectedCostSurfaceIds, setSelectedCostSurfaceIds] = useState<CostSurface['id'][]>([]); | ||
const [filters, setFilters] = useState<Parameters<typeof useProjectCostSurfaces>[1]>({ | ||
sort: COST_SURFACE_TABLE_COLUMNS.name, | ||
}); | ||
|
||
const allProjectCostSurfacesQuery = useProjectCostSurfaces( | ||
pid, | ||
{ | ||
...filters, | ||
search, | ||
}, | ||
{ | ||
select: (data) => | ||
data?.map((cs) => ({ | ||
id: cs.id, | ||
name: cs.name, | ||
scenarioUsageCount: cs.scenarioUsageCount, | ||
})), | ||
keepPreviousData: true, | ||
placeholderData: [], | ||
} | ||
); | ||
|
||
const costSurfaceIds = allProjectCostSurfacesQuery.data?.map((cs) => cs.id); | ||
|
||
const handleSelectAll = useCallback( | ||
(evt: ChangeEvent<HTMLInputElement>) => { | ||
setSelectedCostSurfaceIds(evt.target.checked ? costSurfaceIds : []); | ||
}, | ||
[costSurfaceIds] | ||
); | ||
|
||
const handleSelectCostSurface = useCallback((evt: ChangeEvent<HTMLInputElement>) => { | ||
if (evt.target.checked) { | ||
setSelectedCostSurfaceIds((prevSelectedCostSurface) => [ | ||
...prevSelectedCostSurface, | ||
evt.target.value, | ||
]); | ||
} else { | ||
setSelectedCostSurfaceIds((prevSelectedCostSurface) => | ||
prevSelectedCostSurface.filter((costSurfaceId) => costSurfaceId !== evt.target.value) | ||
); | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
setSelectedCostSurfaceIds([]); | ||
}, [search]); | ||
|
||
const toggleSeeOnMap = useCallback( | ||
(costSurfaceId: CostSurface['id']) => { | ||
const newSelectedCostSurfaces = [...visibleCostSurfaces]; | ||
if (!newSelectedCostSurfaces.includes(costSurfaceId)) { | ||
newSelectedCostSurfaces.push(costSurfaceId); | ||
} else { | ||
const i = newSelectedCostSurfaces.indexOf(costSurfaceId); | ||
newSelectedCostSurfaces.splice(i, 1); | ||
} | ||
dispatch(setVisibleCostSurfaces(newSelectedCostSurfaces)); | ||
}, | ||
[dispatch, visibleCostSurfaces] | ||
); | ||
|
||
const handleSort = useCallback( | ||
(_sortType: (typeof filters)['sort']) => { | ||
const sort = filters.sort === _sortType ? `-${_sortType}` : _sortType; | ||
|
||
setFilters((prevFilters) => ({ | ||
...prevFilters, | ||
sort, | ||
})); | ||
}, | ||
[filters.sort] | ||
); | ||
|
||
const displayBulkActions = selectedCostSurfaceIds.length > 0; | ||
|
||
const data: DataItem[] = allProjectCostSurfacesQuery.data?.map((wdpa) => ({ | ||
...wdpa, | ||
name: wdpa.name, | ||
scenarios: wdpa.scenarioUsageCount, | ||
isVisibleOnMap: visibleCostSurfaces?.includes(wdpa.id), | ||
})); | ||
|
||
return ( | ||
<div className="space-y-6"> | ||
<InventoryTable | ||
loading={allProjectCostSurfacesQuery.isFetching} | ||
data={data} | ||
noDataMessage={noDataMessage} | ||
columns={COST_SURFACE_TABLE_COLUMNS} | ||
sorting={filters.sort} | ||
selectedIds={selectedCostSurfaceIds} | ||
onSortChange={handleSort} | ||
onSelectAll={handleSelectAll} | ||
onSelectRow={handleSelectCostSurface} | ||
onToggleSeeOnMap={toggleSeeOnMap} | ||
ActionsComponent={ActionsMenu} | ||
/> | ||
{displayBulkActions && ( | ||
<FeaturesBulkActionMenu selectedFeaturesIds={selectedCostSurfaceIds} /> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default InventoryPanelCostSurface; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import axios, { AxiosResponse, CreateAxiosDefaults, isAxiosError } from 'axios'; | ||
import Jsona from 'jsona'; | ||
import { signOut } from 'next-auth/react'; | ||
|
||
const dataFormatter = new Jsona(); | ||
|
||
const APIConfig: CreateAxiosDefaults<unknown> = { | ||
baseURL: `${process.env.NEXT_PUBLIC_API_URL}/api/v1`, | ||
headers: { 'Content-Type': 'application/json' }, | ||
} satisfies CreateAxiosDefaults; | ||
|
||
export const JSONAPI = axios.create({ | ||
...APIConfig, | ||
transformResponse: (data) => { | ||
try { | ||
const parsedData = JSON.parse(data); | ||
return { | ||
data: dataFormatter.deserialize(parsedData), | ||
meta: parsedData.meta, | ||
}; | ||
} catch (error: unknown) { | ||
if (isAxiosError(error)) { | ||
throw new Error(error.response.statusText); | ||
} | ||
throw error; | ||
} | ||
}, | ||
}); | ||
|
||
const onResponseSuccess = (response: AxiosResponse<unknown>) => response; | ||
|
||
const onResponseError = async (error) => { | ||
// Any status codes that falls outside the range of 2xx cause this function to trigger | ||
if (isAxiosError(error)) { | ||
if (error.response.status === 401) { | ||
await signOut(); | ||
} | ||
} | ||
// Do something with response error | ||
return Promise.reject(error as Error); | ||
}; | ||
|
||
JSONAPI.interceptors.response.use(onResponseSuccess, onResponseError); | ||
|
||
export const API = axios.create({ | ||
...APIConfig, | ||
}); | ||
|
||
API.interceptors.response.use(onResponseSuccess, onResponseError); | ||
|
||
const APIInstances = { | ||
JSONAPI, | ||
API, | ||
}; | ||
|
||
export default APIInstances; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface CostSurface { | ||
id: string; | ||
name: string; | ||
scenarioUsageCount: number; | ||
} |