|
| 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; |
0 commit comments