adding cookie storage for current organization.

This commit is contained in:
killian-larcher
2024-12-15 21:26:28 +01:00
parent f4fa966d89
commit 5484e8841a
3 changed files with 55 additions and 19 deletions
@@ -1,10 +1,11 @@
"use client"
import {useEffect} from "react";
import {useEffect, useState} from "react";
import {ComboBox} from "@/components/wrappers/combobox";
import {Organization} from "@prisma/client";
import {useStore} from "@/state-management/store";
import {getCurrentOrganizationSlug, setCurrentOrganizationSlug} from "@/features/dashboard/organization";
export type organizationComboBoxProps = {
organizations: Organization[]
@@ -14,33 +15,50 @@ export type organizationComboBoxProps = {
export function OrganizationComboBox(props: organizationComboBoxProps) {
const {organizationId, moveToAnotherOrganization} = useStore((state) => state);
// const {organizationId, moveToAnotherOrganization} = useStore((state) => state);
const [organizationSlug, setOrganizationSlug] = useState<string>()
const {organizations, defaultOrganization} = props
useEffect(() => {
if (organizationId == "") {
moveToAnotherOrganization(defaultOrganization.id)
} else {
const organization = organizations.find(organization => organization.id === organizationId)
if (!organization) moveToAnotherOrganization(defaultOrganization.id)
}
}, [organizationId])
getCurrentOrganizationSlug().then(slug => {
console.log("sssssss", slug)
if (slug == "") {
setOrganizationSlug(defaultOrganization.slug)
setCurrentOrganizationSlug(defaultOrganization.slug)
} else {
setOrganizationSlug(slug)
const organization = organizations.find(organization => organization.slug === slug)
if (!organization) {
setOrganizationSlug(defaultOrganization.slug)
setCurrentOrganizationSlug(defaultOrganization.slug)
}
}
})
}, [organizationSlug])
const values = organizations.map(organization => {
return ({
value: organization.id,
value: organization.slug,
label: organization.name,
})
})
const onValueChange = async (id: string) => moveToAnotherOrganization(id)
const onValueChange = (slug: string) => {
if (organizationSlug !== slug) {
setOrganizationSlug(slug)
setCurrentOrganizationSlug(slug)
}
}
return (
<ComboBox
sideBar={true}
sideBar
values={values}
defaultValue={organizationId}
defaultValue={organizationSlug}
onValueChange={onValueChange}/>
)
}
+10 -6
View File
@@ -21,6 +21,7 @@ import {
} from "@/components/ui/popover"
import {FormControl} from "@/components/ui/form";
import {SidebarMenuButton} from "@/components/ui/sidebar";
import {Separator} from "@/components/ui/separator";
export type comboBoxProps = {
values: Array<{ value: string, label: string }>
@@ -33,7 +34,7 @@ export type comboBoxProps = {
export function ComboBox(props: comboBoxProps) {
const {values: choices, defaultValue: defaultChoice = "", onValueChange, searchField = false} = props;
const {values: choices, defaultValue: defaultChoice = "", onValueChange, searchField = false, sideBar = false} = props;
const [value, setValue] = useState<string>()
@@ -46,7 +47,7 @@ export function ComboBox(props: comboBoxProps) {
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
{props.sideBar ?
{sideBar ?
<SidebarMenuButton>
{value
? choices.find((choice) => choice.value === value)?.label
@@ -69,10 +70,7 @@ export function ComboBox(props: comboBoxProps) {
</PopoverTrigger>
<PopoverContent
className="p-0 popover-content-width-full"
>
<PopoverContent className="p-0 popover-content-width-full">
<Command>
{searchField ? <CommandInput placeholder="Search choice..." className="h-9"/> : null}
<CommandList>
@@ -100,6 +98,12 @@ export function ComboBox(props: comboBoxProps) {
</CommandGroup>
</CommandList>
</Command>
<Separator/>
{sideBar ?
<SidebarMenuButton>
+ Create new organization
</SidebarMenuButton>
: null}
</PopoverContent>
</Popover>
)
+14
View File
@@ -0,0 +1,14 @@
'use server';
import {cookies} from 'next/headers';
const COOKIE_NAME = 'PORTABASE_ORGANIZATION_SLUG';
export async function getCurrentOrganizationSlug() {
return (await cookies()).get(COOKIE_NAME)?.value || "default";
}
export async function setCurrentOrganizationSlug(slug: string) {
return (await cookies()).set(COOKIE_NAME, slug).get(COOKIE_NAME)?.value;
}