|
| 1 | +import { observer } from 'mobx-react-lite' |
| 2 | +import { useReducer } from 'react' |
| 3 | +import { message } from 'react-message-popup' |
| 4 | + |
| 5 | +import { Input } from '@mx-space/kami-design' |
| 6 | +import { MdiEmailFastOutline } from '@mx-space/kami-design/components/Icons' |
| 7 | + |
| 8 | +import { apiClient } from '~/utils/client' |
| 9 | + |
| 10 | +import { useSubscribeStatus } from './query' |
| 11 | + |
| 12 | +interface SubscribeModalProps { |
| 13 | + onConfirm: () => void |
| 14 | +} |
| 15 | + |
| 16 | +const subscibeTextMap: Record<string, string> = { |
| 17 | + post_c: '文章', |
| 18 | + note_c: '笔记', |
| 19 | + say_c: '说说', |
| 20 | + recently_c: '速记', |
| 21 | +} |
| 22 | + |
| 23 | +const initialState = { |
| 24 | + email: '', |
| 25 | + types: { |
| 26 | + post_c: false, |
| 27 | + note_c: false, |
| 28 | + say_c: false, |
| 29 | + recently_c: false, |
| 30 | + }, |
| 31 | +} |
| 32 | + |
| 33 | +type Action = |
| 34 | + | { type: 'set'; data: Partial<typeof initialState> } |
| 35 | + | { type: 'reset' } |
| 36 | + |
| 37 | +const useFormData = () => { |
| 38 | + const [state, dispatch] = useReducer( |
| 39 | + (state: typeof initialState, payload: Action) => { |
| 40 | + switch (payload.type) { |
| 41 | + case 'set': |
| 42 | + return { ...state, ...payload.data } |
| 43 | + case 'reset': |
| 44 | + return initialState |
| 45 | + } |
| 46 | + }, |
| 47 | + { ...initialState }, |
| 48 | + ) |
| 49 | + return [state, dispatch] as const |
| 50 | +} |
| 51 | + |
| 52 | +export const SubscribeModal = observer<SubscribeModalProps>(({ onConfirm }) => { |
| 53 | + const [state, dispatch] = useFormData() |
| 54 | + |
| 55 | + const query = useSubscribeStatus() |
| 56 | + |
| 57 | + const handleSubList = async () => { |
| 58 | + if (!state.email) { |
| 59 | + message.error('请输入邮箱') |
| 60 | + return |
| 61 | + } |
| 62 | + if (Object.values(state.types).every((type) => !type)) { |
| 63 | + message.error('请选择订阅类型') |
| 64 | + return |
| 65 | + } |
| 66 | + const { email, types } = state |
| 67 | + await apiClient.subscribe.subscribe( |
| 68 | + email, |
| 69 | + Object.keys(types).filter((name) => state.types[name]) as any[], |
| 70 | + ) |
| 71 | + |
| 72 | + message.success('订阅成功') |
| 73 | + dispatch({ type: 'reset' }) |
| 74 | + onConfirm() |
| 75 | + } |
| 76 | + |
| 77 | + return ( |
| 78 | + <form action="#" onSubmit={handleSubList} className="flex flex-col gap-5"> |
| 79 | + <Input |
| 80 | + type="text" |
| 81 | + placeholder="留下你的邮箱哦 *" |
| 82 | + required |
| 83 | + prefix={<MdiEmailFastOutline />} |
| 84 | + value={state.email} |
| 85 | + onChange={(e) => { |
| 86 | + dispatch({ type: 'set', data: { email: e.target.value } }) |
| 87 | + }} |
| 88 | + /> |
| 89 | + <div className="flex gap-10"> |
| 90 | + {Object.keys(state.types) |
| 91 | + .filter((type) => query.data?.allowTypes.includes(type as any)) |
| 92 | + .map((name) => ( |
| 93 | + <fieldset |
| 94 | + className="inline-flex items-center children:cursor-pointer text-lg" |
| 95 | + key={name} |
| 96 | + > |
| 97 | + <input |
| 98 | + type="checkbox" |
| 99 | + onChange={(e) => { |
| 100 | + dispatch({ |
| 101 | + type: 'set', |
| 102 | + data: { |
| 103 | + types: { |
| 104 | + ...state.types, |
| 105 | + [name]: e.target.checked, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }) |
| 109 | + }} |
| 110 | + checked={state.types[name]} |
| 111 | + id={name} |
| 112 | + /> |
| 113 | + <label htmlFor={name} className="text-shizuku"> |
| 114 | + {subscibeTextMap[name]} |
| 115 | + </label> |
| 116 | + </fieldset> |
| 117 | + ))} |
| 118 | + </div> |
| 119 | + <button className="btn yellow">订阅</button> |
| 120 | + </form> |
| 121 | + ) |
| 122 | +}) |
0 commit comments