-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement categories + sidebar (#21)
- Loading branch information
Showing
33 changed files
with
1,342 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { request } from './request'; | ||
import { buildEndpointUrl } from './utils'; | ||
import { getFormData } from '../utils'; | ||
|
||
export interface Category { | ||
__internal: string | false; | ||
name: string; | ||
savePath: string; | ||
hashList: string[]; | ||
} | ||
|
||
export type CategoryCollection = Record<string, Category>; | ||
export type CategoryOperationPayload = { | ||
assign: { category: string; list: string[] }; | ||
create: { category: string; savePath: string }; | ||
edit: { category: string; savePath: string }; | ||
delete: { categories: string[] }; | ||
}; | ||
export type CategoryOperation = keyof CategoryOperationPayload; | ||
export type CategoryOperationOptions = { | ||
[K in CategoryOperation]: [operation: K, options: CategoryOperationPayload[K]]; | ||
}[CategoryOperation]; | ||
|
||
export type CategoryOperationResponse = [CategoryOperation, boolean]; | ||
|
||
export const apiV2CreateCategory = ({ category, savePath }: { category: string; savePath: string }) => | ||
request(buildEndpointUrl(`/api/v2/torrents/createCategory`), { | ||
method: 'POST', | ||
body: getFormData({ | ||
category, | ||
savePath, | ||
}), | ||
}).then(() => ['create', true] as CategoryOperationResponse); | ||
|
||
export const apiV2EditCategory = ({ category, savePath }: { category: string; savePath: string }) => | ||
request(buildEndpointUrl(`/api/v2/torrents/editCategory`), { | ||
method: 'POST', | ||
body: getFormData({ | ||
category, | ||
savePath, | ||
}), | ||
}).then(() => ['edit', true] as CategoryOperationResponse); | ||
|
||
export const apiV2DeleteCategory = ({ categories }: { categories: string[] }) => | ||
request(buildEndpointUrl(`/api/v2/torrents/removeCategories`), { | ||
method: 'POST', | ||
body: getFormData({ | ||
categories: categories.join('\n'), | ||
}), | ||
}).then(() => ['delete', true] as CategoryOperationResponse); | ||
|
||
export const apiV2AssignToCategory = ({ category, list }: { category: string; list: string[] }) => | ||
request(buildEndpointUrl(`/api/v2/torrents/setCategory`), { | ||
method: 'POST', | ||
body: getFormData({ | ||
category, | ||
hashes: list.join('|'), | ||
}), | ||
}).then(() => ['assign', true] as CategoryOperationResponse); | ||
|
||
export const apiV2OperateCategory = (params: CategoryOperationOptions) => { | ||
switch (params[0]) { | ||
case 'assign': | ||
return apiV2AssignToCategory(params[1]); | ||
case 'create': | ||
return apiV2CreateCategory(params[1]); | ||
case 'edit': | ||
return apiV2EditCategory(params[1]); | ||
case 'delete': | ||
return apiV2DeleteCategory(params[1]); | ||
default: | ||
throw new Error('Invalid operation'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { useMutation } from 'react-query'; | ||
import { apiV2OperateCategory, CategoryOperationResponse } from '../../api'; | ||
import { LazyReason } from '../../types'; | ||
|
||
export const useCategoryOperationsMutation = ( | ||
{ onSuccess, onError } = {} as { | ||
onSuccess?: LazyReason<Promise<void> | void, CategoryOperationResponse>; | ||
onError?: LazyReason<Promise<void> | void>; | ||
} | ||
) => { | ||
const mutationObject = useMutation(apiV2OperateCategory, { | ||
retry: false, | ||
onSuccess, | ||
onError, | ||
}); | ||
|
||
return mutationObject; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './app'; | ||
export * from './category'; | ||
export * from './login'; | ||
export * from './torrents'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.