📦 EqualifyEverything / equalify-dashboard

📄 edit-property.tsx · 257 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257import { useState } from 'react';
import { ExclamationTriangleIcon } from '@radix-ui/react-icons';
import {
  QueryClient,
  useMutation,
  useQuery,
  useQueryClient,
} from '@tanstack/react-query';
import {
  ActionFunctionArgs,
  LoaderFunctionArgs,
  redirect,
  useLoaderData,
  useNavigate,
} from 'react-router-dom';
import { toast } from '~/components/alerts';
import { Button } from '~/components/buttons';
import { DangerDialog } from '~/components/dialogs';
import { PropertyForm } from '~/components/forms';
import { SEO } from '~/components/layout';
import { propertyQuery } from '~/queries/properties';
import { deleteProperty, sendToScan, updateProperty } from '~/services';
import { assertNonNull } from '~/utils/safety';
import { LoadingProperty } from './loading';

/**
 * Loader function to fetch property data
 * @param queryClient - The Query Client instance.
 * @returns Loader function to be used with React Router.
 */
export const propertyLoader =
  (queryClient: QueryClient) =>
    async ({ params }: LoaderFunctionArgs) => {
      assertNonNull(
        params.propertyId,
        'Property ID is missing in the route parameters',
      );

      const initialProperty = await queryClient.ensureQueryData(
        propertyQuery(params.propertyId),
      );
      return { initialProperty, propertyId: params.propertyId };
    };

/**
 * Handles updating a property.
 * @param queryClient - The Query Client instance.
 * @returns Action function to be used with React Router.
 */
export const updatePropertyAction =
  (queryClient: QueryClient) =>
    async ({ request, params }: ActionFunctionArgs) => {
      assertNonNull(params.propertyId, 'No property ID provided');

      try {
        const formData = await request.formData();
        const propertyName = formData.get('propertyName') as string;
        const propertyUrl = formData.get('propertyUrl') as string;
        const propertyDiscovery = formData.get('propertyDiscovery') as string;

        const response = await updateProperty(
          params.propertyId,
          propertyName,
          propertyUrl,
          propertyDiscovery,
        );

        await queryClient.invalidateQueries({
          queryKey: ['property', params.propertyId],
        });
        await queryClient.invalidateQueries({ queryKey: ['properties'] });

        if (response.status === 'success') {
          toast.success({ title: 'Success', description: 'Property updated successfully!' });
          return redirect(`/properties`);
        } else {
          toast.error({ title: 'Error', description: 'Failed to update property.' });
          throw new Response('Failed to update property', { status: 500 });
        }
      } catch (error) {
        toast.error({ title: 'Error', description: 'An error occurred while updating the property.' });
        throw error;
      }
    };

const EditProperty = () => {
  const navigate = useNavigate();
  const queryClient = useQueryClient();
  const { initialProperty, propertyId } = useLoaderData() as Awaited<
    ReturnType<ReturnType<typeof propertyLoader>>
  >;

  const [isFormChanged, setIsFormChanged] = useState(false);
  const [isSending, setIsSending] = useState(false);

  const { data: property, isLoading } = useQuery({
    ...propertyQuery(propertyId!),
    initialData: initialProperty,
  });

  const { mutate: deleteMutate } = useMutation({
    mutationFn: () => {
      const response = deleteProperty(propertyId!);
      queryClient.refetchQueries({ queryKey: ['filters'] });
      return response;
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['properties'] });
      toast.success({ title: 'Success', description: 'Property deleted successfully!' });
      navigate('/properties');
    },
    onError: () => {
      toast.error({ title: 'Error', description: 'Failed to delete property.' });
    },
  });

  const handleFormChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    const { name, value } = event.target;
    if (name === 'propertyName' && value.trim() !== property?.name.trim()) {
      setIsFormChanged(true);
    } else if (
      name === 'propertyUrl' &&
      value.trim() !== property?.propertyUrl.trim()
    ) {
      setIsFormChanged(true);
    } else if (name === 'propertyDiscovery' && value !== property?.discovery) {
      setIsFormChanged(true);
    } else {
      setIsFormChanged(false);
    }
  };

  const handleDeleteProperty = async () => {
    deleteMutate();
  };

  const handleSendToScan = async () => {
    setIsSending(true);
    try {
      const response = await sendToScan([propertyId!]);
      if (response.status === 'success') {
        toast.success({ title: 'Success', description: 'Property sent to scan successfully!' });
      } else {
        toast.error({ title: 'Error', description: 'Failed to send property to scan.' });
      }
    } catch (error) {
      toast.error({ title: 'Error', description: 'An error occurred while sending the property to scan.' });
      console.error(error);
    } finally {
      setIsSending(false);
    }
  };
  return (
    <>
      <SEO
        title={`Edit ${property?.name || 'Property'} - Equalify`}
        description={`Edit the details of ${property?.name || 'this property'} on Equalify.`}
        url={`https://dashboard.equalify.app/properties/${propertyId}/edit`}
      />

      <div className="flex w-full flex-col-reverse justify-between sm:flex-row sm:items-center">
        <h1
          id="edit-property-heading"
          className="text-2xl font-bold md:text-3xl"
        >
          Edit {property?.name || 'Property'}
        </h1>

        <Button
          className="w-fit justify-end place-self-end bg-[#005031]"
          onClick={handleSendToScan}
          disabled={isSending}
          aria-disabled={isSending}
        >
          {isSending ? 'Sending...' : 'Send to Scan'}
        </Button>
      </div>

      <section
        aria-labelledby="edit-property-heading"
        className="mt-7 space-y-6 rounded-lg bg-white p-6 shadow"
        aria-live="polite"
      >
        {isLoading ? (
          <LoadingProperty />
        ) : (
          <PropertyForm
            actionUrl={`/properties/${propertyId}/edit`}
            defaultValues={{
              propertyName: property?.name || '',
              propertyUrl: property?.propertyUrl || '',
              propertyDiscovery: property?.discovery || 'single',
            }}
            formId="edit-property-form"
            onChange={handleFormChange}
          />
        )}

        <div className="space-x-6">
          <Button
            variant={'outline'}
            className="w-fit"
            onClick={() => navigate('/properties')}
            aria-label="Cancel editing property"
          >
            Cancel
          </Button>
          <Button
            type="submit"
            form="edit-property-form"
            className="w-fit bg-[#1D781D] text-white"
            disabled={!isFormChanged}
            aria-disabled={!isFormChanged}
            aria-live="polite"
          >
            Update Property
          </Button>
        </div>
      </section>

      <section
        aria-labelledby="danger-zone-heading"
        className="mt-7 space-y-6 rounded-lg bg-white p-6 shadow"
        aria-live="polite"
      >
        <h2 id="danger-zone-heading" className="text-lg text-[#cf000f]">
          Danger Zone
        </h2>

        <DangerDialog
          title="Confirm Property Deletion"
          description="Are you sure you want to delete your property? This action cannot be undone."
          onConfirm={handleDeleteProperty}
          triggerButton={
            <Button
              className="gap-2 bg-[#cf000f]"
              aria-describedby="delete-property-description"
              aria-label="Delete property"
            >
              Delete Property
              <ExclamationTriangleIcon aria-hidden />
            </Button>
          }
        />
        <p
          id="delete-property-description"
          className="mt-2 text-sm text-gray-600"
        >
          Deleting your property is irreversible. Please proceed with caution.
        </p>
      </section>
    </>
  );
};

export default EditProperty;