From 0ca6cb424335490475baa19b1caa21e53778392e Mon Sep 17 00:00:00 2001 From: 273do Date: Wed, 15 Apr 2026 14:27:03 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E3=83=96=E3=83=A9=E3=82=A6=E3=82=B6?= =?UTF-8?q?=E3=81=8B=E3=82=89email=E7=B5=8C=E7=94=B1=E3=81=AE=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E3=82=92=E3=83=AA=E3=82=AF=E3=82=A8=E3=82=B9=E3=83=88?= =?UTF-8?q?=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88=E3=81=86=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/app/components/ai-feedback.tsx | 3 ++- frontend/app/routes/home.tsx | 33 +++++++++++++++++++------ frontend/app/utils/api.ts | 24 ++++++++++++++++++ frontend/app/utils/types.ts | 16 +++++++++++- frontend/app/utils/use-extract-steps.ts | 4 +-- frontend/app/utils/use-via-email.ts | 18 ++++++++++++++ 6 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 frontend/app/utils/use-via-email.ts diff --git a/frontend/app/components/ai-feedback.tsx b/frontend/app/components/ai-feedback.tsx index 53cd9a3..7c56532 100644 --- a/frontend/app/components/ai-feedback.tsx +++ b/frontend/app/components/ai-feedback.tsx @@ -18,6 +18,7 @@ import { } from "./ui/select"; import { Separator } from "./ui/separator"; +import type { LanguagesType } from "~/utils/types"; import { useAIFeedback } from "~/utils/use-ai-feedback"; type props = { @@ -51,7 +52,7 @@ export const AIFeedback = ({ id, models }: props) => { feedbackTrigger({ id, llm: model, - lang: i18n.language as "ja" | "en", + lang: i18n.language as LanguagesType, }); // eslint-disable-next-line react-hooks/exhaustive-deps }, [model]); diff --git a/frontend/app/routes/home.tsx b/frontend/app/routes/home.tsx index 037c1ce..9977201 100644 --- a/frontend/app/routes/home.tsx +++ b/frontend/app/routes/home.tsx @@ -30,11 +30,13 @@ import { surveySchema, type SurveyAnswers, } from "~/core/survey-schema"; +import type { LanguagesType } from "~/utils/types"; import { useEstimateSleep } from "~/utils/use-estimate-sleep"; import { useExtractSteps } from "~/utils/use-extract-steps"; +import { useViaEmail } from "~/utils/use-via-email"; const Home = () => { - const { t } = useTranslation(); + const { t, i18n } = useTranslation(); const [file, setFile] = useState(null); const [emailValue, setEmailValue] = useState(""); @@ -57,6 +59,9 @@ const Home = () => { const { estimateSleepTrigger, isEstimateSleepMutating, estimateSleepData } = useEstimateSleep(); + // email経由の解析処理 + const { viaEmailTrigger } = useViaEmail(DEFAULT_EXTRACT_STEP_QUERY); + const isLoading = isExtractStepMutating || isEstimateSleepMutating; const isFormComplete = isValid && file; @@ -73,20 +78,34 @@ const Home = () => { setEmailValue(email || ""); + let bedtime_answer; + + if (bedtime > "03:00" && bedtime < "20:45") { + bedtime_answer = 1; + } else bedtime_answer = 0; + if (email) { console.log("Email provided:", email); + // + + await viaEmailTrigger({ + xmlFile: file, + req: { + answers: { + charging_before_bed_answer: Number(chargingBeforeBedAnswer), + carrying_a_smartphone_answer: Number(carryingASmartphoneAnswer), + bedtime_answer, + }, + lang: i18n.language as LanguagesType, + email_to: email, + }, + }); } else { try { const extractStepResult = await extractStepTrigger(file); const { id, step_data } = extractStepResult; - let bedtime_answer; - - if (bedtime > "03:00" && bedtime < "20:45") { - bedtime_answer = 1; - } else bedtime_answer = 0; - const result = await estimateSleepTrigger({ id, step_data, diff --git a/frontend/app/utils/api.ts b/frontend/app/utils/api.ts index 8b3bee1..bcdd859 100644 --- a/frontend/app/utils/api.ts +++ b/frontend/app/utils/api.ts @@ -5,6 +5,7 @@ import type { ExtractedStepsResponse, LLMFeedbackRequest, LLMFeedbackResponse, + ViaEmailArg, } from "./types"; import { API_ENDPOINT } from "~/core/constants"; @@ -86,3 +87,26 @@ export const postAIFeedbackRequest = async ( return data; }; + +/** + * email経由で解析〜フィードバックまでを行う + * @param path path + * @param { arg } bodyリクエスト + */ +export const postViaEmail = async ( + path: string, + { arg }: { arg: ViaEmailArg } +): Promise => { + const formData = new FormData(); + formData.append("xml_file", arg.xmlFile); + formData.append("req", JSON.stringify(arg.req)); + + const response = await fetch(`${API_ENDPOINT}${path}`, { + method: "POST", + body: formData, + }); + + if (!response.ok) { + throw new Error(`Request failed: ${response.status}`); + } +}; diff --git a/frontend/app/utils/types.ts b/frontend/app/utils/types.ts index 2c7b825..240df4e 100644 --- a/frontend/app/utils/types.ts +++ b/frontend/app/utils/types.ts @@ -46,11 +46,25 @@ export type EstimateSleepResponse = { models: string[]; }; +export type LanguagesType = "ja" | "en"; + +// email経由の解析関連のスキーマ +export type ViaEmailRequest = { + answers: Answers; + lang: LanguagesType; + email_to: string; +}; + +export type ViaEmailArg = { + xmlFile: File; + req: ViaEmailRequest; +}; + // フィードバック関連のスキーマ export type LLMFeedbackRequest = { id: string; llm: string; - lang: "ja" | "en"; + lang: LanguagesType; }; export type LLMFeedbackResponse = { diff --git a/frontend/app/utils/use-extract-steps.ts b/frontend/app/utils/use-extract-steps.ts index 1cbacd6..88e9f09 100644 --- a/frontend/app/utils/use-extract-steps.ts +++ b/frontend/app/utils/use-extract-steps.ts @@ -13,13 +13,13 @@ export type DateRangeParams = { endDateOfExtract: Date; }; -type ExtractStepsParams = MonthsParams | DateRangeParams; +export type ExtractStepsParams = MonthsParams | DateRangeParams; /** * クエリパラメータの作成 * @param params クエリパラメータ(月数指定 or 日付範囲指定) */ -const buildQuery = (params: ExtractStepsParams): string => { +export const buildQuery = (params: ExtractStepsParams): string => { if ("monthsOfExtract" in params) { return `months_of_extract=${params.monthsOfExtract}&include_recorded_sleep=${params.includeRecordedSleep}`; } diff --git a/frontend/app/utils/use-via-email.ts b/frontend/app/utils/use-via-email.ts new file mode 100644 index 0000000..b3d108b --- /dev/null +++ b/frontend/app/utils/use-via-email.ts @@ -0,0 +1,18 @@ +import useSWRMutation from "swr/mutation"; + +import { postViaEmail } from "./api"; +import { buildQuery, type ExtractStepsParams } from "./use-extract-steps"; + +/** + * email経由で解析〜フィードバック送信を行うフック + * @param params クエリパラメータ(月数指定 or 日付範囲指定) + */ +export const useViaEmail = (params: ExtractStepsParams) => { + const q = buildQuery(params); + + const { trigger } = useSWRMutation(`/via-email?${q}`, postViaEmail); + + return { + viaEmailTrigger: trigger, + }; +};