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

回答が送信できないのを修正 #300

Merged
merged 6 commits into from
Dec 29, 2023
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
2 changes: 1 addition & 1 deletion src/app/admin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getCachedToken } from '@/features/user/api/mcToken';
import styles from '../page.module.css';

const Home = async () => {
const token = getCachedToken() ?? '';
const token = (await getCachedToken()) ?? '';
const answers = await getAllAnswers(token);
return (
<main className={styles['main']}>
Expand Down
2 changes: 1 addition & 1 deletion src/app/forms/[formId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import AnswerForm from '@/features/form/components/AnswerForm';
import { getCachedToken } from '@/features/user/api/mcToken';

const Home = async ({ params }: { params: { formId: number } }) => {
const token = getCachedToken() ?? '';
const token = (await getCachedToken()) ?? '';
const questions = await getFormQuestions(params.formId, token);

return <AnswerForm questions={questions} formId={params.formId} />;
Expand Down
2 changes: 1 addition & 1 deletion src/app/forms/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import FormList from '@/features/form/components/FormList';
import { getCachedToken } from '@/features/user/api/mcToken';

const Home = async () => {
const token = getCachedToken() ?? '';
const token = (await getCachedToken()) ?? '';
const forms = await getForms(token);
return <FormList forms={forms} />;
};
Expand Down
5 changes: 2 additions & 3 deletions src/features/form/api/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,10 @@ export const postAnswers = async (
token: string
): Promise<boolean> => {
const answersJson = JSON.stringify({
uuid: '3fa85f64-5717-4562-b3fc-2c963f66afa6', //todo: user側の処理を実装したら書き換える
timestamp: new Date(),
form_id,
form_id: Number(form_id),
answers,
});

const response = await fetch(`http://localhost:9000/forms/answers`, {
method: 'POST',
headers: {
Expand Down
2 changes: 1 addition & 1 deletion src/features/form/components/AnswerForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const AnswerForm = ({ questions: questions, formId }: Props) => {
}
});

const token = getCachedToken() ?? '';
const token = (await getCachedToken()) ?? '';
if (await postAnswers(formId, formAnswers, token)) {
toggleIsSubmitted(true);
reset();
Expand Down
10 changes: 7 additions & 3 deletions src/features/user/api/mcToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import type { RequestCookies } from 'next/dist/compiled/@edge-runtime/cookies';

const KEY = 'SEICHI_PORTAL__MC_TOKEN';

export const getCachedToken = (cookie?: RequestCookies) => {
const cache = cookie ? cookie.get(KEY) : cookies().get(KEY);
export const getCachedToken = (
cookie?: RequestCookies
): Promise<string | undefined> => {
return new Promise((resolve, _reject) => {
const cache = cookie ? cookie.get(KEY) : cookies().get(KEY);

return cache?.value;
resolve(cache?.value);
});
};

export const saveTokenToCache = ({
Expand Down
4 changes: 2 additions & 2 deletions src/features/user/components/AuthenticatedTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { ReactNode } from 'react';

type Props = { children: ReactNode };

export const AuthenticatedTemplate = ({ children }: Props) => {
const isAuthenticated = !!getCachedToken();
export const AuthenticatedTemplate = async ({ children }: Props) => {
const isAuthenticated = !!(await getCachedToken());

return isAuthenticated ? <>{children}</> : null;
};
4 changes: 2 additions & 2 deletions src/features/user/components/UnauthenticatedTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { ReactNode } from 'react';

type Props = { children: ReactNode };

export const UnauthenticatedTemplate = ({ children }: Props) => {
const isUnauthenticated = !getCachedToken();
export const UnauthenticatedTemplate = async ({ children }: Props) => {
const isUnauthenticated = !(await getCachedToken());

return isUnauthenticated ? <>{children}</> : null;
};
4 changes: 2 additions & 2 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NextResponse } from 'next/server';
import { getCachedToken } from '@/features/user/api/mcToken';
import type { NextRequest } from 'next/server';

export const middleware = (request: NextRequest) => {
export const middleware = async (request: NextRequest) => {
if (request.method !== 'GET') {
return;
}
Expand All @@ -11,7 +11,7 @@ export const middleware = (request: NextRequest) => {
return;
}

if (!!getCachedToken(request.cookies)) {
if (!!(await getCachedToken(request.cookies))) {
return;
}

Expand Down