Skip to content

Commit

Permalink
feat: toggle booth opened state
Browse files Browse the repository at this point in the history
  • Loading branch information
algoORgoal committed May 11, 2024
1 parent 8de07d2 commit 6f48f32
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
17 changes: 17 additions & 0 deletions src/features/booth/api/booth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,20 @@ export const editBooth = async (
const data = await response.json();
return data;
};

export const updateBoothOpened = async (
accessToken: string,
boothId: number,
opened: boolean,
) => {
const response = await fetch(`${API_URL}/api/booths/${boothId}`, {
method: HTTPMethod.PATCH,
headers: {
[`${HTTPHeaderKey.CONTENT_TYPE}`]: HTTPHeaderValue.APPLICATION_JSON,
[`${HTTPHeaderKey.AUTHORIZATION}`]: getAuthorziationValue(accessToken),
},
body: JSON.stringify({ enabled: opened }),
});
const data = await response.json();
return data;
};
22 changes: 16 additions & 6 deletions src/features/booth/ui/SwitchButton.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
"use client";

import useAuthFetch from "@/src/shared/model/auth/useAuthFetchList";
import { Label } from "@/src/shared/ui/label";
import { Switch } from "@/src/shared/ui/switch";
import React, { useState } from "react";
import { updateBoothOpened } from "../api/booth";

export function SwitchButton() {
const [isOpened, setIsOpened] = useState<boolean>(false);
export function SwitchButton({
boothId,
initialOpened,
}: Readonly<{ boothId: number; initialOpened?: boolean }>) {
const [opened, setIsOpened] = useState<boolean>(false);

const updateAuthBoothOpened = useAuthFetch(updateBoothOpened);

const toggleBoothOpened = async () => {
await updateAuthBoothOpened(boothId, !opened);
setIsOpened((currentOpened) => !currentOpened);
};

return (
<div className="flex items-center space-x-2">
<Label htmlFor="booth-mode">영업</Label>
<Switch
id="booth-mode"
checked={isOpened}
onCheckedChange={() =>
setIsOpened((currentIsOpened) => !currentIsOpened)
}
checked={opened}
onCheckedChange={toggleBoothOpened}
/>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/boothList/ui/BoothList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function BoothList() {
{...booth}
editButton={<EditButton boothId={booth.id!} />}
deleteButton={<DeleteButton boothId={booth.id!} />}
switchButton={<SwitchButton />}
switchButton={<SwitchButton boothId={booth.id!} />}
/>
))}
</div>
Expand Down

0 comments on commit 6f48f32

Please sign in to comment.