Skip to content

Commit

Permalink
Merge pull request #34 from team-Ollie/main
Browse files Browse the repository at this point in the history
fix: api 연결
  • Loading branch information
iOdiO89 authored Jul 1, 2024
2 parents d70117f + 74a8aec commit c88fe58
Show file tree
Hide file tree
Showing 49 changed files with 867 additions and 189 deletions.
91 changes: 91 additions & 0 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Sample workflow for building and deploying a Next.js site to GitHub Pages
#
# To get started with Next.js see: https://nextjs.org/docs/getting-started
#
name: Test Next Build

on:
pull_request:
branches: ["main"]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write

# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: "pages"
cancel-in-progress: false

jobs:
# Build job
build:
runs-on: ubuntu-latest

env:
NEXT_PUBLIC_API_URL: ${{secrets.NEXT_PUBLIC_API_URL}}

steps:
- name: Checkout
uses: actions/checkout@v4
- name: Detect package manager
id: detect-package-manager
run: |
if [ -f "${{ github.workspace }}/yarn.lock" ]; then
echo "manager=yarn" >> $GITHUB_OUTPUT
echo "command=install" >> $GITHUB_OUTPUT
echo "runner=yarn" >> $GITHUB_OUTPUT
exit 0
elif [ -f "${{ github.workspace }}/package.json" ]; then
echo "manager=npm" >> $GITHUB_OUTPUT
echo "command=ci" >> $GITHUB_OUTPUT
echo "runner=npx --no-install" >> $GITHUB_OUTPUT
exit 0
else
echo "Unable to determine package manager"
exit 1
fi
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: "20"
cache: ${{ steps.detect-package-manager.outputs.manager }}
- name: Setup Pages
uses: actions/configure-pages@v5
with:
# Automatically inject basePath in your Next.js configuration file and disable
# server side image optimization (https://nextjs.org/docs/api-reference/next/image#unoptimized).
#
# You may remove this line if you want to manage the configuration yourself.
static_site_generator: next
- name: Restore cache
uses: actions/cache@v4
with:
path: |
.next/cache
# Generate a new cache whenever packages or source files change.
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json', '**/yarn.lock') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }}
# If source files changed but packages didn't, rebuild from a prior cache.
restore-keys: |
${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json', '**/yarn.lock') }}-
- name: Install dependencies
run: ${{ steps.detect-package-manager.outputs.manager }} ${{ steps.detect-package-manager.outputs.command }}
- name: Check environment variable
run: echo NEXT_PUBLIC_API_URL
- name: Create .env file
run: |
echo NEXT_PUBLIC_API_URL=${{ secrets.NEXT_PUBLIC_API_URL }} >> .env
- name: Build with Next.js
run: ${{ steps.detect-package-manager.outputs.runner }} next build
- name: Export with Next.js
run: ${{ steps.detect-package-manager.outputs.runner }} next export
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: ./out
11 changes: 10 additions & 1 deletion .github/workflows/nextjs.yml → .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
push:
branches: ["deploy"]
pull_request:
branches: ["deploy", "main"]
branches: ["deploy"]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
Expand All @@ -30,6 +30,10 @@ jobs:
# Build job
build:
runs-on: ubuntu-latest

env:
NEXT_PUBLIC_API_URL: ${{secrets.NEXT_PUBLIC_API_URL}}

steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down Expand Up @@ -75,6 +79,11 @@ jobs:
${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json', '**/yarn.lock') }}-
- name: Install dependencies
run: ${{ steps.detect-package-manager.outputs.manager }} ${{ steps.detect-package-manager.outputs.command }}
- name: Check environment variable
run: echo NEXT_PUBLIC_API_URL
- name: Create .env file
run: |
echo NEXT_PUBLIC_API_URL=${{ secrets.NEXT_PUBLIC_API_URL }} >> .env
- name: Build with Next.js
run: ${{ steps.detect-package-manager.outputs.runner }} next build
- name: Export with Next.js
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
/.next/
/out/

# production
/build
# # production
# /build

# misc
.DS_Store
Expand Down
25 changes: 25 additions & 0 deletions apis/admin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { CalendarDate } from "./calendar";
import client, { ResponseBody } from "./client";

export interface Program {
name: string;
dueDate: CalendarDate;
openDate: CalendarDate;
location: string;
category: string;
host: string;
schedule: string;
description: string;
}

async function postAttendanceCode(challengeIdx: number): Promise<ResponseBody> {
const { data } = await client.post(`/challenges/attendance/${challengeIdx}`);
return data;
}

async function postProgram(body: Program): Promise<ResponseBody> {
const { data } = await client.post(`/programs`, body);
return data;
}

export { postAttendanceCode, postProgram };
7 changes: 4 additions & 3 deletions apis/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ interface GetMonthCalendarResponse extends ResponseBody {
export interface MonthCalendarProps {
programIdx: number;
name: string;
category: string | null;
location: string | null;
openDate: {
year: number;
month: number;
Expand All @@ -19,7 +21,7 @@ export interface MonthCalendarProps {
};
}

type CalendarDate = {
export type CalendarDate = {
year: number;
month: number;
day: number;
Expand All @@ -38,7 +40,7 @@ interface GetProgramDetailBody {

// 챌린지 월별 조회
export const getMonthCalendar = async (): Promise<GetMonthCalendarResponse> => {
const response = await client.get("/programs");
const response = await client.get("/programs/list");
// console.log("calenderData", response.data.result);
return response.data.result;
};
Expand All @@ -48,6 +50,5 @@ export const getProgramDetail = async (
): Promise<GetProgramDetailBody> => {
// const response = await client.get(`/programs/${programIdx}`);
const response = await client.get(`/programs/2`);
// console.log("calenderDetail", response.data.result);
return response.data.result;
};
71 changes: 69 additions & 2 deletions apis/challenge.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import client, { ResponseBody } from "./client";
import client, { ResponseBody, ResponseBody2 } from "./client";

interface GetMyChallengeListResponse extends ResponseBody {
result: Challenge[];
Expand All @@ -13,9 +13,76 @@ export interface Challenge {
attendanceRate: number;
totalAttendanceRate: number;
}
interface GetChallengeAdsResponse extends ResponseBody2 {
result: {
mostParticipatedChallenge: Challenge;
mostAttendancedChallenge: Challenge;
mostRecentlyStartedChallenge: Challenge;
};
}

interface getChallengeSearchResponse extends ResponseBody2 {
result: Challenge[];
}

export interface AttendanceRequestBody {
challengeIdx: number;
attendanceCode: string;
}

async function getMyChallengeList(): Promise<GetMyChallengeListResponse> {
const { data } = await client.get(`/challenges`);
return data;
}

export { getMyChallengeList };
type AttendanceDate = {
year: number;
month: number;
day: number;
};

export interface GetChallengeDetailBody {
attendanceDate: AttendanceDate;
}

async function getChallengDetail(): Promise<GetChallengeDetailBody> {
const response = await client.get(
`/challenges/attendance/2?year=2024&month=6`,
);
return response.data.result;
}

async function getChallengeAds(): Promise<GetChallengeAdsResponse> {
const { data } = await client.get(`/challenges/ads`);
return data;
}

async function getChallengeSearch(
keyword: string,
): Promise<getChallengeSearchResponse> {
const { data } = await client.get(`/challenges/search?searchWord=${keyword}`);
return data;
}

async function postNewChallenge(challengeIdx: number): Promise<ResponseBody> {
const { data } = await client.post(`/challenges/participation`, {
challengeIdx,
});
return data;
}

async function postAttendance(
body: AttendanceRequestBody,
): Promise<ResponseBody> {
const { data } = await client.post(`/challenges/attendance`, body);
return data;
}

export {
getMyChallengeList,
getChallengeAds,
getChallengeSearch,
postNewChallenge,
postAttendance,
getChallengDetail,
};
18 changes: 17 additions & 1 deletion apis/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ interface ResponseBody {
code: number;
message: string;
}
interface ResponseBody2 {
isSuccess: boolean;
message: string;
}

export const setTokenFromLocalStorage = (access_token: string) => {
localStorage.setItem("access_token", access_token);
Expand All @@ -18,6 +22,18 @@ const getTokenFromLocalStorage = () => {
return accessToken;
};

export const setIsAdminAtLocalStorage = (is_admin: string) => {
localStorage.setItem("is_admin", is_admin);
};

const getIsAdminFromLocalStorage = () => {
const isAdmin = localStorage.getItem("is_admin");
if (!isAdmin) {
return null;
}
return isAdmin;
};

const client = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_URL,
withCredentials: true,
Expand Down Expand Up @@ -49,4 +65,4 @@ client.interceptors.request.use(
);

export default client;
export type { ResponseBody };
export type { ResponseBody, ResponseBody2 };
36 changes: 36 additions & 0 deletions apis/hooks/admin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { Program, postAttendanceCode, postProgram } from "../admin";
import { useRouter } from "next/router";

function usePostAttendanceCode(challengeIdx: number) {
const { mutate } = useMutation({
mutationKey: ["postAttendanceCode", challengeIdx],
mutationFn: () => postAttendanceCode(challengeIdx),
onSuccess: (data) => window.alert(`인증번호: ${data}`),
onError: () => window.alert("에러 발생. 앱 관리자에게 문의해주세요."),
});

return { mutate };
}

function usePostProgram() {
const router = useRouter();
const queryclient = useQueryClient();

const { mutate } = useMutation({
mutationKey: ["postProgram"],
mutationFn: (body: Program) => postProgram(body),
onSuccess: () => {
queryclient.invalidateQueries({
queryKey: ["getMyChallengeList"],
});
window.alert("프로그램이 성공적으로 등록되었습니다.");
router.push("/");
},
onError: () => router.push("/404"),
});

return { mutate };
}

export { usePostAttendanceCode, usePostProgram };
Loading

0 comments on commit c88fe58

Please sign in to comment.