1
0
mirror of https://github.com/tooot-app/app synced 2025-06-05 22:19:13 +02:00

Partial fix #495

Add list
Edit list info
This commit is contained in:
xmflsct
2022-11-30 22:42:42 +01:00
parent de7498b218
commit bb3ddd2779
29 changed files with 506 additions and 224 deletions

View File

@ -1,8 +1,8 @@
import apiInstance from '@api/instance'
import { AxiosError } from 'axios'
import { useQuery, UseQueryOptions } from 'react-query'
import { useMutation, UseMutationOptions, useQuery, UseQueryOptions } from 'react-query'
export type QueryKey = ['Lists']
export type QueryKeyLists = ['Lists']
const queryFunction = async () => {
const res = await apiInstance<Mastodon.List[]>({
@ -12,13 +12,49 @@ const queryFunction = async () => {
return res.body
}
const useListsQuery = ({
options
}: {
options?: UseQueryOptions<Mastodon.List[], AxiosError>
}) => {
const queryKey: QueryKey = ['Lists']
const useListsQuery = ({ options }: { options?: UseQueryOptions<Mastodon.List[], AxiosError> }) => {
const queryKey: QueryKeyLists = ['Lists']
return useQuery(queryKey, queryFunction, options)
}
export { useListsQuery }
type MutationVarsLists =
| {
type: 'add'
payload: Omit<Mastodon.List, 'id'>
}
| {
type: 'edit'
payload: Mastodon.List
}
const mutationFunction = async (params: MutationVarsLists) => {
const body = new FormData()
switch (params.type) {
case 'add':
body.append('title', params.payload.title)
body.append('replies_policy', params.payload.replies_policy)
return apiInstance<Mastodon.List>({
method: 'post',
url: 'lists',
body
}).then(res => res.body)
case 'edit':
body.append('title', params.payload.title)
body.append('replies_policy', params.payload.replies_policy)
return apiInstance<Mastodon.List>({
method: 'put',
url: `lists/${params.payload.id}`,
body
}).then(res => res.body)
}
}
const useListsMutation = (
options: UseMutationOptions<Mastodon.List, AxiosError, MutationVarsLists>
) => {
return useMutation(mutationFunction, options)
}
export { useListsQuery, useListsMutation }