view previous.c @ 112:d6d578b49a19

[PostDog] Got CRUD working.
author June Park <parkjune1995@gmail.com>
date Sun, 04 Jan 2026 11:38:44 -0800
parents 48f260576059
children
line wrap: on
line source

  char urlInput[1024] = "https://httpbin.org/get";
  bool urlEditMode = false;

  char jsonInput[JSON_INPUT_BUFFER_LEN] = "{\"key\":\"value\"}";
  bool jsonEditMode = false;

  char headersInput[HEADER_INPUT_BUFFER_LEN] = "Content-Type: application/json";
  bool headersEditMode = false;

  char responseText[16384] = "Response will appear here...\n\nTry the default URL or enter your own!";

  char paramsInput[PARAM_INPUT_BUFFER_LEN] = "key1=value1\nkey2=value2";
  bool paramsEditMode = false;

  ActiveTab activeTab = ActiveTab_JSON; // 0 = JSON, 1 = Headers, 2 = Params

  // HTTP method selection
  int methodActive = 0;
  bool methodDropdown = false;
  const char *methods[] = { "GET", "POST", "PUT", "DELETE" };

  // Scroll support
  Vector2 jsonScroll = { 0, 0 };
  Vector2 headersScroll = { 0, 0 };
  Vector2 paramsScroll = { 0, 0 };
  Vector2 responseScroll = { 0, 0 };
  Vector2 historyScroll = { 0, 0 };

  // History
  HistoryItem historyItems[MAX_HISTORY_ITEMS];
  int historyCount = 0;
  int selectedHistoryIndex = -1;

  // Load initial history
  historyCount = PostDog_HistoryDistory_ItemsLoad(historyItems, MAX_HISTORY_ITEMS);

  while (!WindowShouldClose())
  {
    // Get current window dimensions for responsive layout
    int screenWidth = GetScreenWidth();
    int screenHeight = GetScreenHeight();

    // Layout calculations
    Rectangle sidebar = { 0, 10, SIDEBAR_WIDTH, screenHeight };

    float mainX = SIDEBAR_WIDTH + GENERIC_PADDING;
    float mainWidth = screenWidth - SIDEBAR_WIDTH - GENERIC_PADDING * 2;

    // URL input box - leave space for SEND button on the right
    Rectangle urlBox = {
      mainX,
      GENERIC_PADDING,
      mainWidth - SEND_BUTTON_WIDTH - GENERIC_PADDING,
      URL_INPUT_HEIGHT
    };

    // SEND button positioned beside URL input
    Rectangle sendButton = {
      urlBox.x + urlBox.width + GENERIC_PADDING,
      GENERIC_PADDING,
      SEND_BUTTON_WIDTH,
      SEND_BUTTON_HEIGHT
    };

    // Method dropdown below URL
    Rectangle methodButton = {
      mainX,
      urlBox.y + urlBox.height + GENERIC_PADDING,
      METHOD_BUTTON_WIDTH,
      METHOD_BUTTON_HEIGHT
    };

    float tabHeight = 30;
    float contentY = methodButton.y + methodButton.height + GENERIC_PADDING + tabHeight;
    float contentHeight = screenHeight - contentY - GENERIC_PADDING;

    float panelWidth = (mainWidth - GENERIC_PADDING) / 2;

    Rectangle tabBar = {
      mainX,
      methodButton.y + methodButton.height + GENERIC_PADDING,
      panelWidth,
      tabHeight
    };

    Rectangle requestPanel = {
      mainX,
      contentY,
      panelWidth,
      contentHeight
    };

    Rectangle responsePanel = {
      mainX + panelWidth + GENERIC_PADDING,
      contentY,
      panelWidth,
      contentHeight
    };

    BeginDrawing();
      ClearBackground(GetColor(GuiGetStyle(DEFAULT, BACKGROUND_COLOR)));

      // --- Sidebar Component---
      DrawRectangleRec(sidebar, Fade(GRAY, 0.1f));
      GuiGroupBox(sidebar, "HISTORY");

      Rectangle refreshBtn = { 
        sidebar.x + SIDEBAR_PADDING_GENERAL, 
        sidebar.y + SIDEBAR_PADDING_GENERAL,
        SIDEBAR_REFERSH_BUTTON_WIDTH,
        SIDEBAR_REFERSH_BUTTON_HEIGHT
      };
      if (GuiButton(refreshBtn, "Refresh"))
      {
        historyCount = PostDog_HistoryDistory_ItemsLoad(historyItems, MAX_HISTORY_ITEMS);
      }

      Rectangle historyArea = { 
        sidebar.x + SIDEBAR_PADDING_GENERAL,
        sidebar.y + SIDEBAR_AREA_PADDING_Y, 
        sidebar.width - SIDEBAR_AREA_PADDING_X, 
        sidebar.height - SIDEBAR_AREA_PADDING_Y
      };
      if (CheckCollisionPointRec(GetMousePosition(), historyArea))
      {
        float wheel = GetMouseWheelMove();
        historyScroll.y += wheel * 20;
        if (historyScroll.y < 0) historyScroll.y = 0;
      }

      BeginScissorMode(historyArea.x, historyArea.y, historyArea.width, historyArea.height);

      if (historyCount == 0)
      {
        DrawText("No requests yet", historyArea.x + 5, historyArea.y + 25, 10, DARKGRAY);
      } 
      else
      {
        int item_y_position = historyArea.y + SIDEBAR_AREA_PADDING_Y + 5 - (int)historyScroll.y;
        for (
            int current_history_item_number = 0;
            current_history_item_number < historyCount;
            current_history_item_number++
        )
        {
          if (item_y_position > historyArea.y - SIDEBAR_HISTORY_ITEM_HEIGHT && item_y_position < historyArea.y + historyArea.height)
          {
            Rectangle itemRect = { historyArea.x, item_y_position, historyArea.width, SIDEBAR_HISTORY_ITEM_HEIGHT - 2 };

            // Draw button for history item
            Color bgColor = (selectedHistoryIndex == current_history_item_number) ? Fade(BLUE, 0.3f) : Fade(LIGHTGRAY, 0.5f);
            if (CheckCollisionPointRec(GetMousePosition(), itemRect) && IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
            {
              // TODO: This is cringe as fuck probably should just have a strucut that we assign and then zero out lol
              char tempUrl[1024], tempMethod[16], tempHeaders[HEADER_INPUT_BUFFER_LEN], tempBody[JSON_INPUT_BUFFER_LEN];

              if (PostDog_HistoryDirectory_LoadRequest(
                    historyItems[current_history_item_number].filename, tempUrl, tempMethod, tempHeaders, tempBody) == 0)
              {
                strncpy(urlInput, tempUrl, sizeof(urlInput) - 1);
                strncpy(headersInput, tempHeaders, sizeof(headersInput) - 1);
                strncpy(jsonInput, tempBody, sizeof(jsonInput) - 1);

                // Set method
                for (int m = 0; m < 4; m++)
                {
                  if (strcmp(methods[m], tempMethod) == 0)
                  {
                    methodActive = m;
                    break;
                  }
                }

                selectedHistoryIndex = current_history_item_number;
                strcpy(responseText, "Request loaded from history. Click SEND to execute.");
              }
            }

            DrawRectangleRec(itemRect, bgColor);
            DrawRectangleLinesEx(itemRect, 1, GRAY);

            // Draw method badge
            DrawText(historyItems[current_history_item_number].method, itemRect.x + 5, item_y_position + 5, 10, BLACK);

            // Draw timestamp (date only)
            char dateStr[16] = "";
            if (strlen(historyItems[current_history_item_number].filename) >= 8)
            {
              snprintf(dateStr, sizeof(dateStr), "%.4s-%.2s-%.2s",
                       historyItems[current_history_item_number].filename, historyItems[current_history_item_number].filename + 4, historyItems[current_history_item_number].filename + 6);
            }
            DrawText(dateStr, itemRect.x + 5, item_y_position + 18, 8, DARKGRAY);

            // Draw time
            char timeStr[16] = "";
            if (strlen(historyItems[current_history_item_number].filename) >= 15) {
              snprintf(timeStr, sizeof(timeStr), "%.2s:%.2s:%.2s",
                       historyItems[current_history_item_number].filename + 9, historyItems[current_history_item_number].filename + 11, historyItems[current_history_item_number].filename + 13);
            }
            DrawText(timeStr, itemRect.x + 5, item_y_position + 28, 8, DARKGRAY);
          }

          item_y_position += SIDEBAR_HISTORY_ITEM_HEIGHT;
        }
      }

      EndScissorMode();

      // --- URL Input Component ---
      GuiLabel((Rectangle){ mainX, GENERIC_PADDING - 15, 100, 20 }, "URL:");
      if (GuiTextBox(urlBox, urlInput, 1024, urlEditMode))
      {
        urlEditMode = !urlEditMode;
      }
      if (urlEditMode && (IsKeyDown(KEY_LEFT_SUPER) || IsKeyDown(KEY_LEFT_CONTROL)) && IsKeyPressed(KEY_C))
      {
        SetClipboardText(urlInput);
      }

      // Send button (beside URL)
      if (GuiButton(sendButton, "SEND") || (urlEditMode && IsKeyDown(KEY_ENTER)))
      {
        strcpy(responseText, "Sending request...\n");

        // Make the actual HTTP request
        char tempResponse[16384];
        const char *selectedMethod = methods[methodActive];

        // Use JSON body for POST/PUT, otherwise use empty body
        const char *requestBody = (methodActive == 1 || methodActive == 2) ? jsonInput : NULL;
        const char *requestHeaders = headersInput;

        // Save request to history
        Postdog_SaveRequestToHistory(urlInput, selectedMethod, requestHeaders, requestBody);

        // Reload history to show the new request
        historyCount = PostDog_HistoryDistory_ItemsLoad(historyItems, MAX_HISTORY_ITEMS);

        // Making the requests.
        PostDog_Make_HttpRequest(urlInput, selectedMethod, requestHeaders,
                       requestBody, tempResponse, sizeof(tempResponse));
        strncpy(responseText, tempResponse, sizeof(responseText) - 1);
        responseText[sizeof(responseText) - 1] = '\0';
      }

      // Tab toggle (3 tabs now)
      float tabWidth = tabBar.width / 3;
      Rectangle jsonTab = { tabBar.x, tabBar.y - 10, tabWidth, tabBar.height };
      Rectangle headersTab = { tabBar.x + tabWidth, tabBar.y - 10, tabWidth, tabBar.height };
      Rectangle paramsTab = { tabBar.x + tabWidth * 2, tabBar.y - 10, tabWidth, tabBar.height };

      if (GuiButton(jsonTab, activeTab ==  ActiveTab_JSON ? "#191#Body" : "Body"))
      {
        activeTab = ActiveTab_JSON;
      }

      if (GuiButton(headersTab, activeTab == ActiveTab_Headers ? "#191#Headers" : "Headers"))
      {
        activeTab = ActiveTab_Headers;
      }

      if (GuiButton(paramsTab, activeTab == ActiveTab_Params ? "#191#Params" : "Params"))
      {
        activeTab = ActiveTab_Params;
      }

      const char *panelTitle;
      switch(activeTab) {
        case  ActiveTab_JSON:
        {
          panelTitle = "Request Body (JSON)";
          break;
        }
        case  ActiveTab_Headers:
        {
          panelTitle = "Request Headers";
          break;
        } 
        case  ActiveTab_Params:
        {
          panelTitle = "Query Parameters";
          break;
        }
      }

      // Panel title
      GuiGroupBox(requestPanel, panelTitle);
      Rectangle textArea = {
        requestPanel.x + 10,
        requestPanel.y + 30,
        requestPanel.width - 20,
        requestPanel.height - 40
      };

      // Handle click outside to disable edit mode
      if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
      {
        if (!CheckCollisionPointRec(GetMousePosition(), textArea))
        {
          jsonEditMode = false;
          headersEditMode = false;
          paramsEditMode = false;
        }
      }

      // Draw border for text area
      DrawRectangleLinesEx(textArea, 1, GRAY);

      // Manual scroll handling with mouse wheel
      if (CheckCollisionPointRec(GetMousePosition(), textArea))
      {
        float wheel = GetMouseWheelMove();
        switch(activeTab)
        {
          case  ActiveTab_JSON:
          {
            jsonScroll.y += wheel * 20;
            if (jsonScroll.y < 0) jsonScroll.y = 0;
          }
          case  ActiveTab_Headers:
          {
            headersScroll.y += wheel * 20;
            if (headersScroll.y < 0) headersScroll.y = 0;
          } 
          case  ActiveTab_Params:
          {
            paramsScroll.y += wheel * 20;
            if (paramsScroll.y < 0) paramsScroll.y = 0;
          }
        }
      }

      char *copyFromInput;
      bool *currentMode; 
      switch(activeTab)
      {
        case  ActiveTab_JSON:
        {
          PostDog_Render_TextWithScroll(textArea, jsonScroll, jsonInput);
          copyFromInput = jsonInput;
          currentMode = &jsonEditMode;
          break;
        }
        case  ActiveTab_Headers:
        {
          PostDog_Render_TextWithScroll(textArea, headersScroll, headersInput);
          copyFromInput = headersInput;
          currentMode = &headersEditMode;
          break;
        } 
        case  ActiveTab_Params:
        {
          PostDog_Render_TextWithScroll(textArea, paramsScroll, paramsInput);
          copyFromInput = paramsInput;
          currentMode = &paramsEditMode;

          Rectangle updateUrlBtn = { textArea.x + 30, textArea.y + textArea.height - 10, 120, 20 };
          // TODO: Automatic update
          if (GuiButton(updateUrlBtn, "Update URL"))
          {
            char tempUrl[1024];
            strncpy(tempUrl, urlInput, sizeof(tempUrl) - 1);

            // Remove existing params if any
            char *questionMark = strchr(tempUrl, '?');
            if (questionMark) *questionMark = '\0';

            Postdog_UpdateUrlWithParams(urlInput, sizeof(urlInput), tempUrl, paramsInput);
          }
          break;
        }
      }

      if ((IsKeyDown(KEY_LEFT_SUPER) || IsKeyDown(KEY_LEFT_CONTROL)) && IsKeyPressed(KEY_C))
      {
        SetClipboardText(copyFromInput);
      }

      Rectangle editBtn = { textArea.x + textArea.width - 60, textArea.y + textArea.height - 10, 50, 20 };
      if (GuiButton(editBtn, "Edit"))
      {
        *currentMode = !(*currentMode);
      }

      // Response Panel with scroll
      GuiGroupBox(responsePanel, "Response");

      Rectangle responseArea = {
        responsePanel.x + 10,
        responsePanel.y + 30,
        responsePanel.width - 20,
        responsePanel.height - 40
      };

      // Manual scroll for response
      if (CheckCollisionPointRec(GetMousePosition(), responseArea))
      {
        float wheel = GetMouseWheelMove();
        responseScroll.y += wheel * 20;
        if (responseScroll.y < 0) responseScroll.y = 0;
      }

      // Draw border
      DrawRectangleLinesEx(responseArea, 1, GRAY);

      // Draw response text with scroll
      PostDog_Render_TextWithScroll(responseArea, responseScroll, responseText);

      if ((IsKeyDown(KEY_LEFT_SUPER) || IsKeyDown(KEY_LEFT_CONTROL)) && IsKeyPressed(KEY_C))
      {
        if (CheckCollisionPointRec(GetMousePosition(), responseArea)) {
          SetClipboardText(responseText);
        }
      }

      // ---  Edit modal  ----
      if (jsonEditMode)
      {
        GuiTextBox(
            (Rectangle){ screenWidth/2 - 300, screenHeight/2 - 200, 600, 400 },
            jsonInput, JSON_INPUT_BUFFER_LEN, true);
      }

      if (headersEditMode)
      {
        GuiTextBox(
            (Rectangle){ screenWidth/2 - 300, screenHeight/2 - 200, 600, 400 },
            headersInput, HEADER_INPUT_BUFFER_LEN, true);
      }

      if (paramsEditMode)
      {
        GuiTextBox(
            (Rectangle){ screenWidth/2 - 300, screenHeight/2 - 200, 600, 400 },
            paramsInput, PARAM_INPUT_BUFFER_LEN, true);
      }

      if (GuiDropdownBox(methodButton, "GET;POST;PUT;DELETE", &methodActive, methodDropdown))
      {
        methodDropdown = !methodDropdown;
      }


    EndDrawing();
  }