Skip to content

Commit

Permalink
refresh after put/post; handle auth state
Browse files Browse the repository at this point in the history
  • Loading branch information
rstijerina committed Jan 17, 2025
1 parent 34fe03b commit c83faeb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
26 changes: 13 additions & 13 deletions react/src/components/LayersPanel/LayersPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ import {
import { zodResolver } from '@hookform/resolvers/zod';
import * as z from 'zod';
import { SortableItem } from './SortableItem';
import { useSelector } from 'react-redux';
import { RootState } from '@hazmapper/redux/store';
import { isTokenValid } from '@hazmapper/utils/authUtils';

const formLayerTheme: ThemeConfig = {
components: {
Expand Down Expand Up @@ -118,13 +121,15 @@ const LayersPanel: React.FC<{
tileLayers?: TileServerLayer[];
projectId: number;
}> = ({ tileLayers = [], projectId }) => {
const isAuthenticated = useSelector((state: RootState) =>
isTokenValid(state.auth.authToken)
);
const [form] = Form.useForm();
const { Header, Content } = Layout;
const { mutate: updateTileLayers, isPending } = usePutTileServer({
projectId,
});
const [editLayerField, setEditLayerField] = useState({});
const [saveButtonEnabled, setSaveButtonEnabled] = useState(false);

const initialValues = useMemo(
() => ({
Expand Down Expand Up @@ -153,10 +158,6 @@ const LayersPanel: React.FC<{
reset(initialValues);
}, [initialValues, reset]);

useEffect(() => {
if (isDirty && !saveButtonEnabled) setSaveButtonEnabled(true);
}, [isDirty, saveButtonEnabled]);

type TLayerOptionsFormData = {
tileLayers: {
layer: TileServerLayer;
Expand Down Expand Up @@ -221,9 +222,6 @@ const LayersPanel: React.FC<{
onFinish={handleSubmit(saveLayerOptions, (error) => {
console.log('error submit data', error);
})}
onValuesChange={() => {
if (!saveButtonEnabled) setSaveButtonEnabled(true);
}}
>
<fieldset disabled={isPending}>
<Flex vertical>
Expand Down Expand Up @@ -347,10 +345,12 @@ const LayersPanel: React.FC<{
});
}}
/>
<DeleteTileLayerButton
projectId={projectId}
tileLayerId={field.layer.id}
/>
{isAuthenticated && (
<DeleteTileLayerButton
projectId={projectId}
tileLayerId={field.layer.id}
/>
)}
</Flex>
</Flex>
<FormItem
Expand All @@ -373,7 +373,7 @@ const LayersPanel: React.FC<{
</DndContext>
</Flex>

{saveButtonEnabled && (
{isDirty && isAuthenticated && (
<Flex
align="center"
justify="center"
Expand Down
14 changes: 14 additions & 0 deletions react/src/hooks/tileServers/useTileServers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,28 @@ export const useGetTileServers = ({
};

export const usePutTileServer = ({ projectId }: UsePostTileServerParams) => {
const queryClient = useQueryClient();
return usePut<TileServerLayer[], TileServerLayer[]>({
endpoint: `/projects/${projectId}/tile-servers/`,
options: {
onSuccess: () =>
queryClient.invalidateQueries({
queryKey: ['useGetTileServers'],
}),
},
});
};

export const usePostTileServer = ({ projectId }: UsePostTileServerParams) => {
const queryClient = useQueryClient();
return usePost<TileServerLayer, TileServerLayer>({
endpoint: `/projects/${projectId}/tile-servers/`,
options: {
onSuccess: () =>
queryClient.invalidateQueries({
queryKey: ['useGetTileServers'],
}),
},
});
};

Expand Down

0 comments on commit c83faeb

Please sign in to comment.