Compare commits

...

2 commits

Author SHA1 Message Date
Théo
ae69c4ed08 Added mutate on groupCreate/groupJoin 2023-04-22 10:34:40 +02:00
Théo
6ec7436cdf Adding leave group 2023-04-22 10:18:01 +02:00
5 changed files with 78 additions and 21 deletions

View file

@ -1,4 +1,5 @@
import Image from 'next/image';
import error404 from '@/public/assets/404.png';
export default function NotFound() {

View file

@ -0,0 +1,59 @@
'use client';
import cookies from 'js-cookie';
import { useContext } from 'react';
import { useForm } from 'react-hook-form';
import { useSWRConfig } from 'swr';
import { UserContext } from '@/context/user';
import Button from '@/ui/Button';
import Select from '@/ui/Select';
type SettingsData = {
group: string;
};
export default function Page() {
const { data: me } = useContext(UserContext);
const { mutate } = useSWRConfig();
const { register, handleSubmit } = useForm<SettingsData>({
defaultValues: {
group: ''
}
});
const groups =
me?.groups.map((group) => ({
title: group.name,
value: group.name
})) || [];
async function onSubmit(data: SettingsData) {
const formData = new FormData();
formData.append('group', data.group);
const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/groupQuit`, {
method: 'POST',
body: formData,
headers: {
Authorization: `Bearer ${cookies.get('token')}}`
}
});
if (res.ok) {
mutate('me');
}
}
return (
<section className="flex h-full w-full flex-col space-y-4">
<form className="flex items-center space-x-2" onSubmit={handleSubmit(onSubmit)}>
<Select {...register('group')} className="w-1/4" options={groups} />
<Button kind="brand" type="submit">
Quitter
</Button>
</form>
</section>
);
}

View file

@ -48,6 +48,6 @@ export const navItems: NavItem[] = [
name: 'Paramètres',
slug: 'settings',
icon: 'equalizer-line',
disabled: true
disabled: false
}
];

View file

@ -26,7 +26,6 @@ type Granted = {
export default function Puzzle({ token, id }: { token: string; id: number }) {
const [granted, setGranted] = useState<Granted | null>(null);
const { data: puzzle, isLoading } = usePuzzle({ token, id });
const { mutate } = useSWRConfig();
const router = useRouter();

View file

@ -299,8 +299,6 @@ function FilterChapter({
initialValue: ''
});
console.log(stored);
let options = [] as { title: string; value: string }[];
options = chapters
@ -312,8 +310,8 @@ function FilterChapter({
return { title: t!.name, value: t!.name };
}) as { title: string; value: string }[];
options?.unshift({ title: 'Pas encore terminé', value: 'no-completed' });
options?.unshift({ title: 'Terminés', value: 'completed' });
options?.unshift({ title: 'Pas encore terminé(s)', value: 'no-completed' });
options?.unshift({ title: 'Terminé(s)', value: 'completed' });
options?.unshift({ title: 'Tout les puzzles', value: '' });
setFilterChapter(chapter.id);
@ -359,22 +357,22 @@ function GroupForm({ chapter, token }: { chapter: Chapter; token: string }) {
});
async function onSubmit(data: GroupData) {
await fetch(`${process.env.NEXT_PUBLIC_API_URL}/${isJoining ? 'groupJoin' : 'groupCreate'}`, {
const res = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/${isJoining ? 'groupJoin' : 'groupCreate'}`,
{
method: 'POST',
body: JSON.stringify(data),
headers: {
Authorization: `Bearer ${token}`
}
});
// TODO: handle errors
// if (res.ok) {
// if (!isJoining) {
// mutate('groups');
// } else {
// mutate('me');
// }
// router.refresh();
// }
}
);
if (res.ok) {
mutate('me');
// TODO REFACTOR
router.refresh();
}
}
return (