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

[Feat] 전체일정 추가 API #1594

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions hooks/calendar/usePublicCalendarRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { useState } from 'react';
import { useMutation } from 'react-query';
import { instanceInCalendar } from 'utils/axios';

const usePublicCalendarRequest = <T>() => {
const [data, setData] = useState<T | null>(null);
const [status, setStatus] = useState<number>(0);
const [error, setError] = useState<string | null>(null);

const sendCalendarRequest = async <T>(
method: 'POST' | 'PATCH',
url: string,
body: Record<string, any>,
onSuccess?: (data: T) => void,
onError?: (error: string) => void
) => {
try {
console.log('받은 데이터: ', body);
const res = await instanceInCalendar.request({
method,
url,
data: body,
});

setStatus(res.status);

if (res.status >= 200 && res.status < 300) {
if (onSuccess) {
onSuccess(res.data || null);
}
if (res.data) {
setData(res.data);
}
return res.data;
}
} catch (error) {
setError('request error');
console.error(error);
if (onError) {
onError('request error');
}
}
};

const postMutation = useMutation<
T,
Error,
{ url: string; data: Record<string, any> }
>({
mutationFn: ({ url, data }) =>
sendCalendarRequest<T>(
'POST',
url,
data,
(response) => {
console.log('Mutation Request successful', response);
},
(error) => {
console.log('Mutation Request Error', error);
}
),
});

return postMutation;
};

export default usePublicCalendarRequest;
79 changes: 79 additions & 0 deletions pages/calendar/test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import React from 'react';
import { EventSchedule, JobSchedule } from 'types/calendar/scheduleTypes';
import usePublicCalendarRequest from 'hooks/calendar/usePublicCalendarRequest';

const exampleEvent: EventSchedule = {
classification: 'EVENT',
eventTag: 'ETC',
author: 'seykim',
title: 'EVENT test',
content: 'string',
link: 'string',
startTime: '2025-01-06T06:28:46.655Z',
endTime: '2025-01-10T06:28:46.655Z',
};

const exampleJob: JobSchedule = {
classification: 'JOB_NOTICE',
jobTag: 'SHORTS_INTERN',
techTag: 'FRONT_END',
author: 'seykim',
title: 'JOB test',
content: 'test',
link: 'string',
startTime: '2025-01-10T06:28:46.655Z',
endTime: '2025-01-15T06:28:46.655Z',
};

const Home = () => {
const {
mutate: mutateEvent,
isLoading: isEventLoading,
isError: isEventError,
isSuccess: isEventSuccess,
error: eventError,
} = usePublicCalendarRequest<any>();

const {
mutate: mutateJob,
isLoading: isJobLoading,
isError: isJobError,
isSuccess: isJobSuccess,
error: jobError,
} = usePublicCalendarRequest<any>();

const handleEventSubmit = () => {
mutateEvent({
url: '/public/event', // 이벤트 URL
data: exampleEvent,
});
};

const handleJobSubmit = () => {
mutateJob({
url: '/public/job', // 직무 URL
data: exampleJob,
});
};

return (
<div>
<div>
<button onClick={handleEventSubmit} disabled={isEventLoading}>
{isEventLoading ? 'Posting Event...' : 'Add Event'}
</button>
{isEventError && <p>Error: {eventError?.message}</p>}
{isEventSuccess && <p>Event added successfully!</p>}
</div>
<div>
<button onClick={handleJobSubmit} disabled={isJobLoading}>
{isJobLoading ? 'Posting Job...' : 'Add Job'}
</button>
{isJobError && <p>Error: {jobError?.message}</p>}
{isJobSuccess && <p>Job added successfully!</p>}
</div>
</div>
);
};

export default Home;
22 changes: 22 additions & 0 deletions types/calendar/scheduleTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export interface EventSchedule {
classification: string;
eventTag: string;
author: string;
title: string;
content: string;
link: string;
startTime: string;
endTime: string;
}

export interface JobSchedule {
classification: string;
jobTag: string;
techTag: string;
author: string;
title: string;
content: string;
link: string;
startTime: string;
endTime: string;
}
17 changes: 17 additions & 0 deletions utils/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ const manageBaseURL = process.env.NEXT_PUBLIC_MANAGE_SERVER_ENDPOINT ?? '/';
const managePartyBaseURL =
process.env.NEXT_PUBLIC_PARTY_MANAGE_SERVER_ENDPOINT ?? '/';
const agendaBaseURL = process.env.NEXT_PUBLIC_AGENDA_SERVER_ENDPOINT ?? '/';
const calendarBaseURL = process.env.NEXT_PUBLIC_CALENDAR_SERVER_ENDPOINT ?? '/';

const instance = axios.create({ baseURL });
const instanceInManage = axios.create({ baseURL: manageBaseURL });
const instanceInPartyManage = axios.create({ baseURL: managePartyBaseURL });
const instanceInAgenda = axios.create({ baseURL: agendaBaseURL });
const instanceInCalendar = axios.create({ baseURL: calendarBaseURL });

instance.interceptors.request.use(
function setConfig(config) {
Expand Down Expand Up @@ -67,6 +69,20 @@ instanceInAgenda.interceptors.request.use(
}
);

instanceInCalendar.interceptors.request.use(
function setConfig(config) {
config.headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${localStorage.getItem('42gg-token')}`,
};
config.withCredentials = true;
return config;
},
function getError(error) {
return Promise.reject(error);
}
);

function isAxiosError<ErrorPayload>(
error: unknown
): error is AxiosError<ErrorPayload> {
Expand All @@ -78,5 +94,6 @@ export {
instanceInManage,
instanceInPartyManage,
instanceInAgenda,
instanceInCalendar,
isAxiosError,
};