Skip to content

Commit

Permalink
feat: [NR-NMP-149] Added regions and subregions (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
raarielgrace authored and lunamoonmoon committed Feb 4, 2025
1 parent dcd47dc commit 63e480d
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 64 deletions.
3 changes: 2 additions & 1 deletion backend/apps/animals/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib import admin
from .models import Animals
from .models import *

admin.site.register(Animals)
admin.site.register(AnimalSubtype)
18 changes: 17 additions & 1 deletion backend/apps/animals/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,20 @@ class Animals(models.Model):

class Meta:
managed = False
db_table = 'animals'
db_table = 'animals'

class AnimalSubtype(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=100)
liquidpergalperanimalperday = models.FloatField()
solidpergalperanimalperday = models.FloatField()
solidperpoundperanimalperday = models.FloatField()
solidliquidseparationpercentage = models.IntegerField()
washwater = models.FloatField()
milkproduction = models.FloatField()
animalid = models.IntegerField()
sortorder = models.IntegerField()

class Meta:
managed = False
db_table = 'animal_subtype'
7 changes: 6 additions & 1 deletion backend/apps/animals/serializers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from rest_framework import serializers
from .models import Animals
from .models import *

class AnimalsSerializer(serializers.ModelSerializer):
class Meta:
model = Animals
fields = '__all__'

class AnimalSubtypeSerializer(serializers.ModelSerializer):
class Meta:
model = AnimalSubtype
fields = '__all__'
28 changes: 0 additions & 28 deletions backend/apps/api/urls.py

This file was deleted.

32 changes: 0 additions & 32 deletions backend/apps/api/views.py

This file was deleted.

1 change: 1 addition & 0 deletions backend/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
# Apps for this project
'apps.crops',
'apps.animals',
'apps.shared',
]

MIDDLEWARE = [
Expand Down
37 changes: 36 additions & 1 deletion frontend/src/views/FarmInformation/FarmInformation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ export default function FarmInformation() {
const apiCache = useContext(APICacheContext);

const [rawAnimalNames, setRawAnimalNames] = useState<string[]>([]);
const [regionOptions, setRegionOptions] = useState<{ value: number; label: string }[]>([]);
const [subregionOptions, setSubregionOptions] = useState<{ value: number; label: string }[]>([]);

// Initialize non-bool values to prevent errors on first render
const [formData, setFormData] = useState<{ [name: string]: any }>({
Year: '',
FarmName: '',
FarmRegion: 0,
FarmSubRegion: null,
});

// Flagging for potential issues if the state.nmpFile object can change
Expand All @@ -53,11 +56,11 @@ export default function FarmInformation() {
Year: parsedData.farmDetails.Year || '',
FarmName: parsedData.farmDetails.FarmName || '',
FarmRegion: parsedData.farmDetails.FarmRegion || 0,
FarmSubRegion: parsedData.farmDetails.FarmSubRegion || null,
HasAnimals: parsedData.farmDetails.HasAnimals || false,
HasDairyCows: parsedData.farmDetails.HasDairyCows || false,
HasBeefCows: parsedData.farmDetails.HasBeefCows || false,
HasPoultry: parsedData.farmDetails.HasPoultry || false,
Animals: [],
HasVegetables: parsedData.farmDetails.HasVegetables || false,
HasBerries: parsedData.farmDetails.HasBerries || false,
Crops: parsedData.farmDetails.HasHorticulturalCrops.toString() || 'false',
Expand All @@ -78,9 +81,31 @@ export default function FarmInformation() {
setRawAnimalNames(animalArray);
}
});
apiCache.callEndpoint('api/regions/').then((response) => {
const { data } = response;
const regions: { value: number; label: string }[] = (
data as { id: number; name: string }[]
).map((row) => ({ value: row.id, label: row.name }));
setRegionOptions(regions);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
const region = formData.FarmRegion;
if (region === 0) {
setSubregionOptions([]);
return;
}
apiCache.callEndpoint(`api/subregions/${region}/`).then((response) => {
const { data } = response;
const subregions: { value: number; label: string }[] = (
data as { id: number; name: string }[]
).map((row) => ({ value: row.id, label: row.name }));
setSubregionOptions(subregions);
});
}, [formData.FarmRegion, apiCache]);

const handleChange = useCallback(
(e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>) => {
const { name, value, type, checked } = e.target as HTMLInputElement;
Expand Down Expand Up @@ -215,6 +240,16 @@ export default function FarmInformation() {
flex="0.35"
/>
</RegionContainer>
<RegionContainer>
<Dropdown
label="Subregion"
name="FarmSubRegion"
value={formData.FarmSubRegion}
options={subregionOptions}
onChange={handleChange}
flex="0.35"
/>
</RegionContainer>
<YesNoRadioButtons
name="Crops"
text="I have crops"
Expand Down

0 comments on commit 63e480d

Please sign in to comment.