Ran prettier on all src files now that printWidth is 120.

This commit is contained in:
Jesse Brault 2024-08-08 10:46:49 -05:00
parent 1ded7331ef
commit 111acea22f
18 changed files with 349 additions and 426 deletions

View File

@ -7,11 +7,7 @@ export interface GetImageDeps {
url: string
}
const getImage = async ({
accessToken,
signal,
url
}: GetImageDeps): Promise<string> => {
const getImage = async ({ accessToken, signal, url }: GetImageDeps): Promise<string> => {
const headers = new Headers()
if (accessToken !== null) {
headers.set('Authorization', `Bearer ${accessToken}`)

View File

@ -2,10 +2,7 @@ import { notFound } from '@tanstack/react-router'
import { AuthContextType } from '../auth'
import { ApiError } from './ApiError'
import ExpiredTokenError from './ExpiredTokenError'
import FullRecipeView, {
RawFullRecipeView,
toFullRecipeView
} from './types/FullRecipeView'
import FullRecipeView, { RawFullRecipeView, toFullRecipeView } from './types/FullRecipeView'
export interface GetRecipeDeps {
authContext: AuthContextType
@ -14,24 +11,16 @@ export interface GetRecipeDeps {
abortSignal: AbortSignal
}
const getRecipe = async ({
authContext,
username,
slug,
abortSignal
}: GetRecipeDeps): Promise<FullRecipeView> => {
const getRecipe = async ({ authContext, username, slug, abortSignal }: GetRecipeDeps): Promise<FullRecipeView> => {
const headers = new Headers()
if (authContext.token !== null) {
headers.set('Authorization', `Bearer ${authContext.token}`)
}
const response = await fetch(
import.meta.env.VITE_MME_API_URL + `/recipes/${username}/${slug}`,
{
const response = await fetch(import.meta.env.VITE_MME_API_URL + `/recipes/${username}/${slug}`, {
signal: abortSignal,
headers,
mode: 'cors'
}
)
})
if (response.ok) {
return toFullRecipeView((await response.json()) as RawFullRecipeView)
} else if (response.status === 401) {

View File

@ -20,18 +20,13 @@ const getRecipeInfos = async ({
if (token !== null) {
headers.set('Authorization', `Bearer ${token}`)
}
const response = await fetch(
import.meta.env.VITE_MME_API_URL +
`/recipes?page=${pageNumber}&size=${pageSize}`,
{
const response = await fetch(import.meta.env.VITE_MME_API_URL + `/recipes?page=${pageNumber}&size=${pageSize}`, {
signal: abortSignal,
headers,
mode: 'cors'
}
)
})
if (response.ok) {
const { pageNumber, pageSize, content } =
(await response.json()) as RawRecipeInfosView
const { pageNumber, pageSize, content } = (await response.json()) as RawRecipeInfosView
return {
pageNumber,
pageSize,

View File

@ -1,13 +1,8 @@
import { LoginResult, RawLoginView } from './types/LoginView'
const login = async (
username: string,
password: string
): Promise<LoginResult> => {
const login = async (username: string, password: string): Promise<LoginResult> => {
try {
const response = await fetch(
import.meta.env.VITE_MME_API_URL + '/auth/login',
{
const response = await fetch(import.meta.env.VITE_MME_API_URL + '/auth/login', {
body: JSON.stringify({ username, password }),
credentials: 'include',
headers: {
@ -15,14 +10,9 @@ const login = async (
},
method: 'POST',
mode: 'cors'
}
)
})
if (response.ok) {
const {
username,
accessToken,
expires: rawExpires
} = (await response.json()) as RawLoginView
const { username, accessToken, expires: rawExpires } = (await response.json()) as RawLoginView
return {
_tag: 'success',
loginView: {
@ -36,13 +26,10 @@ const login = async (
if (response.status === 401) {
error = 'Invalid username or password.'
} else if (response.status === 500) {
error =
'There was an internal server error. Please try again later.'
error = 'There was an internal server error. Please try again later.'
} else {
error = 'Unknown error.'
console.error(
`Unknown error: ${response.status} ${response.statusText}`
)
console.error(`Unknown error: ${response.status} ${response.statusText}`)
}
return {
_tag: 'failure',

View File

@ -11,14 +11,11 @@ export class ExpiredRefreshTokenError extends ApiError {
const refresh = async (): Promise<LoginView> => {
let response: Response
try {
response = await fetch(
import.meta.env.VITE_MME_API_URL + '/auth/refresh',
{
response = await fetch(import.meta.env.VITE_MME_API_URL + '/auth/refresh', {
credentials: 'include',
method: 'POST',
mode: 'cors'
}
)
})
} catch (fetchError) {
if (fetchError instanceof TypeError) {
throw fetchError // rethrow network issues
@ -27,11 +24,7 @@ const refresh = async (): Promise<LoginView> => {
}
}
if (response.ok) {
const {
username,
accessToken,
expires: rawExpires
} = (await response.json()) as RawLoginView
const { username, accessToken, expires: rawExpires } = (await response.json()) as RawLoginView
return {
username,
accessToken,

View File

@ -32,12 +32,7 @@ const RecipeCard = ({
slug
}}
>
<img
className={classes.recipeImage}
src={mainImageUrl}
alt={mainImageAlt}
title={mainImageAlt}
/>
<img className={classes.recipeImage} src={mainImageUrl} alt={mainImageAlt} title={mainImageAlt} />
</Link>
<div className={classes.infoContainer}>
<div className={classes.infoRow}>

View File

@ -7,17 +7,9 @@ export interface RecipeVisibilityIconProps {
const RecipeVisibilityIcon = ({ isPublic }: RecipeVisibilityIconProps) =>
isPublic ? (
<FontAwesomeIcon
icon="globe"
className={classes.recipeVisibilityIcon}
size="sm"
/>
<FontAwesomeIcon icon="globe" className={classes.recipeVisibilityIcon} size="sm" />
) : (
<FontAwesomeIcon
icon="lock"
className={classes.recipeVisibilityIcon}
size="sm"
/>
<FontAwesomeIcon icon="lock" className={classes.recipeVisibilityIcon} size="sm" />
)
export default RecipeVisibilityIcon

View File

@ -1,10 +1,4 @@
import {
createFileRoute,
redirect,
useNavigate,
useRouter,
useSearch
} from '@tanstack/react-router'
import { createFileRoute, redirect, useNavigate, useRouter, useSearch } from '@tanstack/react-router'
import { FormEvent, useState } from 'react'
import { z } from 'zod'
import login from '../api/login'
@ -25,17 +19,13 @@ const Login = () => {
const password = (formData.get('password') as string | null) ?? ''
const loginResult = await login(username, password)
if (loginResult._tag === 'success') {
auth.putToken(
loginResult.loginView.accessToken,
loginResult.loginView.username,
async () => {
auth.putToken(loginResult.loginView.accessToken, loginResult.loginView.username, async () => {
await router.invalidate()
await navigate({
to: redirect ?? '/recipes',
search: {}
})
}
)
})
} else {
setError(loginResult.error)
}
@ -44,9 +34,7 @@ const Login = () => {
return (
<div>
<h2>Login Page</h2>
{expired ? (
<p>Your session has expired. Please login again.</p>
) : null}
{expired ? <p>Your session has expired. Please login again.</p> : null}
<form onSubmit={onSubmit}>
<label htmlFor="username">Username</label>
<input id="username" name="username" type="text" />

View File

@ -38,11 +38,7 @@ export const Route = createFileRoute('/recipes/$username/$slug')({
} = useQuery(
{
enabled: recipe !== undefined,
queryKey: [
'images',
recipe?.mainImage.owner.username,
recipe?.mainImage.filename
],
queryKey: ['images', recipe?.mainImage.owner.username, recipe?.mainImage.filename],
queryFn: ({ signal }) =>
getImage({
accessToken: authContext.token,

View File

@ -1,40 +1,34 @@
import React from 'react';
import './button.css';
import React from 'react'
import './button.css'
interface ButtonProps {
/**
* Is this the principal call to action on the page?
*/
primary?: boolean;
primary?: boolean
/**
* What background color to use
*/
backgroundColor?: string;
backgroundColor?: string
/**
* How large should the button be?
*/
size?: 'small' | 'medium' | 'large';
size?: 'small' | 'medium' | 'large'
/**
* Button contents
*/
label: string;
label: string
/**
* Optional click handler
*/
onClick?: () => void;
onClick?: () => void
}
/**
* Primary UI component for user interaction
*/
export const Button = ({
primary = false,
size = 'medium',
backgroundColor,
label,
...props
}: ButtonProps) => {
const mode = primary ? 'storybook-button--primary' : 'storybook-button--secondary';
export const Button = ({ primary = false, size = 'medium', backgroundColor, label, ...props }: ButtonProps) => {
const mode = primary ? 'storybook-button--primary' : 'storybook-button--secondary'
return (
<button
type="button"
@ -44,5 +38,5 @@ export const Button = ({
>
{label}
</button>
);
};
)
}

View File

@ -1,21 +1,22 @@
import { Meta } from "@storybook/blocks";
import { Meta } from '@storybook/blocks'
import Github from "./assets/github.svg";
import Discord from "./assets/discord.svg";
import Youtube from "./assets/youtube.svg";
import Tutorials from "./assets/tutorials.svg";
import Styling from "./assets/styling.png";
import Context from "./assets/context.png";
import Assets from "./assets/assets.png";
import Docs from "./assets/docs.png";
import Share from "./assets/share.png";
import FigmaPlugin from "./assets/figma-plugin.png";
import Testing from "./assets/testing.png";
import Accessibility from "./assets/accessibility.png";
import Theming from "./assets/theming.png";
import AddonLibrary from "./assets/addon-library.png";
import Github from './assets/github.svg'
import Discord from './assets/discord.svg'
import Youtube from './assets/youtube.svg'
import Tutorials from './assets/tutorials.svg'
import Styling from './assets/styling.png'
import Context from './assets/context.png'
import Assets from './assets/assets.png'
import Docs from './assets/docs.png'
import Share from './assets/share.png'
import FigmaPlugin from './assets/figma-plugin.png'
import Testing from './assets/testing.png'
import Accessibility from './assets/accessibility.png'
import Theming from './assets/theming.png'
import AddonLibrary from './assets/addon-library.png'
export const RightArrow = () => <svg
export const RightArrow = () => (
<svg
viewBox="0 0 14 14"
width="8px"
height="14px"
@ -30,6 +31,7 @@ export const RightArrow = () => <svg
>
<path d="m11.1 7.35-5.5 5.5a.5.5 0 0 1-.7-.7L10.04 7 4.9 1.85a.5.5 0 1 1 .7-.7l5.5 5.5c.2.2.2.5 0 .7Z" />
</svg>
)
<Meta title="Configure your project" />
@ -38,6 +40,7 @@ export const RightArrow = () => <svg
# Configure your project
Because Storybook works separately from your app, you'll need to configure it for your specific stack and setup. Below, explore guides for configuring Storybook with popular frameworks and tools. If you get stuck, learn how you can ask for help from our community.
</div>
<div className="sb-section">
<div className="sb-section-item">
@ -84,6 +87,7 @@ export const RightArrow = () => <svg
# Do more with Storybook
Now that you know the basics, let's explore other parts of Storybook that will improve your experience. This list is just to get you started. You can customise Storybook in many ways to fit your needs.
</div>
<div className="sb-section">
@ -203,6 +207,7 @@ export const RightArrow = () => <svg
target="_blank"
>Discover tutorials<RightArrow /></a>
</div>
</div>
<style>

View File

@ -1,7 +1,7 @@
import type { Meta, StoryObj } from '@storybook/react';
import { fn } from '@storybook/test';
import type { Meta, StoryObj } from '@storybook/react'
import { fn } from '@storybook/test'
import { Header } from './Header';
import { Header } from './Header'
const meta = {
title: 'Example/Header',
@ -10,24 +10,24 @@ const meta = {
tags: ['autodocs'],
parameters: {
// More on how to position stories at: https://storybook.js.org/docs/configure/story-layout
layout: 'fullscreen',
layout: 'fullscreen'
},
args: {
onLogin: fn(),
onLogout: fn(),
onCreateAccount: fn(),
},
} satisfies Meta<typeof Header>;
onCreateAccount: fn()
}
} satisfies Meta<typeof Header>
export default meta;
type Story = StoryObj<typeof meta>;
export default meta
type Story = StoryObj<typeof meta>
export const LoggedIn: Story = {
args: {
user: {
name: 'Jane Doe',
},
},
};
name: 'Jane Doe'
}
}
}
export const LoggedOut: Story = {};
export const LoggedOut: Story = {}

View File

@ -1,17 +1,17 @@
import React from 'react';
import React from 'react'
import { Button } from './Button';
import './header.css';
import { Button } from './Button'
import './header.css'
type User = {
name: string;
};
name: string
}
interface HeaderProps {
user?: User;
onLogin?: () => void;
onLogout?: () => void;
onCreateAccount?: () => void;
user?: User
onLogin?: () => void
onLogout?: () => void
onCreateAccount?: () => void
}
export const Header = ({ user, onLogin, onLogout, onCreateAccount }: HeaderProps) => (
@ -24,14 +24,8 @@ export const Header = ({ user, onLogin, onLogout, onCreateAccount }: HeaderProps
d="M10 0h12a10 10 0 0110 10v12a10 10 0 01-10 10H10A10 10 0 010 22V10A10 10 0 0110 0z"
fill="#FFF"
/>
<path
d="M5.3 10.6l10.4 6v11.1l-10.4-6v-11zm11.4-6.2l9.7 5.5-9.7 5.6V4.4z"
fill="#555AB9"
/>
<path
d="M27.2 10.6v11.2l-10.5 6V16.5l10.5-6zM15.7 4.4v11L6 10l9.7-5.5z"
fill="#91BAF8"
/>
<path d="M5.3 10.6l10.4 6v11.1l-10.4-6v-11zm11.4-6.2l9.7 5.5-9.7 5.6V4.4z" fill="#555AB9" />
<path d="M27.2 10.6v11.2l-10.5 6V16.5l10.5-6zM15.7 4.4v11L6 10l9.7-5.5z" fill="#91BAF8" />
</g>
</svg>
<h1>Acme</h1>
@ -53,4 +47,4 @@ export const Header = ({ user, onLogin, onLogout, onCreateAccount }: HeaderProps
</div>
</div>
</header>
);
)

View File

@ -1,32 +1,32 @@
import type { Meta, StoryObj } from '@storybook/react';
import { within, userEvent, expect } from '@storybook/test';
import type { Meta, StoryObj } from '@storybook/react'
import { within, userEvent, expect } from '@storybook/test'
import { Page } from './Page';
import { Page } from './Page'
const meta = {
title: 'Example/Page',
component: Page,
parameters: {
// More on how to position stories at: https://storybook.js.org/docs/configure/story-layout
layout: 'fullscreen',
},
} satisfies Meta<typeof Page>;
layout: 'fullscreen'
}
} satisfies Meta<typeof Page>
export default meta;
type Story = StoryObj<typeof meta>;
export default meta
type Story = StoryObj<typeof meta>
export const LoggedOut: Story = {};
export const LoggedOut: Story = {}
// More on interaction testing: https://storybook.js.org/docs/writing-tests/interaction-testing
export const LoggedIn: Story = {
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
const loginButton = canvas.getByRole('button', { name: /Log in/i });
await expect(loginButton).toBeInTheDocument();
await userEvent.click(loginButton);
await expect(loginButton).not.toBeInTheDocument();
const canvas = within(canvasElement)
const loginButton = canvas.getByRole('button', { name: /Log in/i })
await expect(loginButton).toBeInTheDocument()
await userEvent.click(loginButton)
await expect(loginButton).not.toBeInTheDocument()
const logoutButton = canvas.getByRole('button', { name: /Log out/i });
await expect(logoutButton).toBeInTheDocument();
},
};
const logoutButton = canvas.getByRole('button', { name: /Log out/i })
await expect(logoutButton).toBeInTheDocument()
}
}

View File

@ -1,14 +1,14 @@
import React from 'react';
import React from 'react'
import { Header } from './Header';
import './page.css';
import { Header } from './Header'
import './page.css'
type User = {
name: string;
};
name: string
}
export const Page: React.FC = () => {
const [user, setUser] = React.useState<User>();
const [user, setUser] = React.useState<User>()
return (
<article>
@ -29,18 +29,17 @@ export const Page: React.FC = () => {
process starting with atomic components and ending with pages.
</p>
<p>
Render pages with mock data. This makes it easy to build and review page states without
needing to navigate to them in your app. Here are some handy patterns for managing page
data in Storybook:
Render pages with mock data. This makes it easy to build and review page states without needing to
navigate to them in your app. Here are some handy patterns for managing page data in Storybook:
</p>
<ul>
<li>
Use a higher-level connected component. Storybook helps you compose such data from the
"args" of child component stories
Use a higher-level connected component. Storybook helps you compose such data from the "args" of
child component stories
</li>
<li>
Assemble data in the page component from your services. You can mock these services out
using Storybook.
Assemble data in the page component from your services. You can mock these services out using
Storybook.
</li>
</ul>
<p>
@ -69,5 +68,5 @@ export const Page: React.FC = () => {
</div>
</section>
</article>
);
};
)
}