|
1 | 1 | import { useApiGet } from '@/hooks/utils/useApiGet';
|
2 | 2 |
|
3 |
| -// Hook to handle category task fetching logic |
| 3 | +interface GetBackupsProps { |
| 4 | + scheduleName?: string; |
| 5 | + onlyLast4Schedule: boolean; |
| 6 | + forced: boolean; |
| 7 | +} |
| 8 | + |
| 9 | +function jsonToQueryParams(json: any) { |
| 10 | + return Object.entries(json) |
| 11 | + .map(([key, value]) => { |
| 12 | + if (value === null || value === undefined) { |
| 13 | + return null; |
| 14 | + } |
| 15 | + // @ts-ignore |
| 16 | + return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`; |
| 17 | + }) |
| 18 | + .filter(Boolean) |
| 19 | + .join('&'); |
| 20 | +} |
| 21 | + |
4 | 22 | export const useBackups = () => {
|
5 |
| - const { data, getData, fetching, error } = useApiGet(); |
| 23 | + const { data, getData, fetching, error } = useApiGet(); |
6 | 24 |
|
7 |
| - const getBackups = async (onlyLast4Schedule:boolean=true, forced:boolean=false) => { |
8 |
| - try { |
9 |
| - // Execute the API call with the generic method |
10 |
| - await getData({ |
11 |
| - url: '/v1/backup/get', |
12 |
| - params: `only_last_for_schedule=${onlyLast4Schedule}&forced=${forced}`, |
13 |
| - }); |
| 25 | + const getBackups = async ({ |
| 26 | + scheduleName, |
| 27 | + onlyLast4Schedule, |
| 28 | + forced, |
| 29 | + }: GetBackupsProps): Promise<void> => { |
| 30 | + try { |
| 31 | + const params = { |
| 32 | + ...(scheduleName && { schedule_name: scheduleName }), |
| 33 | + only_last_for_schedule: onlyLast4Schedule, |
| 34 | + forced, |
| 35 | + }; |
14 | 36 |
|
15 |
| - // This code will be executed only in case of success |
16 |
| - // console.log('Request successful, execute final action...'); |
17 |
| - } catch (error) { |
18 |
| - // Error handling |
19 |
| - // console.error('Error during call:', error); |
20 |
| - } finally { |
21 |
| - // This code will always be executed |
22 |
| - // console.log('Final action after request') |
23 |
| - } |
24 |
| - }; |
| 37 | + await getData({ |
| 38 | + url: '/v1/backups', |
| 39 | + params: jsonToQueryParams(params), |
| 40 | + }); |
| 41 | + } catch (e) { |
| 42 | + console.error('Error:', e); |
| 43 | + } finally { |
| 44 | + // This code will always be executed |
| 45 | + // console.log('Final action after request') |
| 46 | + } |
| 47 | + }; |
25 | 48 |
|
26 |
| - // Return the function for the call and the necessary data |
27 |
| - return { getBackups, data, fetching, error }; |
| 49 | + return { |
| 50 | + getBackups, |
| 51 | + data, |
| 52 | + fetching, |
| 53 | + error, |
| 54 | + }; |
28 | 55 | };
|
0 commit comments