Skip to content

Commit

Permalink
added wizard, zustand, and login to osm
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiemann96 committed Sep 11, 2023
1 parent 04d47ae commit 2c75ba7
Show file tree
Hide file tree
Showing 17 changed files with 910 additions and 2 deletions.
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
"@capacitor/core": "^5.3.0",
"@capacitor/ios": "^5.3.0",
"@heroicons/react": "^2.0.18",
"@hookform/resolvers": "^3.3.1",
"@radix-ui/react-dialog": "^1.0.4",
"@radix-ui/react-dropdown-menu": "^2.0.5",
"@radix-ui/react-label": "^2.0.2",
"@radix-ui/react-slot": "^1.0.2",
"@radix-ui/react-toast": "^1.1.4",
"@types/node": "20.5.9",
"@types/react": "18.2.21",
"@types/react-dom": "18.2.7",
Expand All @@ -31,10 +34,14 @@
"postcss": "8.4.29",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-hook-form": "^7.46.1",
"swiper": "^10.2.0",
"tailwind-merge": "^1.14.0",
"tailwindcss": "3.3.3",
"tailwindcss-animate": "^1.0.7",
"typescript": "5.2.2"
"typescript": "5.2.2",
"zod": "^3.22.2",
"zustand": "^4.4.1"
},
"devDependencies": {
"@capacitor/cli": "^5.3.0",
Expand Down
Binary file added public/senseboxbike.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Metadata } from "next";
import { Inter } from "next/font/google";
import { Navbar } from "@/components/ui/Navbar";
import { cn } from "@/lib/utils";
import { Toaster } from "@/components/ui/toaster"

const inter = Inter({ subsets: ["latin"] });

Expand Down Expand Up @@ -33,6 +34,7 @@ export default function RootLayout({
<nav>
<Navbar />
</nav>
<Toaster />
</body>
</html>
);
Expand Down
29 changes: 28 additions & 1 deletion src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
'use client'

Check failure on line 1 in src/app/page.tsx

View workflow job for this annotation

GitHub Actions / Prettier

src/app/page.tsx#L1

There are issues with this file's formatting, please run Prettier to fix the errors
import ConnectionSelection from "@/components/Home/ConnectionSelection";
import { Swiper, SwiperSlide } from 'swiper/react';
import { Navigation, Pagination } from 'swiper/modules';
import 'swiper/css';
import 'swiper/css/navigation';
import 'swiper/css/pagination';
import Welcome from "@/components/Wizard/Welcome";
import OpenSenseMapLogin from "@/components/Wizard/OpenSenseMapLogin";

export default function Home() {
return <ConnectionSelection />;
return (
<Swiper
spaceBetween={50}
modules={[Navigation]}
slidesPerView={1}
onSlideChange={() => console.log('slide change')}
onSwiper={(swiper) => console.log(swiper)}
className="flex text-center h-full justify-center"
>
<SwiperSlide>
<Welcome />
</SwiperSlide>
<SwiperSlide>
<OpenSenseMapLogin />
</SwiperSlide>
<SwiperSlide>
<ConnectionSelection />
</SwiperSlide>
</Swiper>
)
}
3 changes: 3 additions & 0 deletions src/components/Home/ConnectionSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ export default function ConnectionSelection() {
<div className="flex w-full flex-col h-full">
<div className="flex h-full flex-col justify-center gap-10">
<div className="flex flex-col items-center text-center gap-5">
<div>
Super! Der Login hat funktioniert ! Jetzt müssen wir nur noch die Box mit dem Gerät verbinden.
</div>
<Image
alt="bike"
src={Logo}
Expand Down
94 changes: 94 additions & 0 deletions src/components/Wizard/OpenSenseMapLogin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
'use client';

Check failure on line 1 in src/components/Wizard/OpenSenseMapLogin.tsx

View workflow job for this annotation

GitHub Actions / Prettier

src/components/Wizard/OpenSenseMapLogin.tsx#L1

There are issues with this file's formatting, please run Prettier to fix the errors
import { useSwiper } from "swiper/react";
import { Button } from "../ui/button";
import { Input } from "../ui/input";
import { useState } from "react";
import useOpenSenseMapAuth from "@/lib/useOpenSenseMapAuth";
import { zodResolver } from "@hookform/resolvers/zod"
import * as z from "zod"; // import * as z from "zod";
import { useForm } from "react-hook-form";
import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from "../ui/form";
import { useToast } from "../ui/use-toast";
import Spinner from "../ui/Spinner";
import { useAuthStore } from "@/lib/store/useAuthStore";

const formSchema = z.object({
email: z.string().email(),
password: z.string(),
});

export default function OpenSenseMapLogin(){
const swiper = useSwiper();
const [loading, setLoading] = useState(false);
const { login } = useOpenSenseMapAuth();
const { toast } = useToast();

const email = useAuthStore(state => state.email);
const password = useAuthStore(state => state.password);



const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
email,
password
},
})

