Skip to content

Commit

Permalink
Merge branch 'Greenstand:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
samwel141 authored Apr 23, 2024
2 parents b343267 + f3a22b0 commit 2517d4e
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 2 deletions.
35 changes: 35 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
# [1.135.0](https://github.com/Greenstand/treetracker-admin-client/compare/v1.134.0...v1.135.0) (2024-01-12)


### Features

* **Grower:** showing sub organisations below organization of grower ([a727067](https://github.com/Greenstand/treetracker-admin-client/commit/a727067e72b157c748cc87555195922213a1f0c7))

# [1.134.0](https://github.com/Greenstand/treetracker-admin-client/compare/v1.133.0...v1.134.0) (2024-01-12)


### Bug Fixes

* fixed undefined intial filter ([758cec0](https://github.com/Greenstand/treetracker-admin-client/commit/758cec031b649e09d2f0b3196a32ece691852eb4))
* selection and query for session dropdown ([6250930](https://github.com/Greenstand/treetracker-admin-client/commit/6250930dbe61d15528feeb27971e8ba10b679355))


### Features

* add session filter to captures page' ([312f3da](https://github.com/Greenstand/treetracker-admin-client/commit/312f3da01839a52c59ee62094879af4772200357))
* added session filter that gets a list of sessions from backend api ([2d09fb1](https://github.com/Greenstand/treetracker-admin-client/commit/2d09fb1451f186420889c023811e91d46c0a1e80))
* fetch sesions and format for the dropdown ([6cd4e3c](https://github.com/Greenstand/treetracker-admin-client/commit/6cd4e3c0ac023a6b09a0d954c86f19f05a6ae2ff))

# [1.133.0](https://github.com/Greenstand/treetracker-admin-client/compare/v1.132.4...v1.133.0) (2024-01-10)


### Bug Fixes

* fixed undefined intial filter ([e439d15](https://github.com/Greenstand/treetracker-admin-client/commit/e439d15ccd68f4c2437ec1390844ff886ee40713))
* removed legacy fields in FilterTop ([3679108](https://github.com/Greenstand/treetracker-admin-client/commit/367910831fb0330202a9b547014f3d9b259a4f56))


### Features

* added session filter that gets a list of sessions from backend api ([1a518e1](https://github.com/Greenstand/treetracker-admin-client/commit/1a518e1ac307995a974e9d53b145746a9dde512c))

## [1.132.4](https://github.com/Greenstand/treetracker-admin-client/compare/v1.132.3...v1.132.4) (2023-10-07)


Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "treetracker-admin-client",
"version": "1.132.4",
"version": "1.135.0",
"private": true,
"dependencies": {
"@date-io/date-fns": "^1.3.13",
Expand Down
15 changes: 15 additions & 0 deletions src/api/treeTrackerApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,21 @@ export default {
handleError(error);
}
},
getSessions() {
try {
const query = `${FIELD_DATA_API}/session`;

return fetch(query, {
method: 'GET',
headers: {
'content-type': 'application/json',
Authorization: session.token,
},
}).then(handleResponse);
} catch (error) {
handleError(error);
}
},
getAdminUserById(id) {
try {
const query = `${API_ROOT}/auth/admin_users/${id}`;
Expand Down
13 changes: 13 additions & 0 deletions src/components/CaptureFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import TextField from '@material-ui/core/TextField';
import MenuItem from '@material-ui/core/MenuItem';
import Autocomplete from '@material-ui/lab/Autocomplete';
import SelectOrg from './common/SelectOrg';
import SelectSession from './common/SelectSession';
import FilterModel, {
ALL_SPECIES,
ALL_SESSIONS,
SPECIES_ANY_SET,
SPECIES_NOT_SET,
ALL_ORGANIZATIONS,
Expand Down Expand Up @@ -94,6 +96,9 @@ function Filter(props) {
const [tag, setTag] = useState(null);
const [tagSearchString, setTagSearchString] = useState('');
const [organizationId, setOrganizationId] = useState(ALL_ORGANIZATIONS);
const [sessionId, setSessionId] = useState(
filter?.session_id || ALL_SESSIONS
);
const [tokenId, setTokenId] = useState(filter?.tokenId || filterOptionAll);

const handleStartDateChange = (date) => {
Expand Down Expand Up @@ -123,6 +128,7 @@ function Filter(props) {
species_id: speciesId,
tag_id: tag ? tag.id : undefined,
organization_id: organizationId,
session_id: sessionId,
tokenId: tokenId.trim(),
};
const filter = new FilterModel(test);
Expand All @@ -144,6 +150,7 @@ function Filter(props) {
setTag(null);
setTagSearchString('');
setOrganizationId(ALL_ORGANIZATIONS);
setSessionId(ALL_SESSIONS);
setTokenId(filterOptionAll);
const filter = new FilterModel();
props.onSubmit && props.onSubmit(filter);
Expand Down Expand Up @@ -338,6 +345,12 @@ function Filter(props) {
setOrganizationId(org.stakeholder_uuid);
}}
/>
<SelectSession
sessionId={sessionId}
handleSelection={(session) => {
setSessionId(session.id);
}}
/>
</Grid>
<Grid className={classes.inputContainer}>
<Button
Expand Down
13 changes: 13 additions & 0 deletions src/components/FilterTop.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import FilterModel, {
ALL_TAGS,
TAG_NOT_SET,
ANY_TAG_SET,
ALL_SESSIONS,
} from '../models/Filter';
import DateFnsUtils from '@date-io/date-fns';
import {
Expand All @@ -31,6 +32,7 @@ import {

// import { SpeciesContext } from '../context/SpeciesContext';
import { TagsContext } from '../context/TagsContext';
import SelectSession from './common/SelectSession';
// import { CircularProgress } from '@material-ui/core';

export const FILTER_WIDTH = 330;
Expand Down Expand Up @@ -95,6 +97,9 @@ function Filter(props) {
const [organizationId, setOrganizationId] = useState(
filter?.organizationId || ALL_ORGANIZATIONS
);
const [sessionId, setSessionId] = useState(
filter?.session_id || ALL_SESSIONS
);
// const [tokenId, setTokenId] = useState(filter?.tokenId || filterOptionAll);

const handleStartDateChange = (date) => {
Expand Down Expand Up @@ -124,6 +129,7 @@ function Filter(props) {
filter.species_id = speciesId;
filter.tag_id = tag ? tag.id : undefined;
filter.organization_id = organizationId;
filter.session_id = sessionId;
// filter.tokenId = tokenId;
props.onSubmit && props.onSubmit(filter);
}
Expand All @@ -141,6 +147,7 @@ function Filter(props) {
setTag(null);
setTagSearchString('');
setOrganizationId(ALL_ORGANIZATIONS);
setSessionId(ALL_SESSIONS);
// setTokenId(filterOptionAll);

const filter = new FilterModel();
Expand Down Expand Up @@ -376,6 +383,12 @@ function Filter(props) {
setOrganizationId(org.stakeholder_uuid);
}}
/>
<SelectSession
sessionId={sessionId}
handleSelection={(session) => {
setSessionId(session.id);
}}
/>
</Grid>
<Grid className={classes.inputContainer}>
<Button
Expand Down
63 changes: 63 additions & 0 deletions src/components/common/SelectSession.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React, { useContext, useEffect, useState } from 'react';
import { TextField, MenuItem } from '@material-ui/core';
import { AppContext } from 'context/AppContext';
import { ALL_SESSIONS /* SESSION_NOT_SET */ } from 'models/Filter';
import { getDateTimeStringLocale } from 'common/locale';

function SelectSession({ sessionId, defaultSessions, handleSelection }) {
const { sessionList } = useContext(AppContext);
const defaultList = defaultSessions?.length
? defaultSessions
: [
{
id: ALL_SESSIONS,
name: 'All',
value: 'All',
},
];
const [sessions, setSessions] = useState(defaultList);

useEffect(() => {
// format the session data for the dropdown
const sesh = sessionList.map((s) => ({
id: s.id,
org: s.organization,
name: `${s.wallet} ${s.organization ? ` / ${s.organization} ` : ''}`,
date: getDateTimeStringLocale(s.created_at),
value: s.id,
}));

setSessions(() => [...defaultList, ...sesh]);
}, [sessionList]);

const handleChange = (e) => {
const session = [...sessions].find((o) => o.id === e.target.value);
handleSelection(session);
};

return (
<TextField
select
data-testid="session-dropdown"
htmlFor="session"
id="session"
label="Session"
name="Session"
value={sessionId}
onChange={handleChange}
>
{[...sessions].map((session) => (
<MenuItem
data-testid="session-item"
key={session.id}
value={session.id}
>
{session.date} -- {session.name}{' '}
{session.organization ? ` / ${session.organization}` : ''}
</MenuItem>
))}
</TextField>
);
}

export default SelectSession;
10 changes: 9 additions & 1 deletion src/context/AppContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ export const AppProvider = (props) => {
const [routes, setRoutes] = useState(getRoutes(localUser));
const [userHasOrg, setUserHasOrg] = useState(false);
const [orgList, setOrgList] = useState([]);
const [sessionList, setSessionList] = useState([]);
const [orgId, setOrgId] = useState(undefined);

// TODO: The below `selectedFilters` state would be better placed under a
Expand All @@ -227,10 +228,11 @@ export const AppProvider = (props) => {
// CustomTableFilter under components/common.
const [selectedFilters, setSelectedFilters] = useState('');

// check if the user has an org load organizations when the user changes
// check if the user has an org, load organizations when the user changes
useEffect(() => {
if (user && token) {
loadOrganizations();
loadSessions();
}
setUserHasOrg(!!user?.policy?.organization?.id);
}, [user, token]);
Expand Down Expand Up @@ -314,6 +316,11 @@ export const AppProvider = (props) => {
setOrgList(orgs);
}

async function loadSessions() {
const { sessions } = await api.getSessions();
setSessionList(sessions);
}

function getOrganizationUUID() {
const orgId = session.user?.policy?.organization?.id || null;
const foundOrg = orgList.find((org) => org.id === orgId);
Expand All @@ -339,6 +346,7 @@ export const AppProvider = (props) => {
routes,
orgId,
orgList,
sessionList,
userHasOrg,
selectedFilters,
updateSelectedFilter,
Expand Down
16 changes: 16 additions & 0 deletions src/models/Filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export const SPECIES_ANY_SET = 'SPECIES_ANY_SET';
export const SPECIES_NOT_SET = 'SPECIES_NOT_SET';
export const ALL_ORGANIZATIONS = 'ALL_ORGANIZATIONS';
export const ORGANIZATION_NOT_SET = 'ORGANIZATION_NOT_SET';
export const ALL_SESSIONS = 'ALL_SESSIONS';
export const SESSION_NOT_SET = 'SESSION_NOT_SET';
export const ANY_SESSION_SET = 'ANY_SESSION_SET';
export const ALL_TAGS = 'ALL_TAGS';
export const TAG_NOT_SET = 'TAG_NOT_SET';
export const ANY_TAG_SET = 'ANY_TAG_SET';
Expand All @@ -26,6 +29,7 @@ export default class Filter {
species_id;
tag_id;
organization_id;
session_id;
tokenId;
status;

Expand Down Expand Up @@ -93,6 +97,14 @@ export default class Filter {
where.organization_id = this.organization_id;
}

if (this.session_id === SESSION_NOT_SET) {
where.session_id = null;
} else if (this.session_id === ANY_SESSION_SET) {
where.session_id = 'not null';
} else if (this.session_id !== ALL_SESSIONS) {
where.session_id = this.session_id;
}

if (this.status) {
where.status = this.status;
}
Expand Down Expand Up @@ -180,6 +192,10 @@ export default class Filter {
numFilters += 1;
}

if (this.session_id && this.session_id !== SESSION_NOT_SET) {
numFilters += 1;
}

if (this.tokenId && this.tokenId !== 'All') {
numFilters += 1;
}
Expand Down

0 comments on commit 2517d4e

Please sign in to comment.