Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4주차 기본/심화/공유 과제] 취미 마이페이지 만들기 (TypeScript, Axios) #10

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
25 changes: 25 additions & 0 deletions week4/homework/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
.env
50 changes: 50 additions & 0 deletions week4/homework/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# React + TypeScript + Vite

This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.

Currently, two official plugins are available:

- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh

## Expanding the ESLint configuration

If you are developing a production application, we recommend updating the configuration to enable type aware lint rules:

- Configure the top-level `parserOptions` property like this:

```js
export default tseslint.config({
languageOptions: {
// other options...
parserOptions: {
project: ['./tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: import.meta.dirname,
},
},
})
```

- Replace `tseslint.configs.recommended` to `tseslint.configs.recommendedTypeChecked` or `tseslint.configs.strictTypeChecked`
- Optionally add `...tseslint.configs.stylisticTypeChecked`
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and update the config:

```js
// eslint.config.js
import react from 'eslint-plugin-react'

export default tseslint.config({
// Set the react version
settings: { react: { version: '18.3' } },
plugins: {
// Add the react plugin
react,
},
rules: {
// other rules...
// Enable its recommended rules
...react.configs.recommended.rules,
...react.configs['jsx-runtime'].rules,
},
})
```
28 changes: 28 additions & 0 deletions week4/homework/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import tseslint from 'typescript-eslint'

export default tseslint.config(
{ ignores: ['dist'] },
{
extends: [js.configs.recommended, ...tseslint.configs.recommended],
files: ['**/*.{ts,tsx}'],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
},
plugins: {
'react-hooks': reactHooks,
'react-refresh': reactRefresh,
},
rules: {
...reactHooks.configs.recommended.rules,
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
},
)
14 changes: 14 additions & 0 deletions week4/homework/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Hobby My Page</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
<script src="https://kit.fontawesome.com/dc54b43422.js" crossorigin="anonymous"></script>
</body>
</html>
33 changes: 33 additions & 0 deletions week4/homework/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "homework",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc -b && vite build",
"lint": "eslint .",
"preview": "vite preview"
},
"dependencies": {
"@emotion/react": "^11.13.3",
"@emotion/styled": "^11.13.0",
"axios": "^1.7.7",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.28.0"
},
"devDependencies": {
"@eslint/js": "^9.13.0",
"@types/react": "^18.3.12",
"@types/react-dom": "^18.3.1",
"@vitejs/plugin-react-swc": "^3.5.0",
"eslint": "^9.13.0",
"eslint-plugin-react-hooks": "^5.0.0",
"eslint-plugin-react-refresh": "^0.4.14",
"globals": "^15.11.0",
"typescript": "~5.6.2",
"typescript-eslint": "^8.11.0",
"vite": "^5.4.10"
}
}
1 change: 1 addition & 0 deletions week4/homework/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions week4/homework/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Global, ThemeProvider } from "@emotion/react";
import { Theme } from "./styles/theme";
import GlobalStyle from "./styles/global";
// import { RouterProvider } from "react-router-dom";
import Router from "./routes/Router";

const App = () => {
return (
<>
<ThemeProvider theme={Theme}>
<Global styles={GlobalStyle} />
<Router />
</ThemeProvider>
</>
);
};

export default App;
13 changes: 13 additions & 0 deletions week4/homework/src/apis/instance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import axios from "axios";

export const instance = axios.create({
baseURL: import.meta.env.VITE_APP_BASE_URL,
});

instance.interceptors.request.use((config) => {
const token = localStorage.getItem("token");
if (token) {
config.headers["token"] = token;
}
return config;
});
73 changes: 73 additions & 0 deletions week4/homework/src/apis/userApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { instance } from './instance';
import { AxiosError } from 'axios';

interface ErrorResponseData {
code: string;
}

interface HobbyResponse {
result: { hobby: string };
}

export const postSignup = async (username: string, password: string, hobby: string) => {
try {
const response = await instance.post("/user", {
username,
password,
hobby,
});
return {success: true, no: response.data.result.no};
} catch (error) {
const axiosError = error as AxiosError<ErrorResponseData>;
const code = axiosError.response?.data?.code;
return {success: false, code};
}

};

export const postLogin = async (username: string, password: string) => {
try {
const response = await instance.post("/login", {
username,
password,
});
return {success: true, token: response.data.result.token};
} catch (error) {
const axiosError = error as AxiosError<ErrorResponseData>;
const code = axiosError.response?.data?.code;
return {success: false, code};
}
};

export const getMyHobby = async () => {
try {
const response = await instance.get<HobbyResponse>("/user/my-hobby");
return {success: true, hobby: response.data.result.hobby};
} catch (error) {
const axiosError = error as AxiosError<ErrorResponseData>;
const code = axiosError.response?.data?.code;
return {success: false, code};
}
};

export const getUserHobby = async (userNo: number) => {
try {
const response = await instance.get<HobbyResponse>(`/user/${userNo}/hobby`);
return { success: true, hobby: response.data.result.hobby };
} catch (error) {
const axiosError = error as AxiosError<ErrorResponseData>;
const code = axiosError.response?.data?.code;
return { success: false, code };
}
};

export const putUserInfo = async (data: { hobby?: string; password?: string }) => {
try {
const response = await instance.put("/user", data);
return { success: true };
} catch (error) {
const axiosError = error as AxiosError<ErrorResponseData>;
const code = axiosError.response?.data?.code;
return { success: false, code };
}
};
1 change: 1 addition & 0 deletions week4/homework/src/assets/eye-slash.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions week4/homework/src/assets/eye.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions week4/homework/src/assets/react.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions week4/homework/src/components/Button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import styled from "@emotion/styled";
import { Theme } from '../styles/theme';

interface ButtonProps {
children: React.ReactNode;
onClick: () => void;
disabled?: boolean;
}

const Button = ({ children, onClick, disabled = false }: ButtonProps) => {
return (
<StyledButton onClick={onClick} disabled={disabled}>
{children}
</StyledButton>
);
};

const StyledButton = styled.button`
width: 100%;
padding: 0.8rem;
margin: 0.5rem 0;
background-color: ${Theme.color.pink};
color: ${Theme.color.white};
border: none;
border-radius: 4px;
font-size: 1.6rem;
cursor: pointer;
transition: background-color 0.3s ease;

&:hover {
background-color: ${Theme.color.boldPink};
}

&:disabled {
background-color: #ccc;
cursor: not-allowed;
}
`;

export default Button;
30 changes: 30 additions & 0 deletions week4/homework/src/components/Input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import styled from "@emotion/styled";

interface InputProps {
type?: string;
placeholder: string;
value: string;
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
}

const Input = ({ type = "text", placeholder, value, onChange }: InputProps) => {
return (
<StyledInput
type={type}
placeholder={placeholder}
value={value}
onChange={onChange}
/>
);
};

const StyledInput = styled.input`
width: 100%;
padding: 0.8rem;
margin: 0.5rem 0;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 1.6rem;
`;

export default Input;
Loading