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

Feat/active #12

Merged
merged 8 commits into from
Aug 19, 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
Binary file added public/png/mood_BAD.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/png/mood_GOOD.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions public/svg/add.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 24 additions & 44 deletions src/features/setting/Container.tsx
Original file line number Diff line number Diff line change
@@ -1,54 +1,34 @@
import { Controller } from 'react-hook-form';
import { useAtom } from 'jotai';
import { useLayoutEffect } from 'react';

import { bottomBtnAtom, titleAtom } from '../shared/layout/atom';

import './style/index.scss';
import { colorset } from './constants';
import useSetting from './hooks/useSetting';
import './style/index.scss';
import PreviewSection from './components/PreviewSection';
import SubmitForm from './components/SubmitForm';

const Container = () => {
const { register, handleSubmit, control, onSubmit } = useSetting();
const [, setTitle] = useAtom(titleAtom);
const [, setBottomBtn] = useAtom(bottomBtnAtom);

return (
<div className="setting-container">
<form onSubmit={handleSubmit(onSubmit)}>
<div>
<span>Today's comment: </span>
<input defaultValue="퇴근하고싶다" {...register('comment')} />
</div>
const { handlePreviewClick, handleSubmit, onSubmit, register, control } =
useSetting();

<div>
{/* <select {...register('mude')}>
{colorset.map((color) => (
<option key={color} value={color}>{color}</option>
))}
</select> */}
<Controller
name="mude"
control={control}
rules={{ required: true }}
render={({ field }) => (
<fieldset {...field}>
{colorset.map((color) => (
<label htmlFor={color} key={color}>
<input
type="radio"
id={color}
name={field.name}
value={color}
checked={field.value === color}
onChange={(e) => {
field.onChange(e.target.value);
}}
/>
{color}
</label>
))}
</fieldset>
)}
/>
</div>
useLayoutEffect(() => {
setTitle({ title: 'Active', back: true });
setBottomBtn({ text: 'Save' });
}, []);

<input type="submit" />
</form>
return (
<div className="setting-container">
<PreviewSection handlePreviewClick={handlePreviewClick} />
<SubmitForm
handleSubmit={handleSubmit}
onSubmit={onSubmit}
register={register}
control={control}
/>
</div>
);
};
Expand Down
23 changes: 23 additions & 0 deletions src/features/setting/components/PreviewSection.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { FunctionComponent } from 'react';

import { previewSubText, previewTitleText } from '../constants';

type PreviewSectionProps = {
handlePreviewClick: () => void;
};

const PreviewSection: FunctionComponent<PreviewSectionProps> = ({
handlePreviewClick,
}) => {
return (
<div className="setting-preview-container">
<div className="setting-preview-title-text">{previewTitleText}</div>
<div className="setting-preview-sub-text">{previewSubText}</div>
<button className="setting-preview-btn" onClick={handlePreviewClick}>
Preview
</button>
</div>
);
};

export default PreviewSection;
54 changes: 54 additions & 0 deletions src/features/setting/components/SubmitForm/DailySticker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { FunctionComponent } from 'react';
import { Controller } from 'react-hook-form';
import { Control } from 'react-hook-form';

import { Inputs } from '../../types';
import { stickerList, wordStickerText } from '../../constants';

import '../../style/index.scss';

type DailyStickerProps = {
control: Control<Inputs, any>;
};

const DailySticker: FunctionComponent<DailyStickerProps> = ({ control }) => {
return (
<div className="setting-sticker-container">
<span className="setting-sticker-title">{wordStickerText}</span>
<Controller
name="mude"
control={control}
rules={{ required: true }}
render={({ field }) => (
<div className="setting-sticker-box" {...field}>
{stickerList.map((sticker) => (
<label
htmlFor={sticker}
key={sticker}
className="setting-sticker-width"
>
<input
type="radio"
id={sticker}
name={field.name}
value={sticker}
checked={field.value === sticker}
onChange={(e) => {
field.onChange(e.target.value);
}}
/>
<img
src={`/png/${sticker}.png`}
className="logo react"
alt="React logo"
/>
</label>
))}
</div>
)}
/>
</div>
);
};

export default DailySticker;
21 changes: 21 additions & 0 deletions src/features/setting/components/SubmitForm/DailyWord.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { FunctionComponent } from 'react';
import { UseFormRegister } from 'react-hook-form';

import { Inputs } from '../../types';
import { wordTitleText } from '../../constants';
import '../../style/index.scss';

type DailyWordProps = {
register: UseFormRegister<Inputs>;
};

const DailyWord: FunctionComponent<DailyWordProps> = ({ register }) => {
return (
<div className="setting-word-container">
<span className="setting-word-title">{wordTitleText}</span>
<input className="setting-word-input" {...register('comment')} />
</div>
);
};

export default DailyWord;
48 changes: 48 additions & 0 deletions src/features/setting/components/SubmitForm/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { useAtom } from 'jotai';
import { FunctionComponent, useEffect, useRef } from 'react';
import {
UseFormHandleSubmit,
SubmitHandler,
UseFormRegister,
Control,
} from 'react-hook-form';

import { activeSaveAtom } from '../../../shared/layout/atom';
import { Inputs } from '../../types';

import DailySticker from './DailySticker';
import DailyWord from './DailyWord';

type SubmitFormProps = {
handleSubmit: UseFormHandleSubmit<Inputs, undefined>;
onSubmit: SubmitHandler<Inputs>;
register: UseFormRegister<Inputs>;
control: Control<Inputs, any>;
};

const SubmitForm: FunctionComponent<SubmitFormProps> = ({
handleSubmit,
onSubmit,
register,
control,
}) => {
const [activeSave, setActiveSave] = useAtom(activeSaveAtom);

const inputRef = useRef<HTMLInputElement>(null);

useEffect(() => {
if (!activeSave) return;
setActiveSave(false);
inputRef.current?.click();
}, [activeSave]);

return (
<form onSubmit={handleSubmit(onSubmit)}>
<DailyWord register={register} />
<DailySticker control={control} />
<input ref={inputRef} type="submit" style={{ visibility: 'hidden' }} />
</form>
);
};

export default SubmitForm;
8 changes: 8 additions & 0 deletions src/features/setting/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
export const colorset = ['red', 'yellow', 'green'];
export const stickerList = ['mood_BAD', 'mood_GOOD'];

export const previewTitleText = 'What is the Active Setting?';
export const previewSubText =
'Congratulations on winning! Press the button to get a giftCongratulations on winning! Press the button to get a giftCongratulations on winning!';

export const wordTitleText = 'Word of the day';
export const wordStickerText = 'Word of the Sticker';
12 changes: 10 additions & 2 deletions src/features/setting/hooks/useSetting.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import { useForm, SubmitHandler } from 'react-hook-form';
import { useNavigate } from 'react-router-dom';

import { Inputs } from '../types';
import { URL } from '../../shared/constants/url';

const useSetting = () => {
const { register, handleSubmit, control } = useForm<Inputs>({
defaultValues: {
mude: 'green',
comment: '퇴근!',
mude: '',
comment: '',
},
});
const navigate = useNavigate();
const onSubmit: SubmitHandler<Inputs> = (data) => console.log(data);

const handlePreviewClick = () => {
navigate(URL.preview);
};

return {
register,
handleSubmit,
control,
onSubmit,
handlePreviewClick,
};
};

Expand Down
Loading