Guest User

Untitled

a guest
Dec 19th, 2024
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.78 KB | None | 0 0
  1. <script setup>
  2. import { ref, computed } from "vue";
  3. import AuthenticatedLayout from "@/Layouts/AuthenticatedLayout.vue";
  4. import { Head, useForm } from "@inertiajs/vue3";
  5. import TextInput from "@/Components/TextInput.vue";
  6. import InputLabel from "@/Components/InputLabel.vue";
  7. import PrimaryButton from "@/Components/PrimaryButton.vue";
  8. import DataTable from "datatables.net-vue3";
  9. import DataTablesCore from "datatables.net";
  10. import EditButton from "@/Components/Buttons/EditButton.vue";
  11. import CustomModal from "@/Components/CustomModal.vue";
  12. import refreshProps from "@/Util/refreshProps";
  13. import Select from "@/Components/Select.vue";
  14. import Checkbox from "../../Components/Checkbox.vue";
  15. import $ from "jquery";
  16. import { DateTime } from "luxon";
  17.  
  18. const props = defineProps({
  19. datatable: {
  20. type: Object,
  21. default: () => {
  22. return {};
  23. },
  24. },
  25. });
  26.  
  27. DataTable.use(DataTablesCore);
  28.  
  29. const limitLength = function (sourceText) {
  30. if (sourceText != null) {
  31. if (sourceText.length <= 30) {
  32. return sourceText;
  33. } else {
  34. return sourceText.substring(0, 30);
  35. }
  36. } else {
  37. return "";
  38. }
  39. };
  40.  
  41. const tableData = computed(() => {
  42. return props.datatable.pagination.data;
  43. });
  44.  
  45. const tableColumns = [
  46. { data: "name", title: "Category", type: "case-insensitive" },
  47. {
  48. data: "description",
  49. title: "Description",
  50. type: "case-insensitive",
  51. render: (data) => {
  52. return limitLength(data);
  53. },
  54. },
  55. { data: "created_at", title: "Created On", type: "custom-date" },
  56. { data: "id", name: "edit", orderable: false },
  57. ];
  58.  
  59. const formStore = useForm({
  60. name: "",
  61. description: "",
  62. });
  63.  
  64. const submitStore = () => {
  65. formStore.post(
  66. route("assets.categories.store"),
  67. refreshProps("assets.categories.index"),
  68. );
  69. };
  70.  
  71. const formUpdate = useForm({
  72. id: 0,
  73. name: "",
  74. description: "",
  75. flagDelete: false,
  76. });
  77.  
  78. const submitUpdate = () => {
  79. formUpdate.put(
  80. route("assets.categories.update", formUpdate.id),
  81. refreshProps("assets.categories.index"),
  82. );
  83. };
  84.  
  85. const submitDelete = () => {
  86. if (confirm("Are you sure?")) {
  87. formUpdate.delete(
  88. route("assets.categories.destroy", formUpdate.id),
  89. refreshProps("assets.categories.index"),
  90. );
  91. }
  92. };
  93.  
  94. const submitMerge = () => {
  95. if (confirm("Are you sure?")) {
  96. formUpdate.put(
  97. route("assets.updateAllInCategory"),
  98. refreshProps("assets.categories.index"),
  99. );
  100. }
  101. };
  102.  
  103. const updateCategory = function (_category) {
  104. formUpdate.id = _category.id;
  105. formUpdate.name = _category.name;
  106. formUpdate.description = _category.description;
  107. };
  108.  
  109. const modalOpen = ref(false);
  110. const mergeModalOpen = ref(false);
  111.  
  112. $.fn.dataTable.ext.type.order["case-insensitive-pre"] = function (data) {
  113. return data?.toString().toLowerCase() || "";
  114. };
  115.  
  116. $.fn.dataTable.ext.type.order["custom-date-pre"] = function (data) {
  117. console.log("Processing data in pre:", data);
  118. const format = "d LLL yyyy";
  119.  
  120. const date = DateTime.fromFormat(data, format);
  121.  
  122. if (!date.isValid) {
  123. console.error("Invalid date:", data);
  124. return 0;
  125. }
  126.  
  127. return date.toISODate();
  128. };
  129. </script>
  130.  
  131. <template>
  132. <Head title="Manage Categories" />
  133.  
  134. <AuthenticatedLayout>
  135. <div class="pt-[34px] sm:px-6 lg:px-[63px]">
  136. <div class="mx-auto">
  137. <div class="grid max-w-[988px] grid-cols-12 gap-6">
  138. <div class="box col-span-12 sm:col-span-12 lg:col-span-6">
  139. <div
  140. class="overflow-hidden rounded-lg bg-palette-5 shadow-sm dark:bg-[#141C25]"
  141. >
  142. <div class="p-6 pb-2">
  143. <h2 class="text-[12px] font-900 dark:text-[#7F99AE]">
  144. Manage Categories
  145. </h2>
  146. </div>
  147. <div class="px-5 pb-5">
  148. <DataTable
  149. :options="{
  150. pageLength: 50,
  151. bPaginate: false,
  152. bLengthChange: false,
  153. bFilter: true,
  154. bInfo: false,
  155. bAutoWidth: true,
  156. deferRender: true,
  157. searching: false,
  158. order: [[0, 'asc']],
  159. typeDetect: false,
  160. }"
  161. :data="tableData"
  162. :columns="tableColumns"
  163. class="display pb-5 pl-5 pr-5 text-[10px]"
  164. >
  165. <template #column-edit="props">
  166. <div class="w-4">
  167. <EditButton
  168. @click="
  169. updateCategory(props.rowData);
  170. modalOpen = true;
  171. "
  172. />
  173. </div>
  174. </template>
  175. </DataTable>
  176. </div>
  177. </div>
  178. </div>
  179.  
  180. <div class="box col-span-12 lg:col-span-6">
  181. <div
  182. class="overflow-hidden rounded-lg bg-palette-5 shadow-sm dark:bg-[#141C25]"
  183. >
  184. <form @submit.prevent="submitStore">
  185. <div class="p-6">
  186. <h2 class="text-[12px] font-900 dark:text-[#7F99AE]">
  187. Add Category
  188. </h2>
  189.  
  190. <div class="mt-5">
  191. <InputLabel class="ml-2" for="name" value="Name" />
  192. <TextInput
  193. id="name"
  194. type="text"
  195. class="mt-1 block w-full"
  196. v-model="formStore.name"
  197. required
  198. placeholder="Enter a category name"
  199. autofocus
  200. />
  201. </div>
  202.  
  203. <div class="mt-5">
  204. <InputLabel
  205. class="ml-2"
  206. for="description"
  207. value="Description"
  208. />
  209. <TextInput
  210. id="description"
  211. type="text"
  212. class="mt-1 block w-full"
  213. v-model="formStore.description"
  214. />
  215. </div>
  216.  
  217. <div class="mt-4 flex">
  218. <PrimaryButton
  219. class="mx-auto max-w-[293px]"
  220. :class="{
  221. 'opacity-25': formStore.processing,
  222. }"
  223. :disabled="formStore.processing"
  224. >
  225. Add Category
  226. </PrimaryButton>
  227. </div>
  228. </div>
  229. </form>
  230. </div>
  231. </div>
  232. </div>
  233. </div>
  234. </div>
  235. </AuthenticatedLayout>
  236.  
  237. <CustomModal
  238. title="Edit Category"
  239. v-model="modalOpen"
  240. @close="modalOpen = false"
  241. >
  242. <form @submit.prevent="submitUpdate" class="w-full sm:w-[440px]">
  243. <input type="hidden" name="id" id="id" v-model="formUpdate.id" />
  244.  
  245. <div class="mt-4">
  246. <InputLabel class="ml-2" for="name" value="Name" />
  247. <TextInput
  248. id="name"
  249. type="text"
  250. class="mt-1 block w-full"
  251. v-model="formUpdate.name"
  252. autofocus
  253. />
  254. </div>
  255.  
  256. <div class="mt-4">
  257. <InputLabel class="ml-2" for="description" value="Description" />
  258. <TextInput
  259. id="description"
  260. type="text"
  261. class="mt-1 block w-full"
  262. v-model="formUpdate.description"
  263. />
  264. </div>
  265.  
  266. <div class="mx-auto mt-[16px] flex">
  267. <PrimaryButton
  268. class="mx-auto max-w-[293px]"
  269. :class="{ 'opacity-25': formUpdate.processing }"
  270. :disabled="formUpdate.processing"
  271. >
  272. Save Category
  273. </PrimaryButton>
  274. </div>
  275. </form>
  276. <div class="mt-[16px] flex w-full sm:w-[440px]">
  277. <PrimaryButton
  278. class="mx-auto max-w-[293px]"
  279. @click="((modalOpen = false), (mergeModalOpen = true))"
  280. >
  281. Merge Categories
  282. </PrimaryButton>
  283. </div>
  284. <form @submit.prevent="submitDelete" class="w-full sm:w-[440px]">
  285. <div class="mt-[16px] flex">
  286. <PrimaryButton
  287. class="mx-auto max-w-[293px]"
  288. :class="{ 'opacity-25': formUpdate.processing }"
  289. :style="{ 'background-color': '#EF3961' }"
  290. :disabled="formUpdate.processing"
  291. >
  292. Delete
  293. </PrimaryButton>
  294. </div>
  295. </form>
  296. </CustomModal>
  297. <CustomModal
  298. title="Select Category to Merge Into"
  299. v-model="mergeModalOpen"
  300. @close="mergeModalOpen = false"
  301. >
  302. <InputLabel class="my-4">Merging {{ formUpdate.name }} into:</InputLabel>
  303.  
  304. <form @submit.prevent="submitMerge" class="w-full sm:w-[440px]">
  305. <Select
  306. name="category"
  307. id="category"
  308. class="mt-1 block w-full"
  309. message="Choose a category"
  310. v-model="formUpdate.id"
  311. required
  312. >
  313. <option
  314. v-for="category in datatable.pagination.data"
  315. :key="category.id"
  316. :value="category.id"
  317. :selected="category.id == formUpdate.id"
  318. >
  319. {{ category.name }}
  320. </option>
  321. </Select>
  322.  
  323. <div class="my-4">
  324. <InputLabel>Check to delete {{ formUpdate.name }} on merge.</InputLabel>
  325. <Checkbox v-model="formUpdate.flagDelete"></Checkbox>
  326. </div>
  327.  
  328. <div class="mt-[16px] flex">
  329. <PrimaryButton
  330. class="mx-auto max-w-[293px]"
  331. @click="mergeModalOpen = false"
  332. >
  333. Complete Merge
  334. </PrimaryButton>
  335. </div>
  336. </form>
  337. </CustomModal>
  338. </template>
  339.  
  340. <style>
  341. @import "datatables.net-dt";
  342.  
  343. div.dt-container div.dt-layout-row {
  344. margin: 0;
  345. }
  346. </style>
  347.  
Advertisement
Add Comment
Please, Sign In to add comment