Skip to content
Merged
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
3 changes: 2 additions & 1 deletion frontend/app/components/ai-feedback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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]);
Expand Down
33 changes: 26 additions & 7 deletions frontend/app/routes/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<File | null>(null);
const [emailValue, setEmailValue] = useState<string>("");

Expand All @@ -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;

Expand All @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions frontend/app/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
ExtractedStepsResponse,
LLMFeedbackRequest,
LLMFeedbackResponse,
ViaEmailArg,
} from "./types";

import { API_ENDPOINT } from "~/core/constants";
Expand Down Expand Up @@ -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<void> => {
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}`);
}
};
16 changes: 15 additions & 1 deletion frontend/app/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions frontend/app/utils/use-extract-steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}
Expand Down
18 changes: 18 additions & 0 deletions frontend/app/utils/use-via-email.ts
Original file line number Diff line number Diff line change
@@ -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,
};
};
Loading