๐Ÿ’ป์›น(Web)

์ฝ”๋“œ ๋ฆฌํŒฉํ† ๋ง

stonesy 2024. 12. 20. 23:57
728x90

๐Ÿ’ฌ์‹ค๋ฌด์—์„œ ํด๋ฆฐ์ฝ”๋“œ๋ž€? = ์œ ์ง€๋ณด์ˆ˜ ์‹œ๊ฐ„์˜ ๋‹จ์ถ•

  1. ํ๋ฆ„ ํŒŒ์•…์ด ์–ด๋ ต๊ณ 
  2. ๋„๋ฉ”์ธ ๋งฅ๋ฝ ํ‘œํ˜„์ด ์•ˆ ๋˜์–ด
  3. ๋™๋ฃŒ์—๊ฒŒ ๋ฌผ์–ด๋ด์•ผ ์•Œ ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ

 

๐Ÿ’ฌ๊ธฐ์กด ์ฝ”๋“œ ํŒŒ์•…

function QuestionPage(){
  const [popupOpened, setPopupOpened] = useState(false); // ์ถ”๊ฐ€์ฝ”๋“œ
  
  async function handleQuestionSubmit(){
		// ์ถ”๊ฐ€์ฝ”๋“œ start
	  const ์—ฐ๊ฒฐ์ „๋ฌธ๊ฐ€ = await ์—ฐ๊ฒฐ์ „๋ฌธ๊ฐ€_๋ฐ›์•„์˜ค๊ธฐ();
	  if(์—ฐ๊ฒฐ์ „๋ฌธ๊ฐ€ !== null){
		  setPopupOpened(true);
	  }
	  // ์ถ”๊ฐ€์ฝ”๋“œ end
		const ์•ฝ๊ด€๋™์˜ = await ์•ฝ๊ด€๋™์˜_๋ฐ›์•„์˜ค๊ธฐ();
		if(!์•ฝ๊ด€๋™์˜){
			await ์•ฝ๊ด€๋™์˜_๋ฐ›์•„์˜ค๊ธฐ();
		}
		await ์งˆ๋ฌธ์ „์†ก(questionValue);
		alert("์งˆ๋ฌธ์ด ๋“ฑ๋ก๋˜์—ˆ์–ด์š”.");
	}
	
	async function handleMyExpertQuestionSubmit(){
		await ์—ฐ๊ฒฐ์ „๋ฌธ๊ฐ€_์งˆ๋ฌธ์ „์†ก(questionValue, ์—ฐ๊ฒฐ์ „๋ฌธ๊ฐ€.id);
		alert(`${์—ฐ๊ฒฐ์ „๋ฌธ๊ฐ€.name}์—๊ฒŒ ์งˆ๋ฌธ์ด ๋“ฑ๋ก๋˜์—ˆ์–ด์š”.`);
	}
	
	return {
		<main>
			<form>
				<textarea palceholder="์–ด๋–ค ๋‚ด์šฉ์ด ๊ถ๊ธˆํ•œ๊ฐ€์š”?"/>
				<Button onClick={handleQuestionSubmit}>์งˆ๋ฌธํ•˜๊ธฐ</Button>
			</form>
		</main>
}

๋ฌธ์ œ1. ํ•˜๋‚˜์˜ ๋ชฉ์ ์ธ ์ฝ”๋“œ๊ฐ€ ํฉ๋ฟŒ๋ ค์ ธ ์žˆ๋‹ค.

๋ฌธ์ œ2. ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ผ์„ ํ•˜๊ณ  ์žˆ๋‹ค.

์„ธ๋ถ€ ๊ตฌํ˜„์„ ๋ชจ๋‘ ์ฝ์–ด์•ผ ํ•จ์ˆ˜์˜ ์—ญํ• ์„ ์•Œ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

๋ฌธ์ œ3. ํ•จ์ˆ˜์˜ ์„ธ๋ถ€ ๊ตฌํ˜„ ๋‹จ๊ณ„๊ฐ€ ์ œ๊ฐ๊ฐ์ด๋‹ค.

 

๐Ÿ’ฌ์ฝ”๋“œ ๋ฆฌํŒฉํ† ๋ง V1

๋ฌธ์ œ3. ํ•จ์ˆ˜์˜ ์„ธ๋ถ€ ๊ตฌํ˜„ ๋‹จ๊ณ„๊ฐ€ ์ œ๊ฐ๊ฐ์ด๋‹ค. → ํ•จ์ˆ˜์˜ ์„ธ๋ถ€ ๊ตฌํ˜„ ๋‹จ๊ณ„ ํ†ต์ผ (์ถ”์ƒํ™”)

async function handleNewExpertQuestionSubmit(){
	await ์งˆ๋ฌธ์ „์†ก(questionValue);
	alert("์งˆ๋ฌธ์ด ๋“ฑ๋ก๋˜์—ˆ์–ด์š”.");
}

async function handleMyExpertQuestionSubmit(){
	await ์—ฐ๊ฒฐ์ „๋ฌธ๊ฐ€_์งˆ๋ฌธ์ „์†ก(questionValue, ์—ฐ๊ฒฐ์ „๋ฌธ๊ฐ€.id);
	alert(`${์—ฐ๊ฒฐ์ „๋ฌธ๊ฐ€.name}์—๊ฒŒ ์งˆ๋ฌธ์ด ๋“ฑ๋ก๋˜์—ˆ์–ด์š”.`);
}

๋ฌธ์ œ1. ํ•˜๋‚˜์˜ ๋ชฉ์ ์ธ ์ฝ”๋“œ๊ฐ€ ํฉ๋ฟŒ๋ ค์ ธ ์žˆ๋‹ค. → ํ•˜๋‚˜์˜ ๋ชฉ์ ์ธ ์ฝ”๋“œ๋Š” ๋ญ‰์ณ๋‘๊ธฐ (์‘์ง‘)

return(
	<main>
		<form>
			<textarea placeholder="์–ด๋–ค ๋‚ด์šฉ์ด ๊ถ๊ธˆํ•œ๊ฐ€์š”?" />
			{์—ฐ๊ฒฐ์ „๋ฌธ๊ฐ€.connected ? (
				<PopupTriggerButton
					popup={(
						<์—ฐ๊ฒฐ์ „๋ฌธ๊ฐ€ํŒ์—…
						onButtonSubmit={handleMyExpertQuestionSubmit} />
					)}
				>
					์งˆ๋ฌธํ•˜๊ธฐ </PopupTriggerButton>
				) : ( ... )
		</form>
	</main>

๋ฌธ์ œ2. ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ผ์„ ํ•˜๊ณ  ์žˆ๋‹ค. → ํ•จ์ˆ˜๊ฐ€ ํ•œ ๊ฐ€์ง€ ์ผ๋งŒ ํ•˜๋„๋ก ์ชผ๊ฐœ๊ธฐ (๋‹จ์ผ์ฑ…์ž„)

: <Button onClick={async() => {
		await openPopupToNotAgreedUsers();
		await handleMyExpertQuestionSubmit();
	}}
	>
	์งˆ๋ฌธํ•˜๊ธฐ
	</Button>

...

async function openPopupToNotAgreedUsers(){
	const ์•ฝ๊ด€๋™์˜ = await ์•ฝ๊ด€๋™์˜_๋ฐ›์•„์˜ค๊ธฐ();
	if(!์•ฝ๊ด€๋™์˜) { 
		await ์•ฝ๊ด€๋™์˜_ํŒ์—…์—ด๊ธฐ();
	}
}

*ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ

(arg1, args, ...) => { ... }

 

๐Ÿ’ฌ์ฝ”๋“œ ๋ฆฌํŒฉํ† ๋ง V2

function QuestionPage(){
	const [openPopup] = usePopup();
	async function handleClick() {
		const confirmed = await openPopup({
			title: "๋ณดํ—˜ ์งˆ๋ฌธํ•˜๊ธฐ",
			contents: <div>์ „๋ฌธ๊ฐ€๊ฐ€ ์„ค๋ช…๋“œ๋ ค์š”</div>,
		});
		if(confirmed){
			await submitQuestion();
		}
	}
	async funcion submitQuestion(์—ฐ๊ฒฐ์ „๋ฌธ๊ฐ€){
		await ์งˆ๋ฌธ์ „์†ก(์—ฐ๊ฒฐ์ „๋ฌธ๊ฐ€.id);
		alert("์งˆ๋ฌธ์„ ์ „์†กํ–ˆ์Šต๋‹ˆ๋‹ค.");
	}
	return <button onClick={handleClick}>์งˆ๋ฌธํ•˜๊ธฐ</button>;
}

 

๐Ÿ’ฌ์ถœ์ฒ˜

https://velog.io/@mari/SLASH21-์‹ค๋ฌด์—์„œ-๋ฐ”๋กœ-์“ฐ๋Š”-Frontend-Clean-Code-ํ† ์Šค๊ฐœ๋ฐœ์ž์ปจํผ๋Ÿฐ์Šค-2021

 

[SLASH21] ์‹ค๋ฌด์—์„œ ๋ฐ”๋กœ ์“ฐ๋Š” Frontend Clean Code - ํ† ์Šค ๊ฐœ๋ฐœ์ž ์ปจํผ๋Ÿฐ์Šค 2021

https://toss.im/slash-21/sessions/3-3 !youtube[edWbHpk9Y] ์ €๋Š” ํ† ์Šค ์•ฑ์˜ '๋ณดํ—˜' ๊ด€๋ จ๋œ ๋Œ€๋‹ค์ˆ˜ ์„œ๋น„์Šค๋ฅผ ์›น๋ทฐ๋กœ ๊ฐœ๋ฐœํ–ˆ์Šต๋‹ˆ๋‹ค. ํด๋ฆฐ์ฝ”๋“œ๋ผ ํ•˜๋ฉด '๋ช…ํ™•ํ•œ ์ด๋ฆ„', '์ค‘๋ณต ์ค„์ด๊ธฐ'๋ฅผ ๋จผ์ € ์ƒ๊ฐํ•˜๋Š”๋ฐ์š”, ์‹ค๋ฌด์—์„ 

velog.io

 

728x90

'๐Ÿ’ป์›น(Web)' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Spring  (0) 2024.12.02
JSP(Jakarta Server Pages)  (0) 2024.12.01
Servlet  (0) 2024.12.01
[๊ณตํ†ตํ”„๋กœ์ ํŠธ]5์ฃผ์ฐจ: React Openvidu ๊ตฌํ˜„  (0) 2024.08.11
[React]์ดˆ๊ธฐ์„ธํŒ…  (0) 2024.07.18