@@ -1,8 +1,10 @@
11import React, { useCallback, useEffect, useState } from 'react';
22import { FormProvider, useForm } from 'react-hook-form';
3+import { useMutation, useQueryClient } from 'react-query';
34import { Link } from 'react-router-dom';
45import api from '../api';
56import { PostOptions } from '../components/PostOptions';
7+import { Spinner } from '../components/Spinner';
68import { useApps } from '../state/apps';
79import { Declare, Listing, PostOption, PostOptionsForm } from '../types/sphinx';
810
@@ -11,7 +13,7 @@ function getAppKeys(apps: PostOption[]): string[] {
1113}
1214
1315export const Apps = () => {
14− const apps = useApps();
16+ const { apps, loading } = useApps();
1517 const form = useForm<PostOptionsForm>({
1618 defaultValues: {
1719 options: []
@@ -28,8 +30,9 @@ export const Apps = () => {
2830 }
2931 }, [apps, options, setValue]);
3032
31− const onSubmit = useCallback((values: PostOptionsForm) => {
32− api.poke<Declare[]>({
33+ const queryClient = useQueryClient();
34+ const { mutate } = useMutation(async (values: PostOptionsForm) => {
35+ return api.poke<Declare[]>({
3336 app: 'sphinx',
3437 mark: 'declarations',
3538 json: apps.filter(a => values.options.includes(a.key)).map(a => ({
@@ -37,9 +40,16 @@ export const Apps = () => {
3740 reach: 'friends'
3841 }))
3942 });
43+ }, {
44+ onSuccess: () => {
45+ queryClient.invalidateQueries('app-listings');
46+ }
47+ })
4048
41− reset();
42− }, [apps, reset]);
49+ const onSubmit = useCallback((values: PostOptionsForm) => {
50+ mutate(values)
51+ reset()
52+ }, [reset, mutate]);
4353
4454 return (
4555 <>
@@ -54,7 +64,16 @@ export const Apps = () => {
5464 </header>
5565 <FormProvider {...form}>
5666 <form onSubmit={handleSubmit(onSubmit)}>
57− <PostOptions options={apps} emptyMessage="All apps already indexed" />
67+ {apps.length === 0 && loading ? (
68+ <div className='flex items-center space-x-2 text-lavender'>
69+ <Spinner className='w-6 h-6' />
70+ <span>Checking apps...</span>
71+ </div>
72+ ) : apps.length === 0 && !loading ? (
73+ <h2 className='text-lavender'>All apps already indexed</h2>
74+ ) : (
75+ <PostOptions options={apps} />
76+ )}
5877 <div className='flex justify-between border-t border-zinc-300 py-3 mt-6'>
5978 <Link to="/search" className='flex items-center rounded-lg text-base font-semibold text-rosy bg-rosy/30 border-2 border-transparent hover:border-rosy leading-none py-2 px-3 transition-colors'>
6079 Back to Search