Skip to content

Commit

Permalink
Edit tag and use team_id
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Nov 19, 2024
1 parent 14a84de commit 84fa981
Show file tree
Hide file tree
Showing 15 changed files with 303 additions and 138 deletions.
29 changes: 29 additions & 0 deletions apps/dashboard/src/actions/delete-tag-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use server";

import { LogEvents } from "@midday/events/events";
import { revalidateTag } from "next/cache";
import { authActionClient } from "./safe-action";
import { deleteTagSchema } from "./schema";

export const deleteTagAction = authActionClient
.schema(deleteTagSchema)
.metadata({
name: "delete-tag",
track: {
event: LogEvents.DeleteTag.name,
channel: LogEvents.DeleteTag.channel,
},
})
.action(async ({ parsedInput: { id }, ctx: { supabase, user } }) => {
const { data } = await supabase
.from("tags")
.delete()
.eq("id", id)
.select("id, name")
.single();

revalidateTag(`tracker_projects_${user.team_id}`);
revalidateTag(`transactions_${user.team_id}`);

return data;
});
9 changes: 9 additions & 0 deletions apps/dashboard/src/actions/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ export const createTransactionTagSchema = z.object({
transactionId: z.string(),
});

export const deleteTagSchema = z.object({
id: z.string(),
});

export const updateTagSchema = z.object({
id: z.string(),
name: z.string(),
});

