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

14.1 Login-sivun lisääminen Tanstackiin | Antti/add-tanstack-library-login-page #58

Merged
merged 25 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8eb5a44
Remove isModalOpen etc, now it will display the Modal if it has somet…
anttiasmala Nov 1, 2024
5565314
Merge branch 'antti/add-tanstack-library' into antti/add-tanstack-lib…
anttiasmala Nov 5, 2024
6d45f0e
Change loading-dots back to 3 dots and run Prettier inside the file t…
anttiasmala Nov 5, 2024
9475ceb
Lower the animation duration due to less keyframes
anttiasmala Nov 5, 2024
b4f276c
Apply stash to DeleteModal.tsx
anttiasmala Nov 6, 2024
8fa0406
Add useQuery. Add a handler for login. Add conditional rendering to m…
anttiasmala Nov 6, 2024
26703ec
ESLint and fixes
anttiasmala Nov 6, 2024
7c61f4a
Remove unnecessary debug lines of code
anttiasmala Nov 6, 2024
251f168
Merge branch antti/add-tanstack-library to antti/add-tanstack-library…
anttiasmala Nov 27, 2024
d997664
Change queryKey and change handleLogin to be awaited during query
anttiasmala Nov 27, 2024
79bfec7
Merge branch antti/add-tanstack-library to antti/add-tanstack-library…
anttiasmala Dec 10, 2024
319b087
Change useQuery to useMutate. useQuery should be used only for GET-re…
anttiasmala Dec 11, 2024
72977d7
Remove unnecessary lines and console.logs
anttiasmala Dec 11, 2024
b934a7e
Remove try/catch block
anttiasmala Dec 12, 2024
cf85967
Merge branch 'antti/add-tanstack-library' into antti/add-tanstack-lib…
anttiasmala Dec 12, 2024
df21970
Add isModalOpen to make them work
anttiasmala Dec 12, 2024
44387b3
Merge branch 'main' into antti/add-tanstack-library-login-page
anttiasmala Jan 9, 2025
41f536e
Fix import on useShowErrorToast
anttiasmala Jan 9, 2025
ed3b883
Remove unnecessary comment
anttiasmala Jan 9, 2025
d19f801
ESLint and fixes
anttiasmala Jan 9, 2025
846bd68
Change Login button's rendering method
anttiasmala Jan 16, 2025
6166fbc
Add different colors while button is disabled
anttiasmala Jan 16, 2025
41e15fc
Change DeleteModal to not close if deleting request fails
anttiasmala Jan 16, 2025
7eb7bba
Change mutationFn to take login credentials as a parameter. Remove th…
anttiasmala Jan 16, 2025
13adf91
Change mutateAsync to use validated email instead of useState email
anttiasmala Jan 16, 2025
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
2 changes: 1 addition & 1 deletion components/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function Button({
return (
<button
className={twMerge(
`mt-6 w-full rounded-md border border-lines bg-primary p-2 text-lg font-medium text-white`,
`mt-6 w-full rounded-md border border-lines bg-primary p-2 text-lg font-medium text-white disabled:bg-gray-300 disabled:text-gray-500`,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ovatko nuo värit yhtään sinnepäinkään mitä niiden kuuluisi olla? 😅

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oikein hyvät! 👍

className,
)}
{...rest}
Expand Down
3 changes: 1 addition & 2 deletions components/DeleteModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ export function DeleteModal({ gift, setIsModalOpen }: DeleteModal) {
async function handleDeletion() {
try {
await deleteGift(gift.uuid);
setIsModalOpen(false);
} catch (e) {
handleErrorToast(handleError(e));
}
await queryClient.invalidateQueries({
queryKey: QueryKeys.GIFTS,
});

setIsModalOpen(false);
}

return (
Expand Down
6 changes: 3 additions & 3 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ export default function Home({

function GiftList() {
const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
const [deleteModalGiftData, setDeleteModalGiftData] = useState<Gift>();
const [isEditModalOpen, setIsEditModalOpen] = useState(false);
const [deleteModalGiftData, setDeleteModalGiftData] = useState<Gift>();
const [editModalGiftData, setEditModalGiftData] = useState<Gift>();

const { error, isFetching, data: giftData } = useGetGifts();
Expand Down Expand Up @@ -203,14 +203,14 @@ function GiftList() {
</div>
</div>
))}
{isEditModalOpen && editModalGiftData && (
{editModalGiftData && isEditModalOpen && (
<EditModal
gift={editModalGiftData}
setIsModalOpen={setIsEditModalOpen}
/>
)}

{isDeleteModalOpen && deleteModalGiftData && (
{deleteModalGiftData && isDeleteModalOpen && (
Comment on lines -206 to +213
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nämä kaksi muutosta liittyy ideaan, jossa poistetaan isDeleteModalOpen ja isEditModalOpen. Tieto siitä pitäisikö Modalin olla auki saadaan tarkistamalla sisältääkö esimerkiksi deleteModalGiftData tietoja vai ei

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jep tarkistus voidaan hyvin tehdä noin, mikä onkin järkevää ja yksinkertaistaa asioita. Muuttujien järjestyksen muuttaminen ei taida sitä kuitenkaan vielä tehdä?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Joo, ei toimi vielä. Voisin tehdä muokkauksen siihen branchiin, missä lisään Tanstackin EditModal ja DeleteModalille.

Vai onko tämä tarkistus parempi lisätä vasta Tanstackin jälkeen?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Voit tehdä siinä samassa branchissä missä lisäät Tanstackin modaaliin.

<DeleteModal
gift={deleteModalGiftData}
setIsModalOpen={setIsDeleteModalOpen}
Expand Down
32 changes: 22 additions & 10 deletions pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import { Logo } from '~/components/Logo';
import { TitleText } from '~/components/TitleText';
import SvgEyeOpen from '~/icons/eye_open';
import SvgEyeSlash from '~/icons/eye_slash';
import { UserLoginDetails } from '~/shared/types';
import { QueryKeys, UserLoginDetails } from '~/shared/types';
import { handleError } from '~/utils/handleError';
import { emailSchema } from '~/shared/zodSchemas';
import { Label } from '~/components/Label';
import { GetServerSidePropsContext } from 'next';
import { handleErrorToast } from '~/utils/handleToasts';
import { ErrorParagraph } from '~/components/ErrorParagraph';
import { useMutation } from '@tanstack/react-query';
import { useShowErrorToast } from '~/hooks/useShowErrorToast';

export async function getServerSideProps(context: GetServerSidePropsContext) {
const cookieData = await validateRequest(context.req, context.res);
Expand Down Expand Up @@ -46,6 +48,15 @@ export default function Login() {

const router = useRouter();

const { mutateAsync, isPending, error } = useMutation({
mutationKey: QueryKeys.LOGIN,
mutationFn: async (loginCredentials: UserLoginDetails) =>
await axios.post('/api/auth/login', loginCredentials),
onSuccess: () => router.push('/'),
});

useShowErrorToast(error);
Comment on lines +51 to +58
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mutationFn saa nyt tarvittavat tiedot parametreina. Lisäsin näemmä myös useShowErrorToast(error) käsittelemään mahdolliset virheet


async function handleSubmit(e: FormEvent<HTMLFormElement>) {
e.preventDefault();
try {
Expand Down Expand Up @@ -78,14 +89,11 @@ export default function Login() {
if (errorFound) {
return;
}

const loginCredentials: UserLoginDetails = {
email: emailValidation.data || '',
password: password,
rememberMe: rememberMe,
};
await axios.post('/api/auth/login', loginCredentials);
await router.push('/');
await mutateAsync({
email: emailValidation.data ?? '',
password,
rememberMe,
});
} catch (e) {
console.error(e);
handleErrorToast(handleError(e));
Expand Down Expand Up @@ -149,7 +157,11 @@ export default function Login() {
Muista minut
</Label>
</div>
<Button type="submit">Kirjaudu sisään</Button>

<Button type="submit" disabled={isPending}>
Kirjaudu sisään{' '}
{isPending && <span className="loading-dots absolute" />}
</Button>
</form>
<p className={`mt-4 text-center text-xs text-gray-500`}>
Ei vielä tunnuksia?{' '}
Expand Down
2 changes: 1 addition & 1 deletion shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export type KeyboardEventKeys =

export const QueryKeys = {
GIFTS: ['gifts'],

LOGIN: ['login'],
// ei vielä käytössä
//GIFT_DETAILS: (uuid: string) => ['gifts', uuid],
};
Loading