const handleLogin = async (values: z.infer<typeof formSchema>) => {
setLoading(true);
const success = await login(values.email, values.password);
setLoading(false);
if (success) {
toast({title:"Login erfolgreich"})
swiper.slideNext();
}
else {
toast({ variant:"destructive", title: "Login fehlgeschlagen"})
}
}


return (
<div className="flex content-center h-full p-2 flex-col justify-center gap-4">
<div>
Bitte loggen Sie sich mit Ihrem openSenseMap-Account ein.
</div>
<Form {...form}>
<form onSubmit={form.handleSubmit(handleLogin)} className="space-y-8">
<FormField
control = {form.control}
name = "email"
render = {({field}) => (
<FormItem>
<FormLabel>Email</FormLabel>
<FormControl>
<Input placeholder="Email" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control = {form.control}
name = "password"
render = {({field}) => (
<FormItem>
<FormLabel>Passwort</FormLabel>
<FormControl>
<Input type="password" placeholder="Passwort" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>

<Button disabled={loading} type="submit">
{loading ? <Spinner/>: "Login"}
</Button>
</form>
</Form>
</div>
)
}
21 changes: 21 additions & 0 deletions src/components/Wizard/Welcome.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useSwiper } from "swiper/react"

Check failure on line 1 in src/components/Wizard/Welcome.tsx

View workflow job for this annotation

GitHub Actions / Prettier

src/components/Wizard/Welcome.tsx#L1

There are issues with this file's formatting, please run Prettier to fix the errors
import { Button } from "../ui/button";
import Logo from "../../../public/senseboxbike.png";
import Image from "next/image";

export default function Welcome() {

const swiper = useSwiper();

return(
<div className="flex justify-center content-center flex-col gap-4 h-full">
<div className="flex flex-col items-center gap-2">
<Image src={Logo} alt="senseBox:bike" width={100} height={100} />
Willkommen beim senseBox Wizard
In den nächsten Schritten werden Sie durch die Einrichtung Ihrer senseBox geführt.
Dazu brauchen sie einen Account auf der <a href ="https://opensensemap.org">openSenseMap</a> sowie eine senseBox:bike mit einem Bluetooth-Modul.
</div>
<Button onClick={() => swiper.slideNext()}>Weiter</Button>
</div>
)
}
10 changes: 10 additions & 0 deletions src/components/ui/Spinner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default function Spinner () {

Check failure on line 1 in src/components/ui/Spinner.tsx

View workflow job for this annotation

GitHub Actions / Prettier

src/components/ui/Spinner.tsx#L1

There are issues with this file's formatting, please run Prettier to fix the errors
return(
<div role="status">
<svg aria-hidden="true" className="w-8 h-8 mr-2 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/>
<path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/>
</svg>
<span className="sr-only">Loading...</span>
</div>)
}
176 changes: 176 additions & 0 deletions src/components/ui/form.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import * as React from "react"

Check failure on line 1 in src/components/ui/form.tsx

View workflow job for this annotation

GitHub Actions / Prettier

src/components/ui/form.tsx#L1

There are issues with this file's formatting, please run Prettier to fix the errors
import * as LabelPrimitive from "@radix-ui/react-label"
import { Slot } from "@radix-ui/react-slot"
import {
Controller,
ControllerProps,
FieldPath,
FieldValues,
FormProvider,
useFormContext,
} from "react-hook-form"

import { cn } from "@/lib/utils"
import { Label } from "@/components/ui/label"

const Form = FormProvider

type FormFieldContextValue<
TFieldValues extends FieldValues = FieldValues,
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
> = {
name: TName
}

const FormFieldContext = React.createContext<FormFieldContextValue>(
{} as FormFieldContextValue
)

const FormField = <
TFieldValues extends FieldValues = FieldValues,
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
>({
...props
}: ControllerProps<TFieldValues, TName>) => {
return (
<FormFieldContext.Provider value={{ name: props.name }}>
<Controller {...props} />
</FormFieldContext.Provider>
)
}

const useFormField = () => {
const fieldContext = React.useContext(FormFieldContext)
const itemContext = React.useContext(FormItemContext)
const { getFieldState, formState } = useFormContext()

const fieldState = getFieldState(fieldContext.name, formState)

if (!fieldContext) {
throw new Error("useFormField should be used within <FormField>")
}

const { id } = itemContext

return {
id,
name: fieldContext.name,
formItemId: `${id}-form-item`,
formDescriptionId: `${id}-form-item-description`,
formMessageId: `${id}-form-item-message`,
...fieldState,
}
}

type FormItemContextValue = {
id: string
}

const FormItemContext = React.createContext<FormItemContextValue>(
{} as FormItemContextValue
)

const FormItem = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => {
const id = React.useId()

return (
<FormItemContext.Provider value={{ id }}>
<div ref={ref} className={cn("space-y-2", className)} {...props} />
</FormItemContext.Provider>
)
})
FormItem.displayName = "FormItem"

const FormLabel = React.forwardRef<
React.ElementRef<typeof LabelPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof LabelPrimitive.Root>
>(({ className, ...props }, ref) => {
const { error, formItemId } = useFormField()

return (
<Label
ref={ref}
className={cn(error && "text-destructive", className)}
htmlFor={formItemId}
{...props}
/>
)
})
FormLabel.displayName = "FormLabel"

const FormControl = React.forwardRef<
React.ElementRef<typeof Slot>,
React.ComponentPropsWithoutRef<typeof Slot>
>(({ ...props }, ref) => {
const { error, formItemId, formDescriptionId, formMessageId } = useFormField()

return (
<Slot
ref={ref}
id={formItemId}
aria-describedby={
!error
? `${formDescriptionId}`
: `${formDescriptionId} ${formMessageId}`
}
aria-invalid={!!error}
{...props}
/>
)
})
FormControl.displayName = "FormControl"

const FormDescription = React.forwardRef<
HTMLParagraphElement,
React.HTMLAttributes<HTMLParagraphElement>
>(({ className, ...props }, ref) => {
const { formDescriptionId } = useFormField()

return (
<p
ref={ref}
id={formDescriptionId}
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
)
})
FormDescription.displayName = "FormDescription"

const FormMessage = React.forwardRef<
HTMLParagraphElement,
React.HTMLAttributes<HTMLParagraphElement>
>(({ className, children, ...props }, ref) => {
const { error, formMessageId } = useFormField()
const body = error ? String(error?.message) : children

if (!body) {
return null
}

return (
<p
ref={ref}
id={formMessageId}
className={cn("text-sm font-medium text-destructive", className)}
{...props}
>
{body}
</p>
)
})
FormMessage.displayName = "FormMessage"

export {
useFormField,
Form,
FormItem,
FormLabel,
FormControl,
FormDescription,
FormMessage,
FormField,
}
Loading

0 comments on commit 2c75ba7

Please sign in to comment.