Skip to content

Commit

Permalink
🔨 chore: refactor code and improvements (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
casperiv0 authored Dec 16, 2021
1 parent ba5538d commit f42c671
Show file tree
Hide file tree
Showing 17 changed files with 333 additions and 499 deletions.
10 changes: 9 additions & 1 deletion packages/client/locales/en/account.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@
"accountSettings": "Account Settings",
"passwordSettings": "Password Settings",
"appearanceSettings": "Appearance Settings",
"connections": "Connections"
"connections": "Connections",
"connectDiscord": "Connect Discord account",
"disconnectDiscord": "Disconnect Discord account",
"disconnectText": "This will remove the ability to login via Discord.",
"connectText": "This will add the ability to login via Discord.",
"disconnecting": "Disconnecting...",
"currentPassword": "Current password",
"newPassword": "New password",
"confirmNewPassword": "Confirm new password"
},
"Notifications": {
"CITIZEN_DELETED": "<span>{citizen}</span> was deleted. Reason: {reason}"
Expand Down
1 change: 1 addition & 0 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@casper124578/useful": "^1.4.2",
"@headlessui/react": "^1.4.2",
"@radix-ui/react-context-menu": "^0.1.4",
"@radix-ui/react-switch": "^0.1.4",
"@snailycad/config": "1.0.0-beta.13",
"@snailycad/schemas": "1.0.0-beta.13",
"axios": "^0.24.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/client/src/components/account/ChangePasswordArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const ChangePasswordArea = () => {
<Formik validate={validate} onSubmit={onSubmit} initialValues={INITIAL_VALUES}>
{({ handleChange, values, errors }) => (
<Form className="mt-2">
<FormField label="Current Password">
<FormField label={t("currentPassword")}>
<PasswordInput
hasError={!!errors.currentPassword}
value={values.currentPassword}
Expand All @@ -56,7 +56,7 @@ export const ChangePasswordArea = () => {
<Error>{errors.currentPassword}</Error>
</FormField>

<FormField label="New Password">
<FormField label={t("newPassword")}>
<PasswordInput
hasError={!!errors.newPassword}
value={values.newPassword}
Expand All @@ -66,7 +66,7 @@ export const ChangePasswordArea = () => {
<Error>{errors.currentPassword}</Error>
</FormField>

<FormField label="Confirm New Password">
<FormField label={t("confirmNewPassword")}>
<PasswordInput
hasError={!!errors.confirmPassword}
value={values.confirmPassword}
Expand Down
8 changes: 4 additions & 4 deletions packages/client/src/components/account/ConnectionsTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ export const ConnectionsTab = () => {
{user?.discordId ? (
<>
<Button onClick={handleUnlink} disabled={state === "loading"} variant="danger">
{state === "loading" ? "Disconnecting.." : "Disconnect Discord account"}
{state === "loading" ? t("disconnecting") : t("disconnectDiscord")}
</Button>
<p className="mt-2 text-base">This will remove the ability to login via Discord.</p>
<p className="mt-2 text-base">{t("disconnectText")}</p>
</>
) : (
<>
<Button onClick={handleConnectClick}>Connect Discord account</Button>
<p className="mt-2 text-base">This will add the ability to login via Discord.</p>
<Button onClick={handleConnectClick}>{t("connectDiscord")}</Button>
<p className="mt-2 text-base">{t("connectText")}</p>
</>
)}
</div>
Expand Down
7 changes: 1 addition & 6 deletions packages/client/src/components/admin/manage/ApiTokenTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,7 @@ export const ApiTokenTab = () => {
</FormField>

<FormField fieldId="enabled" label={common("enabled")}>
<Toggle
text="enable/disable"
toggled={values.enabled}
onClick={handleChange}
name="enabled"
/>
<Toggle toggled={values.enabled} onClick={handleChange} name="enabled" />
</FormField>

<div className="flex">
Expand Down
45 changes: 12 additions & 33 deletions packages/client/src/components/admin/manage/DisabledFeatures.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,15 @@ export const DisabledFeaturesArea = () => {
<Formik onSubmit={onSubmit} initialValues={INITIAL_VALUES}>
{({ handleChange, handleSubmit, values }) => (
<form className="mt-3 space-y-5" onSubmit={handleSubmit}>
<FormField boldLabel fieldId="tow" label={"Tow"}>
<Toggle
text="enable/disable"
toggled={values[feature.TOW]}
onClick={handleChange}
name={feature.TOW}
/>
<FormField boldLabel fieldId={feature.TOW} label={"Tow"}>
<Toggle toggled={values[feature.TOW]} onClick={handleChange} name={feature.TOW} />
<small className="mt-2 text-base">
Enable/Disable tow. When enabled, this will allow citizens to call tow.
</small>
</FormField>

<FormField boldLabel fieldId="bleeter" label={"Bleeter"}>
<FormField boldLabel fieldId={feature.BLEETER} label={"Bleeter"}>
<Toggle
text="enable/disable"
toggled={values[feature.BLEETER]}
onClick={handleChange}
name={feature.BLEETER}
Expand All @@ -85,9 +79,8 @@ export const DisabledFeaturesArea = () => {
</small>
</FormField>

<FormField boldLabel fieldId="courthouse" label={"Courthouse"}>
<FormField boldLabel fieldId={feature.COURTHOUSE} label={"Courthouse"}>
<Toggle
text="enable/disable"
toggled={values[feature.COURTHOUSE]}
onClick={handleChange}
name={feature.COURTHOUSE}
Expand All @@ -97,21 +90,15 @@ export const DisabledFeaturesArea = () => {
</small>
</FormField>

<FormField boldLabel fieldId="taxi" label={"Taxi"}>
<Toggle
text="enable/disable"
toggled={values[feature.TAXI]}
onClick={handleChange}
name={feature.TAXI}
/>
<FormField boldLabel fieldId={feature.TAXI} label={"Taxi"}>
<Toggle toggled={values[feature.TAXI]} onClick={handleChange} name={feature.TAXI} />
<small className="mt-2 text-base">
When enabled, this will allow citizens to call a taxi to pick them up.
</small>
</FormField>

<FormField boldLabel fieldId="truck-logs" label={"Truck Logs"}>
<FormField boldLabel fieldId={feature.TRUCK_LOGS} label={"Truck Logs"}>
<Toggle
text="enable/disable"
toggled={values[feature.TRUCK_LOGS]}
onClick={handleChange}
name={feature.TRUCK_LOGS}
Expand All @@ -122,22 +109,16 @@ export const DisabledFeaturesArea = () => {
</small>
</FormField>

<FormField boldLabel fieldId="aop" label={"Area of Play"}>
<Toggle
text="enable/disable"
toggled={values[feature.AOP]}
onClick={handleChange}
name={feature.AOP}
/>
<FormField boldLabel fieldId={feature.AOP} label={"Area of Play"}>
<Toggle toggled={values[feature.AOP]} onClick={handleChange} name={feature.AOP} />
<small className="mt-2 text-base">
{/* eslint-disable-next-line quotes */}
{'When disabled, this will hide "- AOP: aop here"'}
</small>
</FormField>

<FormField boldLabel fieldId="business" label={"Businesses"}>
<FormField boldLabel fieldId={feature.BUSINESS} label={"Businesses"}>
<Toggle
text="enable/disable"
toggled={values[feature.BUSINESS]}
onClick={handleChange}
name={feature.BUSINESS}
Expand All @@ -149,11 +130,10 @@ export const DisabledFeaturesArea = () => {

<FormField
boldLabel
fieldId="allowDuplicateCitizenNames"
fieldId={feature.ALLOW_DUPLICATE_CITIZEN_NAMES}
label={"Allow Duplicate Citizen Names"}
>
<Toggle
text="enable/disable"
toggled={values[feature.ALLOW_DUPLICATE_CITIZEN_NAMES]}
onClick={handleChange}
name={feature.ALLOW_DUPLICATE_CITIZEN_NAMES}
Expand All @@ -166,11 +146,10 @@ export const DisabledFeaturesArea = () => {

<FormField
boldLabel
fieldId="discordAuth"
fieldId={feature.DISCORD_AUTH}
label={"Allow users to authenticate with Discord."}
>
<Toggle
text="enable/disable"
toggled={values[feature.DISCORD_AUTH]}
onClick={handleChange}
name={feature.DISCORD_AUTH}
Expand Down
8 changes: 8 additions & 0 deletions packages/client/src/components/ems-fd/StatusesArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ export const StatusesArea = () => {
const onDutyCode = codes10.values.find((v) => v.shouldDo === ShouldDoType.SET_ON_DUTY);
const isOnDutyActive = !isButtonDisabled && onDutyCode?.id === activeDeputy?.status?.id;

if (!onDutyCode && codes10.values.length <= 0) {
return (
<div className="text-lg mt-2 px-4 py-3 bg-gray-300/50 dark:bg-gray-2 dark:border-t-[1.5px] dark:border-gray-3">
This CAD does not have any 10 codes. Please ask an admin to add some.
</div>
);
}

return (
<ul className="status-buttons-grid mt-2 px-4 py-2 bg-gray-300/50 dark:bg-gray-2 dark:border-t-[1.5px] dark:border-gray-3">
<li>
Expand Down
86 changes: 27 additions & 59 deletions packages/client/src/components/form/Toggle.tsx
Original file line number Diff line number Diff line change
@@ -1,66 +1,34 @@
import * as React from "react";
import { useTranslations } from "use-intl";
import * as SwitchPrimitive from "@radix-ui/react-switch";
import { classNames } from "lib/classNames";

interface Props {
type ButtonProps = JSX.IntrinsicElements["button"];

interface Props extends ButtonProps {
toggled: boolean;
onClick: (value: any) => void;
name: string;
text?: "enable/disable" | "on/off" | "true/false";
}

enum Directions {
RIGHT = 25,
LEFT = 175,
onClick: (value: any) => void;
}

export const Toggle = ({ toggled, name, text = "on/off", onClick }: Props) => {
const [x, setX] = React.useState(() => getDirection(toggled));
const t = useTranslations("Common");

const trueText = text === "true/false" ? t("yes") : text === "on/off" ? t("on") : t("enabled");
const falseText = text === "true/false" ? t("no") : text === "on/off" ? t("off") : t("disabled");

React.useEffect(() => {
setX(getDirection(toggled));
}, [toggled]);

return (
<div
className={`w-[100px] ${
text === "enable/disable" && "w-1/4 min-w-[206px]"
} bg-gray-200 dark:bg-gray-2 flex items-center justify-between rounded-lg relative overflow-hidden mt-1`}
>
<div
style={{ transform: `translateX(${x}%)` }}
className="absolute dark:bg-gray-3 bg-dark-gray h-7 w-2/6 rounded-md pointer-events-none transition-all duration-200"
/>

<button
onClick={() => onClick({ target: { name, value: true } })}
type="button"
className={`w-full p-1 cursor-pointer pointer-events-auto z-10 ${
toggled && "text-white font-semibold"
}`}
>
{trueText}
</button>
<button
onClick={() => onClick({ target: { name, value: false } })}
type="button"
className={`w-full p-1 cursor-pointer pointer-events-auto z-10 ${
!toggled && "text-white font-semibold"
}`}
export const Toggle = React.forwardRef<HTMLButtonElement, Props>(
({ toggled, onClick, name, ...props }, ref) => {
return (
<SwitchPrimitive.Root
ref={ref as any}
onCheckedChange={() => onClick({ target: { name, value: !toggled } })}
{...props}
id={name}
name={name}
checked={toggled}
className="relative h-6 transition-all rounded-full shadow-sm w-11 bg-gray-400/50 dark:bg-gray-3 disabled:opacity-50 disabled:cursor-not-allowed"
>
{falseText}
</button>
</div>
);
};

function getDirection(toggled: boolean) {
if (toggled === true) {
return Directions.RIGHT;
}

return Directions.LEFT;
}
<SwitchPrimitive.Thumb
className={classNames(
"block w-4 h-4 transition-all rounded-full switch-component",
toggled ? "bg-gray-4 dark:bg-[#e4e4e4]" : "bg-gray-600/40 dark:bg-gray-600",
)}
/>
</SwitchPrimitive.Root>
);
},
);
8 changes: 8 additions & 0 deletions packages/client/src/components/leo/StatusesArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ export const StatusesArea = () => {
const onDutyCode = codes10.values.find((v) => v.shouldDo === ShouldDoType.SET_ON_DUTY);
const isOnDutyActive = !isButtonDisabled && onDutyCode?.id === activeOfficer?.status?.id;

if (!onDutyCode && codes10.values.length <= 0) {
return (
<div className="text-lg mt-2 px-4 py-3 bg-gray-300/50 dark:bg-gray-2 dark:border-t-[1.5px] dark:border-gray-3">
This CAD does not have any 10 codes. Please ask an admin to add some.
</div>
);
}

return (
<ul className="status-buttons-grid mt-2 px-4 py-2 bg-gray-300/50 dark:bg-gray-2 dark:border-t-[1.5px] dark:border-gray-3">
<li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export const CreateIncidentModal = () => {
<FormRow>
<FormField label={t("firearmsInvolved")}>
<Toggle
text="true/false"
toggled={values.firearmsInvolved}
name="firearmsInvolved"
onClick={handleChange}
Expand All @@ -94,7 +93,6 @@ export const CreateIncidentModal = () => {

<FormField label={t("injuriesOrFatalities")}>
<Toggle
text="true/false"
toggled={values.injuriesOrFatalities}
name="injuriesOrFatalities"
onClick={handleChange}
Expand All @@ -103,12 +101,7 @@ export const CreateIncidentModal = () => {
</FormField>

<FormField label={t("arrestsMade")}>
<Toggle
text="true/false"
toggled={values.arrestsMade}
name="arrestsMade"
onClick={handleChange}
/>
<Toggle toggled={values.arrestsMade} name="arrestsMade" onClick={handleChange} />
<Error>{errors.arrestsMade}</Error>
</FormField>
</FormRow>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as React from "react";
import Link from "next/link";
import { Menu, Transition } from "@headlessui/react";
import { useRouter } from "next/router";
import { ChevronDown } from "react-bootstrap-icons";
import Link from "next/link";
import { useFeatureEnabled } from "hooks/useFeatureEnabled";
import { Feature } from "types/prisma";

Expand Down Expand Up @@ -49,8 +49,9 @@ export const CitizenDropdown = () => {
{items.map((item) => {
const upperCase = item.replace(/ +/g, "_").toUpperCase() as Feature;
const lower = item.replace(/ +/g, "-").toLowerCase();
const isDisabled = ["Courthouse"].includes(item);

if (!enabled[upperCase]) {
if (!enabled[upperCase] || isDisabled) {
return null;
}

Expand Down
Loading

0 comments on commit f42c671

Please sign in to comment.