File size: 4,293 Bytes
ec4551b | 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 | 'use client';
import { useFetch } from '@gitroom/helpers/utils/custom.fetch';
import useSWR from 'swr';
import React, { FC, useCallback, useState } from 'react';
import { Button } from '@gitroom/react/form/button';
import { useRouter } from 'next/navigation';
import { useModals } from '@gitroom/frontend/components/layout/new-modal';
import { FieldValues, FormProvider, useForm } from 'react-hook-form';
import { useT } from '@gitroom/react/translation/get.transation.service.client';
import { Input } from '@gitroom/react/form/input';
import { useToaster } from '@gitroom/react/toaster/toaster';
import { ModalWrapperComponent } from '@gitroom/frontend/components/new-launch/modal.wrapper.component';
export const ApiModal: FC<{
identifier: string;
title: string;
update: () => void;
}> = (props) => {
const { title, identifier, update } = props;
const fetch = useFetch();
const router = useRouter();
const modal = useModals();
const toaster = useToaster();
const [loading, setLoading] = useState(false);
const closePopup = useCallback(() => {
modal.closeAll();
}, []);
const methods = useForm({
mode: 'onChange',
});
const close = useCallback(() => {
if (closePopup) {
return closePopup();
}
modal.closeAll();
}, []);
const submit = useCallback(
async (data: FieldValues) => {
setLoading(true);
const add = await fetch(`/third-party/${identifier}`, {
method: 'POST',
body: JSON.stringify({
api: data.api,
}),
});
if (add.ok) {
toaster.show('Integration added successfully', 'success');
if (closePopup) {
closePopup();
} else {
modal.closeAll();
}
router.refresh();
if (update) update();
return;
}
const { message } = await add.json();
methods.setError('api', {
message,
});
setLoading(false);
},
[props]
);
const t = useT();
return (
<div className="relative">
<FormProvider {...methods}>
<form
className="gap-[8px] flex flex-col"
onSubmit={methods.handleSubmit(submit)}
>
<div className="pt-[10px]">
<Input label="API Key" name="api" />
</div>
<div>
<Button loading={loading} type="submit">
{t('add_integration', 'Add Integration')}
</Button>
</div>
</form>
</FormProvider>
</div>
);
};
export const ThirdPartyListComponent: FC<{ reload: () => void }> = (props) => {
const fetch = useFetch();
const modals = useModals();
const { reload } = props;
const integrationsList = useCallback(async () => {
return (await fetch('/third-party/list')).json();
}, []);
const { data } = useSWR('third-party-list', integrationsList, {
revalidateOnFocus: false,
revalidateOnReconnect: false,
revalidateIfStale: false,
revalidateOnMount: true,
refreshWhenHidden: false,
refreshWhenOffline: false,
});
const addApiKey = useCallback(
(title: string, identifier: string) => () => {
modals.openModal({
title: `Add API key for ${title}`,
withCloseButton: false,
children: (
<ApiModal identifier={identifier} title={title} update={reload} />
),
});
},
[]
);
return (
<div className="grid grid-cols-4 gap-[10px] justify-items-center justify-center">
{data?.map((p: any) => (
<div
onClick={addApiKey(p.title, p.identifier)}
key={p.identifier}
className="w-full h-full p-[20px] min-h-[100px] text-[14px] bg-newTableHeader hover:bg-newTableBorder rounded-[8px] transition-all text-textColor relative flex flex-col gap-[15px] cursor-pointer"
>
<div>
<img
className="w-[32px] h-[32px]"
src={`/icons/third-party/${p.identifier}.png`}
/>
</div>
<div className="whitespace-pre-wrap text-left text-lg">{p.title}</div>
<div className="whitespace-pre-wrap text-left">{p.description}</div>
<div className="w-full flex">
<Button className="w-full">Add</Button>
</div>
</div>
))}
</div>
);
};
|