217 lines
9.9 KiB
TypeScript
217 lines
9.9 KiB
TypeScript
import { clearTokens } from '@nalssigak/shared/api/auth';
|
|
import { listBlockedOptions, listBlockedQueryKey, meQueryKey, unblockMutation, updateMeMutation, withdrawMutation } from '@nalssigak/shared/api/client/@tanstack/react-query.gen';
|
|
import { AVATAR_COLORS, CHARACTERS, MOODS } from '@nalssigak/shared/lib';
|
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
import { useState } from 'react';
|
|
import { Pressable, View } from 'react-native';
|
|
import { Text } from '@/components/text';
|
|
|
|
import { Avatar, Ic } from '@/components/ui';
|
|
import { useAuth } from '@/lib/auth';
|
|
|
|
import { isCommentList } from './comments';
|
|
import { Body, Field, useSoft } from './parts';
|
|
|
|
/** 탈퇴: 한 번 더 묻고 지운다. 계정이 없어지니 이 기기 토큰도 지운다 */
|
|
function Withdraw({ onDone }: { onDone: () => void }) {
|
|
const withdraw = useMutation(withdrawMutation());
|
|
const [asking, setAsking] = useState(false);
|
|
const [failed, setFailed] = useState(false);
|
|
if (!asking)
|
|
return (
|
|
<Pressable onPress={() => setAsking(true)} hitSlop={8} className="self-center p-1">
|
|
<Text className="text-[13px] font-semibold text-ink3">탈퇴하기</Text>
|
|
</Pressable>
|
|
);
|
|
const run = () => {
|
|
setFailed(false);
|
|
withdraw.mutate(
|
|
{},
|
|
{
|
|
onSuccess: (res) => {
|
|
if (!res.result?.success) return setFailed(true);
|
|
clearTokens();
|
|
onDone();
|
|
},
|
|
onError: () => setFailed(true),
|
|
},
|
|
);
|
|
};
|
|
return (
|
|
<View className="gap-2.5 rounded-[18px] p-3.5" style={{ backgroundColor: 'rgb(255 59 92 / 0.08)' }} accessibilityRole="alert">
|
|
<Text className="text-center text-sm font-bold text-ink">{'탈퇴하면 계정과 남긴 댓글·좋아요가\n모두 지워져요. 되돌릴 수 없어요.'}</Text>
|
|
<View className="flex-row gap-2">
|
|
<Pressable onPress={() => setAsking(false)} disabled={withdraw.isPending} className="flex-1 rounded-[14px] bg-card p-3">
|
|
<Text className="text-center text-sm font-extrabold text-ink">그만둘게요</Text>
|
|
</Pressable>
|
|
<Pressable onPress={run} disabled={withdraw.isPending} className="flex-1 rounded-[14px] bg-heart p-3 disabled:opacity-60">
|
|
<Text className="text-center text-sm font-extrabold text-white">탈퇴할게요</Text>
|
|
</Pressable>
|
|
</View>
|
|
{failed && <Text className="text-center text-[13px] font-bold text-heart">탈퇴하지 못했어요. 잠시 뒤에 다시 해 주세요.</Text>}
|
|
</View>
|
|
);
|
|
}
|
|
|
|
/** 차단한 사람: 있을 때만 보이고, 풀면 그 사람 댓글이 다시 보인다 */
|
|
function Blocked() {
|
|
const s = useSoft();
|
|
const queryClient = useQueryClient();
|
|
const list = useQuery(listBlockedOptions());
|
|
const unblock = useMutation(unblockMutation());
|
|
const [failed, setFailed] = useState(false);
|
|
const users = list.data?.users ?? [];
|
|
if (!users.length) return null;
|
|
const release = (user_id: string) => {
|
|
setFailed(false);
|
|
unblock.mutate(
|
|
{ path: { user_id } },
|
|
{
|
|
onSuccess: (res) => {
|
|
if (!res.result?.success) return setFailed(true);
|
|
queryClient.invalidateQueries({ queryKey: listBlockedQueryKey() });
|
|
queryClient.invalidateQueries({ predicate: isCommentList });
|
|
},
|
|
onError: () => setFailed(true),
|
|
},
|
|
);
|
|
};
|
|
return (
|
|
<View accessibilityLabel="차단한 사람">
|
|
<Text className="mb-2 text-sm font-extrabold text-ink">차단한 사람</Text>
|
|
<View className="gap-2">
|
|
{users.map((u) => (
|
|
<View key={u.user_id} className="flex-row items-center gap-2.5">
|
|
<Avatar character={u.character} color={u.color} />
|
|
<Text className="flex-1 text-sm font-bold text-ink">{u.nickname}</Text>
|
|
<Pressable onPress={() => release(u.user_id)} disabled={unblock.isPending} className="rounded-[12px] px-3 py-2 disabled:opacity-50" style={{ backgroundColor: s.soft }}>
|
|
<Text className="text-[13px] font-extrabold text-ink">풀기</Text>
|
|
</Pressable>
|
|
</View>
|
|
))}
|
|
</View>
|
|
{failed && <Text className="mt-1.5 text-[13px] font-bold text-heart">잠시 뒤에 다시 해 주세요.</Text>}
|
|
</View>
|
|
);
|
|
}
|
|
|
|
/** 내 정보: 캐릭터·배경색·기분·닉네임·로그아웃·차단한 사람·탈퇴 */
|
|
export function MeSheet({ onDone }: { onDone: () => void }) {
|
|
const { me, logout } = useAuth();
|
|
const s = useSoft();
|
|
const queryClient = useQueryClient();
|
|
const update = useMutation(updateMeMutation());
|
|
const [nickname, setNickname] = useState(me?.nickname ?? '');
|
|
const [error, setError] = useState('');
|
|
if (!me) return null;
|
|
|
|
const save = (body: { nickname?: string; character?: string; color?: string; mood?: string }) => {
|
|
setError('');
|
|
update.mutate(
|
|
{ body },
|
|
{
|
|
onSuccess: (res) => {
|
|
if (!res.result?.success) return setError(res.result?.desc === 'INVALID_INPUT' ? '닉네임은 2~12자로 써 주세요.' : '잠시 뒤에 다시 해 주세요.');
|
|
queryClient.setQueryData(meQueryKey(), res);
|
|
// 댓글에 보이는 내 캐릭터·닉네임도 바뀐다
|
|
queryClient.invalidateQueries({ predicate: (q) => (q.queryKey[0] as { _id?: string } | undefined)?._id === 'listComments' });
|
|
},
|
|
onError: () => setError('서버에 닿지 못했어요. 잠시 뒤에 다시 해 주세요.'),
|
|
},
|
|
);
|
|
};
|
|
|
|
// 저장하는 동안은 고른 값을 바로 선택으로 보여 준다. 실패하면 me 값으로 돌아간다
|
|
const saving = update.isPending ? update.variables?.body : undefined;
|
|
const character = saving?.character ?? me.character;
|
|
const color = saving?.color ?? me.color;
|
|
const mood = saving?.mood ?? me.mood;
|
|
const nick = nickname.trim();
|
|
|
|
return (
|
|
<Body className="gap-3">
|
|
<View className="items-center gap-1.5">
|
|
<Avatar character={me.character} color={me.color} mood={me.mood} size={64} />
|
|
<Text className="text-center text-sm font-semibold text-ink2">{me.login_id}(으)로 로그인했어요</Text>
|
|
</View>
|
|
<View accessibilityLabel="내 캐릭터">
|
|
<Text className="mb-2 text-sm font-extrabold text-ink">내 캐릭터</Text>
|
|
<View className="flex-row flex-wrap gap-2">
|
|
{CHARACTERS.map(([key, label]) => (
|
|
<Pressable
|
|
key={key}
|
|
accessibilityRole="radio"
|
|
accessibilityState={{ checked: character === key }}
|
|
disabled={update.isPending}
|
|
onPress={() => save({ character: key })}
|
|
className={`items-center gap-1 rounded-[18px] border-2 py-2.5 ${character === key ? 'border-orange' : 'border-transparent'}`}
|
|
style={{ width: '31.5%', backgroundColor: s.field }}
|
|
>
|
|
<Ic name={key} size={48} />
|
|
<Text className={`font-jua text-[15px] ${character === key ? 'text-orange' : 'text-ink'}`}>{label}</Text>
|
|
</Pressable>
|
|
))}
|
|
</View>
|
|
</View>
|
|
<View accessibilityLabel="배경색">
|
|
<Text className="mb-2 text-sm font-extrabold text-ink">배경색</Text>
|
|
<View className="flex-row flex-wrap gap-2.5">
|
|
{Object.entries(AVATAR_COLORS).map(([key, hex]) => (
|
|
<Pressable
|
|
key={key}
|
|
accessibilityRole="radio"
|
|
accessibilityState={{ checked: color === key }}
|
|
accessibilityLabel={key}
|
|
disabled={update.isPending}
|
|
onPress={() => save({ color: key })}
|
|
className={`size-9 rounded-full border-[3px] ${color === key ? 'border-ink' : 'border-white'}`}
|
|
style={{ backgroundColor: hex }}
|
|
/>
|
|
))}
|
|
</View>
|
|
</View>
|
|
<View accessibilityLabel="오늘 기분">
|
|
<Text className="mb-2 text-sm font-extrabold text-ink">오늘 기분</Text>
|
|
<View className="flex-row flex-wrap gap-2">
|
|
{MOODS.map(([key, emoji, label]) => (
|
|
<Pressable
|
|
key={key}
|
|
accessibilityRole="radio"
|
|
accessibilityState={{ checked: mood === key }}
|
|
disabled={update.isPending}
|
|
onPress={() => save({ mood: mood === key ? '' : key })}
|
|
className={`items-center gap-0.5 rounded-[14px] border-2 py-2 ${mood === key ? 'border-orange' : 'border-transparent'}`}
|
|
style={{ width: '23%', backgroundColor: s.field }}
|
|
>
|
|
<Text className="text-[22px] leading-[26px]">{emoji}</Text>
|
|
<Text className={`text-xs font-bold ${mood === key ? 'text-orange' : 'text-ink2'}`}>{label}</Text>
|
|
</Pressable>
|
|
))}
|
|
</View>
|
|
<Text className="mt-1.5 text-xs font-semibold text-ink3">한 번 더 누르면 지워져요</Text>
|
|
</View>
|
|
<View className="flex-row items-center gap-2">
|
|
<View className="flex-1">
|
|
<Field value={nickname} onChangeText={setNickname} maxLength={12} accessibilityLabel="닉네임" placeholder="닉네임 (2~12자)" onSubmitEditing={() => nick.length >= 2 && save({ nickname: nick })} />
|
|
</View>
|
|
<Pressable onPress={() => save({ nickname: nick })} disabled={update.isPending || nick === me.nickname || nick.length < 2} className="rounded-[14px] bg-ink-btn px-4 py-3 disabled:opacity-40">
|
|
<Text className="text-sm font-extrabold text-white">{saving?.nickname != null ? '저장 중…' : '바꾸기'}</Text>
|
|
</Pressable>
|
|
</View>
|
|
<Text className={`min-h-5 text-center text-[13px] font-bold ${saving ? 'text-ink2' : 'text-heart'}`}>{saving ? (saving.nickname == null ? '저장 중…' : '') : error}</Text>
|
|
<Pressable
|
|
onPress={() => {
|
|
logout();
|
|
onDone();
|
|
}}
|
|
className="rounded-[14px] p-[13px]"
|
|
style={{ backgroundColor: s.soft }}
|
|
>
|
|
<Text className="text-center text-[15px] font-extrabold text-ink">로그아웃</Text>
|
|
</Pressable>
|
|
<Blocked />
|
|
<Withdraw onDone={onDone} />
|
|
</Body>
|
|
);
|
|
}
|