Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/webapp/src/containers/saga.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ import {
onLoadingWatercourseFail,
onLoadingWatercourseStart,
} from './watercourseSlice';
import { api, FarmLibraryTags, FarmTags } from '../store/api/apiSlice';
import { api, invalidateTags } from '../store/api/apiSlice';
import { FARM_LIBRARY_TAGS, FARM_TAGS, FarmLibraryTags, FarmTags } from '../store/api/apiTags';
import {
getSoilSampleLocationsSuccess,
onLoadingSoilSampleLocationFail,
Expand Down Expand Up @@ -628,7 +629,11 @@ export function* clearOldFarmStateSaga() {
yield put(resetTasks());
yield put(resetDateRange());

yield put(api.util.invalidateTags([...FarmTags, ...FarmLibraryTags]));
// if tags are declared as objects
yield put(invalidateTags([...Object.values(FARM_TAGS), ...Object.values(FARM_LIBRARY_TAGS)]));

// if tags are declared as array of strings
yield put(invalidateTags([...FarmTags, ...FarmLibraryTags]));

// Reset finance loading state
yield put(setIsFetchingData(true));
Expand Down
61 changes: 12 additions & 49 deletions packages/webapp/src/store/api/apiSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,57 +63,20 @@ import type {
IrrigationPrescriptionDetails,
} from './types';

/**
* These tags have endpoints that do not return farm specific data, and are not created by a farm
*
* LiteFarm provides these data as defaults
*/
export const LibraryTags = [
'AnimalSexes',
'AnimalIdentifierTypes',
'AnimalIdentifierColors',
'AnimalMovementPurposes',
'AnimalOrigins',
'AnimalUses',
'AnimalRemovalReasons',
'DefaultAnimalBreeds',
'SoilAmendmentMethods',
'SoilAmendmentPurposes',
'SoilAmendmentFertiliserTypes',
];
import { addDaysToDate } from '../../util/date';
import { API_TAGS, ApiTag, FARM_TAGS } from './apiTags';

/**
* These tags contain endpoints that return farm specific data
* Invalidates one or more RTK Query cache tags.
*
* These data should not persist when switching farms,
* or should be stored in a separate farm store.
*/
export const FarmTags = [
'Animals',
'AnimalBatches',
'CustomAnimalBreeds',
'CustomAnimalTypes',
'FarmAddon',
'IrrigationPrescriptions',
'IrrigationPrescriptionDetails',
'SoilAmendmentProduct',
'Sensors',
'SensorReadings',
'Weather',
];

/**
* These tags contain endpoints that could either return farm specific data
* or farm neutral defaults data.
* This helper provides a type-safe wrapper around `api.util.invalidateTags`
* ensuring only valid `ApiTag` values (defined in `API_TAGS`) can be used.
*
* For data safety these data should also not persist when switching farms,
* or should be stored in a separate farm store.
* @param {ApiTag[]} tags - An array of tag names to invalidate.
* @returns - The invalidateTags action,
* which can be dispatched directly or used inside a saga (e.g., `yield put(...)`).
*/
export const FarmLibraryTags = [
// 'count' param returns farm specific data
'DefaultAnimalTypes',
];
import { addDaysToDate } from '../../util/date';
export const invalidateTags = (tags: ApiTag[]) => api.util.invalidateTags(tags);

export const api = createApi({
baseQuery: fetchBaseQuery({
Expand All @@ -129,17 +92,17 @@ export const api = createApi({
},
responseHandler: 'content-type',
}),
tagTypes: [...LibraryTags, ...FarmTags, ...FarmLibraryTags],
tagTypes: API_TAGS,
endpoints: (build) => ({
// redux-toolkit.js.org/rtk-query/usage-with-typescript#typing-query-and-mutation-endpoints
// <ResultType, QueryArg>
getAnimals: build.query<Animal[], void>({
query: () => `${animalsUrl}`,
providesTags: ['Animals'],
providesTags: [FARM_TAGS.ANIMALS], // if tags are declared as objects
}),
getAnimalBatches: build.query<AnimalBatch[], void>({
query: () => `${animalBatchesUrl}`,
providesTags: ['AnimalBatches'],
providesTags: ['AnimalBatches'], // if tags are declared as array of strings
}),
getDefaultAnimalTypes: build.query<DefaultAnimalType[], string | void>({
query: (param = '') => `${defaultAnimalTypesUrl}${param}`,
Expand Down
120 changes: 120 additions & 0 deletions packages/webapp/src/store/api/apiTags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright 2025 LiteFarm.org
* This file is part of LiteFarm.
*
* LiteFarm is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LiteFarm is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details, see <https://www.gnu.org/licenses/>.
*/

// Central registry for all API tags in the application

/**
* These tags have endpoints that do not return farm specific data, and are not created by a farm
*
* LiteFarm provides these data as defaults
*/
export const LIBRARY_TAGS = {
ANIMAL_SEXES: 'AnimalSexes',
ANIMAL_IDENTIFIER_TYPES: 'AnimalIdentifierTypes',
ANIMAL_IDENTIFIER_COLORS: 'AnimalIdentifierColors',
ANIMAL_MOVEMENT_PURPOSES: 'AnimalMovementPurposes',
ANIMAL_ORIGINS: 'AnimalOrigins',
ANUMAL_USES: 'AnimalUses',
ANIMAL_REMOVAL_REASONS: 'AnimalRemovalReasons',
DEFAULT_ANIMAL_BREEDS: 'DefaultAnimalBreeds',
SOIL_AMENDMENT_METHODS: 'SoilAmendmentMethods',
SOIL_AMENDMENT_PURPOSES: 'SoilAmendmentPurposes',
SOIL_AMENDMENT_FERTILIZER_TYPES: 'SoilAmendmentFertiliserTypes',
} as const;

/**
* These tags contain endpoints that return farm specific data
*
* These data should not persist when switching farms,
* or should be stored in a separate farm store.
*/
export const FARM_TAGS = {
ANIMALS: 'Animals',
ANIMAL_BATCHES: 'AnimalBatches',
CUSTOM_ANIMAL_BATCHES: 'CustomAnimalBreeds',
CUSTOM_ANIMAL_TYPES: 'CustomAnimalTypes',
FARM_ADDON: 'FarmAddon',
IRRIGATION_PRESCRIPTION: 'IrrigationPrescriptions',
IRRIGATION_PRESCRIPTION_DETAILS: 'IrrigationPrescriptionDetails',
SOIL_AMENDMENT_PRODUCT: 'SoilAmendmentProduct',
SENSORS: 'Sensors',
SENSOR_READING: 'SensorReadings',
WEATHER: 'Weather',
} as const;

/**
* These tags contain endpoints that could either return farm specific data
* or farm neutral defaults data.
*
* For data safety these data should also not persist when switching farms,
* or should be stored in a separate farm store.
*/
export const FARM_LIBRARY_TAGS = {
// 'count' param returns farm specific data
DEFAULT_ANIMAL_TYPES: 'DefaultAnimalTypes',
} as const;

/**
* If the tags are array of strings
*/
export const LibraryTags = [
'AnimalSexes',
'AnimalIdentifierTypes',
'AnimalIdentifierColors',
'AnimalMovementPurposes',
'AnimalOrigins',
'AnimalUses',
'AnimalRemovalReasons',
'DefaultAnimalBreeds',
'SoilAmendmentMethods',
'SoilAmendmentPurposes',
'SoilAmendmentFertiliserTypes',
] as const;

export const FarmTags = [
'Animals',
'AnimalBatches',
'CustomAnimalBreeds',
'CustomAnimalTypes',
'FarmAddon',
'IrrigationPrescriptions',
'IrrigationPrescriptionDetails',
'SoilAmendmentProduct',
'Sensors',
'SensorReadings',
'Weather',
] as const;

export const FarmLibraryTags = [
// 'count' param returns farm specific data
'DefaultAnimalTypes',
] as const;

// helper to preserve literal tuple
function tuple<T extends readonly string[]>(...args: T) {
return args;
}

// un-comment if declaring tags as objects
const TAG_GROUPS = { LIBRARY_TAGS, FARM_TAGS, FARM_LIBRARY_TAGS } as const;

//un-comment if declaring tags as array of strings
//const TAG_GROUPS = { LibraryTags, FarmTags, FarmLibraryTags } as const;

export const API_TAGS = tuple(
...Object.values(TAG_GROUPS).flatMap((group) => Object.values(group)),
);

export type ApiTag = (typeof API_TAGS)[number];