-
Notifications
You must be signed in to change notification settings - Fork 0
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
feature(admin): supabase 연결 및 설문 생성 로직 작성 #1
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,4 +41,5 @@ Thumbs.db | |
.nx/cache | ||
|
||
# env | ||
.env | ||
.env | ||
.env.local |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,68 @@ | ||
import { useEffect } from 'react'; | ||
import { Survey, useCreateSurvey } from '../hooks/query/useServey'; | ||
|
||
const payload: Survey = { | ||
title: '설문입니다.', | ||
sections: [ | ||
{ | ||
items: [ | ||
{ | ||
title: 'radio input 1번째', | ||
type: 'radio', | ||
required: true, | ||
options: [ | ||
{ text: 'radio option1 1번째' }, | ||
{ text: 'radio option2 1번째' }, | ||
{ text: 'radio option3 1번째' }, | ||
], | ||
}, | ||
{ | ||
title: 'checkbox input 1번째', | ||
type: 'radio', | ||
required: true, | ||
options: [ | ||
{ text: 'checkbox option1 1번째' }, | ||
{ text: 'checkbox option2 1번째' }, | ||
{ text: 'checkbox option3 1번째' }, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
items: [ | ||
{ | ||
title: 'select 1번째', | ||
type: 'select', | ||
required: true, | ||
options: [ | ||
{ | ||
text: 'select option1 1번째', | ||
}, | ||
{ | ||
text: 'select option2 1번째', | ||
}, | ||
{ | ||
text: 'select option3 1번째', | ||
}, | ||
], | ||
}, | ||
{ title: 'select 1번째', type: 'textarea', required: true }, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
export function App() { | ||
return <div>Admin!!</div>; | ||
const mutate = useCreateSurvey(); | ||
|
||
const onMutateSurvey = () => { | ||
mutate(payload); | ||
}; | ||
return ( | ||
<div> | ||
<h2>설문 만들기</h2> | ||
<button onClick={onMutateSurvey}>제출</button> | ||
</div> | ||
); | ||
} | ||
|
||
export default App; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { useSupabaseContext } from '@ssoon-servey/supabase'; | ||
|
||
interface Option { | ||
text: string; | ||
} | ||
|
||
interface Item { | ||
title: string; | ||
type: 'radio' | 'select' | 'checkbox' | 'textarea'; | ||
required: boolean; | ||
options?: Option[]; | ||
} | ||
|
||
interface Section { | ||
title?: string; | ||
items: Item[]; | ||
} | ||
|
||
export interface Survey { | ||
title: string; | ||
description?: string; | ||
sections: Section[]; | ||
} | ||
|
||
export const useCreateSurvey = () => { | ||
const { supabase } = useSupabaseContext(); | ||
|
||
const createSurvey = async (title: string, description?: string) => { | ||
const { data: survey } = await supabase | ||
.from('surveys') | ||
.insert([{ title, description }]) | ||
.select(); | ||
return survey; | ||
}; | ||
|
||
const createSections = async (serveyId: number, sections: Section[]) => { | ||
const { data: survey_section } = await supabase | ||
.from('survey_sections') | ||
.insert( | ||
sections.map((section) => ({ | ||
survey_title: section.title, | ||
survey_id: serveyId, | ||
})) | ||
) | ||
.select(); | ||
return survey_section; | ||
}; | ||
|
||
const createSurveyItems = async ( | ||
sectionIds: number[], | ||
sections: Section[] | ||
) => { | ||
const insertItems: { | ||
section_id: number; | ||
question_title: string; | ||
question_type: 'radio' | 'select' | 'checkbox' | 'textarea'; | ||
question_required: boolean; | ||
hasOption: boolean; | ||
}[] = []; | ||
|
||
sectionIds.forEach((id, i) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p3: forEach가 아니라 map으로 함수를 반환하게 사용할 수 없을까요? 사소하지만 함수형으로 사용하도록 노력하면 명시적으로 작성할 수 있을 것 같아요 |
||
sections[i].items.forEach((item) => | ||
insertItems.push({ | ||
section_id: id, | ||
question_title: item.title, | ||
question_type: item.type, | ||
question_required: item.required, | ||
hasOption: !!item.options, | ||
}) | ||
); | ||
}); | ||
|
||
const { data: survey_items } = await supabase | ||
.from('survey_items') | ||
.insert(insertItems) | ||
.select(); | ||
|
||
return survey_items; | ||
}; | ||
|
||
const createItemOptions = async ( | ||
surveyItemIds: number[], | ||
sections: Section[] | ||
) => { | ||
const insertOptions: { item_id: number; option_text: string }[] = []; | ||
|
||
surveyItemIds.forEach((id, i) => { | ||
const options = sections.flatMap((section) => | ||
section.items.map((item) => item.options) | ||
); | ||
options[i]?.forEach((option) => { | ||
insertOptions.push({ | ||
item_id: id, | ||
option_text: option.text, | ||
}); | ||
}); | ||
}); | ||
|
||
await supabase.from('question_options').insert(insertOptions).select(); | ||
}; | ||
|
||
const mutate = async (payload: Survey) => { | ||
const { title, description, sections } = payload; | ||
|
||
const survey = await createSurvey(title, description); | ||
if (!survey) return; | ||
|
||
const survey_section = await createSections(survey[0].id, sections); | ||
if (!survey_section) return; | ||
|
||
const survey_items = await createSurveyItems( | ||
survey_section.map((section) => section.id), | ||
sections | ||
); | ||
if (!survey_items) return; | ||
|
||
await createItemOptions( | ||
survey_items.map((item) => item.id), | ||
sections | ||
); | ||
}; | ||
|
||
return mutate; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { StrictMode } from 'react'; | ||
import * as ReactDOM from 'react-dom/client'; | ||
import { SupabaseProvider } from '@ssoon-servey/supabase'; | ||
|
||
import App from './app/app'; | ||
|
||
|
@@ -8,6 +9,8 @@ const root = ReactDOM.createRoot( | |
); | ||
root.render( | ||
<StrictMode> | ||
<App /> | ||
<SupabaseProvider supabaseKey={import.meta.env.VITE_SUPABASE_KEY}> | ||
<App /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. p3: 가장 최상단에서 ErrorBoundary로 묶어서 처리하면 이후 에러가 전파되는 상황에서 에러를 처리하기가 좋습니닷 |
||
</SupabaseProvider> | ||
</StrictMode> | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/// <reference types="vite/client" /> | ||
|
||
interface ImportMetaEnv { | ||
readonly VITE_SUPABASE_KEY: string; | ||
} | ||
|
||
interface ImportMeta { | ||
readonly env: ImportMetaEnv; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"presets": [ | ||
[ | ||
"@nx/react/babel", | ||
{ | ||
"runtime": "automatic", | ||
"useBuiltIns": "usage" | ||
} | ||
] | ||
], | ||
"plugins": [] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"extends": ["plugin:@nx/react", "../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.ts", "*.tsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.js", "*.jsx"], | ||
"rules": {} | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# supabase | ||
|
||
This library was generated with [Nx](https://nx.dev). | ||
|
||
## Running unit tests | ||
|
||
Run `nx test supabase` to execute the unit tests via [Vitest](https://vitest.dev/). |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "@ssoon-servey/supabase", | ||
"version": "0.0.1", | ||
"main": "./index.js", | ||
"types": "./index.d.ts", | ||
"exports": { | ||
".": { | ||
"import": "./index.mjs", | ||
"require": "./index.js" | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "supabase", | ||
"$schema": "../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "libs/supabase/src", | ||
"projectType": "library", | ||
"tags": [], | ||
"targets": {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './supabase'; | ||
export * from './types'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { createContext, useContext } from 'react'; | ||
import { createClient } from '@supabase/supabase-js'; | ||
import type { Database } from '../types'; | ||
|
||
export type SupabaseContextValue = { | ||
supabase: ReturnType<typeof createClient<Database>>; | ||
}; | ||
|
||
export const SupabaseContext = createContext<SupabaseContextValue | null>(null); | ||
SupabaseContext.displayName = 'SupabaseContext'; | ||
|
||
export const SupabaseProvider = ({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. supabase를 컨텍스트로 묶으신 이유가 있을까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. web,admin 프로젝트에서 supabase 인스턴스 얻는 코드를 공통으로 사용하고 싶어서 묶었습니다! |
||
children, | ||
supabaseKey, | ||
}: { | ||
children: React.ReactNode; | ||
supabaseKey: string; | ||
}) => { | ||
const supabaseUrl = 'https://tmeoawlhihzuoqxebciw.supabase.co'; | ||
const supabase = createClient<Database>(supabaseUrl, supabaseKey); | ||
|
||
return ( | ||
<SupabaseContext.Provider value={{ supabase }}> | ||
{children} | ||
</SupabaseContext.Provider> | ||
); | ||
}; | ||
|
||
export const useSupabaseContext = () => { | ||
const context = useContext(SupabaseContext); | ||
|
||
if (context === null) { | ||
throw new Error( | ||
'useSupabaseContext must be used within a <SupabaseProvider />' | ||
); | ||
} | ||
return context; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
p4: 직접 연결되는 컴포넌트 네이밍은 handle-
props로 넘길때는 on- 각각 접두사를 사용하면 이후 명확하게 알 수 있어요
https://youngmin.hashnode.dev/react-1