add save changes to app general tab

This commit is contained in:
Davor 2022-08-02 23:43:18 +02:00
parent 08094e233c
commit 97aa678830
4 changed files with 61 additions and 43 deletions

View file

@ -58,7 +58,7 @@ export const AppSingle: React.FC = () => {
const tabs = [ const tabs = [
{ {
name: 'General', name: 'General',
component: <GeneralTab automaticUpdates={app.automaticUpdates} onChange={_.noop} />, component: <GeneralTab />,
}, },
{ name: 'Advanced Configuration', component: <AdvancedTab /> }, { name: 'Advanced Configuration', component: <AdvancedTab /> },
]; ];

View file

@ -1,17 +1,36 @@
import React from 'react'; import _ from 'lodash';
import { Switch } from '@headlessui/react'; import React, { useEffect, useRef } from 'react';
import { SubmitHandler, useForm } from 'react-hook-form';
import { Switch } from 'src/components/Form';
import { App, useApps } from 'src/services/apps';
function classNames(...classes: any) { export const GeneralTab = () => {
return classes.filter(Boolean).join(' '); const { app, editApp } = useApps();
} const { control, reset, handleSubmit } = useForm<App>({
defaultValues: { automaticUpdates: false },
});
interface GeneralTabProps { useEffect(() => {
automaticUpdates: boolean; if (!_.isEmpty(app)) {
onChange: () => void; reset(app);
} }
return () => {
reset({ automaticUpdates: false });
};
}, [app, reset]);
const onSubmit: SubmitHandler<App> = (data) => {
try {
editApp(data);
} catch (e: any) {
// continue
}
};
export const GeneralTab = ({ automaticUpdates, onChange }: GeneralTabProps) => {
return ( return (
<>
<form onSubmit={handleSubmit(onSubmit)}>
<div className="py-4"> <div className="py-4">
<div className="md:grid md:grid-cols-3 md:gap-6"> <div className="md:grid md:grid-cols-3 md:gap-6">
<div className="md:col-span-2"> <div className="md:col-span-2">
@ -22,24 +41,21 @@ export const GeneralTab = ({ automaticUpdates, onChange }: GeneralTabProps) => {
</p> </p>
</div> </div>
<div className="my-auto ml-auto"> <div className="my-auto ml-auto">
<Switch <Switch control={control} name="automaticUpdates" />
checked={automaticUpdates} </div>
onChange={onChange} </div>
className={classNames( </div>
automaticUpdates ? 'bg-primary-600' : 'bg-gray-200', <div className="py-3 sm:flex">
'relative inline-flex flex-shrink-0 h-6 w-11 border-2 border-transparent rounded-full cursor-pointer transition-colors ease-in-out duration-200 focus:outline-none', <div className="ml-auto sm:flex sm:flex-row-reverse">
)} <button
type="submit"
className="w-full inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-primary-600 text-base font-medium text-white hover:bg-primary-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary-500 sm:ml-3 sm:w-auto sm:text-sm"
> >
<span Save changes
aria-hidden="true" </button>
className={classNames(
automaticUpdates ? 'translate-x-5' : 'translate-x-0',
'pointer-events-none inline-block h-5 w-5 rounded-full bg-white shadow transform ring-0 transition ease-in-out duration-200',
)}
/>
</Switch>
</div>
</div> </div>
</div> </div>
</form>
</>
); );
}; };

View file

@ -1,5 +1,5 @@
import { useDispatch, useSelector } from 'react-redux'; import { useDispatch, useSelector } from 'react-redux';
import { fetchApps, fetchAppBySlug, updateAppBySlug, installAppBySlug, clearCurrentApp, deleteApp } from '../redux'; import { fetchApps, fetchAppBySlug, updateApp, installAppBySlug, clearCurrentApp, deleteApp } from '../redux';
import { getCurrentApp, getAppLoading, getAppsLoading, getApps } from '../redux/selectors'; import { getCurrentApp, getAppLoading, getAppsLoading, getApps } from '../redux/selectors';
export function useApps() { export function useApps() {
@ -17,8 +17,8 @@ export function useApps() {
return dispatch(fetchAppBySlug(slug)); return dispatch(fetchAppBySlug(slug));
} }
function editAppBySlug(data: any) { function editApp(data: any) {
return dispatch(updateAppBySlug(data)); return dispatch(updateApp(data));
} }
function installApp(data: any) { function installApp(data: any) {
@ -38,7 +38,7 @@ export function useApps() {
app, app,
loadApp, loadApp,
loadApps, loadApps,
editAppBySlug, editApp,
appLoading, appLoading,
appTableLoading, appTableLoading,
installApp, installApp,

View file

@ -68,7 +68,7 @@ export const fetchAppBySlug = (slug: string) => async (dispatch: Dispatch<any>)
dispatch(setAppLoading(false)); dispatch(setAppLoading(false));
}; };
export const updateAppBySlug = (app: any) => async (dispatch: Dispatch<any>) => { export const updateApp = (app: any) => async (dispatch: Dispatch<any>) => {
dispatch(setAppLoading(true)); dispatch(setAppLoading(true));
try { try {
@ -83,6 +83,8 @@ export const updateAppBySlug = (app: any) => async (dispatch: Dispatch<any>) =>
payload: transformApp(data), payload: transformApp(data),
}); });
showToast('App is updated!', ToastType.Success);
dispatch(fetchApps()); dispatch(fetchApps());
} catch (err) { } catch (err) {
console.error(err); console.error(err);