import { cache, clearWorkplace } from "./cache"; import { setLinks, setWorkplace } from "../app/local_storage/rxdbSlice"; import taskbarApi from "./backend"; const WORKPLACE_TYPES_PATH = "api/workplace_types"; const WORKPLACE_PATH = "api/workplaces"; const EXCLUSIVE_PROGRAMS_PATH = "api/exclusive_programs"; const LINKS_PATH = "api/sources"; const HELPDESK_ISSUES_PATH = "api/helpdesk_issues"; const HELPDESK_HISTORY_PATH = "api/helpdesk_history"; const HELPDESK_DEPARTMENTS_PATH = "api/helpdesk_departments"; export const taskbarHttpApi = taskbarApi.injectEndpoints({ endpoints: (builder) => ({ fetchLocation: builder.query({ query: () => `${WORKPLACE_TYPES_PATH}/?hostname=${window.hostname}`, }), fetchWorkplace: builder.query({ query: (hostname) => ({ url: `${WORKPLACE_PATH}/${hostname}/`, validateStatus: (response) => { if (response.status === 404) clearWorkplace(); return response.status === 200; }, }), transformResponse: async (response) => { cache("workplace", response, setWorkplace); return response; }, }), createWorkplace: builder.mutation({ query: ({ selectedWorkplace, selectedExclusive, description }) => ({ url: `${WORKPLACE_PATH}/`, method: "POST", body: { hostname: window.hostname, workplace_type: selectedWorkplace.id, exclusive_program: selectedExclusive?.id, description, }, }), transformResponse: async (response) => { cache("workplace", response, setWorkplace); return response; }, }), fetchExclusivePrograms: builder.query({ query: () => ({ url: `${EXCLUSIVE_PROGRAMS_PATH}/?hostname=${window.hostname}`, }), }), fetchLinks: builder.query({ query: (hostname) => ({ url: `${LINKS_PATH}/${hostname}/`, }), transformResponse: async (response) => { cache("links", response, setLinks); return response; }, }), fetchHelpdeskDepartments: builder.query({ query: () => ({ url: `${HELPDESK_DEPARTMENTS_PATH}/?hostname=${window.hostname}`, }), }), createHelpdeskIssue: builder.mutation({ query: (body) => { return { url: `${HELPDESK_ISSUES_PATH}/`, method: "POST", body, }; }, }), fetchHelpdeskIssue: builder.mutation({ query: (id) => ({ url: `${HELPDESK_ISSUES_PATH}/${id}/`, method: "GET", }), }), updateHelpdeskIssue: builder.mutation({ query: ({ id, body }) => { return { url: `${HELPDESK_ISSUES_PATH}/${id}/`, method: "PATCH", body, }; }, }), fetchHelpdeskHistory: builder.mutation({ query: ({ limit, offset }) => ({ url: `${HELPDESK_HISTORY_PATH}/${window.hostname}/?limit=${limit}&offset=${offset}`, method: "GET", }), }), }), }); export const { useFetchLocationQuery, useFetchWorkplaceQuery, useLazyFetchWorkplaceQuery, useCreateWorkplaceMutation, useFetchExclusiveProgramsQuery, useFetchLinksQuery, useFetchHelpdeskDepartmentsQuery, useCreateHelpdeskIssueMutation, useFetchHelpdeskIssueMutation, useUpdateHelpdeskIssueMutation, useFetchHelpdeskHistoryMutation, } = taskbarHttpApi;