export const deleteTransactionTagSchema = z.object({
tagId: z.string(),
transactionId: z.string(),
Expand Down
33 changes: 33 additions & 0 deletions apps/dashboard/src/actions/update-tag-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"use server";

import { LogEvents } from "@midday/events/events";
import { revalidateTag } from "next/cache";
import { authActionClient } from "./safe-action";
import { updateTagSchema } from "./schema";

export const updateTagAction = authActionClient
.schema(updateTagSchema)
.metadata({
name: "update-tag",
track: {
event: LogEvents.UpdateTag.name,
channel: LogEvents.UpdateTag.channel,
},
})
.action(async ({ parsedInput: { name, id }, ctx: { supabase, user } }) => {
const { data } = await supabase
.from("tags")
.update({
name,
})
.eq("id", id)
.select("id, name")
.single();

revalidateTag(`tracker_projects_${user.team_id}`);

// TODO: Fix transaction sheet rerendering
// revalidateTag(`transactions_${user.team_id}`);

return data;
});
18 changes: 5 additions & 13 deletions apps/dashboard/src/components/assign-user.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { useUserContext } from "@/store/user/hook";
import { createClient } from "@midday/supabase/client";
import {
getCurrentUserTeamQuery,
getTeamMembersQuery,
} from "@midday/supabase/queries";
import { getTeamMembersQuery } from "@midday/supabase/queries";
import {
Select,
SelectContent,
Expand Down Expand Up @@ -30,23 +28,17 @@ export function AssignUser({ selectedId, isLoading, onSelect }: Props) {
const [value, setValue] = useState<string>();
const supabase = createClient();
const [users, setUsers] = useState<User[]>([]);
const { team_id: teamId } = useUserContext((state) => state.data);

useEffect(() => {
setValue(selectedId);
}, [selectedId]);

useEffect(() => {
async function getUsers() {
const { data: userData } = await getCurrentUserTeamQuery(supabase);

if (userData?.team_id) {
const { data: membersData } = await getTeamMembersQuery(
supabase,
userData.team_id,
);
const { data: membersData } = await getTeamMembersQuery(supabase, teamId);

setUsers(membersData?.map(({ user }) => user));
}
setUsers(membersData?.map(({ user }) => user));
}

getUsers();
Expand Down
10 changes: 4 additions & 6 deletions apps/dashboard/src/components/attachments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import { deleteAttachmentAction } from "@/actions/delete-attachment-action";
import { useUpload } from "@/hooks/use-upload";
import { createClient } from "@midday/supabase/client";
import { getCurrentUserTeamQuery } from "@midday/supabase/queries";
import { useUserContext } from "@/store/user/hook";
import { cn } from "@midday/ui/cn";
import { useToast } from "@midday/ui/use-toast";
import { stripSpecialCharacters } from "@midday/utils";
Expand Down Expand Up @@ -33,11 +32,12 @@ type Props = {
};

export function Attachments({ prefix, data, onUpload }: Props) {
const supabase = createClient();
const { toast } = useToast();
const [files, setFiles] = useState<Attachment[]>([]);
const { uploadFile } = useUpload();

const { team_id: teamId } = useUserContext((state) => state.data);

const handleOnDelete = async (id: string) => {
setFiles((files) => files.filter((file) => file?.id !== id));
await deleteAttachmentAction(id);
Expand All @@ -54,15 +54,13 @@ export function Attachments({ prefix, data, onUpload }: Props) {
})),
]);

const { data: userData } = await getCurrentUserTeamQuery(supabase);

const uploadedFiles = await Promise.all(
acceptedFiles.map(async (acceptedFile) => {
const filename = stripSpecialCharacters(acceptedFile.name);

const { path } = await uploadFile({
bucket: "vault",
path: [userData?.team_id, "transactions", prefix, filename],
path: [teamId, "transactions", prefix, filename],
file: acceptedFile,
});

Expand Down
11 changes: 4 additions & 7 deletions apps/dashboard/src/components/modals/import-modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import { importTransactionsAction } from "@/actions/transactions/import-transactions";
import { useUpload } from "@/hooks/use-upload";
import { useUserContext } from "@/store/user/hook";
import { zodResolver } from "@hookform/resolvers/zod";
import { createClient } from "@midday/supabase/client";
import { getCurrentUserTeamQuery } from "@midday/supabase/queries";
import { AnimatedSizeContainer } from "@midday/ui/animated-size-container";
import { Button } from "@midday/ui/button";
import {
Expand Down Expand Up @@ -48,10 +47,11 @@ export function ImportModal({ currencies, defaultCurrency }: Props) {
null,
);

const { team_id: teamId } = useUserContext((state) => state.data);

const [pageNumber, setPageNumber] = useState<number>(0);
const page = pages[pageNumber];

const supabase = createClient();
const { uploadFile } = useUpload();

const { toast } = useToast();
Expand Down Expand Up @@ -218,15 +218,12 @@ export function ImportModal({ currencies, defaultCurrency }: Props) {
setIsImporting(true);

if (data.import_type === "csv") {
const { data: userData } =
await getCurrentUserTeamQuery(supabase);

const filename = stripSpecialCharacters(
data.file.name,
);
const { path } = await uploadFile({
bucket: "vault",
path: [userData?.team_id, "imports", filename],
path: [teamId, "imports", filename],
file,
});

Expand Down
35 changes: 16 additions & 19 deletions apps/dashboard/src/components/select-account.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { createBankAccountAction } from "@/actions/create-bank-account-action";
import { useUserContext } from "@/store/user/hook";
import { formatAccountName } from "@/utils/format";
import { createClient } from "@midday/supabase/client";
import {
getCurrentUserTeamQuery,
getTeamBankAccountsQuery,
} from "@midday/supabase/queries";
import { getTeamBankAccountsQuery } from "@midday/supabase/queries";
import { ComboboxDropdown } from "@midday/ui/combobox-dropdown";
import { useAction } from "next-safe-action/hooks";
import { useEffect, useState } from "react";
Expand All @@ -27,6 +25,8 @@ export function SelectAccount({ placeholder, onChange, value }: Props) {
const [data, setData] = useState([]);
const supabase = createClient();

const { team_id: teamId } = useUserContext((state) => state.data);

const createBankAccount = useAction(createBankAccountAction, {
onSuccess: async ({ data: result }) => {
if (result) {
Expand All @@ -38,22 +38,19 @@ export function SelectAccount({ placeholder, onChange, value }: Props) {

useEffect(() => {
async function fetchData() {
const { data: userData } = await getCurrentUserTeamQuery(supabase);
if (userData?.team_id) {
const repsonse = await getTeamBankAccountsQuery(supabase, {
teamId: userData.team_id,
});
const repsonse = await getTeamBankAccountsQuery(supabase, {
teamId,
});

setData(
repsonse.data?.map((account) => ({
id: account.id,
label: account.name,
logo: account?.logo_url,
currency: account.currency,
type: account.type,
})),
);
}
setData(
repsonse.data?.map((account) => ({
id: account.id,
label: account.name,
logo: account?.logo_url,
currency: account.currency,
type: account.type,
})),
);
}

if (!data.length) {
Expand Down
48 changes: 22 additions & 26 deletions apps/dashboard/src/components/select-category.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { createCategoriesAction } from "@/actions/create-categories-action";
import { useUserContext } from "@/store/user/hook";
import { getColorFromName } from "@/utils/categories";
import { createClient } from "@midday/supabase/client";
import {
getCategoriesQuery,
getCurrentUserTeamQuery,
} from "@midday/supabase/queries";
import { getCategoriesQuery } from "@midday/supabase/queries";
import { ComboboxDropdown } from "@midday/ui/combobox-dropdown";
import { Spinner } from "@midday/ui/spinner";
import { useAction } from "next-safe-action/hooks";
Expand Down Expand Up @@ -46,31 +44,29 @@ export function SelectCategory({
const [isLoading, setIsLoading] = useState(true);
const supabase = createClient();

const { team_id: teamId } = useUserContext((state) => state.data);

useEffect(() => {
async function fetchData() {
const { data: userData } = await getCurrentUserTeamQuery(supabase);

if (userData?.team_id) {
const response = await getCategoriesQuery(supabase, {
teamId: userData.team_id,
limit: 1000,
});
const response = await getCategoriesQuery(supabase, {
teamId,
limit: 1000,
});

if (response.data) {
setData([
...response.data.map(transformCategory),
...(uncategorized
? [
{
id: "uncategorized",
label: "Uncategorized",
color: "#606060",
slug: "uncategorized",
},
]
: []),
]);
}
if (response.data) {
setData([
...response.data.map(transformCategory),
...(uncategorized
? [
{
id: "uncategorized",
label: "Uncategorized",
color: "#606060",
slug: "uncategorized",
},
]
: []),
]);
}

setIsLoading(false);
Expand Down
Loading

0 comments on commit 84fa981

Please sign in to comment.