refactor(silo card): changes to allow viewers to see and able to attach and detach

This commit is contained in:
2025-09-06 09:00:46 -05:00
parent e03e92c18d
commit 796a8dccd2

View File

@@ -21,13 +21,28 @@ import { toast } from "sonner";
import ChartData from "./ChartData"; import ChartData from "./ChartData";
import { AttachSilo } from "./AttachSilo"; import { AttachSilo } from "./AttachSilo";
import { DetachSilo } from "./DetachSilo"; import { DetachSilo } from "./DetachSilo";
import { useSessionStore } from "@/lib/store/sessionStore";
import { useModuleStore } from "@/lib/store/useModuleStore";
import { useGetUserRoles } from "@/lib/store/useGetRoles";
export default function SiloCard(data: any) { export default function SiloCard(data: any) {
const token = localStorage.getItem("auth_token"); const token = localStorage.getItem("auth_token");
const [submitting, setSubmitting] = useState(false); const [submitting, setSubmitting] = useState(false);
const { refetch } = useQuery(getStockSilo()); const { refetch } = useQuery(getStockSilo());
const { user } = useSessionStore();
const { userRoles } = useGetUserRoles();
const { modules } = useModuleStore();
const silo = data.silo; const silo = data.silo;
// roles that can do the silo adjustments
const roles = ["systemAdmin", "technician", "admin", "manager"];
const module = modules.filter((n) => n.name === "logistics");
const accessRoles = userRoles.filter(
(n) => n.module_id === module[0]?.module_id
) as any;
const form = useForm({ const form = useForm({
defaultValues: { defaultValues: {
newLevel: "", newLevel: "",
@@ -46,7 +61,7 @@ export default function SiloCard(data: any) {
dataToSubmit, dataToSubmit,
{ headers: { Authorization: `Bearer ${token}` } } { headers: { Authorization: `Bearer ${token}` } }
); );
console.log(res.data); //console.log(res.data);
if (res.data.success) { if (res.data.success) {
toast.success(res.data.message); toast.success(res.data.message);
@@ -70,6 +85,8 @@ export default function SiloCard(data: any) {
} }
}, },
}); });
console.log(accessRoles);
return ( return (
<LstCard> <LstCard>
<div className="flex flex-row"> <div className="flex flex-row">
@@ -109,82 +126,98 @@ export default function SiloCard(data: any) {
</ul> </ul>
</div> </div>
) : ( ) : (
<form <>
onSubmit={(e) => { {user &&
e.preventDefault(); roles.includes(accessRoles[0]?.role) && (
e.stopPropagation(); <form
}} onSubmit={(e) => {
> e.preventDefault();
<form.Field e.stopPropagation();
name="newLevel" }}
validators={{ >
// We can choose between form-wide and field-specific validators <form.Field
onChange: ({ value }) => name="newLevel"
value.length > 1 validators={{
? undefined // We can choose between form-wide and field-specific validators
: "You must enter a value greate than 1", onChange: ({ value }) =>
}} value.length > 1
children={(field) => { ? undefined
return ( : "You must enter a value greate than 1",
<div className="m-2 min-w-48 max-w-96 p-2"> }}
<div className="flex flex-row"> children={(field) => {
<Label htmlFor="newLevel"> return (
New level <div className="m-2 min-w-48 max-w-96 p-2">
</Label> <div className="flex flex-row">
<div> <Label htmlFor="newLevel">
<Disclaimer /> New level
</div> </Label>
</div> <div>
<div className="flex flex-row"> <Disclaimer />
<Input </div>
name={field.name} </div>
value={ <div className="flex flex-row">
field.state.value <Input
} name={
onBlur={ field.name
field.handleBlur }
} value={
type="decimal" field
onChange={(e) => .state
field.handleChange( .value
e.target.value }
) onBlur={
} field.handleBlur
/> }
<Button type="decimal"
className="ml-1" onChange={(
variant="outline" e
type="submit" ) =>
onClick={ field.handleChange(
form.handleSubmit e
} .target
disabled={submitting} .value
> )
{submitting ? ( }
<span className="w-24"> />
Submitting... <Button
</span> className="ml-1"
) : ( variant="outline"
<span className="w-24"> type="submit"
Submit onClick={
</span> form.handleSubmit
)} }
</Button> disabled={
</div> submitting
}
>
{submitting ? (
<span className="w-24">
Submitting...
</span>
) : (
<span className="w-24">
Submit
</span>
)}
</Button>
</div>
{field.state.meta.errors {field.state.meta
.length ? ( .errors
<em> .length ? (
{field.state.meta.errors.join( <em>
"," {field.state.meta.errors.join(
)} ","
</em> )}
) : null} </em>
</div> ) : null}
); </div>
}} );
/> }}
</form> />
</form>
)}
</>
)} )}
</div> </div>
</LstCard> </LstCard>