-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from acelaya-forks/feature/settings-section
Create settings page
- Loading branch information
Showing
13 changed files
with
280 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import type { ActionFunctionArgs, LoaderFunctionArgs } from '@remix-run/node'; | ||
import { useFetcher, useLoaderData } from '@remix-run/react'; | ||
import type { Settings as AppSettings } from '@shlinkio/shlink-web-component/settings'; | ||
import { ShlinkWebSettings } from '@shlinkio/shlink-web-component/settings'; | ||
import { useCallback } from 'react'; | ||
import { Authenticator } from 'remix-auth'; | ||
import type { SessionData } from '../auth/session-context'; | ||
import { serverContainer } from '../container/container.server'; | ||
import { SettingsService } from '../settings/SettingsService.server'; | ||
|
||
export async function loader( | ||
{ request }: LoaderFunctionArgs, | ||
authenticator: Authenticator<SessionData> = serverContainer[Authenticator.name], | ||
settingsService: SettingsService = serverContainer[SettingsService.name], | ||
) { | ||
const { userId } = await authenticator.isAuthenticated(request, { failureRedirect: '/login' }); | ||
return settingsService.userSettings(userId); | ||
} | ||
|
||
export async function action( | ||
{ request }: ActionFunctionArgs, | ||
authenticator: Authenticator<SessionData> = serverContainer[Authenticator.name], | ||
settingsService: SettingsService = serverContainer[SettingsService.name], | ||
) { | ||
const [sessionData, newSettings] = await Promise.all([ | ||
authenticator.isAuthenticated(request), | ||
request.json(), | ||
]); | ||
if (!sessionData) { | ||
return {}; | ||
} | ||
|
||
await settingsService.saveUserSettings(sessionData.userId, newSettings); | ||
return {}; | ||
} | ||
|
||
export default function Settings() { | ||
const settings = useLoaderData<typeof loader>(); | ||
const fetcher = useFetcher(); | ||
// TODO Add some deferring | ||
const submitSettings = useCallback((newSettings: AppSettings) => fetcher.submit(newSettings, { | ||
method: 'POST', | ||
encType: 'application/json', | ||
}), [fetcher]); | ||
|
||
return ( | ||
<div className="tw-container lg:tw-p-5 tw-p-3 mx-auto"> | ||
<ShlinkWebSettings | ||
settings={settings} | ||
updateSettings={submitSettings} | ||
defaultShortUrlsListOrdering={{}} | ||
/> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import type { ActionFunctionArgs } from '@remix-run/node'; | ||
import { json } from '@remix-run/node'; | ||
import { createRemixStub } from '@remix-run/testing'; | ||
import type { Settings } from '@shlinkio/shlink-web-component/settings'; | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
import { fromPartial } from '@total-typescript/shoehorn'; | ||
import type { Authenticator } from 'remix-auth'; | ||
import type { SessionData } from '../../app/auth/session-context'; | ||
import SettingsComp, { action as settingsAction, loader } from '../../app/routes/settings.$'; | ||
import type { SettingsService } from '../../app/settings/SettingsService.server'; | ||
|
||
describe('settings', () => { | ||
const isAuthenticated = vi.fn(); | ||
const authenticator = fromPartial<Authenticator<SessionData>>({ isAuthenticated }); | ||
const userSettings = vi.fn(); | ||
const saveUserSettings = vi.fn(); | ||
const settingsService = fromPartial<SettingsService>({ userSettings, saveUserSettings }); | ||
|
||
describe('loader', () => { | ||
it('checks if user is authenticated and returns their settings', async () => { | ||
const settings = fromPartial<Settings>({ | ||
ui: { theme: 'dark' }, | ||
}); | ||
userSettings.mockResolvedValue(settings); | ||
isAuthenticated.mockResolvedValue({ userId: 1 }); | ||
|
||
const result = await loader(fromPartial({ request: {} }), authenticator, settingsService); | ||
|
||
expect(result).toEqual(settings); | ||
expect(userSettings).toHaveBeenCalled(); | ||
expect(isAuthenticated).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('action', () => { | ||
const setUp = () => (args: ActionFunctionArgs) => settingsAction(args, authenticator, settingsService); | ||
const request = fromPartial<Request>({ json: vi.fn().mockResolvedValue({}) }); | ||
|
||
it('does not save settings when user is not logged in', async () => { | ||
const action = setUp(); | ||
|
||
isAuthenticated.mockResolvedValue(null); | ||
|
||
await action(fromPartial({ request })); | ||
|
||
expect(isAuthenticated).toHaveBeenCalledWith(request); | ||
expect(saveUserSettings).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('saves settings when user is logged in', async () => { | ||
const action = setUp(); | ||
|
||
isAuthenticated.mockResolvedValue({ userId: 1 }); | ||
|
||
await action(fromPartial({ request })); | ||
|
||
expect(isAuthenticated).toHaveBeenCalledWith(request); | ||
expect(saveUserSettings).toHaveBeenCalledWith(1, {}); | ||
}); | ||
}); | ||
|
||
// Skipping for now, as createRemixStub always results in a 404 page | ||
describe.skip('<Settings />', () => { | ||
const setUp = () => { | ||
const RemixStub = createRemixStub([ | ||
{ | ||
path: '/settings', | ||
Component: SettingsComp, | ||
loader: () => json({}), | ||
action: () => json({}), | ||
}, | ||
]); | ||
return render(<RemixStub />); | ||
}; | ||
|
||
it('renders settings component', async () => { | ||
setUp(); | ||
|
||
await waitFor(() => expect(screen.getByRole('heading', { name: 'User interface' })).toBeInTheDocument()); | ||
expect(screen.getByRole('heading', { name: 'Real-time updates' })).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.