Skip to content

Commit 9d71277

Browse files
authored
Merge pull request #4092 from LiteFarmOrg/LF-5214-rtk-query-api-layer-and-i18n
LF-5214: RTK Query API layer
2 parents cc5a189 + fbee85c commit 9d71277

File tree

6 files changed

+156
-1
lines changed

6 files changed

+156
-1
lines changed

packages/webapp/src/apiConfig.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ export const marketDirectoryPartnersUrl = `${URI}/market_directory_partners`;
9999
export const supportTicketUrl = `${URI}/support_ticket`;
100100
export const offlineEventLogUrl = `${URI}/offline_event_log`;
101101
export const tapeSurveyUrl = `${URI}/tape_survey`;
102+
export const farmNoteUrl = `${URI}/farm_note`;
103+
export const farmNotesReadUrl = `${URI}/farm_notes_read`;
102104

103105
export const url = URI;
104106

@@ -163,5 +165,7 @@ export default {
163165
weatherUrl,
164166
marketDirectoryInfoUrl,
165167
marketDirectoryPartnersUrl,
168+
farmNoteUrl,
169+
farmNotesReadUrl,
166170
url,
167171
};

packages/webapp/src/store/api/apiSlice.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ import { API_TAGS, ApiTag } from './apiTags';
7878
*/
7979
export const invalidateTags = (tags: ApiTag[]) => api.util.invalidateTags(tags);
8080

81-
const NON_JSON_ENDPOINT_KEYS = new Set(['addSupportTicket']);
81+
const NON_JSON_ENDPOINT_KEYS = new Set(['addSupportTicket', 'addFarmNote', 'editFarmNote']);
8282

8383
export const api = createApi({
8484
baseQuery: fetchBaseQuery({

packages/webapp/src/store/api/apiTags.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ export const FarmTags = [
5555
'Weather',
5656
'MarketDirectoryInfo',
5757
'TapeSurvey',
58+
'FarmNote',
59+
'FarmNotesRead',
5860
] as const;
5961

6062
/**
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2026 LiteFarm.org
3+
* This file is part of LiteFarm.
4+
*
5+
* LiteFarm is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* LiteFarm is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details, see <https://www.gnu.org/licenses/>.
14+
*/
15+
16+
import { api } from './apiSlice';
17+
import { farmNoteUrl } from '../../apiConfig';
18+
import { FarmNote } from './types';
19+
20+
type FarmNoteData = {
21+
note: string;
22+
is_private: boolean;
23+
};
24+
25+
interface AddFarmNoteReqBody {
26+
file?: File;
27+
data: FarmNoteData;
28+
}
29+
30+
interface EditFarmNoteReqBody {
31+
id: string;
32+
file?: File;
33+
data: FarmNoteData & {
34+
image_url?: null;
35+
};
36+
}
37+
38+
export const farmNoteApi = api.injectEndpoints({
39+
endpoints: (build) => ({
40+
getFarmNotes: build.query<FarmNote[], void>({
41+
query: () => ({
42+
url: farmNoteUrl,
43+
method: 'GET',
44+
}),
45+
providesTags: ['FarmNote'],
46+
}),
47+
addFarmNote: build.mutation<FarmNote, AddFarmNoteReqBody>({
48+
query: ({ file, data }) => {
49+
// Mirrors supportTicketApi.ts
50+
// Must similarly add to non-JSON endpoint keys in apiSlice.ts
51+
const formData = new FormData();
52+
if (file) {
53+
formData.append('_file_', file);
54+
}
55+
formData.append('data', JSON.stringify(data));
56+
return {
57+
url: farmNoteUrl,
58+
method: 'POST',
59+
body: formData,
60+
};
61+
},
62+
invalidatesTags: ['FarmNote'],
63+
}),
64+
editFarmNote: build.mutation<FarmNote, EditFarmNoteReqBody>({
65+
query: ({ id, file, data }) => {
66+
const formData = new FormData();
67+
if (file) {
68+
formData.append('_file_', file);
69+
}
70+
formData.append('data', JSON.stringify(data));
71+
72+
return {
73+
url: `${farmNoteUrl}/${id}`,
74+
method: 'PATCH',
75+
body: formData,
76+
};
77+
},
78+
invalidatesTags: ['FarmNote'],
79+
}),
80+
deleteFarmNote: build.mutation<void, string>({
81+
query: (id) => ({
82+
url: `${farmNoteUrl}/${id}`,
83+
method: 'DELETE',
84+
}),
85+
invalidatesTags: ['FarmNote'],
86+
}),
87+
}),
88+
});
89+
90+
export const {
91+
useGetFarmNotesQuery,
92+
useAddFarmNoteMutation,
93+
useEditFarmNoteMutation,
94+
useDeleteFarmNoteMutation,
95+
} = farmNoteApi;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2026 LiteFarm.org
3+
* This file is part of LiteFarm.
4+
*
5+
* LiteFarm is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* LiteFarm is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details, see <https://www.gnu.org/licenses/>.
14+
*/
15+
16+
import { api } from './apiSlice';
17+
import { farmNotesReadUrl } from '../../apiConfig';
18+
import { FarmNotesRead } from './types';
19+
20+
export const farmNotesReadApi = api.injectEndpoints({
21+
endpoints: (build) => ({
22+
getFarmNotesRead: build.query<FarmNotesRead, void>({
23+
query: () => ({
24+
url: farmNotesReadUrl,
25+
method: 'GET',
26+
}),
27+
providesTags: ['FarmNotesRead'],
28+
}),
29+
markFarmNotesRead: build.mutation<void, void>({
30+
query: () => ({
31+
url: farmNotesReadUrl,
32+
method: 'PATCH',
33+
}),
34+
invalidatesTags: ['FarmNotesRead'],
35+
}),
36+
}),
37+
});
38+
39+
export const { useGetFarmNotesReadQuery, useMarkFarmNotesReadMutation } = farmNotesReadApi;

packages/webapp/src/store/api/types/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,18 @@ export interface SupportTicketData {
413413
attachments: {};
414414
};
415415
}
416+
417+
export interface FarmNote {
418+
id: string;
419+
farm_id: string;
420+
user_id: string;
421+
note: string;
422+
is_private: boolean;
423+
image_url?: string | null;
424+
updated_at: string;
425+
to_sync?: boolean; // client-only flag for offline-queued notes
426+
}
427+
428+
export interface FarmNotesRead {
429+
last_read_at: string | null;
430+
}

0 commit comments

Comments
 (0)