Skip to content

Commit

Permalink
Merge pull request #51 from School-of-Company/chore/form-router-toast
Browse files Browse the repository at this point in the history
🔀 신청폼 라우팅 진행
  • Loading branch information
happytaeyoon authored Dec 3, 2024
2 parents 919960b + cc74ccb commit 6368e84
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
'use client';
import axios from 'axios';
import { useRouter } from 'next/navigation';
import { useForm } from 'react-hook-form';
import { toast } from 'react-toastify';
import { Input, RadioGroup } from '@/entities/application';
import { Button } from '@/shared/ui';
import { handleStandardFormsSubmit } from '@/widgets/application/model/applicationFormHandler';
import { StandardForms } from '../../../../types/type';

// import TrainingRadioGroup from '@/entities/application/ui/TrainingRadioGroup';

const StandardForm = ({ params }: { params: number }) => {
Expand All @@ -22,6 +25,7 @@ const StandardForm = ({ params }: { params: number }) => {
informationStatus: false,
},
});
const router = useRouter();

const yesNoOptions = [
{ value: 'yes', label: '예' },
Expand All @@ -31,25 +35,26 @@ const StandardForm = ({ params }: { params: number }) => {
const onSubmit = async (data: StandardForms) => {
try {
await handleStandardFormsSubmit(data, params);
toast.success('신청 되었습니다.');

const qrBody = {
phoneNumber: data.phoneNumber,
authority: 'ROLE_STANDARD',
};

const response = await axios.post('/api/sms/qr', qrBody);

console.log('QR SMS API Response:', response.data);
alert('QR SMS 요청이 성공적으로 처리되었습니다.');
toast.success('QR이 발송 되었습니다. 문자를 확인 해 주세요.');
router.back();
} catch (error) {
if (axios.isAxiosError(error)) {
const errorMessage =
error.response?.data?.message || '알 수 없는 오류가 발생했습니다.';
console.error('QR SMS API Error:', errorMessage);
alert(`QR SMS API 호출에 실패했습니다: ${errorMessage}`);
toast.error(`QR SMS API 호출에 실패했습니다: ${errorMessage}`);
} else {
console.error('폼 제출 중 에러 발생:', error);
alert('폼 제출에 실패했습니다. 다시 시도해주세요.');
toast.error('폼 제출에 실패했습니다. 다시 시도해주세요.');
}
}
};
Expand Down
47 changes: 32 additions & 15 deletions src/widgets/application/ui/form/ApplicationForm/trainee/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use client';
import axios from 'axios';
import { useRouter } from 'next/navigation';
import { useState } from 'react';
import { useForm } from 'react-hook-form';
import { toast } from 'react-toastify';
import { Input, RadioGroup } from '@/entities/application';
import TrainingRadioGroup from '@/entities/application/ui/TrainingRadioGroup';
import { Button } from '@/shared/ui';
Expand All @@ -28,6 +30,7 @@ const TraineeForm = ({ params }: { params: number }) => {
informationStatus: false,
},
});
const router = useRouter();

const schoolLevelOptions = [
{ value: '초등학교', label: '초등학교' },
Expand All @@ -43,24 +46,38 @@ const TraineeForm = ({ params }: { params: number }) => {
];

const onSubmit = async (data: TraineeForms) => {
await handleTraineeFormsSubmit(data, params);
if (trainingId && selectedValue) {
const result = await TrainingRadioGroup.handleTrainingSubmit(
selectedValue,
trainingId,
);
console.log('Training Submit Result:', result);
}
try {
await handleTraineeFormsSubmit(data, params);
toast.success('신청 되었습니다.');

const qrBody = {
phoneNumber: data.phoneNumber,
authority: 'ROLE_TRAINEE',
};
if (trainingId && selectedValue) {
const result = await TrainingRadioGroup.handleTrainingSubmit(
selectedValue,
trainingId,
);
console.log('Training Submit Result:', result);
}

const response = await axios.post('/api/sms/qr', qrBody);
const qrBody = {
phoneNumber: data.phoneNumber,
authority: 'ROLE_TRAINEE',
};

console.log('QR SMS API Response:', response.data);
alert('QR SMS 요청이 성공적으로 처리되었습니다.');
const response = await axios.post('/api/sms/qr', qrBody);
console.log('QR SMS API Response:', response.data);
toast.success('QR이 발송 되었습니다. 문자를 확인 해 주세요.');
router.back();
} catch (error) {
if (axios.isAxiosError(error)) {
const errorMessage =
error.response?.data?.message || '알 수 없는 오류가 발생했습니다.';
console.error('API Error:', errorMessage);
toast.error(`오류가 발생했습니다: ${errorMessage}`);
} else {
console.error('폼 제출 중 에러 발생:', error);
toast.error('폼 제출에 실패했습니다. 다시 시도해주세요.');
}
}
};

const handleTrainingIdChange = (
Expand Down

0 comments on commit 6368e84

Please sign in to comment.