Skip to content

Commit

Permalink
reverted from server action for now
Browse files Browse the repository at this point in the history
  • Loading branch information
aatbip committed Feb 8, 2024
1 parent b6d7909 commit 414bff3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 43 deletions.
18 changes: 1 addition & 17 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import ThemeRegistry from './ThemeRegistry';
import { AppContextProvider } from '@/context';
import { ToggleDecider } from '@/hoc/ToggleDecider';
import { Footer } from '@/layouts/Footer';
import { apiUrl } from '@/config';
import { revalidateTag } from 'next/cache';

const inter = Inter({ subsets: ['latin'] });

Expand All @@ -23,21 +21,7 @@ export default function RootLayout({ children }: { children: React.ReactNode })
<ThemeRegistry options={{ key: 'mui' }}>
<ToggleDecider>
{children}
<Footer
handleSave={async (customFieldAccessPayload, settingsPayload, token, portalId) => {
'use server';
await fetch(`${apiUrl}/api/custom-field-access?token=${token}&portalId=${portalId}`, {
method: 'PUT',
body: customFieldAccessPayload,
});
await fetch(`${apiUrl}/api/settings?token=${token}&portalId=${portalId}`, {
method: 'PUT',
body: settingsPayload,
});
revalidateTag('settings');
revalidateTag('customFieldAccess');
}}
/>
<Footer />
</ToggleDecider>
</ThemeRegistry>
</body>
Expand Down
63 changes: 37 additions & 26 deletions src/layouts/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import { useAppState } from '@/hooks/useAppState';
import { arraysHaveSameElements } from '@/utils/arrayHaveSameElements';
import { useState } from 'react';

interface Prop {
handleSave(customFieldAccessPayload: string, settingsPayload: string, token: string, portalId: string): Promise<void>;
}

export const Footer = ({ handleSave }: Prop) => {
export const Footer = () => {
const appState = useAppState();
const [loading, setLoading] = useState(false);

Expand All @@ -18,37 +14,52 @@ export const Footer = ({ handleSave }: Prop) => {
appState?.setAppState((prev) => ({ ...prev, mutableCustomFieldAccess: appState?.customFieldAccess }));
};

const initiateSave = async () => {
const handleSave = async () => {
setLoading(true);
let accesses = appState?.mutableCustomFieldAccess.map(({ id, permission }: any) => ({
customFieldId: id,
permissions: permission,
}));
setLoading(true);
try {
await handleSave(
JSON.stringify({
token: appState?.token as string,
portalId: appState?.portalId as string,
accesses: accesses,
}),
JSON.stringify({
token: appState?.token as string,
portalId: appState?.portalId as string,
profileLinks: ['profile_settings'],
}),
appState?.token as string,
appState?.portalId as string,
);
} finally {
setLoading(false);
}
await fetch(`/api/custom-field-access`, {
method: 'PUT',
body: JSON.stringify({
token: appState?.token,
portalId: appState?.portalId,
accesses: accesses,
}),
});
await fetch(`/api/settings`, {
method: 'PUT',
body: JSON.stringify({
token: appState?.token,
portalId: appState?.portalId,
profileLinks: appState?.mutableSettings,
}),
});
const settingsRes = await fetch(`/api/settings?token=${appState?.token}&portalId=${appState?.portalId}`);
const settings = await settingsRes.json();
const customFieldAccessRes = await fetch(
`/api/custom-field-access?token=${appState?.token}&portalId=${appState?.portalId}`,
);
const customFieldAccess = await customFieldAccessRes.json();
appState?.setAppState((prev) => ({
...prev,
settings: settings.data.profileLinks,
mutableSettings: settings.data.profileLinks,
}));
appState?.setAppState((prev) => ({
...prev,
customFieldAccess: customFieldAccess.data,
mutableCustomFieldAccess: customFieldAccess.data,
}));
setLoading(false);
};

return (
<>
{arraysHaveSameElements(appState?.mutableSettings, appState?.settings) &&
JSON.stringify(appState?.customFieldAccess) === JSON.stringify(appState?.mutableCustomFieldAccess) ? null : (
<FooterSave type="1" loading={loading} handleSave={initiateSave} handleCancel={handleCancel} />
<FooterSave type="1" loading={loading} handleSave={handleSave} handleCancel={handleCancel} />
)}
</>
);
Expand Down

0 comments on commit 414bff3

Please sign in to comment.