From eee411a182019e0521499fb895ecb781f15e5204 Mon Sep 17 00:00:00 2001 From: Joan Mon Date: Sun, 10 Nov 2024 23:34:11 +0100 Subject: [PATCH] feat(crm-status): Edit crm status with moderator role --- src/components/ChangeCRMStatus.tsx | 71 ++++++++++++++++++++++++++++++ src/components/SolicitudCard.tsx | 15 ++++++- src/helpers/constants.ts | 19 ++++++++ src/types/database.ts | 1 + 4 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 src/components/ChangeCRMStatus.tsx diff --git a/src/components/ChangeCRMStatus.tsx b/src/components/ChangeCRMStatus.tsx new file mode 100644 index 0000000..5061cef --- /dev/null +++ b/src/components/ChangeCRMStatus.tsx @@ -0,0 +1,71 @@ +'use client'; + +import { supabase } from '@/lib/supabase/client'; +import { useState } from 'react'; +import { CRMStatus, CrmStatusActive, CrmStatusFinished } from '@/helpers/constants'; + +type ChangeCRMStatusRequestButtonProps = { + helpRequestId: number; + onStatusUpdate: (status: string) => void; + currentStatus: string | null; + currentCrmStatus: string | null; +}; + +export default function ChangeCRMStatus({ + helpRequestId, + onStatusUpdate, + currentStatus, + currentCrmStatus, +}: ChangeCRMStatusRequestButtonProps) { + const [crmStatus, setCrmStatus] = useState(currentCrmStatus || CrmStatusActive); + const [error, setError] = useState({}); + + const updateStatusRequest = async (newCrmStatus: string) => { + var status = currentStatus || 'active'; + if (newCrmStatus === CrmStatusFinished) { + status = 'finished'; + } else if (newCrmStatus !== CrmStatusFinished && status == 'finished') { + status = 'active'; + } + const { data, error } = await supabase + .from('help_requests') + .update({ status, crm_status: newCrmStatus }) + .eq('id', helpRequestId) + .select(); + + onStatusUpdate(status); + + return { data, error }; + }; + + async function handleUpdateSubmit(newStatus: string) { + const { data, error } = await updateStatusRequest(newStatus); + if (error) { + setError(error); + return; + } + } + + if (error === undefined) return <>; + + return ( + <> + + + ); +} diff --git a/src/components/SolicitudCard.tsx b/src/components/SolicitudCard.tsx index 7e6ab24..f7a9630 100644 --- a/src/components/SolicitudCard.tsx +++ b/src/components/SolicitudCard.tsx @@ -13,6 +13,8 @@ import { useRole } from '@/context/RoleProvider'; import { useState } from 'react'; import ChangeUrgencyHelpRequest from './ChangeUrgencyHelpRequest'; import ChangeStatusButton from './ChangeStatusButton'; +import ChangeCRMStatus from './ChangeCRMStatus'; +import { UserRoles } from '@/helpers/constants'; type SolicitudCardProps = { caso: HelpRequestData; @@ -32,7 +34,8 @@ export default function SolicitudCard({ const { getTownById } = useTowns(); const additionalInfo = caso.additional_info as HelpRequestAdditionalInfo; const special_situations = 'special_situations' in additionalInfo ? additionalInfo.special_situations : undefined; - const isAdmin = role === 'admin'; + const isAdmin = role === UserRoles.admin; + const isCrmUser = role === UserRoles.moderator; const [deleted, setDeleted] = useState(false); const isMyRequest = session.user?.id && session.user.id === caso.user_id; const [updateUrgency, setUpdateUrgency] = useState(caso.urgency); @@ -194,7 +197,7 @@ export default function SolicitudCard({ Ver solicitud )} - + {!isCrmUser && } {isAdmin && ( )} + {isCrmUser && ( + + )} {isAdmin && setDeleted(true)} />} diff --git a/src/helpers/constants.ts b/src/helpers/constants.ts index 37a254e..17b7591 100644 --- a/src/helpers/constants.ts +++ b/src/helpers/constants.ts @@ -24,3 +24,22 @@ export const tiposAyudaArray: TipoAyudaOption[] = Object.entries(tiposAyudaOptio id: id as TipoAyudaId, label, })); + +export const CrmStatusActive = 'active'; +export const CrmStatusFollowUp = 'followup'; +export const CrmStatusAssigned = 'assigned'; +export const CrmStatusProgress = 'progress'; +export const CrmStatusFinished = 'finished'; + +export const CRMStatus = { + [CrmStatusActive]: 'Activo', + [CrmStatusFollowUp]: 'Volver a llamar', + [CrmStatusAssigned]: 'Asignado', + [CrmStatusProgress]: 'En Progreso', + [CrmStatusFinished]: 'Hecho', +}; + +export const UserRoles = { + admin: 'admin', + moderator: 'moderator', +}; diff --git a/src/types/database.ts b/src/types/database.ts index 93d05ab..0e8c342 100644 --- a/src/types/database.ts +++ b/src/types/database.ts @@ -190,6 +190,7 @@ export type Database = { town_id: number | null; type: string | null; urgency: string | null; + crm_status: string | null; user_id: string | null; }; Insert: {