-
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
db2e9d5
commit 9bfd4a3
Showing
11 changed files
with
178 additions
and
26 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
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
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,22 @@ | ||
import { Router, Request, Response } from "express"; | ||
import { Field, FieldModel } from "../db/entities/field"; | ||
|
||
const router = Router(); | ||
|
||
router.get("/getFieldByFarmerId/:farmer_id", async(req: Request, res: Response) => { | ||
try{ | ||
const docs = await FieldModel.findOne({farmer_id: req.params.farmer_id}) | ||
.then(doc => { | ||
return doc; | ||
}) | ||
.catch(err => { | ||
return err; | ||
}) | ||
res.json(docs); | ||
}catch(e){ | ||
console.error(e); | ||
res.status(500).json(e); | ||
} | ||
}); | ||
|
||
export default router; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,109 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import { Field } from "../../types/field"; | ||
import { Farmer, getAllFarmers } from "../../services/farmers"; | ||
import { Column, Row, Select, SelectItem, Toggle } from "carbon-addons-iot-react"; | ||
import { FieldAPI } from "../../services/fields"; | ||
import { CropTemplateAPI } from "../../services/cropTemplate" | ||
import { OrganizeReputationActions } from '../Crops/helperFunctions'; | ||
|
||
const fieldAPI = new FieldAPI(); | ||
const cropTemplateAPI = new CropTemplateAPI(); | ||
const ReputationActions = (props: any) => { | ||
const [field, setField] = useState<Field>(); | ||
const [farmer, setFarmer] = useState<string>(); | ||
const [farmerList, setFarmerList] = useState<Farmer[]>([]); | ||
const [reputationActionMap, setReputationActionMap] = useState<any>({}); | ||
|
||
const handleClick = async(key: string) => { | ||
|
||
const response = await cropTemplateAPI.updateRepActions(field, props.selectedCrop, farmer!, key, true); | ||
if(response.status === 200){ | ||
let newReputationActionMap = [...reputationActionMap]; | ||
newReputationActionMap[0][key] = true; | ||
setReputationActionMap(newReputationActionMap); | ||
} | ||
} | ||
|
||
const getValueMap = (actionMap: any) => { | ||
// console.log('actionMap: ', actionMap); | ||
let actions = [] | ||
for(let key in actionMap[0]) { | ||
actions.push( | ||
<Row> | ||
<Toggle | ||
toggled={actionMap[0][key]} | ||
id={key} | ||
labelA="Incomplete" | ||
labelB="Complete" | ||
labelText={key} | ||
onClick={() => handleClick(key)} | ||
/> | ||
</Row> | ||
) | ||
} | ||
return(actions); | ||
} | ||
|
||
const getFarmerList = async() => { | ||
const farmers = await getAllFarmers(); | ||
setFarmerList(farmers); | ||
} | ||
|
||
const handleFarmerSelection = async(event:any) => { | ||
const fieldResponse = await fieldAPI.getFieldForFarmer(event.target.value); | ||
setFarmer(event.target.value); | ||
setField(fieldResponse); | ||
setReputationActionMap(OrganizeReputationActions(fieldResponse)); | ||
} | ||
|
||
useEffect(() => { | ||
if(!farmerList.length){ | ||
getFarmerList(); | ||
} | ||
|
||
},[]) | ||
|
||
return( | ||
<> | ||
<Row> | ||
<Column> | ||
<Select | ||
defaultValue="select-crop" | ||
helperText="" | ||
id="select-1" | ||
labelText="Farmers" | ||
size="md" | ||
onChange={handleFarmerSelection} | ||
> | ||
<SelectItem | ||
disabled | ||
hidden | ||
text="Select A Farmer" | ||
value="select-farmer" | ||
/> | ||
{ | ||
farmerList.map((item: Farmer) => { | ||
return( | ||
<SelectItem | ||
text={item.name} | ||
value={item._id} | ||
/> | ||
) | ||
}) | ||
} | ||
</Select> | ||
</Column> | ||
</Row> | ||
{(Object.keys(reputationActionMap).length > 0) && getValueMap(reputationActionMap).map((item) => { | ||
return( | ||
<> | ||
{item} | ||
</> | ||
) | ||
}) | ||
} | ||
</> | ||
) | ||
} | ||
|
||
export default ReputationActions; |
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,10 @@ | ||
import axios from 'axios'; | ||
import { Field } from '../types/field'; | ||
export class FieldAPI{ | ||
APIBase = "/api/fields/"; | ||
|
||
async getFieldForFarmer(farmer_id: any): Promise<Field> { | ||
const data = await axios.get<Field>(this.APIBase + "getFieldByFarmerId/" + farmer_id); | ||
return data.data; | ||
} | ||
} |